postgresSQL数据库主备流复制性能测试

数据库主备搭建步骤

  • 修改postgresql.conf配置
# postgresql.conf
max_connections = 500
shared_buffers = 2GB
temp_buffers = 64MB
work_mem = 64MB
max_stack_depth = 8MB
dynamic_shared_memory_type = posix
log_destination = 'stderr'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%a.log'
log_truncate_on_rotation = on
log_rotation_age = 1440
log_rotation_size = 50MB
log_min_duration_statement = 0
log_connections = on
log_disconnections = on
log_line_prefix = '<%m> '
log_timezone = 'PRC'
datestyle = 'iso, mdy'
timezone = 'PRC'
lc_messages = 'en_US.UTF-8'
lc_monetary = 'en_US.UTF-8'
lc_numeric = 'en_US.UTF-8'
lc_time = 'en_US.UTF-8'
default_text_search_config = 'pg_catalog.english'
port = 5432
fsync = off
max_wal_size = 4GB
checkpoint_timeout = 20min   
synchronous_commit = off
listen_addresses = '*'
wal_level = replica
max_wal_senders = 5  # 设置了可以最多有几个流复制的链接
wal_keep_segments = 1024 #保留1024个WAL文件,每个wal文件16M,总共大小16M*1024=16G
wal_log_hints = on
hot_standby = on
  • 修改pg_hba.conf配置

    # psql_hba.con
    # TYPE    DATABASE    USER    ADDRESS    METHOD
    local        all       all                 trust
    host         replication postgres 0.0.0.0/0 trust
    host         all       all    127.0.0.1/32 trust
    host         replication postgres ::1/128 trust
    host         all       all    0.0.0.0/0      trust
    host         replication postgres all md5
    host         all       all    all          md5
    
  • 重启主数据节点服务

    su - postgres -c "/usr/local/appsys/package/pgsql/bin/pg_ctl -D /usr/local/appdata/data/datazddi/ restart -l /home/postgres/pg.log"
    
  • 开始基础备份

    # 删除/usr/local/appdata/data/datazddi文件夹下面数据
    rm -fr /usr/local/appdata/data/datazddi/*
    sudo -u postgres /usr/local/appsys/package/pgsql/bin/pg_basebackup -h 10.2.0.246 -p 5432 -U postgres -F p -x -P -R -D /usr/local/appdata/data/datazddi
    
  • 备份完成后,启动服务

    su - postgres -c "/usr/local/appsys/package/pgsql/bin/pg_ctl -D /usr/local/appdata/data/datazddi/ start -l /home/postgres/pg.log"
    
  • 启动完成后,再主数据节点上查看主备流复制状态

    cloudipam=# select * from pg_stat_replication ;
     pid  | usesysid | usename  | application_name | client_addr | client_hostname | client_port |         backend_start         | backend_xmin |   state   | sent_location | write_location | flush_location | repla
    y_location | sync_priority | sync_state 
    ------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+--------------+-----------+---------------+----------------+----------------+------
    -----------+---------------+------------
     1053 |       10 | postgres | walreceiver      | 10.2.0.238  |                 |       51830 | 2021-03-30 17:42:14.499251+08 |              | streaming | 0/4004888     | 0/4004888      | 0/4004888      | 0/400
    4888       |             0 | async
    

监控命令

#cpu使用率:
vmstat | sed -n '3p'| awk {
   'print $13, $14,$15,$16'}
#系统负载:
`uptime |  awk {
    'print $8'} | sed 's/,//g'`
#内存利用率:
head -5 /proc/meminfo | awk 'NR==1{MemTotal=$2}NR==2{MemFree=$2}NR==4{Buffers=$2}NR==5{Cached=$2;print (MemTotal-MemFree-Buffers-Cached)/MemTotal}'
#网卡出入口带宽:
ifstat -i eth0 -n 1 1 | awk 'NR>2 {print $1,$2}'
#磁盘IO占有率:
iostat -d -x -k 1 1 | sed -n '4p'| awk '{print $14}'
# 查看主备数据库差异大小(在主数据节点上执行):
[root@localhost ~]# psql -d cloudipam -U postgres -c 'select client_addr,pg_xlog_location_diff(pg_current_xlog_location(),replay_location)/1024/1024 as MB  from pg_stat_replication;' | awk 'NR==3{ip1=$1;mb1=$3}NR==4{ip2=$1;mb2=$3}NR==5{ip3=$1;mb3=$3}NR==6{ip4=$1;mb4=$3}NR==7{ip5=$1;mb5=$3;print ip1,mb1,ip2,mb2,ip3,mb3,ip4,mb4,ip5,mb5}'
10.2.0.238 0.00000000000000000000 10.2.2.16 0.00000000000000000000 10.2.2.15 0.00000000000000000000 10.2.2.14 0.00000000000000000000 10.2.2.17 0.00000000000000000000
# 查看主备数据库时延
SELECT
        CASE
                WHEN pg_last_xlog_receive_location() = pg_last_xlog_replay_location() THEN 0
                ELSE EXTRACT (EPOCH FROM now() - pg_last_xact_replay_timestamp())
        END
AS replication_lag;"

#查看LSN
sql="
SELECT 
	CASE
		WHEN pg_is_in_recovery()
		THEN 
			coalesce(pg_last_xlog_receive_location(), pg_last_xlog_replay_location())
		ELSE
			pg_current_xlog_flush_location()
	END;"
#AS replication_lag;"
echo -e "`psql -c "$sql" -d cloudipam -U postgres`"

建立主备流复制时间测试

测试拓扑

postgresSQL数据库主备流复制性能测试_第1张图片

测试步骤
  1. 在数据上执行监控脚本
echo "cs us sy id load mem_use_rate in out %util"
while true
do
	echo -e "`vmstat | sed -n '3p'| awk {
     'print $13, $14,$15,$16'}` \c";echo -e "`uptime |  awk {
     'print $8'} | sed 's/,//g'` \c";echo -e "`head -5 /proc/meminfo | awk 'NR==1{MemTotal=$2}NR==2{MemFree=$2}NR==4{Buffers=$2}NR==5{Cached=$2;print (MemTotal-MemFree-Buffers-Cached)/MemTotal}'` \c"; echo -e "`ifstat -i eth0 -n 1 1 | awk 'NR>2 {print $1,$2}'` \

你可能感兴趣的:(数据库,postgresql)