運維開發技術交流羣歡迎你們加入一塊兒學習(QQ:722381733)web
開始部署postgres主從(若是沒不會安裝postgres的請去上一個博文中查看)sql
這裏我使用了兩臺服務器部署數據庫
主:192.168.254.21服務器
從:192.168.254.22運維
主配置(192.168.254.21)dom
一、進入部署postgres服務的用戶socket
[root@web1 ~]# su - postgres Last login: Thu May 23 18:17:45 CST 2019 on pts/0 [postgres@web1 ~]$
二、啓動服務ide
[postgres@web1 ~]$ pg_ctl start -l /usr/local/pgsql-10.5/log/pg_server.log waiting for server to start.... done server started [postgres@web1 ~]$
三、進入postgres數據庫,建立用戶並受權post
[postgres@web1 ~]$ psql psql (10.5) Type "help" for help. postgres=# CREATE ROLE replica login replication encrypted password 'replica'; CREATE ROLE postgres=#
四、檢查因此庫用戶後退出學習
postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} replica | Replication | {} test | Superuser | {} postgres=# \q [postgres@web1 ~]$
五、進入安裝目錄的庫文件配置目錄
[postgres@web1 ~]$ cd /usr/local/pgsql-10.5/data/ [postgres@web1 data]$ ls base pg_hba.conf pg_multixact pg_snapshots pg_tblspc pg_xact postmaster.opts global pg_hba.conf.2019-05-23 pg_notify pg_stat pg_twophase postgresql.auto.conf postmaster.pid pg_commit_ts pg_ident.conf pg_replslot pg_stat_tmp PG_VERSION postgresql.conf pg_dynshmem pg_logical pg_serial pg_subtrans pg_wal postgresql.conf.2019-05-23 [postgres@web1 data]$
六、修改postgresql.conf配置文件,修改以下(修改時記得備份)
wal_level = hot_standby #熱備模式 max_wal_senders = 32 #這個設置了能夠最多有幾個流複製鏈接,差很少有幾個從,就設置幾個 wal_keep_segments = 10240 #設置流複製保留的最多的xlog數目(重要配置) wal_sender_timeout = 60s #設置流複製主機發送數據的超時時間 max_connections = 100 #這個設置要注意下,從庫的max_connections必需要大於主庫的
七、打開pg_hba.conf配置文件,設置以下(修改時記得備份)
# TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all trust # IPv4 local connections: #host all all 127.0.0.1/32 trust host all all 0.0.0.0/0 md5 #全部ip均可以經過密碼鏈接 # IPv6 local connections: host all all ::1/128 trust # Allow replication connections from localhost, by a user with the # replication privilege. local replication all trust host replication all 127.0.0.1/32 trust host replication all ::1/128 trust host replication replica 192.168.254.22/32 md5
八、保存退出後重啓主服務器上的postgres庫
[postgres@web1 data]$ pg_ctl restart -l /usr/local/pgsql-10.5/log/pg_server.log waiting for server to shut down.... done server stopped waiting for server to start.... done server started [postgres@web1 data]$
***在此主就配置完了,如今配置從服務器***
從配置(192.168.254.22)
一、進入部署postgres服務的用戶
[root@web2 ~]# su - postgres Last login: Thu May 23 19:27:48 CST 2019 on pts/0 [postgres@web2 ~]$
二、這裏爲了方便我直接刪除data目錄用於備份主服務的數據,若是有特殊的要求建議新建個data1,而後用來作從存儲目錄(線上項目禁用rm,可經過mv移到垃圾箱)
[postgres@web2 ~]$ rm /usr/local/pgsql-10.5/data/* -r [postgres@web2 ~]$
三、拷貝主數據庫中的數據,注意指定的目錄,若是新建了目錄就指定到新建的目錄。
[postgres@web2 ~]$ pg_basebackup -F p --progress -D /usr/local/pgsql-10.5/data/ -h 192.168.254.21 -p 5432 -U replica --password Password: 31380/31380 kB (100%), 1/1 tablespace [postgres@web2 ~]$
***注這裏要求輸入密碼,就是主數據庫剛剛新建用戶時建的密碼(replica)***
四、複製配置文件並更名,注意文件路徑及文件名稱
[postgres@web2 ~]$ cp /usr/local/pgsql-10.5/share/recovery.conf.sample /usr/local/pgsql-10.5/data/recovery.conf [postgres@web2 ~]$ cd /usr/local/pgsql-10.5/data/ [postgres@web2 data]$ ls backup_label pg_dynshmem pg_logical pg_serial pg_subtrans pg_wal postgresql.conf.2019-05-23 base pg_hba.conf pg_multixact pg_snapshots pg_tblspc pg_xact recovery.conf global pg_hba.conf.2019-05-23 pg_notify pg_stat pg_twophase postgresql.auto.conf pg_commit_ts pg_ident.conf pg_replslot pg_stat_tmp PG_VERSION postgresql.conf [postgres@web2 data]$ ls /usr/local/pgsql-10.5/data/ |grep recovery.conf recovery.conf [postgres@web2 data]$
五、編輯recovery.conf文件,配置以下(修改是記得備份)
standby_mode = on #說明這臺postgres,爲從庫 primary_conninfo = 'host=192.168.254.21 port=5432 user=replica password=replica' #配置要同步的ip及庫用戶還有密碼 recovery_target_timeline = 'latest' #流複製同步到最新的數據
六、編輯postgresql.conf文件,配置以下
listen_addresses = '*' #這表示監聽的ip,這裏我設置監聽全部ip,也能夠設置成從本機ip wal_level = hot_standby #注:這個名稱要與主的一致 max_connections = 1000 #最大鏈接數要大於主的 hot_standby = on #說明這臺機器不單單用於數據歸檔,也用於查詢 max_standby_streaming_delay = 30s #數據流備份最大延遲 wal_receiver_status_interval = 1s #多久向主報告一次從的狀態,固然從每次數據複製都會向主報告狀態,這裏只是設置最長的間隔時間 hot_standby_feedback = on #若是有錯誤的數據複製,是否向主進行反饋
七、配置完成後啓動從數據庫服務
[postgres@web2 data]$ pg_ctl start -l /usr/local/pgsql-10.5/log/pg_server.log waiting for server to start.... done server started [postgres@web2 data]$
***這裏主從就配置完成啦,接下來就是測試***
測試
主:一、psql #進入數據器
二、\l #查看全部庫
三、create database test; #新建庫
四、\l #檢查是否建立
[postgres@web1 data]$ psql psql (10.5) Type "help" for help. postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- name | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | 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 (4 rows) postgres=# create database test; CREATE DATABASE postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- name | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | 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 test | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | (5 rows) postgres=#
從:一、psql #進入數據庫
二、\l #查看庫是否與主的一致
[postgres@web2 data]$ pg_ctl start -l /usr/local/pgsql-10.5/log/pg_server.log waiting for server to start.... done server started [postgres@web2 data]$ psql psql (10.5) Type "help" for help. postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- name | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | 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 test | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | (5 rows) postgres=#
***測試沒問題哈,這個也是意料中的事情!若是上述博文有什麼問題,但願大神讀客指出來,歡迎在線批評!!!***