前言:一次次搭建失败,终于成功了,这篇文章作为自己在测试环境搭建的一次成功案例,记录下来,虽然都是最基本的功能,后续再完善补充,亲测可以用哦
一、POSTGRESQL主从复制搭建
一、POSTGRESQL主从复制搭建
1、安装POSTGRESQL
PGSQL10.3
下载地址:
https://yum.postgresql.org/testing/10/redhat/rhel-6-x86_64/repoview/postgresqldbserver10.group.html
下载
· postgresql10 - PostgreSQL client programs and libraries
· postgresql10-contrib - Contributed source and binaries distributed with PostgreSQL
· postgresql10-libs - The shared libraries required for any PostgreSQL clients
· postgresql10-server - The programs needed to create and run a PostgreSQL server
依次使用rpm –ivh 进行安装(两个节点都安装)
安装顺序:
rpm -ivh postgresql10-libs-10.3-1PGDG.rhel6.x86_64.rpm
rpm -ivh postgresql10-10.3-1PGDG.rhel6.x86_64.rpm
rpm -ivh postgresql10-server-10.3-1PGDG.rhel6.x86_64.rpm
rpm -ivh postgresql10-contrib-10.3-1PGDG.rhel6.x86_64.rpm
2、启动POSTGRESQL服务
(1)第一步启动
service postgresql-10 initdb
(2)第二步启动
service postgresql-10 start
3、修改监听
(1)修改监听地址
vi /var/lib/pgsql/10/data/postgresql.conf
# - Connection Settings -
#这里原本是#listen_addresses ='localhost'修改为以下黄色字体
listen_addresses = '192.168.159.128'
(2)查看监听
显示黄色特色字体即可
netstat -anlp |grep post
tcp 0 0 192.168.159.128:5432 0.0.0.0:* LISTEN 4784/postmaster
udp 0 0 ::1:57424 ::1:57424 ESTABLISHED 4784/postmaster
unix 2 [ ACC ] STREAM LISTENING 30353 4784/postmaster /var/run/postgresql/.s.PGSQL.5432
unix 2 [ ACC ] STREAM LISTENING 30356 4784/postmaster /tmp/.s.PGSQL.5432
4、主从搭建
(1)主库环境
su - postgres
psql
创建一个用户复制的用户replica
CREATE ROLE replica login replication encrypted password 'replica';
修改pg_hba.conf文件,指定replica登录网络(最后一添加)
vi /var/lib/pgsql/10/data/pg_hba.conf
host replication replica 192.168.159.0/24 md5
host all replica 192.168.159.0/24 trust
主库配置文件修改以下几项,其他不变
vi /var/lib/pgsql/10/data/postgresql.conf
listen_addresses = '*'
wal_level = hot_standby #热备模式
max_wal_senders= 6 #可以设置最多几个流复制链接,差不多有几个从,就设置多少
wal_keep_segments = 10240 #重要配置
wal_send_timeout = 60s
max_connections = 512 #从库的 max_connections要大于主库
archive_mode = on #允许归档
archive_command = 'cp %p /url/path%f' #根据实际情况设置
host all replica 192.168.159.0/24 trust
主库配置文件修改以下几项,其他不变
vi /var/lib/pgsql/10/data/postgresql.conf
listen_addresses = '*'
wal_level = hot_standby #热备模式
max_wal_senders= 6 #可以设置最多几个流复制链接,差不多有几个从,就设置多少
wal_keep_segments = 10240 #重要配置
wal_send_timeout = 60s
max_connections = 512 #从库的 max_connections要大于主库
archive_mode = on #允许归档
archive_command = 'cp %p /url/path%f' #根据实际情况设置
(2)从库环境
把备库的数据文件夹目录清空
rm -rf /var/lib/pgsql/10/data/*
rm -rf /var/lib/pgsql/10/data/*
在备库上运行
pg_basebackup -F p --progress -D /var/lib/pgsql/10/data/ -h 192.168.159.128 -p 5432 -U replica --password
输入密码replica
!!!注意,复制完成后,在备库一定要将数据目录下的所有文件重新授权
chown -R postgres.postgres /var/lib/pgsql/10/data/
创建recovery.cnf 文件
cp /usr/pgsql-10/share/recovery.conf.sample /var/lib/pgsql/10/data/recovery.conf
vi /var/lib/pgsql/10/data/recovery.conf
standby_mode = on
primary_conninfo = 'host=192.168.159.128 port=5432 user=replica password=replica'
recovery_target_timeline = 'latest'
trigger_file = '/var/lib/pgsql/10/data/trigger.kenyon'
vi /var/lib/pgsql/10/data/postgresql.conf
listen_addresses ='*'
listen_addresses ='*'
wal_level = hot_standby
max_connections =1000 #一般从的最大链接要大于主的
hot_standby =on #说明这台机器不仅仅用于数据归档,也用于查询
max_standby_streaming_delay =30s
wal_receiver_status_interval = 10s #多久向主报告一次从的状态
hot_standby_feedback = on #如果有错误的数据复制,是否向主进行范例
启动备库
service postgresql-10 start
如果无法启动,如下操作:
scp /var/lib/pgsql/10/data/postmaster.opts 192.168.159.129:/var/lib/pgsql/10/data/
chown -R postgres.postgres /var/lib/pgsql/10/data/
chmod 700 data/
5、验证主从功能
主库查询
su - postgres
psql
postgres=# select client_addr,sync_state from pg_stat_replication;
client_addr | sync_state
-----------------+------------
192.168.159.129 | async
(1 row)
在主库创建表
create table test2 (name varchar(3));
在从库查询表
\d
主库查询黄色字体
[root@ha1 data]# ps aux | grep postgres
postgres 4686 0.0 0.8 356584 15592 ? S 15:22 0:00 /usr/pgsql-10/bin/postmaster -D /var/lib/pgsql/10/data
postgres 4688 0.0 0.0 209416 1572 ? Ss 15:22 0:00 postgres: logger process
postgres 4690 0.0 0.1 356700 3744 ? Ss 15:22 0:00 postgres: checkpointer process
postgres 4691 0.0 0.1 356584 3044 ? Ss 15:22 0:00 postgres: writer process
postgres 4692 0.0 0.3 356584 5980 ? Ss 15:22 0:00 postgres: wal writer process
postgres 4693 0.0 0.1 356992 2616 ? Ss 15:22 0:00 postgres: autovacuum launcher process
postgres 4694 0.0 0.0 211516 1540 ? Ss 15:22 0:00 postgres: archiver process
postgres 4695 0.0 0.0 211648 1784 ? Ss 15:22 0:02 postgres: stats collector process
postgres 4696 0.0 0.1 356876 2272 ? Ss 15:22 0:00 postgres: bgworker: logical replication launcher
postgres 4711 0.7 0.4 358576 8536 ? Ss 15:24 0:35 postgres: dbuser exampledb 192.168.159.1(63172) idle
postgres 4845 0.0 0.1 357384 3424 ? Ss 15:39 0:00 postgres: wal sender process replica 192.168.159.129(46554) streaming 0/401B0A0
root 5049 0.0 0.0 103256 840 pts/3 S+ 16:42 0:00 grep postgres
备库查询黄色字体
[root@ha2 data]# ps aux | grep postgres
postgres 4827 0.0 2.2 398228 42844 ? S 15:39 0:00 /usr/pgsql-10/bin/postmaster -D /var/lib/pgsql/10/data
postgres 4829 0.0 0.0 209416 1536 ? Ss 15:39 0:00 postgres: logger process
postgres 4830 0.0 0.2 398324 3948 ? Ss 15:39 0:00 postgres: startup process recovering 000000010000000000000004
postgres 4831 0.0 0.1 398228 3496 ? Ss 15:39 0:00 postgres: checkpointer process
postgres 4832 0.0 0.1 398228 2772 ? Ss 15:39 0:00 postgres: writer process
postgres 4833 0.0 0.0 211516 1604 ? Ss 15:39 0:00 postgres: stats collector process
postgres 4834 0.1 0.1 405444 3688 ? Ss 15:39 0:06 postgres: wal receiver process streaming 0/401B0A0
root 4912 0.0 0.0 103252 828 pts/3 S+ 16:42 0:00 grep postgres
6、POSTGRESQL相关
(1)创建LINUX用户
useradd dbuser
passwd dbuser
(2)创建数据库用户
CREATE USER dbuser WITH PASSWORD 'password';
(3)创建数据库
CREATE DATABASE exampledb OWNER dbuser;
(4)赋予权限
GRANT ALL PRIVILEGES ON DATABASE exampledb TO dbuser;
(5)以dbuser的身份连接数据库exampledb
su - dbuser
psql -d exampledb
(6)网络登陆方式
vi /var/lib/pgsql/10/data/pg_hba.conf(在IPV4那里添加以下)
host exampledb dbuser 192.168.159.0/24 trust
切换dbuser用户登陆
psql -h 192.168.159.128 -U dbuser -p password -d exampledb -p 5432
(7)重启和停止命令
service postgresql-10 restart
service postgresql-10 stop
(8)重要配置文件
pg_hba.conf
postgresql.conf
(9)PG的安装目录及命令目录
查看进程是否启动,可以找到
命令目录/usr/pgsql-10/bin/
安装目录/var/lib/pgsql/10/data
[root@ha1 ~]# ps -ef|grep post
root 1732 1 0 02:25 ? 00:00:00 /usr/libexec/postfix/master
postfix 1742 1732 0 02:25 ? 00:00:00 pickup -l -t fifo -u
postfix 1743 1732 0 02:25 ? 00:00:00 qmgr -l -t fifo -u
postgres 2322 1 1 02:38 ? 00:00:00 /usr/pgsql-10/bin/postmaster -D /var/lib/pgsql/10/data
postgres 2324 2322 0 02:38 ? 00:00:00 postgres: logger process
postgres 2329 2322 0 02:38 ? 00:00:00 postgres: checkpointer process
postgres 2330 2322 0 02:38 ? 00:00:00 postgres: writer process
postgres 2331 2322 0 02:38 ? 00:00:00 postgres: wal writer process
postgres 2332 2322 0 02:38 ? 00:00:00 postgres: autovacuum launcher process
postgres 2333 2322 0 02:38 ? 00:00:00 postgres: stats collector process
postgres 2334 2322 0 02:38 ? 00:00:00 postgres: bgworker: logical replication launcher
root 2336 2232 0 02:39 pts/0 00:00:00 grep post
7、主备手动切换
7、主备手动切换
