安装PostgreSQL并配置三节点流复制环境,就不仔细说了,大致步骤如下:
1.下载源码
2.解压安装,如果在./configure --prefix=/usr/pgsql-10执行时提示要--without-readline,而系统已经有了readline一般是没有安装devel库
3.initdb
4.修改data目录下的postgresql.conf文件,主要有三个地方:listen_address='*', wal_level=replica(10好像是默认的,9要改为hot_standby),hot_standby=on
5.修改data目录下的pg_hba.conf,添加网段允许
6.备节点上data目录下执行pg_basebackup -Fp -R -h 主IP -D ./ ,然后修改recovery.conf文件,添加trigger_file='/tmp/.tfile'到最后一行,启动数据库就与主节点保持异步了。如果想保持同步,则在主节点的postgresql.conf中修改synchronse_names='frist 1(*)' , 备节点的recovery.conf的primary_info添加application_name='standby1'即可。
7.其他备节点同上操作。
安装pgpool
1. 下载源码安装:
wget http://www.pgpool.net/download.php?f=pgpool-II-3.7.4.tar.gz
2. 解压并安装
tar -xzvf download.php\?f\=pgpool-II-3.7.4.tar.gz
cd pgpool-II-3.7.4/
./configure --prefix=/usr/pgpool --with-pgsql=/usr/pgsql-10/
make && make install
3.配置节点互信
ssh-keygen
然后将所有的id_rsa.pub汇总到au里面,然后分发到每个节点
4.添加环境变量
echo "export PATH=$PATH:/usr/pgpool/bin">>~/.bash_profile
source ~/.bash_profile
5.修改pcp.conf
cd /usr/pgpool/etc
cp pcp.conf.sample pcp.conf
pg_md5 -u postgres postgres 生成的密码是错的,很奇怪。
pg_md5 -u postgres postgres
e8a48653851e28c69d0506508fb27fc5
添加-m参数会在该目录下生成一个pool_passwd文件,里面的md5密码又是对的
通过SQL语句查出来的密码是:
postgres=# select passwd from pg_shadow where usename='postgres';
md53175bce1d3201d16594cebf9d7eb3f9d
echo "postgres:md53175bce1d3201d16594cebf9d7eb3f9d">>pcp.conf
6.生成pool_passwd
pg_md5 -m -u postgres postgres (使用该命令会自动生成文件)
7.修改pgpool.conf
cp pgpool.conf.sample pgpool.conf
listen_addresses = '*' ...... backend_hostname0 = '10.9.8.185' # Host name or IP address to connect to for backend 0 backend_port0 = 5432 # Port number for backend 0 backend_weight0 = 1 # Weight for backend 0 (only in load balancing mode) backend_data_directory0 = '/var/lib/pgsql/10/data' # Data directory for backend 0 backend_flag0 = 'ALLOW_TO_FAILOVER' # Controls various backend behavior # ALLOW_TO_FAILOVER, DISALLOW_TO_FAILOVER # or ALWAYS_MASTER backend_hostname1 = '10.9.8.189' backend_port1 = 5432 backend_weight1 = 1 backend_data_directory1 = '/var/lib/pgsql/10/data' backend_flag1 = 'ALLOW_TO_FAILOVER' backend_hostname2 = '10.9.8.191' backend_port2 = 5432 backend_weight2 = 1 backend_data_directory2 = '/var/lib/pgsql/10/data' backend_flag2 = 'ALLOW_TO_FAILOVER'
......
follow_master_command = '/usr/pgpool/etc/failover.sh %H %R'
8.修改failover.sh脚本
new_master=$1 pgdata=$2 # Do nothing if standby goes down. if [ $failed_node = 1 ]; then exit 0; fi # Create the trigger file. #/usr/bin/ssh -T $new_master /bin/touch $trigger_file /usr/bin/ssh -T $new_master /usr/pgsql-10/bin/pg_ctl promote -D $pgdata exit 0;
9.修改pool_hba.conf文件
echo "host all all 10.9.8.1/24 md5" >> pool_hba.conf
10.启动pgpool 查看节点状态
pgpool -n>/var/log/pgpool.log 2>&1 &
psql -h 10.9.8.187 -U postgres -p 9999 -d postgres
postgres=# show pool_nodes; node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replication_delay ---------+------------+------+--------+-----------+--------+------------+-------------------+------------------- 0 | 10.9.8.185 | 5432 | up | 0.333333 | master | 0 | true | 0 1 | 10.9.8.189 | 5432 | up | 0.333333 | slave | 0 | false | 0 2 | 10.9.8.191 | 5432 | up | 0.333333 | slave | 0 | false | 0 (3 行记录) postgres=#
后面需要验证的问题有:
1.主从切换后,另外一个slave会不会和新的主节点保持同步?
2.主从切换是找同步节点吗,还是在两个slave节点中node_id顺序靠前的这个?
3.如果两个slave都是异步节点,会找xlog(wal)lsn最大的节点做master吗?
4.主节点重新上线,会自动跟新主节点保持同步吗?