PostgreSQL主从复制部署
目录
1.主从介绍
2.主从机器分配
3.安装postgresql
4.主库配置
4.1 创建复制用户,进行主从同步使用
4.2 主库上配置从库采用repl账号
4.3 修改主库配置文件data目录下的postgresql.conf,按照下面的配置进行修改。
4.4 重启主库服务
5.从库配置
5.1 从库安装完成后,不初始化,若已经初始化,删除其data目录
5.2 从库配置文件配置,根据下面的配置进行修改。
5.3 创建恢复文件recovery.conf
5.4 启动从库服务
- 验证主从配置
1.主从介绍
PostgreSQL在9之后引入了主从的流复制机制,集群中主库工作在连续归档模式下,备库工作在恢复模式下,备库持续从主服务器读取WAL文件;连续归档不需要对数据库表做任何改动,对主服务器的性能影响相对较低。我们采取的是基于文件日志传送和流复制的方式。
2.主从机器分配
IP地址 | DB版本 | 主从关系 |
172.18.1.23 | 9.6.3 | 主 |
172.18.1.24 | 9.6.3 | 从 |
3.安装postgresql
postgresql的单机部署
我们安装在2个机器上安装postgresql,上面的连接是单机部署文档。
4.主库配置
4.1 创建复制用户,进行主从同步使用
psql (9.6.3)
Type "help" for help.
#授权
postgres=# create role repl login replication encrypted password 'baifendian';
CREATE ROLE
4.2 主库上配置从库采用repl账号
vim /opt/postgresql/data/pg_hba.conf
#只需要台添加下面两行,repl是用来做备份的用户,后面的172.18.1.0/24是该网段内的IP地址
host replication repl 172.18.1.0/24 md5
host all repl 172.18.1.0/24 trust
4.3 修改主库配置文件data目录下的postgresql.conf,按照下面的配置进行修改。
[root@bfd-yiz-1p23 /opt/postgresql/data]# cat postgresql.conf |grep -v "^#"|grep -v "^$"|grep -v $'^\t'
listen_addresses = '*' # what IP address(es) to listen on;
port = 5432 # (change requires restart)
max_connections = 512 # (change requires restart)
shared_buffers = 128MB # min 128kB
dynamic_shared_memory_type = posix # the default is the first option
wal_level = replica # minimal, replica, or logical
archive_mode = on # enables archiving; off, on, or always
archive_command = 'cp %p /opt/postgresql/data/pg_archive/%f' # command to use to archive a logfile segment
max_wal_senders = 6 # max number of walsender processes
wal_keep_segments = 10240 # in logfile segments, 16MB each; 0 disables
wal_sender_timeout = 60s # in milliseconds; 0 disables
log_directory = '/opt/postgresql/log' # directory where log files are written
修改完,要创建刚刚配置的一些目录结构:
[root@bfd-yiz-1p23 /opt/postgresql/data]# mkdir /opt/postgresql/data/pg_archive
[root@bfd-yiz-1p23 /opt/postgresql]# mkdir log
4.4 重启主库服务
pg_ctl -D /opt/postgresql/data -l logfile restart
查看进程:
5.从库配置
5.1 从库安装完成后,不初始化,若已经初始化,删除其data目录
这时候会把主节点那边的数据文件都拷贝过来了。
[postgres@bfd-yiz-1p24 /opt/postgresql/data]$ rm -rf *
[postgres@bfd-yiz-1p24 /opt/postgresql/data]$ ls
[postgres@bfd-yiz-1p24 /opt/postgresql/data]$
[postgres@bfd-yiz-1p24 /opt/postgresql/data]$ pg_basebackup -h 172.18.1.23 -U repl -D /opt/postgresql/data -X stream -P
Password:
39209/39209 kB (100%), 1/1 tablespace
[postgres@bfd-yiz-1p24 /opt/postgresql/data]$ ls #看到数据,文件都已经备份过来了
backup_label.old pg_archive pg_hba.conf pg_notify pg_stat pg_twophase postgresql.conf
base pg_clog pg_ident.conf pg_replslot pg_stat_tmp PG_VERSION postmaster.opts
global pg_commit_ts pg_logical pg_serial pg_subtrans pg_xlog postmaster.pid
logfile pg_dynshmem pg_multixact pg_snapshots pg_tblspc postgresql.auto.conf
备注:
-h,主库主机,-p,主库服务端口;
-U,复制用户;
-F,p是默认输出格式,输出数据目录和表空间相同的布局,t表示tar格式输出;
-P,同—progress,显示进度;
-D,输出到指定目录;
因为主库采用的是md5认证,这里需要密码认证。
5.2 从库配置文件配置,根据下面的配置进行修改。
[root@bfd-yiz-1p24 /opt/postgresql/data]# cat postgresql.conf |grep -v "^#"|grep -v "^$"|grep -v $'^\t'
listen_addresses = '*' # what IP address(es) to listen on;
port = 5432 # (change requires restart)
max_connections = 1000 # (change requires restart)
shared_buffers = 128MB # min 128kB
dynamic_shared_memory_type = posix # the default is the first option
wal_level = replica # minimal, replica, or logical
archive_mode = on # enables archiving; off, on, or always
archive_command = 'cp %p /opt/postgresql/data/pg_archive/%f' # command to use to archive a logfile segment
wal_sender_timeout = 60s # in milliseconds; 0 disables
hot_standby = on # "on" allows queries during recovery
max_standby_streaming_delay = 30s # max delay before canceling queries
wal_receiver_status_interval = 10s # send replies at least this often
hot_standby_feedback = on # send info from standby to prevent
log_directory = '/opt/postgresql/log' # directory where log files are written,
5.3 创建恢复文件recovery.conf
[postgres@bfd-yiz-1p24 /opt/postgresql]$ cp /opt/postgresql/share/recovery.conf.sample recovery.conf
# 调整参数:
recovery_target_timeline = 'latest' #同步到最新数据
standby_mode = on #指明从库身份
primary_conninfo = 'host=172.18.1.23 port=5432 user=repl password=baifendian' #连接到主库信息
5.4 启动从库服务
pg_ctl -D /opt/postgresql/data -l logfile restart
查看你进程
6. 验证主从配置
[postgres@bfd-yiz-1p23 /opt/postrgesql]$ psql
psql (9.6.3)
Type "help" for help.
postgres=# select client_addr,sync_state from pg_stat_replication;
client_addr | sync_state
-------------+------------
172.18.1.24 | async
(1 row)
postgres=#
在主机器上创建数据库,检查从数据库上是否同步。
主:我们创建一个数据库wyl
[postgres@bfd-yiz-1p23 /opt/postrgesql]$ psql
psql (9.6.3)
Type "help" for help.
postgres=# select client_addr,sync_state from pg_stat_replication;
client_addr | sync_state
-------------+------------
172.18.1.24 | async
(1 row)
postgres=# create database wyl;
从:在主上创建了一个wyl数据库,在从上我们发现也有一个对应的wyl数据库对应。
psql (9.6.3)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
wyl | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)
postgres=#
还没有评论,来说两句吧...