1. 程式人生 > 其它 >簡單寫一寫Pgsql-12叢集的搭建過程

簡單寫一寫Pgsql-12叢集的搭建過程

# 獲取安裝包,網速較慢,建議Ke Xue Shang Wang
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install -y postgresql12-server

# 資料目錄建立與賦權
sudo mkdir /mnt/data/pgdata
sudo chown -R postgres.postgres /mnt/data/pgdata

# 環境變數
sudo vim /usr/lib/systemd/system/postgresql-12.service
Environment
=PGDATA=/mnt/data/pgdata sudo systemctl daemon-reload # 初始化(以下操作在postgres角色下進行) /usr/pgsql-12/bin/postgresql-12-setup initdb systemctl start postgresql-12 # 建立主從備份使用者及改密 psql create role pgpool with login password 'postgres'; create role repl login replication encrypted password 'postgres'; alter user postgres with password 'postgres'; 主節點進行以下配置 cat
>> /mnt/data/pgdata/pg_hba.conf << EOF host all all 0.0.0.0/0 md5 host replication repl 本機ip/32 trust host replication repl 節點1 ip/32 trust #從節點1 host replication repl 節點n ip/32 trust #從節點n host all all 本機ip 最後一位替換為0/24 md5 EOF cat
>> /mnt/data/pgdata/postgresql.conf << EOF listen_addresses = '*' #開啟後子節點才能訪問 archive_mode = on max_connections = 21000 archive_command = 'cp "%p" "/var/lib/pgsql/archivedir/%f"' max_wal_senders = 10 max_replication_slots = 10 wal_level = replica hot_standby = on wal_log_hints = on EOF 從節點進行以下配置: cat >> /mnt/data/pgdata/postgresql.conf << EOF primary_conninfo = 'host=主節點ip port=5432 user=repl(主節點對應賬號) password=密碼' recovery_target_timeline = latest hot_standby = on max_standby_streaming_delay = 30s wal_receiver_status_interval = 10s hot_standby_feedback = on EOF echo "standby_mode = on" >> /mnt/data/pgdata/standby.signal # 重啟服務 使用啟停方式,不要restart # 從庫進行以下驗證: rm ‐rf /mnt/data/pgdata/* pg_basebackup -h 主節點ip -U repl -D /data/pgdata -X stream -P iptables -I INPUT -p tcp --dport 5432 -j ACCEPT # 查詢同步狀態 select client_addr,usename,backend_start,application_name,sync_state,sync_priority FROM pg_stat_replication;