zoukankan      html  css  js  c++  java
  • postgresql主从切换(promote方式主从切换)

    主:192.168.1.130

    从:192.168.1.131

    1、主备库进程查看
    主库
    [postgres@localhost data]$ pg_controldata /opt/postgresql-11.6/data/| grep 'Database cluster state'
    Database cluster state:               in production
    [postgres@localhost data]$

    备库
    [postgres@localhost data]$ pg_controldata /opt/postgresql-11.6/data/| grep 'Database cluster state'
    Database cluster state:               in archive recovery
    [postgres@localhost data]$

    2、停掉主库
    pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log stop

    查看状态已经处于stop状态
    [postgres@localhost data]$ pg_controldata /opt/postgresql-11.6/data/| grep 'Database cluster state'
    Database cluster state:               shut down


    3、提升从库为主库
    这个时候从库保持运行状态,不需要停掉
    [postgres@localhost data]$ pg_ctl promote -D /opt/postgresql-11.6/data/
    waiting for server to promote.... done
    server promoted

    查看状态
    [postgres@localhost data]$ pg_controldata /opt/postgresql-11.6/data/| grep 'Database cluster state'
    Database cluster state:               in production



    4、验证
    这个时候从库的recovery.conf文件会自动命名为recovery.done

    尝试在原来的从库写入数据

    insert into tb_hxl01 values(20,'name6');
    insert into tb_hxl01 values(21,'name7');
    insert into tb_hxl01 values(22,'name8');
    insert into tb_hxl01 values(23,'name9');
    insert into tb_hxl01 values(24,'name10');
    insert into tb_hxl01 values(25,'name10');
    写入新增数据,重库启动后,模拟差异数据是否同步到从库



    5、将原来的主库部署成为重库
    5.1 创建recovery.conf文件
    在原来主库的data目录下创建recovery.conf文件
    standby_mode='on'
    recovery_target_timeline = 'latest'
    primary_conninfo = 'host=192.168.1.131 port=5432 user=repl password=repl'

    5.2 启动
    [postgres@localhost data]$ pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log start

    查看状态
    [postgres@localhost data]$ pg_controldata /opt/postgresql-11.6/data/| grep 'Database cluster state'
    Database cluster state:               in archive recovery


    这个时候发现数据库无法连接
    [postgres@localhost log]$ psql
    psql: could not connect to server: No such file or directory
            Is the server running locally and accepting
            connections on Unix domain socket "/tmp/.s.PGSQL.5432"?


    查看错误日志postgres.log
    2020-01-19 15:07:55.297 CST [31189] FATAL:  hot standby is not possible because max_connections = 100 is a lower setting than on the master server (its value was 1000)

    解决办法,修改当前数据库的max_connections与主库保持一致

    5.3 验证刚才从库写入的数据是否同步过来
    查看新备库中数据是否与现主库内容相同,9.6以后的版本应该会自动同步差异

     

    -- The End --

  • 相关阅读:
    MySQL主库异常,从库手动切换为主库方案
    快速搭建应用服务日志收集系统(Filebeat + ElasticSearch + kibana)
    CentOS7设置DNS服务器
    nginx/iptables动态IP黑白名单实现方案
    Python批量复制和重命名文件
    centos 7 配置php运行环境 (新)
    配置Nginx和php-fpm用Sock套接字连接时,找不到php-fpm.sock的原因
    php-fpm nginx 9000端口
    nginx与php-fpm通信的两种方式
    centos 7.2 常用命令useradd的使用
  • 原文地址:https://www.cnblogs.com/hxlasky/p/12213877.html
Copyright © 2011-2022 走看看