本篇內(nèi)容主要講解“PostgreSQL怎么搭建流復(fù)制”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“PostgreSQL怎么搭建流復(fù)制”吧!
PostgreSQL通過(guò)流復(fù)制Streaming Replication可輕松實(shí)現(xiàn)高可用HA環(huán)境的搭建.
Step 1 主庫(kù):創(chuàng)建用戶
創(chuàng)建復(fù)制用戶replicator
testdb=# CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'replicator'; CREATE ROLE
Step 2 主庫(kù):參數(shù)配置
配置archive_mode等參數(shù)
archive_mode = ON wal_level = replica max_wal_senders = 10 archive_command = '/home/xdb/archive.sh %p %f' listen_addresses = '*'
也可用alter system命令修改
ALTER SYSTEM SET wal_level TO 'replica'; ALTER SYSTEM SET archive_mode TO 'ON'; ALTER SYSTEM SET max_wal_senders TO '10'; ALTER SYSTEM SET listen_addresses TO '*';
重啟數(shù)據(jù)庫(kù)
pg_ctl -D $PGDATA restart -mf
Step 3 主庫(kù):訪問(wèn)配置
修改pg_hba.conf文件
host replication replicator 192.168.26.26/32 md5
生效配置
pg_ctl -D $PGDATA reload
Step 4 從庫(kù):從主庫(kù)備份中恢復(fù)
在從庫(kù)上使用pg_basebackup創(chuàng)建備庫(kù)
192.168.26.25是主庫(kù)IP,192.168.26.26是從庫(kù)IP
pg_basebackup -h 192.168.26.25 -U replicator -p 5432 -D $PGDATA -P -Xs -R
配置從庫(kù)postgres.conf
hot_standby = ON hot_standby_feedback = ON ALTER SYSTEM SET hot_standby TO 'ON'; ALTER SYSTEM SET hot_standby_feedback TO 'ON';
配置從庫(kù)recovery.conf
$ cat $PGDATA/recovery.conf standby_mode = 'on' primary_conninfo = 'host=192.168.26.25 port=5432 user=replicator password=replicator' restore_command = 'cp /data/archivelog/%f %p' archive_cleanup_command = 'pg_archivecleanup /data/archivelog %r'
Step 5 從庫(kù):啟動(dòng)數(shù)據(jù)庫(kù)
[xdb@localhost testdb]$ pg_ctl -D $PGDATA start waiting for server to start....2019-03-13 12:13:30.239 CST [1870] LOG: listening on IPv4 address "0.0.0.0", port 5432 2019-03-13 12:13:30.239 CST [1870] LOG: listening on IPv6 address "::", port 5432 2019-03-13 12:13:30.252 CST [1870] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432" 2019-03-13 12:13:30.379 CST [1870] LOG: redirecting log output to logging collector process 2019-03-13 12:13:30.379 CST [1870] HINT: Future log output will appear in directory "pg_log". done server started
Step 6 驗(yàn)證復(fù)制環(huán)境
確認(rèn)相關(guān)進(jìn)程是否已啟動(dòng)
#主庫(kù) [xdb@localhost testdb]$ ps -ef|grep sender xdb 1646 1532 0 12:13 ? 00:00:00 postgres: walsender replicator 192.168.26.26(35294) streaming 0/43000140 xdb 1659 1440 0 12:17 pts/1 00:00:00 grep --color=auto sender [xdb@localhost testdb]$ #從庫(kù) [xdb@localhost testdb]$ ps -ef|grep receiver xdb 1879 1870 0 12:13 ? 00:00:00 postgres: walreceiver streaming 0/43000140 xdb 1884 1799 0 12:18 pts/0 00:00:00 grep --color=auto receiver [xdb@localhost testdb]$ ps -ef|grep startup xdb 1872 1870 0 12:13 ? 00:00:00 postgres: startup recovering 000000100000000000000043 xdb 1887 1799 0 12:18 pts/0 00:00:00 grep --color=auto startup [xdb@localhost testdb]$
Step 7 監(jiān)控
查詢pg_stat_replication數(shù)據(jù)字典表
testdb=# SELECT * FROM pg_stat_replication; -[ RECORD 1 ]----+------------------------------ pid | 1646 usesysid | 90113 usename | replicator application_name | walreceiver client_addr | 192.168.26.26 client_hostname | client_port | 35294 backend_start | 2019-03-13 12:13:30.852269+08 backend_xmin | state | streaming sent_lsn | 0/43000140 write_lsn | 0/43000140 flush_lsn | 0/43000140 replay_lsn | 0/43000140 write_lag | flush_lag | replay_lag | sync_priority | 0 sync_state | async testdb=#
同步復(fù)制
從庫(kù)配置參數(shù)recovery.conf,在primary_conninfo中添加application_name
primary_conninfo = 'user=replicator password=replicator host=192.168.26.25 port=5432 application_name = standby_26'
主庫(kù)配置參數(shù)
synchronous_standby_names = 'standby_26' synchronous_commit = on
重啟數(shù)據(jù)庫(kù),驗(yàn)證是否配置成功
testdb=# \x Expanded display is on. testdb=# SELECT * FROM pg_stat_replication; -[ RECORD 1 ]----+------------------------------ pid | 2257 usesysid | 90113 usename | replicator application_name | standby_26 client_addr | 192.168.26.26 client_hostname | client_port | 35418 backend_start | 2019-03-13 15:17:57.330573+08 backend_xmin | 634 state | streaming sent_lsn | 0/54D4DBD0 write_lsn | 0/54D4DBD0 flush_lsn | 0/54D4DBD0 replay_lsn | 0/54D4DBD0 write_lag | 00:00:00.00101 flush_lag | 00:00:00.001954 replay_lag | 00:00:00.002145 sync_priority | 1 sync_state | sync
到此,相信大家對(duì)“PostgreSQL怎么搭建流復(fù)制”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
分享標(biāo)題:PostgreSQL怎么搭建流復(fù)制-創(chuàng)新互聯(lián)
文章分享:http://chinadenli.net/article34/deshpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、標(biāo)簽優(yōu)化、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容