1、从软件包安装pg_auto_failover
# Add the repository to your system curl https://install.citusdata.com/community/rpm.sh | sudo bash # Install pg_auto_failover sudo yum install -y pg-auto-failover10_11 # Confirm installation /usr/pgsql-11/bin/pg_autoctl --version
2、安装并运行监视器
$ export PGDATA=./monitor $ export PGPORT=5000 $ pg_autoctl create monitor --ssl-self-signed --hostname localhost --auth trust --run 为安装节点 获取监视器节点的后缀 URI(连接字符串): $ pg_autoctl show uri --monitor --pgdata ./monitor postgres://autoctl_node@localhost:5000/pg_auto_failover?sslmode=require
3、安装并运行master PostgreSQL 实例
$ export PGDATA=./node_1 $ export PGPORT=5002 $ pg_autoctl create postgres \ --hostname localhost \ --auth trust \ --ssl-self-signed \ --monitor 'postgres://autoctl_node@localhost:5000/pg_auto_failover?sslmode=require' \ --run
4、安装并运行slave PostgreSQL 实例
$ export PGDATA=./node_2 $ export PGPORT=5001 $ pg_autoctl create postgres \ --hostname localhost \ --auth trust \ --ssl-self-signed \ --monitor 'postgres://autoctl_node@localhost:5000/pg_auto_failover?sslmode=require' \ --run
5、常用操作
查看系统的状态: $ pg_autoctl show state Name | Node | Host:Port | LSN | Reachable | Current State | Assigned State -------+-------+----------------+-----------+-----------+---------------------+-------------------- node_1 | 1 | localhost:5001 | 0/30000D8 | yes | primary | primary node_2 | 2 | localhost:5002 | 0/30000D8 | yes | secondary | secondary 启动手动故障转移,也称为切换: $ pg_autoctl perform switchover 其他命令: pg_autoctl stop pg_autoctl drop node --destroy pg_autoctl drop monitor --destroy
6、查看在应用程序级别使用的连接字符串,请使用以下命令:
$ pg_autoctl show uri --formation default --pgdata ... postgres://localhost:5002,localhost:5001/postgres?target_session_attrs=read-write&sslmode=require