使用pg_ctl和systemctl交叉启停服务测试

网友投稿 873 2022-10-04

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

使用pg_ctl和systemctl交叉启停服务测试

系统环境:Centos 7.6PostgreSQL 13 RC1

1. 创建postgres用户和组

# groupadd dba -g 1000# useradd postgres -g 1000 -u 1000# echo "PostgreSQL@(2020)"|passwd postgres --stdin

建议固化uid与gid,统一配置流复制或集群。

2. 创建目录

# mkdir -p {/opt/pg13,/opt/data5413}# chown -R postgres: {/opt/pg13,/opt/data5413}# chmod 0755 /opt/pg13# chmod 0700 /opt/data5413

推荐数据目录带上端口号,尤其当存在多个实例时。

3. 编译安装

下载文件并解压

# su - postgres$ wget https://ftp.postgresql.org/pub/source/v13rc1/postgresql-13rc1.tar.gz$ tar -zxvf postgresql-13rc1.tar.gz

with-systemd参数说明(9.6增加):

Add configure option --with-systemd to enable calling sd_notify() at server start and stop (Peter Eisentraut)This allows the use of systemd service units of type notify, which greatly simplifies the management of PostgreSQL under systemd.

当我们使用systemd管理数据库服务时,Type方式如果为notify时,需要使用with-systemd参数,否则当我们使用systemctl管理数据库服务时会夯住,此时数据库其实已经启动并可接受连接。当使用Type方式为forking时不受影响。

编译

$ cd postgresql-13rc1/$ ./configure --prefix=/opt/pg13 --with-systemd

出现如下错误

configure: error: header file is required for systemd support

安装systemd-devel包解决

# yum localinstall /media/Packages/systemd-devel-219-62.el7.x86_64.rpm

安装

$ gmake world

当看到最后一行显示为:

PostgreSQL, contrib, and documentation successfully made. Ready to install.

说明已经编译成功

$ gmake install-world //包含扩展包和文档

当看到最后一行显示为:

PostgreSQL, contrib, and documentation installation complete.

说明已经安装成功

查看版本

$ /opt/pg13/bin/postgres --versionpostgres (PostgreSQL) 13rc1

4. 初始化数据目录

$ /opt/pg13/bin/initdb -D/opt/data5413 \-EUTF8 \-Upostgres \-W

5. 修改数据库参数

$ vi /opt/data5413/postgresql.conflisten_addresses='0.0.0.0'port=5413logging_collector=onlog_destination=csvloglog_filename='pg_log_%u.log'log_file_mode=0600log_truncate_on_rotation=onlog_rotation_age=1d

6.使用pg_ctl启停数据库

启动数据库

$ /opt/pg13/bin/pg_ctl start -D /opt/data5413

停止数据库

$ /opt/pg13/bin/pg_ctl stop -D /opt/data5413

7.使用systemctl管理服务

# vi /usr/lib/systemd/system/postgresql-13.service [Unit]Description=PostgreSQL 13 database serverAfter=syslog.target network.target[Service]Type=forkingTimeoutSec=120User=postgresEnvironment=PGDATA=/opt/data5413ExecStart=/opt/pg13/bin/pg_ctl start -w -D "/opt/data5413/" -l "/opt/data5413/log/startup.log"ExecStop=/opt/pg13/bin/pg_ctl stop -m fast -w -D "/opt/data5413/"ExecReload=/opt/pg13/bin/pg_ctl reload -D "/opt/data5413/"[Install]WantedBy=multi-user.target

重新加载服务配置文件

systemctl daemon-reload

通过systemctl启停服务

systemctl start postgresql-13systemctl stop postgresql-13systemctl reload postgresql-13systemctl restart postgresql-13

观察控制台日志,启停正常

8.使用pg_ctl和systemctl交叉测试启停服务

8.1 pg_ctl先启动服务

postgres用户使用pg_ctl启动服务

$ /opt/pg13/bin/pg_ctl start -D /opt/data5413

再通过systemctl查看状态

systemctl status postgresql-13● postgresql-13.service - PostgreSQL 13 database server Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; disabled; vendor preset: disabled) Active: inactive (dead)

发现服务未启动的

通过ps查看其实服务是正常启动的

$ ps f -u postgres PID TTY STAT TIME COMMAND 7844 ? S 0:00 sshd: postgres@pts/1 7845 pts/1 Ss 0:00 \_ -bash 4214 ? S 0:00 sshd: postgres@pts/0 4215 pts/0 Ss 0:00 \_ -bash19324 pts/0 R+ 0:00 \_ ps f -u postgres19303 ? Ss 0:00 /opt/pg13/bin/postgres -D /opt/data541319304 ? Ss 0:00 \_ postgres: logger 19308 ? Ss 0:00 \_ postgres: checkpointer 19309 ? Ss 0:00 \_ postgres: background writer 19310 ? Ss 0:00 \_ postgres: walwriter 19311 ? Ss 0:00 \_ postgres: autovacuum launcher 19312 ? Ss 0:00 \_ postgres: stats collector 19313 ? Ss 0:00 \_ postgres: logical replication launcher

那通过systemctl是否可以stop掉pg_ctl启动的服务呢?答案是否定的

再使用systemctl启动服务试试

systemctl start postgresql-13Job for postgresql-13.service failed because the control process exited with error code. See "systemctl status postgresql-13.service" and "journalctl -xe" for details.

执行是失败的,查看下启动日志

tailf -n 3 /opt/data5413/log/startup.log2020-12-23 11:03:29.490 CST [19154] HINT: Future log output will appear in directory "log".2020-12-23 11:14:42.066 CST [19432] FATAL: lock file "postmaster.pid" already exists2020-12-23 11:14:42.066 CST [19432] HINT: Is another postmaster (PID 19303) running in data directory "/opt/data5413"?

很明显:通过pg_ctl启动的服务是正常的,因此systemctl直接退出。

pg_ctl先正常关闭服务

$ /opt/pg13/bin/pg_ctl stop -D /opt/data5413

8.2 systemctl先启动服务

测试通过systemctl启动的服务,使用pg_ctl是否能正常关闭

# systemctl start postgresql-13

查看状态

# systemctl status postgresql-13

服务运行状态正常

通过pg_ctl关闭服务

$ /opt/pg13/bin/pg_ctl stop -D /opt/data5413waiting for server to shut down.... doneserver stopped

可以正常关闭

也可以再通过systemctl进行启动

9.小结

1.使用pg_ctl启动服务后不能通过systemctl启停服务,需要通过pg_ctl正常关闭后才可操作。2.使用systemctl启动服务后,可以通过pg_clt来进行管理

推荐方式:尽量不要交叉使用,要么使用root用户或者有sudo权限的用户通过systemctl来管理服务,要么使用宿主用户postgres通过pg_ctl来管理。

上一篇:Man Page Of gethrtime
下一篇:CentOS 7使用ISO镜像配置本地yum源
相关文章

 发表评论

暂时没有评论,来抢沙发吧~