• postgresql 11.6部署主从部署(归档模式)


    环境:
    OS:CentOs 7
    Postgres-11.6

    1.安装步骤
    1.1    环境部署
    数据库部署
    节点    ip    角色
    Host01    192.168.1.130    主
    Host02    192.168.1.131    从

    1.2  配置等效连接
    因为我们需要将主库的归档日志通过scp免密传输到备库
    等效连接配置可以参考
    https://www.cnblogs.com/hxlasky/p/12204180.html

    1.3  主库安装
    1.3.1 安装介质准备
    下载地址: https://www.postgresql.org/ftp/source/
    我这里下载的是11.6版本,介质如下:
    postgresql-11.6.tar.gz

    这里下载的源码,所以下面的步骤是源码安装
    1.3.2 安装依赖包
    yum install readline
    yum install gcc
    yum -y install -y readline-devel
    yum install zlib-devel


    1.3.3 编译安装
    [root@localhost soft]# tar -xvf postgresql-11.6.tar.gz
    [root@localhost soft]#mkdir -p /opt/postgresql-11.6
    [root@localhost soft]# cd postgresql-11.6
    [root@localhost soft]#./configure --prefix=/opt/postgresql-11.6
    [root@localhost soft]#make
    [root@localhost soft]#make install


    1.3.4 创建相应的用户
    [root@localhost opt]# groupadd postgres
    [root@localhost opt]# useradd -g postgres postgres

    1.3.5 创建数据及日志目录,并做相应授权
    [root@localhost soft]#mkdir -p /opt/postgresql-11.6/{data,log}
    [root@localhost soft]#chown -R postgres:postgres /opt/postgresql-11.6

    1.3.6 初始化数据库
    #su - postgres
    [postgres@localhost bin]$ cd /opt/postgresql-11.6/bin
    [postgres@localhost bin]$ ./initdb -D /opt/postgresql-11.6/data/


    1.3.7 启动数据库
    [postgres@localhost bin]$ cd /opt/postgresql-11.6/bin
    [postgres@localhost bin]$./pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log start


    1.3.8 修改环境变量
    [postgres@localhost ~]$ more .bash_profile
    # .bash_profile

    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
            . ~/.bashrc
    fi

    # User specific environment and startup programs

    PATH=$PATH:$HOME/.local/bin:$HOME/bin:/opt/postgresql-11.6/bin

    export PATH

    1.3.9 登陆使用
    [postgres@localhost bin]$cd /opt/postgresql-11.6/bin
    [postgres@localhost bin]$ ./psql
    psql (11.6)
    Type "help" for help.

    postgres=# du
                                       List of roles
     Role name |                         Attributes                         | Member of
    -----------+------------------------------------------------------------+-----------
     postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

    1.3.10 修改postgres用户的访问密码并测试建库建表
    PostgreSQL 数据库默认会创建一个postgres的数据库用户作为数据库的管理员,默认密码为空,我们需要修改为指定的密码,这里设定为postgres.
    su - postgres
    psql
    # ALTER USER postgres WITH PASSWORD 'postgres';
    # select * from pg_shadow ;
    # create database hxl;
    # c hxl

    project=# create table person(id integer, name text);
    project=# insert into person values (1, 'hxl');
    project=# select * from person;

    1.3.11 配置postgresql允许远程访问
    只需要修改data目录下的pg_hba.conf和postgresql.conf这两个文件:
    pg_hba.conf:配置对数据库的访问权限;
    postgresql.conf:配置PostgreSQL数据库服务器的相应的参数

    vim /opt/postgresql-11.6/data/pg_hba.conf

    # IPv4 local connections:
    host    all             all             127.0.0.1/32            trust
    host    all             all             0.0.0.0/0               md5


    重新加载配置文件
    su - postgres
    pg_ctl -D /opt/postgresql-11.6/data reload


    修改postgresql.conf
    vim /opt/postgresql-11.6/data/postgresql.conf

    listen_addresses = '*'   # what IP address(es) to listen on;
    修改该改参数需要重启动

    pg_ctl -D /opt/postgresql-11.6/data -l /opt/postgresql-11.6/log/postgres.log stop
    pg_ctl -D /opt/postgresql-11.6/data -l /opt/postgresql-11.6/log/postgres.log start

    到这里主库已经按照好了,下面进行主库的配置



    1.3.12    主从配置
    1.3.12.1 创建同步账号
    [postgres@localhost data]$ psql
    psql (11.6)
    Type "help" for help.

    postgres=# CREATE ROLE repl login replication encrypted password 'repl';
    CREATE ROLE

    1.3.12.2 修改配置文件(pg_hba.conf)


    在该文件最后添加如下两行:
    host    replication     repl            192.168.1.0/24          md5
    host    all             repl            192.168.1.0/24          trust


    1.3.12.3 修改配置文件(postgresql.conf)
    找到相应的参数进行如下配置修改
    wal_level = replica ##这个是设置主为wal的主机
    archive_mode = on

    archive_command = 'ssh 192.168.1.131 test ! -f /opt/postgresql-11.6/data/pg_archive/%f && scp %p 192.168.1.131:/opt/postgresql-11.6/data/pg_archive/%f'

    max_wal_senders = 6 ##这个设置了可以最多有几个流复制连接,差不多有几个从,就设置几个
    wal_keep_segments = 10240 ##设置流复制保留的最多的xlog数目
    wal_sender_timeout = 60s ##设置流复制主机发送数据的超时时间


    1.3.12.4 创建归档日志目录

    mkdir -p /opt/postgresql-11.6/data/pg_archive


    1.3.12.5 重启主库
    pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log restart

    1.3.12.6 检查归档是否传输到备库
    在主库上执行如下命令
    psql -c "select pg_switch_wal()"
    或是执行

    postgres=# select pg_switch_wal();

    可以看到已经生成了2个新的归档日志
    归档日志相应的传输到备库
     



    1.4  从库安装

    1.4.1 安装
    从库的安装跟主库安装步骤一致,需要启动数据库



    1.4.2 停掉从库
    若从库的数据库已经在运行的话,事先将其停掉
    [postgres@localhost data]$ pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log stop
    waiting for server to shut down.... done
    server stopped




    1.4.3 准备data目录
    从库安装完成后,不初始化,若已经初始化,删除其data目录
    若之前安装的pg有data目录的话需要将其删除掉,并创建一个空的相同的目录
    su - postgres
    [postgres@localhost postgresql-11.6]$ cd /opt/postgresql-11.6
    [postgres@localhost postgresql-11.6]$ mv data bakdata
    [postgres@localhost postgresql-11.6]$ mkdir data


    root用户下修改权限
    chown -R postgres:postgres /opt/postgresql-11.6
    chmod 0700 /opt/postgresql-11.6/data

    1.4.4 基础同步主库的数据文件
    [postgres@localhost postgresql-11.6]$ pg_basebackup -RFp --progress -D /opt/postgresql-11.6/data -h 192.168.1.130 -p 5432 -U repl --password
    Password:
    113625/113625 kB (100%), 1/1 tablespace

    可以看到data目录下的所有文件都同步过来了,使用了R参数会生成一个recovery.conf文件,下面我们直接修改该文件即可

    [postgres@localhost data]$ pwd
    /opt/postgresql-11.6/data
    [postgres@localhost data]$ ls -al
    [postgres@localhost data]$ ls -1
    backup_label
    base
    global
    pg_archive
    pg_commit_ts
    pg_dynshmem
    pg_hba.conf
    pg_ident.conf
    pg_logical
    pg_multixact
    pg_notify
    pg_replslot
    pg_serial
    pg_snapshots
    pg_stat
    pg_stat_tmp
    pg_subtrans
    pg_tblspc
    pg_twophase
    PG_VERSION
    pg_wal
    pg_xact
    postgresql.auto.conf
    postgresql.conf
    recovery.conf

    1.4.4 修改recovery.conf文件
    前面的步骤已经生成了recovery.conf文件,
    该文件的内容是这样的
    [postgres@localhost data]$ more recovery.conf
    standby_mode = 'on'
    primary_conninfo = 'user=repl password=repl host=192.168.1.130 port=5432 sslmode=disable sslcompression=0 target_session_attrs=any'
    我们这里不做流复制, primary_conninfo先注释掉,修改该文件,内容如下:
    restore_command = 'cp /opt/postgresql-11.6/data/pg_archive/%f %p'
    standby_mode = on


    1.4.5 修改从库postgresql.conf文件
    修改如下内容项:
    max_connections = 1000 #一般查多于写的应用从库的最大连接数要比较大
    hot_standby = on       #说明这台机器不仅仅是用于数据归档,也用于数据查询
    max_standby_streaming_delay = 30s  #数据流备份的最大延迟时间
    wal_receiver_status_interval = 1s  #多久向主报告一次从的状态,当然从每次数据复制都会向主报告状态,这里只是设置最长的间隔时间
    hot_standby_feedback = on          #如果有错误的数据复制,是否向主进行反馈



    1.4.5 启动从库

    pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log start



    1.4.6    验证
    1.4.6.1 查看同步情况
    [postgres@localhost pg_wal]$ psql
    postgres=# x
    Expanded display is on.
    postgres=#  select * from pg_stat_replication;
    (0 rows)

    这里不是流复制,所有没有显示
    1.4.6.2 主库尝试创建对象看是否同步到从库
    psql -h localhost -U uhxl -d hxl
    create table tb_test
    (
     id bigserial primary key not null,
     name varchar(64)
    );

    insert into tb_test(name) values('name1');
    insert into tb_test(name) values('name2');
    insert into tb_test(name) values('name3');
    insert into tb_test(name) values('name4');
    insert into tb_test(name) values('name5');


    查看从库是否同步


    这种归档模式的同步需要主库上执行检查点切换后,主库的数据才会同步到从库
    或是手工进行检查点的切换
    select pg_switch_wal();

    为啥要切换生成日志,因为架设的是基于文件的备库,只有归档传输到备库后才会应用,否则记录还在主库的xlog中.

  • 相关阅读:
    linux 运维必备150个命令
    CentOS 6.5 安装nginx 1.6.3
    centos 6.5 zabbix3.0.4 监控apache
    iOS更改ShareSDK默认的分享功能界面
    使用AFNetworking时, 控制器点击返回销毁了, 但还是会执行请求成功或失败的block, 导致野指针异常
    iOS性能优化
    'Invalid update: invalid number of rows in section xx. The number of rows contained in an existing section after the update (xxx)...
    iOS 改变UITextField中光标颜色
    使用ShareSDK完成Facebook第三方登录和Facebook分享时没办法跳转到Facebook应用
    [!] Unable to satisfy the following requirements:
  • 原文地址:https://www.cnblogs.com/hxlasky/p/12208561.html
Copyright © 2020-2023  润新知