赞
踩
安装
1.1.下载PostgreSQL 12安装包
下载地址:https://ftp.postgresql.org/pub/source/
1.2. 安装依赖包
yum install wget gcc gcc-c++ epel-release llvm5.0 llvm5.0-devel clang libicu-devel perl-ExtUtils-Embed readline readline-devel zlib zlib-devel openssl openssl-devel pam-devel libxml2-devel libxslt-devel openldap-devel systemd-devel tcl-devel python-devel -y
# tar -zxvf postgresql-12.0.tar.gz
1.4. 编译安装
到解压出来的目录下
- # postgresql-12.0]$#./configure --prefix=/usr/local/pgsql/12 --enable-nls --with-python --with-tcl --with-openssl --with-pam --with-ldap --with-systemd --with-libxml --with-libxslt
- # make && make install
- # chown -R postgres:postgres /usr/local/pgsql -R
- # vi /etc/profile.d/pgsql.sh
- 添加
- export PATH=/usr/local/pgsql/12/bin:$PATH
- # source /etc/profile.d/pgsql.sh
切换到postgres用户,创建data目录
# su - postgres
pgsql_data]$ mkdir data
初始化
$ initdb -D /pgsql_data/data/ -U postgres —locale=en_US.UTF8 -E UTF8
1.7. 启动数据库
pg_ctl -D /pgsql_data/data -l /pgsql_data/data/serverlog start
至此单节点PG配置完成。
2 PG主备配置
主端:172.20.5.163 :5432
备端:172.20.5.225 :5432
修改配置文件:
- cd /pgsql_data/data
- $ cp pg_hba.conf pg_hba.conf.bak
- # 允许repl用户进行流复制
- cat <<-eof >>/pgsql_data/data/pg_hba.conf
- host all all 0.0.0.0/0 md5
- host replication repl 172.20.0.0/22 trust
- eof
配置postgresql.conf
- cat <<-eof >/pgsql_data/data/postgresql.conf
- listen_addresses = '*'
- port = 5432
- max_connections = 1000
- superuser_reserved_connections = 100
- full_page_writes = on
- wal_log_hints = off
- max_wal_senders = 500
- hot_standby = on
- log_destination = 'csvlog'
- logging_collector = on
- log_directory = 'log'
- log_filename = 'postgresql-%Y-%m-%d_%H%M%S'
- log_rotation_age = 1d
- log_rotation_size = 10MB
- log_statement = 'mod'
- log_timezone = 'PRC'
- timezone = 'PRC'
- unix_socket_directories = '/tmp'
- shared_buffers = 512MB
- temp_buffers = 16MB
- work_mem = 32MB
- effective_cache_size = 2GB
- maintenance_work_mem = 128MB
- #max_stack_depth = 2MB
- dynamic_shared_memory_type = posix
- ## PITR
- full_page_writes = on
- wal_buffers = 16MB
- wal_writer_delay = 200ms
- commit_delay = 0
- commit_siblings = 5
- wal_level = replica
- archive_mode = on
- archive_command = 'test ! -f /data/pgdata/archivedir/%f && cp %p /data/pgdata/archivedir/%f'
- archive_timeout = 60s
- Eof
重启数据库。
- psql -U postgres -c "ALTER USER postgres WITH PASSWORD 'postgres';"
- psql -U postgres -c "CREATE USER repl WITH PASSWORD 'postgres' REPLICATION;"
修改文件:pg_hba.conf
添加:
host replication repl 172.20.5.225/24 trust
2.3 备库端操作
2.3.1 备库端安装PG同主库端,备库安装到初始化步骤。
2.3.2 清空data 目录,重建DATA目录:
2.3.3 执行命令,流复制建库:
pg_basebackup -h 172.20.5.163 -p 5432 -U repl -w -Fp -Xs -Pv -R -D /pgsql_data/data/
-h 连接的是主库端的IP,-p 主库port。
注意报错:
此错误出现在配置流复制备节点执行pg_basebackup的时候
解决方法:重启下主端节点,由于主节点有新的配置,因此需要重启使其生效。
2.3.4 修改从库端配置文件postgresql.conf
- max_connections = 150
- wal_level = hot_standby
- archive_mode = on
- hot_standby = on
- archive_timeout = 60s
- max_standby_streaming_delay = 30s
- wal_receiver_status_interval = 10s
- hot_standby_feedback = on
2.3.5 修改文件:postgresql.auto.conf
添加:
primary_conninfo = 'user=repl passfile=''/home/postgres/.pgpass'' host=172.20.5.163 port=5432 sslmode=prefer sslcompression=0 gssencmode=disable target_session_attrs=any'
2.3.6 修改data目录权限,启动备库
- ]$ chmod -R 0750 /pgsql_data/data
- ]$ pg_ctl -D /pgsql_data/data -l /pgsql_data/data/serverlog start
2.4 主备状态检查
主库端:
select * from pg_stat_replication;
postgres=# select pg_is_in_recovery(); 主机结果为:f,备机结果为t
备库端:
至此,PG主备配置完成。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。