时间:2021-07-01 10:21:17 帮助过:7人阅读
-D指定把备份写到哪个目录,这里尤其要注意一点就是做基础备份之前从库的数据目录(/data/psql110/data/)目录需要手动清空。
-l表示指定一个备份的标识。
[postgres@localhost data]$ cat /data/psql110/data/recovery.conf
standby_mode = ‘on‘
primary_conninfo = ‘user=replica password=replica host=10.0.0.100 port=10280 sslmode=prefer sslcompression=1 krbsrvname=postgres‘
运行命令后看到如下进度提示就说明生成基础备份成功:
[postgres@localhost data]$ pg_basebackup -h 10.0.0.100 -U replica -p 10280 -F p -x -P -R -D /data/psql110/data/ -l replbackup
Password: 密码(replica)
46256/46256 kB (100%), 1/1 tablespace
[postgres@localhost data]$
如上由于我们在pg_hba.conf中指定的md5认证方式,所以需要输入密码。最后还需要修改一下从库数据目录下的postgresql.conf文件,将hot_standby改为启用状态,即hot_standby=on。到此为止就算配置结束了,我们现在可以启动从库,
[postgres@localhost data]$ egrep -v ‘^#|^$‘ /data/psql110/data/postgresql.conf|grep "hot_standby"
wal_level = hot_standby # minimal, archive, hot_standby, or logical
hot_standby = on # "on" allows queries during recovery
[postgres@localhost data]$ pg_ctl -D /data/psql110/data -l /data/psql110/log/postgres.log start
server starting
从库上查看到流复制进程:
[postgres@localhost data]$ ss -lntup|grep postgres
tcp LISTEN 0 128 :::10280 :::* users:(("postgres",23161,4))
tcp LISTEN 0 128 *:10280 *:* users:(("postgres",23161,3))
[postgres@localhost data]$ ps -ef|grep postgres
root 5663 4716 0 18:12 pts/0 00:00:00 su - postgres
postgres 5664 5663 0 18:12 pts/0 00:00:00 -bash
postgres 5855 5664 0 18:13 pts/0 00:00:00 /bin/bash /usr/local/pgsql/bin/psql
postgres 5857 5855 0 18:13 pts/0 00:00:00 /usr/local/pgsql/bin/psql.bin
root 12406 7244 0 18:34 pts/1 00:00:00 su - postgres
postgres 12407 12406 0 18:34 pts/1 00:00:00 -bash
root 13861 13810 0 18:47 pts/3 00:00:00 su - postgres
postgres 13862 13861 0 18:47 pts/3 00:00:00 -bash
root 21768 21736 0 19:54 pts/2 00:00:00 su - postgres
postgres 21769 21768 0 19:54 pts/2 00:00:00 -bash
postgres 23161 1 0 20:05 pts/2 00:00:00 /usr/local/pgsql/bin/postgres -D /data/psql110/data
postgres 23164 23161 0 20:05 ? 00:00:00 postgres: startup process recovering 000000010000000000000007
postgres 23165 23161 0 20:05 ? 00:00:00 postgres: checkpointer process
postgres 23166 23161 0 20:05 ? 00:00:00 postgres: writer process
postgres 23167 23161 0 20:05 ? 00:00:00 postgres: stats collector process
postgres 23168 23161 0 20:05 ? 00:00:00 postgres: wal receiver process streaming 0/7000140
postgres 23240 21769 0 20:06 pts/2 00:00:00 ps -ef
postgres 23241 21769 0 20:06 pts/2 00:00:00 grep postgres
主库上查看到流复制进程:
[postgres@localhost pg_xlog]$ ps -ef|grep postgres
root 2904 2642 0 00:40 pts/0 00:00:00 su - postgres
postgres 2905 2904 0 00:40 pts/0 00:00:00 -bash
postgres 2939 1 0 00:42 pts/0 00:00:00 /usr/local/pgsql/bin/postgres -D /data/pgsql100/data
postgres 2941 2939 0 00:42 ? 00:00:00 postgres: checkpointer process
postgres 2942 2939 0 00:42 ? 00:00:00 postgres: writer process
postgres 2943 2939 0 00:42 ? 00:00:00 postgres: wal writer process
postgres 2944 2939 0 00:42 ? 00:00:00 postgres: autovacuum launcher process
postgres 2945 2939 0 00:42 ? 00:00:00 postgres: stats collector process
root 3109 3064 0 00:58 pts/2 00:00:00 su - postgres
postgres 3110 3109 0 00:58 pts/2 00:00:00 -bash
postgres 3151 3110 0 00:59 pts/2 00:00:00 /bin/bash /usr/local/pgsql/bin/psql -p10280
postgres 3153 3151 0 00:59 pts/2 00:00:00 /usr/local/pgsql/bin/psql.bin -p10280
root 3189 3087 0 01:07 pts/3 00:00:00 su - postgres
postgres 3190 3189 0 01:07 pts/3 00:00:00 -bash
postgres 3272 2939 0 01:25 ? 00:00:00 postgres: postgres testdb01 [local] idle
postgres 3415 2939 0 02:16 ? 00:00:00 postgres: wal sender process replica 10.0.0.110(34021) streaming 0/7000140
postgres 3422 3190 0 02:17 pts/3 00:00:00 ps -ef
postgres 3423 3190 0 02:17 pts/3 00:00:00 grep postgres
此时从库上可以看到流复制的进程,同样的主库也能看到该进程。表明主从流复制配置成功。
同步测试演示:
创建库和建表做测试,在master服务器(10.0.0.100)中的创建testdb02库并且建一张表并添加几条数据:
master上操作:
postgres=# create database testdb02;
CREATE DATABASE
检查:
[postgres@localhost pg_xlog]$ psql -p10280 -c ‘\list‘|grep testdb02
testdb02 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
testdb01=# \c testdb02
You are now connected to database "testdb02" as user "postgres".
testdb02=# \d
No relations found.
创建表:
CREATE TABLE weather ( city varchar(80), temp_lo int, temp_hi int, prcp real,date date);
testdb02=# \d
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
public | weather | table | postgres
(1 row)
testdb02=# \d weather
Table "public.weather"
Column | Type | Modifiers
---------+-----------------------+-----------
city | character varying(80) |
temp_lo | integer |
temp_hi | integer |
prcp | real |
date | date |
testdb02=#
testdb02=# INSERT INTO weather (city, temp_lo, temp_hi, prcp, date) VALUES (‘China05‘, ‘47‘, ‘59‘, ‘1.0‘, ‘1994-12-15‘);
INSERT 0 1
testdb02=# INSERT INTO weather (city, temp_lo, temp_hi, prcp, date) VALUES (‘China04‘, ‘46‘, ‘58‘, ‘2.0‘, ‘1994-12-14‘);\
INSERT 0 1
testdb02=# select * from weather;
city | temp_lo | temp_hi | prcp | date
---------+---------+---------+------+------------
China05 | 47 | 59 | 1 | 1994-12-15
China04 | 46 | 58 | 2 | 1994-12-14
(2 rows)
testdb02=#
从库上检查:
[postgres@localhost data]$ psql -p10280 -c ‘\list‘|grep testdb02
testdb02 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres=# \c testdb02;
You are now connected to database "testdb02" as user "postgres".
testdb02=# \d
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
public | weather | table | postgres
(1 row)
testdb02=# \d weather;
Table "public.weather"
Column | Type | Modifiers
---------+-----------------------+-----------
city | character varying(80) |
temp_lo | integer |
temp_hi | integer |
prcp | real |
date | date |
testdb02=# select * from weather;
city | temp_lo | temp_hi | prcp | date
---------+---------+---------+------+------------
China05 | 47 | 59 | 1 | 1994-12-15
China04 | 46 | 58 | 2 | 1994-12-14
(2 rows)
testdb02=#
可以看到完美同步,那么从库是否能删除呢?测试一下:
从库上测试删除数据库testdb02;
postgres=# drop database testdb02;
ERROR: cannot execute DROP DATABASE in a read-only transaction
postgres=# drop database testdb01;
ERROR: cannot execute DROP DATABASE in a read-only transaction
standby的数据无法删除,正如我们之前说的,standby只提供只读服务,而只有master才能进行读写操作,所以master才有权限删除数据。master删除的同时standby中的数据也将同步删除,
主库中执行
关于异步流复制的内容到这里.
参考博文:
http://blog.csdn.net/wzyzzu/article/details/53331206
本文出自 “10931853” 博客,请务必保留此出处http://wujianwei.blog.51cto.com/10931853/1977210
postgresql主从实现之异步流复制
标签:postgresql主从实现之异步流复制