postgresql主從同步
目錄
一、環境准備:
二、主庫安裝配置:
三、從庫安裝配置
四、 將服務設置為開機自啟
一、環境准備:
主庫IP:192.168.1.55
從庫IP:192.168.1.56
安裝介質准備
下載地址: https://www.postgresql.org/ftp/source/
我這里下載的是11.6版本,介質如下:
postgresql-11.6.tar.gz
貼個百度網盤分享
鏈接:https://pan.baidu.com/s/14biksO-jRu5EvLEhXuwHHw
提取碼:vhji
二、主庫安裝配置:
1.安裝依賴包
yum -y install readline gcc -y readline-devel zlib-devel
2.編譯安裝
tar -xvf postgresql-11.6.tar.gz
mkdir -p /opt/postgresql-11.6
cd postgresql-11.6
./configure --prefix=/opt/postgresql-11.6 --with-blocksize=32 && make && make install
3.創建相應的用戶
groupadd postgres
useradd -g postgres postgres
創建數據及日志目錄,並做相應授權
mkdir -p /opt/postgresql-11.6/{data,log}
chown -R postgres:postgres /opt/postgresql-11.6
4.初始化數據庫
su - postgres
[postgres@localhost bin]$ cd /opt/postgresql-11.6/bin
[postgres@localhost bin]$ ./initdb -D /opt/postgresql-11.6/data/
5.啟動數據庫
[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
6.修改環境變量
[postgres@localhost ~]$ vim .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
source .bash_profile
7. 登陸使用
[postgres@localhost bin]$cd /opt/postgresql-11.6/bin
[postgres@localhost bin]$ ./psql
8.修改postgres用戶的訪問密碼並測試建庫建表
9.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;
10.配置postgresql允許遠程訪問
只需要修改data目錄下的pg_hba.conf和postgresql.conf這兩個文件:
pg_hba.conf:配置對數據庫的訪問權限;
postgresql.conf:配置PostgreSQL數據庫服務器的相應的參數
11.vim /opt/postgresql-11.6/data/pg_hba.conf
12.重新加載配置文件
su - postgres
pg_ctl -D /opt/postgresql-11.6/data reload
13.修改postgresql.conf
vim /opt/postgresql-11.6/data/postgresql.conf
listen_addresses = '*' # what IP address(es) to listen on;
14.修改該改參數需要重啟動
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
到這里主庫已經按照好了,下面進行主庫的配置
15.創建同步賬號
[postgres@localhost data]$ psql
psql (11.6)
Type "help" for help.
postgres=# CREATE ROLE repl login replication encrypted password 'repl';
CREATE ROLE
16.修改配置文件(pg_hba.conf)
vim /opt/postgresql-11.6/data/pg_hba.conf
在該文件最后添加如下兩行:
host replication repl 192.168.1.0/24 md5
host all repl 192.168.1.0/24 trust
17. 修改配置文件(postgresql.conf)
vim /opt/postgresql-11.6/data/postgresql.conf
找到相應的參數進行如下配置修改
wal_level = replica
archive_mode = on
archive_command = 'cp %p /opt/postgresql-11.6/data/pg_archive/%f'
##%p = path of file to archive
##%f = file name only
max_wal_senders = 6
wal_keep_segments = 10240
wal_sender_timeout = 60s
18.創建歸檔日志目錄
mkdir -p /opt/postgresql-11.6/data/pg_archive
重啟主庫
pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log restart
三、從庫安裝配置
1.從庫的安裝跟主庫安裝步驟一致,需要啟動數據庫
停掉從庫
若從庫的數據庫已經在運行的話,事先將其停掉
[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
2.准備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
3.同步主庫的數據文件
[postgres@localhost postgresql-11.6]$ pg_basebackup -Fp --progress -D /opt/postgresql-11.6/data -h 192.168.1.55 -p 5432 -U repl --password
輸入密碼 repl
查看
4.創建recovery.conf文件
從模板文件拷貝到data目錄
[postgres@localhost share]$ cp /opt/postgresql-11.6/share/recovery.conf.sample /opt/postgresql-11.6/data/recovery.conf
對其進行修改,參數如下:
在data目錄下創建recovery.conf文件,內容如下
standby_mode = on # 這個說明這台機器為從庫
primary_conninfo = 'host=192.168.1.55 port=5432 user=repl password=repl' # 這個說明這台機器對應主庫的信息
recovery_target_timeline = 'latest' # 這個說明這個流復制同步到最新的數據
5.修改從庫postgresql.conf文件
修改如下內容項:
max_connections = 1000 #一般查多於寫的應用從庫的最大連接數要比較大
hot_standby = on #說明這台機器不僅僅是用於數據歸檔,也用於數據查詢
max_standby_streaming_delay = 30s #數據流備份的最大延遲時間
wal_receiver_status_interval = 1s #多久向主報告一次從的狀態,當然從每次數據復制都會向主報告狀態,這里只是設置最長的間隔時間
hot_standby_feedback = on #如果有錯誤的數據復制,是否向主進行反饋
6.啟動從庫
pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log start
7.主庫上查看
對於如何主從切換具體看博客鏈接:
https://www.cnblogs.com/hxlasky/p/12192612.html
四、將服務設置為開機自啟
1.修改postgresql文件配置
vim /etc/init.d/postgresql
#prefix設置為你的安裝路徑
#PGUSER設置為操作postgreSQL的用戶(默認為postgres)
#PGLOG是日志路徑
#注:如果設置的日志路徑沒有server.log,需要手動創建此文檔
cd /opt/postgresql-11.6/data
touch server.log
以下為腳本內容:
#! /bin/sh
# chkconfig: 2345 98 02
# description: PostgreSQL RDBMS
# This is an example of a start/stop script for SysV-style init, such
# as is used on Linux systems. You should edit some of the variables
# and maybe the 'echo' commands.
#
# Place this file at /etc/init.d/postgresql (or
# /etc/rc.d/init.d/postgresql) and make symlinks to
# /etc/rc.d/rc0.d/K02postgresql
# /etc/rc.d/rc1.d/K02postgresql
# /etc/rc.d/rc2.d/K02postgresql
# /etc/rc.d/rc3.d/S98postgresql
# /etc/rc.d/rc4.d/S98postgresql
# /etc/rc.d/rc5.d/S98postgresql
# Or, if you have chkconfig, simply:
# chkconfig --add postgresql
#
# Proper init scripts on Linux systems normally require setting lock
# and pid files under /var/run as well as reacting to network
# settings, so you should treat this with care.
# Original author: Ryan Kirkpatrick <pgsql@rkirkpat.net>
# contrib/start-scripts/linux
## EDIT FROM HERE
# Installation prefix
prefix=/opt/postgresql-11.6
# Data directory
PGDATA="/opt/postgresql-11.6/data"
# Who to run the postmaster as, usually "postgres". (NOT "root")
PGUSER=postgres
# Where to keep a log file
PGLOG="$PGDATA/serverlog"
# It's often a good idea to protect the postmaster from being killed by the
# OOM killer (which will tend to preferentially kill the postmaster because
# of the way it accounts for shared memory). To do that, uncomment these
# three lines:
#PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
#PG_MASTER_OOM_SCORE_ADJ=-1000
#PG_CHILD_OOM_SCORE_ADJ=0
# Older Linux kernels may not have /proc/self/oom_score_adj, but instead
# /proc/self/oom_adj, which works similarly except for having a different
# range of scores. For such a system, uncomment these three lines instead:
#PG_OOM_ADJUST_FILE=/proc/self/oom_adj
#PG_MASTER_OOM_SCORE_ADJ=-17
#PG_CHILD_OOM_SCORE_ADJ=0
## STOP EDITING HERE
# The path that is to be used for the script
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# What to use to start up the postmaster. (If you want the script to wait
# until the server has started, you could use "pg_ctl start" here.)
DAEMON="$prefix/bin/postmaster"
# What to use to shut down the postmaster
PGCTL="$prefix/bin/pg_ctl"
set -e
# Only start if we can find the postmaster.
test -x $DAEMON ||
{
echo "$DAEMON not found"
if [ "$1" = "stop" ]
then exit 0
else exit 5
fi
}
# If we want to tell child processes to adjust their OOM scores, set up the
# necessary environment variables. Can't just export them through the "su".
if [ -e "$PG_OOM_ADJUST_FILE" -a -n "$PG_CHILD_OOM_SCORE_ADJ" ]
then
DAEMON_ENV="PG_OOM_ADJUST_FILE=$PG_OOM_ADJUST_FILE PG_OOM_ADJUST_VALUE=$PG_CHILD_OOM_SCORE_ADJ"
fi
# Parse command line parameters.
case $1 in
start)
echo -n "Starting PostgreSQL: "
test -e "$PG_OOM_ADJUST_FILE" && echo "$PG_MASTER_OOM_SCORE_ADJ" > "$PG_OOM_ADJUST_FILE"
su - $PGUSER -c "$DAEMON_ENV $DAEMON -D '$PGDATA' >>$PGLOG 2>&1 &"
echo "ok"
;;
stop)
echo -n "Stopping PostgreSQL: "
su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s"
echo "ok"
;;
restart)
echo -n "Restarting PostgreSQL: "
su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s"
test -e "$PG_OOM_ADJUST_FILE" && echo "$PG_MASTER_OOM_SCORE_ADJ" > "$PG_OOM_ADJUST_FILE"
su - $PGUSER -c "$DAEMON_ENV $DAEMON -D '$PGDATA' >>$PGLOG 2>&1 &"
echo "ok"
;;
reload)
echo -n "Reload PostgreSQL: "
su - $PGUSER -c "$PGCTL reload -D '$PGDATA' -s"
echo "ok"
;;
status)
su - $PGUSER -c "$PGCTL status -D '$PGDATA'"
;;
*)
# Print help
echo "Usage: $0 {start|stop|restart|reload|status}" 1>&2
exit 1
;;
esac
exit 0
2.給postgresql分配執行權限
chmod a+x /etc/init.d/postgresql
3.添加開機自啟
chkconfig --add postgresql
4.重啟服務器驗證設置是否成功(可不做)
reboot
5.查詢服務啟動是否正常
systemctl status postgresql
嘻嘻 好久沒有更新了呢
因為最近一直都和L待在一起
嘿嘿嘿
補個情話叭
想你的夜晚 數羊沒用
數星星沒用 喝酒沒用
沒用 沒用 沒用
除了想你 什么都沒用