发布时间:2020-06-14 19:40:55来源:阅读:
由于某个项目前期资源紧张,使用的Postgresql单节点数据库;为解决项目数据库安全性问题,小蜗牛应要求整理Postgresql数据库主从高可用方案,以下操作为部署主从流复制测试过程,后续整理pgpool高可用、负载均衡方案。。
yum install -y gcc-c++ zlib-devel vim openssh-clients
tar zxvf postgresql-9.3.4.tar.gz
mkdir /opt/pgsql-9.3
cd postgresql-9.3.4
./configure --prefix=/opt/pgsql-9.3/ --without-readline
make -j 2
make install
vim /etc/profile
export PGHOME=/opt/pgsql-9.3
export PGDATA=/var/lib/pgsql/9.3/data/
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGHOME/lib/
export PATH=$PGHOME/bin:$PATH:$HOME/bin
source /etc/profile
useradd postgres -d /var/lib/pgsql
su - postgres
initdb
pg_ctl -D /var/lib/pgsql/9.3/data/ -l logfile start
psql
postgres# CREATE ROLE replica login replication encrypted password 'replica';
vi /var/lib/pgsql/9.3/data/pg_hba.conf
host replication replica 192.168.52.0/24 trust
vi /var/lib/pgsql/9.3/data/postgresql.conf
listen_addresses = '*'
wal_level = hot_standby # 这个是设置主为wal的主机
max_wal_senders = 2 # 这个设置了可以最多有几个流复制连接,差不多有几个从,就设置几个
wal_keep_segments = 7 # 设置流复制保留的最多的xlog数目
wal_sender_timeout = 60s # 设置流复制主机发送数据的超时时间
max_connections = 100 # 这个设置要注意下,从库的max_connections必须要大于主库的
pg_ctl -D /var/lib/pgsql/9.3/data/ -l logfile restart
su - postgres
pg_basebackup -F p --progress -D $PGDATA -h 192.168.52.101 -p 5432 -U replica --password
vim $PGDATA/recovery.conf
standby_mode = on # 这个说明这台机器为从库
primary_conninfo = 'host=192.168.52.101 port=5432 user=replica password=replica' # 这个说明这台机器对应主库的信息
trigger_file = '/var/lib/pgsql/9.3/data/postgresql.trigger.192.168.52.101'
recovery_target_timeline = 'latest' # 这个说明这个流复制同步到最新的数据
vi $PGDATA/postgresql.conf
max_connections = 1000 # 一般查多于写的应用从库的最大连接数要比较大
hot_standby = on # 说明这台机器不仅仅是用于数据归档,也用于数据查询
max_standby_streaming_delay = 30s # 数据流备份的最大延迟时间
wal_receiver_status_interval = 1s # 多久向主报告一次从的状态,当然从每次数据复制都会向主报告状态,这里只是设置最长的间隔时间
hot_standby_feedback = on # 如果有错误的数据复制,是否向主进行反馈
$ psql
psql (9.3.4)
Type "help" for help.
postgres=# select client_addr,sync_state from pg_stat_replication;
client_addr | sync_state
----------------+------------
192.168.52.102 | async
(1 row)
postgres=#
ps aux | grep postgres
主节点有如下进程
postgres 74442 0.0 0.2 258652 2424 ? Ss 15:02 0:00 postgres: wal sender process replica 192.168.52.102(58199) streaming 0/30101D0
从节点有如下进程
postgres 1676 0.0 0.0 113328 748 ? Ss 15:01 0:00 postgres: stats collector process
postgres@master ~]$ psql
psql (9.3.4)
Type "help" for help.
postgres=# create table t_user(id int primary key,name varchar(30));
CREATE TABLE
postgres=# dt
List of relations
Schema | Name | Type | Owner
--------+--------+-------+----------
public | t_user | table | postgres
(1 row)
postgres=# insert into t_user values(1,'kbson');
INSERT 0 1
postgres=# insert into t_user values(2,'kbsonlong');
INSERT 0 1
postgres=# select * from t_user;
id | name
----+-----------
1 | kbson
2 | kbsonlong
(2 rows)
postgres=#
从库查看是否同步
[postgres@slave ~]$ psql
psql (9.3.4)
Type "help" for help.
postgres=# dt
No relations found. ##最开始从库并没有表
postgres=# dt
List of relations
Schema | Name | Type | Owner
--------+--------+-------+----------
public | t_user | table | postgres
(1 row)
postgres=# select * from t_user;
id | name
----+-----------
1 | kbson
2 | kbsonlong
(2 rows)
postgres=#
[postgres@master data]$ pg_ctl stop
此时查看从库会提示连接不上主库
LOG: replication terminated by primary server
DETAIL: End of WAL reached on timeline 1 at 0/3000518.
FATAL: could not send end-of-streaming message to primary: no COPY in progress
LOG: record with zero length at 0/3000518
FATAL: could not connect to the primary server: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
FATAL: could not connect to the primary server: could not connect to server: Connection refused
Is the server running on host "192.168.52.101" and accepting
TCP/IP connections on port 5432?
FATAL: could not connect to the primary server: could not connect to server: Connection refused
Is the server running on host "192.168.52.101" and accepting
TCP/IP connections on port 5432?
在从库生成一个触发文件,就是在配置从库中recovery.conf中配置的trigger_file
[postgres@kbsonlong data]$ touch /var/lib/pgsql/9.3/data/postgresql.trigger.5432
[postgres@kbsonlong data]$ ll /var/lib/pgsql/9.3/data/postgresql.trigger.5432
-rw-rw-r-- 1 postgres postgres 0 Jul 30 17:49 /var/lib/pgsql/9.3/data/postgresql.trigger.5432
此时日志会提示找到触发文件,自动切换为新的主库
LOG: trigger file found: /var/lib/pgsql/9.3/data/postgresql.trigger.5432
LOG: redo done at 0/30004B0
LOG: last completed transaction was at log time 2017-07-30 17:38:43.592351+08
LOG: selected new timeline ID: 2
LOG: archive recovery complete
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
在新的主库写入测试数据,方便后面旧的主库恢复为从库是查看是否同步数据
postgres=# select * from t_user;
id | name
----+-----------
1 | kbson
2 | kbsonlong
(2 rows)
postgres=# insert into t_user values(3,'kbsonlong3');
insert into t_user values(4,'kbsonlong4');
insert into t_user values(5,'kbsonlong5'); INSERT 0 1
postgres=# INSERT 0 1
postgres=#
INSERT 0 1
postgres=# select * from t_user;
id | name
----+------------
1 | kbson
2 | kbsonlong
3 | kbsonlong3
4 | kbsonlong4
5 | kbsonlong5
(5 rows)
postgres=#
[postgres@master data]$ cat recovery.conf |grep -v '^#'
standby_mode = on
primary_conninfo = 'host=192.168.52.102 port=5432 user=replica password=replica' ##注意host指向新的主库
trigger_file = '/var/lib/pgsql/9.3/data/postgresql.trigger.5432'
recovery_target_timeline = 'latest'
[postgres@master data]$
pg_ctl start
[postgres@master data]$ psql
psql (9.3.4)
Type "help" for help.
postgres=# select * from t_user;
id | name
----+------------
1 | kbson
2 | kbsonlong
3 | kbsonlong3
4 | kbsonlong4
5 | kbsonlong5
(5 rows)
postgres=# insert into t_user values(6,'kbsonlong6');
ERROR: cannot execute INSERT in a read-only transaction
STATEMENT: insert into t_user values(6,'kbsonlong6');
ERROR: cannot execute INSERT in a read-only transaction
postgres=#
可以看到刚才在新的主库写入的测试数据已经同步,并且无法再次新的从库写入数据
PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下
Linux文件即为linux系统上的启动脚本
#chmod a+x linux
#cp linux /etc/init.d/postgresql
prefix设置为postgresql的安装路径:/opt/pgsql-9.3
PGDATA设置为postgresql的数据目录路径:/var/libg/pgsql/9.3/data
#service postgresql start
#chkconfig --add postgresql
Valentina Studio (数据库管理工具) v10.5.3 免费版
43.06MB
db commander(数据库处理工具)5.65 中文汉化版
7.3M
mongodb(开源数据库软件)下载 v4.0.3官方免费版
219M
oracle11g(数据库管理系统)v11.2.0.1.0 精简版
4.17GB
pl sql developer(Oracle数据库存储程序单元的开发软件)2021 免费版
25.2M
HeidiSQL(MySQL服务器数据管理工具) v11.0.0.6055 免费版
10.1M
betterwmf(CAD图像复制软件)v7.5 官方版
2.7M
easyrecovery pro(硬盘数据恢复工具) v14.0.0.4 免费版
78.6M
奇奇剪贴复制工具 v4.7 免费版
2.69M
小牛文件恢复软件(数据恢复工具) v4.8.0 最新版
6.1M
硬盘保护卡克星(数据破解抹除工具) 1.2 免费版
0.11MB
阿里云盘(数据同步软件) v1.4 电脑版
42.4M
顶尖数据恢复软件(电脑数据恢复工具) 6.30 破解版
7.5M
流星游戏加速器下载
15.81 MB
流量宝官方下载
44.2M
Autodesk CFD(流体三维仿真软件) v2021 免费版
553KB
Emby Server(流媒体服务软件,)v4.0.2.0 破解版
101M
旺财流水账(财务管理软件) 9.2 免费版
4.85MB
爱莫流程图下载
1.8M
迅捷流程图制作软件 v3.5.0.0 官方版
1.9M
2020-03-23
broadcom 43XX无线频繁掉线,掉线后无法搜到网络
如何重新安装Flash Player
Kubernetes主机间curl cluster ip时通时不通
Ideapad Y330如何使用恢复光盘来重新安装Windows XP
手机与电脑无法连接
安装配置docker私有仓库
万全T168服务器类似救护车报警声故障分析
MariaDB初学者管理命令
增加Linux最大打开文件数/文件描述符