主机环境配置hostnamectl set-hostname node1hostnamectl set-hostname node2hostnamectl set-hostname node3------------------------------------------------------------------------------------关闭防火墙systemctl status firewalld.servicesystemctl stop firewalld.servicesystemctl disable firewalld.serviceSELINUXvi etc/selinux/config将SELINUX=enforcing 改为SELINUX=disabled配置流复制主备,主节点配置编辑vi pg_hba.conf文件,添加如下内容:host all all 0.0.0.0/0 md5host replication replica 0.0.0.0/0 md5编辑postgresql.conf文件,添加如下内容:listen_addresses = '*'port = 5432wal_level = replicamax_wal_senders = 32max_replication_slots = 32hot_standby = onhot_standby_feedback = onmax_standby_streaming_delay = 30swal_receiver_status_interval = 10swal_sender_timeout = 60swal_log_hints = onunix_socket_directories = '.'unix_socket_permissions = 0700bgwriter_delay = '10ms'datestyle = 'iso, mdy'full_page_writes = 'True'huge_pages = 'False'lc_messages = 'C'lc_monetary = 'C'lc_numeric = 'C'lc_time = 'C'tcp_keepalives_idle = 0tcp_keepalives_interval = 0tcp_keepalives_count = 0wal_receiver_status_interval = '1s'wal_writer_delay = '10ms'autovacuum_max_workers=10maintenance_work_mem = 256MBvacuum_cost_limit=150max_worker_processes = 128checkpoint_timeout = 10minlog_disconnections = onwal_writer_delay = 10mslog_destination = 'csvlog'logging_collector = onlog_directory = '/opt/pg_root/pg_log'log_filename = 'postgresql-log.%a'log_file_mode = 0600log_truncate_on_rotation = onlog_rotation_age = 1440log_rotation_size = 1000000log_min_duration_statement = 500log_statement = ddlhuge_pages = offpg_hba.confhost all all 0.0.0.0/0 md5host replication replica 0.0.0.0/0 md5创建 .pgpass文件su - postgresvi ~/.pgpass1主机IP:5432:postgres:postgres:1234562主机IP:5432:postgres:postgres:1234561主机IP:5432:replication:replica:REPLICA3212主机IP:5432:replication:replica:REPLICA321chmod 0600 .pgpass流复制配置 在node1节点#创建replica用户create role replica login replication encrypted password 'REPLICA321';配置主从复制备库配置从此处开始配置备库,首先通过pg_basebackup命令行工具在从库上生成基础备份命令如下:rm -rf opt/pg_root/*pg_basebackup -F p -P -R -D $PGDATA -h 10.110.8.97 -p 5432 -U replicaREPLICA321会生成:standby.signal 文件postgresql.auto.conf 文件会添加连接信息primary_conninfo = 'user=replica password=REPLICA321 host=10.110.8.97 port=5432 sslmode=disable sslcompression=0 gssencmode=disable target_session_attrs=any'promote_trigger_file = '/tmp/trigger_file'***********************************************************************************************************pgpool-ii集群中间件安装注:集群代理程序和主数据库节点安装在一台机器上。su - rootcd opt/soft_bak/上传 pgpool-II-4.3.1.tar.gztar zxvf pgpool-II-4.3.1.tar.gzcd pgpool-II-4.3.1/./configure -prefix=/opt/pgpool -with-pgsql=path -with-pgsql=/opt/pgsqlmake&&make install------------------------------------------------------------------------------------------------------------------------------以下操作分别在主节点和备节点同时执行:postgres配置两台数据库服务器之间的信任关系su - postgresssh-keygenssh-copy-id postgres@备节点IP 主节点执行ssh-copy-id postgres@主节点IP 备节点执行------------------------------------------------------------------------------------------------------------------------------配置pgpool-ii代理的归属权限su - rootchown -R postgres:postgres opt/pgpoolmkdir var/run/pgpoolcd var/run/pgpoolchown -R postgres:postgres var/run/pgpool------------------------------------------------------------------------------------------------------------------------------复制pgpool代理,配置文件su - postgrescd opt/pgpool/etc/cp pcp.conf.sample pcp.confcp pgpool.conf.sample pgpool.confcp pool_hba.conf.sample pool_hba.conf编辑vi pool_hba.conf文件,添加如下内容:host all all 127.0.0.1/32 md5host all all 0.0.0.0/0 md5配置pgpool管理密码cd opt/pgpool/binpg_md5 cuipeng #复制md5值cd opt/pgpool/etcvi pcp.confpostgres:4da80f53678a7a6e837c3e5cdd60477d (pg_md5 cuipeng 生成的密码)登录主节点 执行psql -h 主数据库IP -p 主数据库端口 -U postgres postgrescreate role srcheck nosuperuser login encrypted password 'SRCHECK';digoal=# select rolname,rolpassword from pg_authid;rolname | rolpassword----------+-------------------------------------postgres | md53175bce1d3201d16594cebf9d7eb3f9ddigoal | md5462f71c79368ccf422f8a773ef40074dsrcheck | md5662c10f61b27a9ab38ce69157186b25f添加,pgpool连接数据库的用户和密码cd opt/pgpool/etcvi pool_passwdpostgres: md53175bce1d3201d16594cebf9d7eb3f9d (从上一步查询中获取)配置pgpool主配置文件su - postgrescd /opt/pgpool/etcvi pgpool.conflisten_addresses = '*'port = 9999pcp_port = 9898backend_hostname0 = '主库ip'backend_port0 = 5432backend_weight0 = 1backend_data_directory0 = '/opt/pg_root'backend_flag0 = 'ALLOW_TO_FAILOVER'backend_hostname1 = '备库ip'backend_port1 = 5432backend_weight1 = 1backend_data_directory1 = '/opt/pg_root'backend_flag1 = 'ALLOW_TO_FAILOVER'enable_pool_hba = onfailover_command = '/opt/pgsql/bin/failover_stream.sh %d %H /tmp/trigger_file'log_statement = onlog_per_node_statement = onhealth_check_period = 1health_check_timeout = 10health_check_user = 'postgres'health_check_password = '123456'health_check_database = 'postgres'enable_pool_hba = onmaster_slavpe_mode = onmaster_slave_sub_mode = 'stream'sr_check_period = 1sr_check_user = 'replica'sr_check_password = 'REPLICA321'登录集群su - postgrespsql -h 127.0.0.1 -p 9999 -U postgres postgres123456 密码(pool_passwd里面配置的密码)show pool_nodes; 登录集群中间件查看各节点状态status列解释0 - 该状态仅仅用于初始化,PCP从不显示它。1 - 节点已启动,还没有连接。2 - 节点已启动,连接被缓冲。2 - 节点已关闭。pool_status, 获取配置pool_nodes, 获取节点信息pool_processes, 获取pgPool-II 进程信息pool_pools, 获取pgPool-II 所有的连接池信息pool_version, 获取pgPool_II 版本信息主从节点故障切换登录pgpool所在的数据库主节点su - rootcd /opt/pgsql/bin上传failover_stream.sh文件chmod 755 failover_stream.sh启动主 PostgreSQL 节点和备 PostgreSQL 节点来初始化基于双机热备。如果主节点失效,备节点将自动切换为主节点。注: failover_stream.sh内容需要修改的内容:#! /bin/sh# Failover command for streaming replication.# This script assumes that DB node 0 is primary, and 1 is standby.## If standby goes down, do nothing. If primary goes down, create a# trigger file so that standby takes over primary node.## Arguments: $1: failed node id. $2: new master hostname. $3: path to# trigger file.host1=172.16.0.195 (根据实际主从节点IP进行配置)host2=172.16.0.196 (根据实际主从节点IP进行配置)failed_node=$1new_master=$2trigger_file=$3# Do nothing if standby goes down.# Create the trigger file.if [ $failed_node = 0 ] && [ $new_master = $host2 ]; then/usr/bin/ssh -T $new_master /bin/touch $trigger_fileelif [ $failed_node = 1 ] && [ $new_master = $host1 ]; then/usr/bin/ssh -T $new_master /bin/touch $trigger_fileelseexit 0;fiexit 0;
文章转载自CP的PostgreSQL厨房,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




