别裁伪体亲风雅,转益多师是汝师。这篇文章主要讲述如何重启数据库服务(包含单实例/流复制/集群)相关的知识,希望能为你提供帮助。
目录环境
文档用途
详细信息
环境系统平台:N/A
版本:4.3.4.5,4.3.4.6,4.3.4.7,4.3.4.8,4.3.4.9,4.5
文档用途
- 本文档概述了单实例数据库、流复制环境和集群环境三种架构下启动和关闭的步骤。并非故障排除文档。 但是可以清楚地了解这三种方式数据库在Linux系统下启动和关闭方式。
1. 单实例数据库启动和关闭
1.1 systemctl方式
systemctl为使用操作系统服务的方式将数据库启动和关闭的方法,方便流复制和集群的管理。
(1)数据库启动
[root@hgv4 ~]# systemctl start hgdb-se4.5.service |
- 查看运行数据库状态
[root@hgv4 ~]# systemctl status hgdb-se4.5.service hgdb-se4.5.service - hgdb4.5-see Loaded: loaded (/usr/lib/systemd/system/hgdb-se4.5.service; enabled; vendor preset: disabled) Active: active (running) since 四 2020-09-10 10:25:08 CST; 41s ago Process: 79115 ExecStop=/highgo/database/4.5/etc/hgdb-se4.5 stop (code=exited, status=1/FAILURE) Process: 79222 ExecStart=/highgo/database/4.5/etc/hgdb-se4.5 start (code=exited, status=0/SUCCESS) Main PID: 79229 (postgres) Tasks: 10 CGroup: /system.slice/hgdb-se4.5.service ├─79229 /highgo/database/4.5/bin/postgres -D /highgo/database/4.5/data ├─79230 postgres: logger ├─79231 postgres: auditwriter ├─79233 postgres: checkpointer ├─79234 postgres: background writer ├─79235 postgres: walwriter ├─79236 postgres: autovacuum launcher ├─79237 postgres: archiver ├─79238 postgres: stats collector └─79239 postgres: logical replication launcher 9月 10 10:25:08 hgv4 systemd[1]: Starting hgdb4.5-see... 9月 10 10:25:08 hgv4 hgdb-se4.5[79222]: Starting HighGo Database Server: 9月 10 10:25:08 hgv4 hgdb-se4.5[79222]: waiting for server to start.... done 9月 10 10:25:08 hgv4 hgdb-se4.5[79222]: server started 9月 10 10:25:08 hgv4 hgdb-se4.5[79222]: HighGo Database Server started successfully 9月 10 10:25:08 hgv4 systemd[1]: Started hgdb4.5-see. |
- 查看数据库的操作系统进程
[root@hgv4 ~]# ps -ef|grep postg root7943510 10:27 ?00:00:00 /highgo/database/4.5/bin/postgres root79441794350 10:27 ?00:00:00 postgres: logger root79442794350 10:27 ?00:00:00 postgres: auditwriter root79444794350 10:27 ?00:00:00 postgres: checkpointer root79445794350 10:27 ?00:00:00 postgres: background writer root79446794350 10:27 ?00:00:00 postgres: walwriter root79449794350 10:27 ?00:00:00 postgres: autovacuum launchr root79450794350 10:27 ?00:00:00 postgres: archiver root79451794350 10:27 ?00:00:00 postgres: stats collector root79452794350 10:27 ?00:00:00 postgres: logical replication launcher root79511762150 10:28 pts/100:00:00 grep --color=auto postg |
[root@hgv4 ~]# systemctl stop hgdb-se4.5.service |
- 查看数据库服务状态
[root@hgv4 ~]# systemctl status hgdb-se4.5.service hgdb-se4.5.service - hgdb4.5-see Loaded: loaded (/usr/lib/systemd/system/hgdb-se4.5.service; enabled; vendor preset: disabled) Active: inactive (dead) since 四 2020-09-10 10:25:59 CST; 2s ago Process: 79291 ExecStop=/highgo/database/4.5/etc/hgdb-se4.5 stop (code=exited, status=0/SUCCESS) Process: 79222 ExecStart=/highgo/database/4.5/etc/hgdb-se4.5 start (code=exited, status=0/SUCCESS) Main PID: 79229 (code=exited, status=0/SUCCESS) 9月 10 10:25:08 hgv4 hgdb-se4.5[79222]: Starting HighGo Database Server: 9月 10 10:25:08 hgv4 hgdb-se4.5[79222]: waiting for server to start.... done 9月 10 10:25:08 hgv4 hgdb-se4.5[79222]: server started 9月 10 10:25:08 hgv4 hgdb-se4.5[79222]: HighGo Database Server started successfully 9月 10 10:25:08 hgv4 systemd[1]: Started hgdb4.5-see. 9月 10 10:25:59 hgv4 systemd[1]: Stopping hgdb4.5-see... 9月 10 10:25:59 hgv4 hgdb-se4.5[79291]: Stopping HighGo Database 9月 10 10:25:59 hgv4 hgdb-se4.5[79291]: waiting for server to shut down.... done 9月 10 10:25:59 hgv4 hgdb-se4.5[79291]: server stopped 9月 10 10:25:59 hgv4 systemd[1]: Stopped hgdb4.5-see. |
- 查看数据库的操作系统进程
[root@hgv4 ~]# ps -ef|grep postg root79428762150 10:27 pts/100:00:00 grep --color=auto postg |
(1)数据库启动
[root@hgv4 ~]# pg_ctl start 等待服务器进程启动 ....2020-09-10 10:27:44.923 CST [79405]日志:正在启动 HighGo Security Enterprise Edition Database System 4.5 on CentOS7 x86_64,build on 20200426 2020-09-10 10:27:44.923 CST [79405]日志:正在监听IPv4地址"0.0.0.0",端口 5866 2020-09-10 10:27:44.923 CST [79405]日志:正在监听IPv6地址"::",端口 5866 2020-09-10 10:27:44.928 CST [79405]日志:在Unix套接字 "/tmp/.s.PGSQL.5866"上侦听 2020-09-10 10:27:44.939 CST [79405]日志:日志输出重定向到日志收集进程 2020-09-10 10:27:44.939 CST [79405]提示:后续的日志输出将出现在目录 "hgdb_log"中. 完成 服务器进程已经启动 |
如果当期环境允许,为了加快数据库关闭速度,数据库可以提前将应用、中间件提前关闭后并结束与数据库的连接残留进程。
- 检查数据库会话连接数
highgo=# select datname,usename,client_addr,count(*) from pg_stat_activity where pid <
>
pg_backend_pid()and state=\'idle\' group by 1,2,3 order by 1,2,4 desc;
datname | usename | client_addr | count ---------+---------+-------------+------- highgo| sysdba||1 (1 行记录) |
- kill除自己会话连接之外的所有用户连接进程
highgo=# select pg_terminate_backend(pid) from pg_stat_activity where state=\'idle\';
pg_terminate_backend ---------------------- t (1 行记录) |
[root@hgv4 ~]# pg_ctl stop -m fast 等待服务器进程关闭 .... 完成 服务器进程已经关闭 |
- 对于 pg_ctl 关闭数据库 -m 参数的说明
-m 选项: 关闭或重启的选项: -m, --mode=MODE可以是 "smart", "fast", 或者 "immediate" 关闭模式有如下几种: smart所有客户端断开连接后退出 fast直接退出, 正确的关闭(默认) immediate不完全的关闭退出; 此方式会导致数据库再次启动时进行实例恢复 |
流复制数据同样可以使用systemctl或者pg_ctl命令方式关闭和启动数据库,对于流复制环境一主多备或一主一备模式,并没有先后启动和关闭顺序上的要求。
如果你想要确定主备节点的角色可参考文档(HighGo Database判断流复制主备角色的方法(Document 017033702)。我们此处我们采用pg_ctl方式进行关闭和启动。
2.1 采用 pg_ctl方式启动和关闭流复制环境
与单实例数据库启动和关闭相同,为加速停止你也可以采用关闭应用和结束残余的用户进程的方法。
(1)启动数据库
- 启动主节点
[root@hgv4 highgo]# pg_ctl start 等待服务器进程启动 ....2020-09-14 14:21:01.520 CST [102693]日志:正在启动 HighGo Security Enterprise Edition Database System 4.5 on CentOS7 x86_64,build on 20200426 2020-09-14 14:21:01.520 CST [102693]日志:正在监听IPv4地址"0.0.0.0",端口 5866 2020-09-14 14:21:01.520 CST [102693]日志:正在监听IPv6地址"::",端口 5866 2020-09-14 14:21:01.522 CST [102693]日志:在Unix套接字 "/tmp/.s.PGSQL.5866"上侦听 2020-09-14 14:21:01.539 CST [102693]日志:日志输出重定向到日志收集进程 2020-09-14 14:21:01.539 CST [102693]提示:后续的日志输出将出现在目录 "hgdb_log"中. 完成 服务器进程已经启 |
- 检查主节点操作系统后台进程
[root@hgv4 highgo]# ps -ef|grep postg postgres48939489380 14:42 ?00:00:00 postgres: logger postgres48941489380 14:42 ?00:00:00 postgres: checkpointer postgres48942489380 14:42 ?00:00:00 postgres: background writer postgres48943489380 14:42 ?00:00:00 postgres: walwriter postgres48944489380 14:42 ?00:00:00 postgres: autovacuum launcher postgres48945489380 14:42 ?00:00:00 postgres: archiver postgres48946489380 14:42 ?00:00:00 postgres: stats collector postgres48947489380 14:42 ?00:00:00 postgres: logical replication launcher postgres48949489380 14:42 ?00:00:00 postgres: walsender repuser hgv4dg(65096) streaming 4/200000D8--流复制日志发送进程, hgv4dg为备节点的主机名。 |
- 启动备节点
[root@hgv4dg highgo]# pg_ctl start 等待服务器进程启动 ....2020-09-14 14:21:01.520 CST [102693]日志:正在启动 HighGo Security Enterprise Edition Database System 4.5 on CentOS7 x86_64,build on 20200426 2020-09-14 14:21:01.520 CST [102693]日志:正在监听IPv4地址"0.0.0.0",端口 5866 2020-09-14 14:21:01.520 CST [102693]日志:正在监听IPv6地址"::",端口 5866 2020-09-14 14:21:01.522 CST [102693]日志:在Unix套接字 "/tmp/.s.PGSQL.5866"上侦听 2020-09-14 14:21:01.539 CST [102693]日志:日志输出重定向到日志收集进程 2020-09-14 14:21:01.539 CST [102693]提示:后续的日志输出将出现在目录 "hgdb_log"中. 完成 服务器进程已经启 |
- 检查备节点操作系统后台进程
[root@hgv4dg highgo]# ps -ef|grep postg postgres49906499050 14:42 ?00:00:00 postgres: logger postgres49907499050 14:42 ?00:00:00 postgres: startuprecovering 000000060000000400000020 postgres49908499050 14:42 ?00:00:00 postgres: checkpointer postgres49909499050 14:42 ?00:00:00 postgres: background writer postgres49910499050 14:42 ?00:00:00 postgres: stats collector postgres49922499050 14:42 ?00:00:00 postgres: walreceiverstreaming 4/200000D8--流复制日志接收进程 |
- 关闭主节点
[root@hgv4 highgo]# pg_ctl stop 等待服务器进程关闭 .... 完成 服务器进程已经关闭 |
- 关闭备节点
[root@hgv4dg highgo]# pg_ctl stop 等待服务器进程关闭 .... 完成 服务器进程已经关闭 |
需采用Linux启动和关闭服务(systemctl)的方式进行关闭和启动,不建议使用 pg_ctl 方式启动和关闭集群中的节点。
- 集群启动顺序:
主节点->
备节点 说明:顺序不能颠倒,严格按照先启动主节点数据库服务,在启动所有备节点数据库服务的顺序进行。 |
- 集群关闭顺序:
备节点->
主节点 说明:顺序不能颠倒,严格按照先关闭所有备节点数据库服务后,再关闭主节点数据库服务的的顺序进行。 |
(1)启动主节点
- 使用systemctl方式启动
[root@hs01 ] systemctl start hgdb-se4.5.service [root@hs01 ] systemctl status hgdb-se4.5.service |
- 查看主节点数据库的操作系统进程
[root@hs01 hgdb_log]# ps -ef|grep post root1025110 15:23 ?00:00:00 /usr/libexec/postfix/master -w postfix10261102510 15:23 ?00:00:00 qmgr -l -t unix -u postfix16252102510 16:30 ?00:00:00 pickup -l -t unix -u root2421710 17:33 ?00:00:00 /highgo/database/4.5/bin/postgres -D /highgo/database/4.5/data root24218242170 17:33 ?00:00:00 postgres: logger process root24220242170 17:33 ?00:00:00 postgres: checkpointer process root24221242170 17:33 ?00:00:00 postgres: writer process root24222242170 17:33 ?00:00:00 postgres: wal writer process root24223242170 17:33 ?00:00:00 postgres: autovacuum launcher process root24224242170 17:33 ?00:00:00 postgres: archiver process root24225242170 17:33 ?00:00:00 postgres: stats collector process root24226242170 17:33 ?00:00:00 postgres: bgworker: logical replication launcher root24238242170 17:33 ?00:00:00 postgres: sysdba highgo 192.168.0.121(39996) idle |
- 查看主节点集群守护进程
[root@hs01 hgdb_log]# ps -ef|grep repmgrd root2424010 17:33 ?00:00:00 /highgo/database/4.5/bin/repmgrd -d root24719165460 17:36 pts/200:00:00 grep --color=auto repmgrd |
- 查看主节点集群状态
- 在 status 一栏中出现 * 表示正常状态,如果出现了其他的符号(比如 !,?),表明集群状态出现故障
[root@hs01 arch]# repmgr cluster show ID | Name| Role| Status| Upstream| Location | Priority | Replication lag | Last replayed LSN ----+---------------+---------+-----------+---------------+----------+----------+-----------------+------------------- 1| 192.168.0.121 | primary | * running || default| 100| n/a| none 2| 192.168.0.122 | standby |running | 192.168.0.121 | default| 100| 0 bytes| 0/70006F0 |
- 使用systemctl方式启动
[root@hs02 ~]# systemctl start hgdb-se4.5.service [root@hs02 ~]# systemctl status hgdb-se4.5.service |
- 查看备节点数据库的操作系统进程
[root@hs02 ~]# ps -ef|grep postg root2056810 17:37 ?00:00:00 /highgo/database/4.5/bin/postgres -D /highgo/database/4.5/data root20569205680 17:37 ?00:00:00 postgres: logger process root20570205680 17:37 ?00:00:00 postgres: startup processrecovering 000000010000000000000007 root20571205680 17:37 ?00:00:00 postgres: checkpointer process root20572205680 17:37 ?00:00:00 postgres: writer process root20573205680 17:37 ?00:00:00 postgres: stats collector process root20574205680 17:37 ?00:00:00 postgres: wal receiver processstreaming 0/70006F0 root20585205680 17:37 ?00:00:00 postgres: sysdba highgo 192.168.0.122(13382) idle |
- 查看备节点集群守护进程
[root@hs02 ~]# ps -ef|grep repmgrd root2058710 17:37 ?00:00:00 /highgo/database/4.5/bin/repmgrd -d root20638155130 17:37 pts/200:00:00 grep --color=auto repmgrd |
- 查看备节点集群状态
[root@hs01 arch]# repmgr cluster show ID | Name| Role| Status| Upstream| Location | Priority | Replication lag | Last replayed LSN ----+---------------+---------+-----------+---------------+----------+----------+-----------------+------------------- 1| 192.168.0.121 | primary | * running || default| 100| n/a| none |
(1)确认数据库主备节点
[root@hs01 arch]# repmgr cluster show ID | Name| Role| Status| Upstream| Location | Priority | Replication lag | Last replayed LSN ----+---------------+---------+-----------+---------------+----------+----------+-----------------+------------------- 1| 192.168.0.121 | primary | * running || default| 100| n/a| none 2| 192.168.0.122 | standby |running | 192.168.0.121 | default| 100| 0 bytes| 0/70007D0 |
- 使用systemctl方式关闭
[root@hs02 ~]# systemctl stop hgdb-se4.5.service [root@hs02 ~]# systemctl status hgdb-se4.5.service |
- 查看备节点数据库的操作系统进程
[root@hs02 ~]# ps -ef|grep postg root26121152650 18:22 pts/100:00:00 grep --color=auto postg -- 显示已无数据库进程 |
- 查看备节点集群守护进程
[root@hs02 etc]# ps -ef|grep repmgrd root26453152650 18:26 pts/100:00:00 grep --color=auto repmgrd -- 显示已无 repmgrd -d 守护进程 |
- 使用systemctl方式关闭
[root@hs01 ~]# systemctl stop hgdb-se4.5.service [root@hs01 ~]# systemctl status hgdb-se4.5.service |
- 查看主节点数据库的操作系统进程
[root@hs01 arch]#ps -ef|grep postg root35230162700 18:35 pts/100:00:00 grep --color=auto postg -- 显示已无数据库进程 |
- 查看主节点集群守护进程
[root@hs01 arch]# ps -ef|grep repmgrd root35337162700 18:37 pts/100:00:00 grep --color=auto repmgrd -- 显示已无 repmgrd -d 守护进程 |
- 查看备节点集群状态
[root@hs01 arch]# repmgr cluster show WARNING: log_switch/true: unknown name/value pair provided; ignoring ERROR: connection to database failed DETAIL: could not connect to server: Connection refused Is the server running on host "192.168.0.121" and accepting TCP/IP connections on port 5866? DETAIL: attempted to connect using: user=sysdba password=hgdb1234 connect_timeout=2 dbname=highgo host=192.168.0.121 port=5866 fallback_application_name=repmgr 说明: 此时使用repmgr cluster show 在各个集群节点查看状态均显示无法连接数据库。此为集群关闭后的正常返回结果。 |
【如何重启数据库服务(包含单实例/流复制/集群)】
推荐阅读
- 如何实现word在线预览
- 接口测试进阶接口脚本使用--apipost(预/后执行脚本)
- 4种Golang并发操作中常见的死锁情形
- 让小伙伴们大呼过瘾的可视化作品!
- HarmonyOS 数据库系列之关系型数据库
- HTTP系列之:HTTP缓存
- Flink Table API & SQL 聚合性能调优
- 更改基于WP_Customize的WordPress模板CSS
- 更改Hestia上的节顺序