金庸武功之“”左右互搏术“”postgresql 主从异步流复制配置

一.环境准备

a.关闭selinxu

b.关闭iptables

c.centos6.5

d.postgresql9.4.4

master:192.168.1.211

slave:  192.168.1.212

时间同步:

#同步系统时间

[[email protected] ~]#  rm  -rf  /etc/localtime

[[email protected] ~]#  ln  -s   /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

[[email protected] ~]#  yum   install  -y  ntpdate

[[email protected] ~]#  /usr/sbin/ntpdate -u 202.120.2.101 && hwclock -w

[[email protected] ~]# systemctl enable ntpdate && systemctl start ntpdate       #  如果在启动ntp时报错,那是端口号被占用了,如下处理

[[email protected] ~]# yum install -y lsof

[[email protected] ~]# lsof -i:123

[[email protected] ~]# kill -9 $进程号

[[email protected] ~]# systemctl enable ntpdate && systemctl start ntpdate

[[email protected] ~]# crontab  -e

*/5 * * * * /usr/sbin/ntpdate -u 202.120.2.101 && hwclock -w

二.安装,master && slave 一样

四、源码包安装

1、在三台安装依赖包

yum -y install gcc*

yum -y install readline-devel

2、在三台增加用户

# adduser postgres

3. 下载PostgreSQL 源码包

# wget https://ftp.postgresql.org/pub/source/v9.4.4/postgresql-9.4.4.tar.bz2

4. 解压源码包

# tar xjf postgresql-9.4.4.tar.bz2      #需要安装     yum install -y bzip2

5. 进入解压后的目录

# cd postgresql-9.4.4

6.开始编译安装PostgreSQL 数据库。

[[email protected] postgresql-9.4.4]# ./configure --prefix=/usr/local/pgsql

[[email protected] postgresql-9.4.4]#make

[[email protected] postgresql-9.4.4]# make install

7.设置环境

pgsql

[[email protected] postgres]# vi  /etc/profile

PATH=$PATH:$HOME/bin:/usr/local/pgsql/bin

保存退出。

让环境变量生效

[[email protected] postgres]# source  /etc/profile

8.初始化数据库

8.1新建数据目录

[[email protected] postgres]# mkdir /data/pg/data

8.2更改权限

[[email protected] postgres]# chown postgres:postgres /data/pg/data

8.3切换到postgres用户

[[email protected] postgres]# su - postgres

8.4   init db

[[email protected] ~]$ /usr/local/pgsql/bin/initdb -D /data/pg/data

到这里数据的初始化就完成

9.系统服务

9.1回到root用户

[[email protected] ~]$ exit

9.2复制安装目录下的linux文件/etc/init.d/

进入postgresql的安装目录

[[email protected] postgres]# cd /root/postgresql-9.4.4/

[[email protected] postgresql-9.4.4]# cp contrib/start-scripts/linux /etc/init.d/postgresql

9.3修改/etc/init.d/postgresql  注意:红色是修改部分

[[email protected] postgresql-9.4.4]# vi /etc/init.d/postgresql

#! /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 <[email protected]>

# contrib/start-scripts/linux

## EDIT FROM HERE

# Installation prefix

prefix=/usr/local/pgsql

# Data directory

PGDATA="/data/pg/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).  Setting the OOM_SCORE_ADJ value

# to -1000 will disable OOM kill altogether.  If you enable this, you probably

# want to compile PostgreSQL with "-DLINUX_OOM_SCORE_ADJ=0", so that

# individual backends can still be killed by the OOM killer.

#OOM_SCORE_ADJ=-1000

# Older Linux kernels may not have /proc/self/oom_score_adj, but instead

# /proc/self/oom_adj, which works similarly except the disable value is -17.

# For such a system, enable this and compile with "-DLINUX_OOM_ADJ=0".

#OOM_ADJ=-17

## 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 -w" here.

# But without -w, pg_ctl adds no value.)

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

}

# Parse command line parameters.

case $1 in

start)

echo -n "Starting PostgreSQL: "

test x"$OOM_SCORE_ADJ" != x && echo "$OOM_SCORE_ADJ" > /proc/self/oom_score_adj

test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adj

su - $PGUSER -c "$DAEMON -D ‘$PGDATA‘ &" >>$PGLOG 2>&1

echo "ok"

;;

stop)

echo -n "Stopping PostgreSQL: "

su - $PGUSER -c "$PGCTL stop -D ‘$PGDATA‘ -s -m fast"

echo "ok"

;;

restart)

echo -n "Restarting PostgreSQL: "

su - $PGUSER -c "$PGCTL stop -D ‘$PGDATA‘ -s -m fast -w"

test x"$OOM_SCORE_ADJ" != x && echo "$OOM_SCORE_ADJ" > /proc/self/oom_score_adj

test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adj

su - $PGUSER -c "$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

9.4启动数据库

[[email protected] postgresql-9.4.4]#  cd /etc/init.d

[[email protected] postgresql-9.4.4]# chmod +x postgresql

[[email protected] postgresql-9.4.4]# /etc/init.d/postgresql start

9.5让数据库开机启动

[[email protected] postgresql-9.4.4]# chkconfig --add postgresql

[[email protected] postgresql-9.4.4]# chkconfig postgresql on

9.6创建数据操作历史记录文件

[[email protected] postgresql-9.4.4]# touch /usr/local/pgsql/.pgsql_history

[[email protected] postgresql-9.4.4]# chown postgres:postgres /usr/local/pgsql/.pgsql_history

10.测试使用

[[email protected] ~]$ createdb test

[[email protected] ~]$ psql test

psql (9.4.4)

Type "help" for help.

test=#

源码编译安装成功

这里安装以后要把数据库停掉,省的后面端口被占用后,配置改完了,重启不生效,楼主就是这个小问题导致试验了好几次都不成功,切记,装完把数据库停掉。

三.配置master

首先,在   vi pg_hba.conf

host    all             all             192.168.1.0/24          md5

host     replication     repluser        192.168.1.0/24          md5

其次,vi postgresql.conf

listen_addresses = ‘*‘

max_connections = 100

wal_level = hot_standby

max_wal_senders = 5

wal_keep_segments=16

在主库增加同步的用户名与密码

[[email protected] ~]$ psql -d postgres

psql (9.4.3)

Type "help" for help.

postgres=# CREATE ROLE repluser REPLICATION LOGIN PASSWORD ‘123456‘;

CREATE ROLE

postgres=#

启动master

四.配置standby

首先把数据库数据目录下的内容删除

cd /data/pg/data

rm -rf *

然后执行基础备份

[[email protected] data]# pg_basebackup -h 192.168.1.211 -U repluser -F p -x -P -R -D /data/pg/data/ -l repluserbackup20170913

执行完后,由于使用了R选项,所以会生成recovery.conf文件

vi  recovery.conf

standby_mode = ‘on‘

primary_conninfo = ‘user=repluser password=123456 host=192.168.1.211 port=5432 sslmode=disable sslcompression=1‘

recovery_target_timeline = ‘latest‘

vi postgresql.conf

hot_standby = on

启动standby

这里启动时要确保数据库之前没有启动,端口没被占用,这样修改的配置文件才会生效。切记

五.验证

在master上建个表,插入数据

postgres=# create table test01(id int primary key, note text);

postgres=# insert into test01 values(1,‘11111‘);

postgres=# insert into test01 values(3,‘22222‘);

postgres=# insert into test01 values(3,‘33333‘);

postgres=# insert into test01 values(4,‘44444‘);

postgres=# select * from test01;

id | note

----+-------

1 | 11111

2 | 22222

3 | 33333

4 | 44444

(4 rows)

postgres=#

在slave上

postgres=# select * from test01;

id | note

----+-------

1 | 11111

2 | 22222

3 | 33333

4 | 44444

(4 rows)

还有一点就是,在master上执行

postgres=# select pg_is_in_recovery();

pg_is_in_recovery

-------------------

f

(1 row)

而在slave上

postgres=# select pg_is_in_recovery();

pg_is_in_recovery

-------------------

t

(1 row)

在master

postgres=# select client_addr,sync_state from pg_stat_replication;

client_addr  | sync_state

---------------+------------

192.168.1.212 | async

(1 row)

说明94是从服务器,在接收流,而且是异步流复制。

此外,还可以分别在主、从节点上运行 ps aux | grep postgres 来查看进程:

主服务器(211)上:

[[email protected] ~]$ ps aux | grep postgres

postgres   1193  0.0  1.2 262720 13020 ?        S    16:56   0:00 /usr/local/pgsql/bin/postmaster -D /data/pg/data

postgres   1195  0.0  0.3 262820  3020 ?        Ss   16:56   0:00 postgres: checkpointer process

postgres   1196  0.0  0.2 262720  2228 ?        Ss   16:56   0:00 postgres: writer process

postgres   1197  0.0  0.5 262720  5192 ?        Ss   16:56   0:00 postgres: wal writer process

postgres   1198  0.0  0.1 263148  1788 ?        Ss   16:56   0:00 postgres: autovacuum launcher process

postgres   1199  0.0  0.0 118012   988 ?        Ss   16:56   0:00 postgres: stats collector process

postgres   1281  0.0  0.2 263352  2340 ?        Ss   17:31   0:00 postgres: wal sender process repluser 192.168.1.212(38215) streaming 0/30194E0

root       1414  0.0  0.1 145452  1608 pts/0    S    19:11   0:00 su - postgres

postgres   1415  0.0  0.1 108320  1884 pts/0    S    19:11   0:00 -bash

postgres   1445  0.0  0.1 110252  1172 pts/0    R+   19:19   0:00 ps aux

postgres   1446  0.0  0.0 103260   876 pts/0    S+   19:19   0:00 grep postgres

可以看到有一个 wal sender 进程。

从服务器(212)上:

[[email protected] ~]$ ps aux | grep postgres

postgres   1155  0.0  1.4 265236 14660 ?        S    17:31   0:00 /usr/local/pgsql/bin/postmaster -D /data/pg/data

postgres   1156  0.0  0.1 265336  1696 ?        Ss   17:31   0:00 postgres: startup process   recovering 000000010000000000000003

postgres   1157  0.0  0.2 265336  2520 ?        Ss   17:31   0:00 postgres: checkpointer process

postgres   1158  0.0  0.2 265236  2048 ?        Ss   17:31   0:00 postgres: writer process

postgres   1159  0.0  0.0 117860   900 ?        Ss   17:31   0:00 postgres: stats collector process

postgres   1160  0.0  0.1 269764  1944 ?        Ss   17:31   0:03 postgres: wal receiver process   streaming 0/30194E0

root       1184  0.0  0.1 145452  1604 pts/0    S    17:49   0:00 su - postgres

postgres   1185  0.0  0.1 108320  1876 pts/0    S    17:49   0:00 -bash

postgres   1247  0.0  0.1 110252  1168 pts/0    R+   19:19   0:00 ps aux

postgres   1248  0.0  0.0 103260   876 pts/0    S+   19:19   0:00 grep postgres

时间: 2024-12-25 06:11:59

金庸武功之“”左右互搏术“”postgresql 主从异步流复制配置的相关文章

金庸武功之“”天山折梅手“”-elk5.2

ELK是Elasticsearch.Logstash.Kibana的简称,这三者是核心套件,但并非全部. Elasticsearch是实时全文搜索和分析引擎,提供搜集.分析.存储数据三大功能:是一套开放REST和JAVA API等结构提供高效搜索功能,可扩展的分布式系统.它构建于Apache Lucene搜索引擎库之上. Logstash是一个用来搜集.分析.过滤日志的工具.它支持几乎任何类型的日志,包括系统日志.错误日志和自定义应用程序日志.它可以从许多来源接收日志,这些来源包括 syslog

金庸武功之““兰花拂穴手””--elk5.5安装

ELK是Elasticsearch.Logstash.Kibana的简称,这三者是核心套件,但并非全部. Elasticsearch是实时全文搜索和分析引擎,提供搜集.分析.存储数据三大功能:是一套开放REST和JAVA API等结构提供高效搜索功能,可扩展的分布式系统.它构建于Apache Lucene搜索引擎库之上. Logstash是一个用来搜集.分析.过滤日志的工具.它支持几乎任何类型的日志,包括系统日志.错误日志和自定义应用程序日志.它可以从许多来源接收日志,这些来源包括 syslog

金庸武功之“无毒掌”---jenkins安装部署

一.环境准备 centos7.2mini  SELinux关闭 防火墙关闭 二.JDK安装 安装方式:rpm安装  软件:jdk-7u80-linux-x64.rpm 进一步查看JDK信息: [[email protected] ~]# rpm -qa | grep java tzdata-java-2012c-1.el6.noarch java-1.6.0-openjdk-1.6.0.0-1.45.1.11.1.el6.x86_64     卸载OpenJDK,执行以下操作: [[email 

金庸武功之“七伤拳”--etcd集群搭建

环境: etcd01:192.168.93.201,centos7.2 etcd02:192.168.93.203,centos7.2 etcd03:192.168.93.203,centos7.2 软件版本: etcd:2.3.7 实施步骤: 以etcd1部署为例,其他2个主机步骤一样: 安装etcd [[email protected] ~]# yum install etcd -y 修改配置文件 vi /etc/etcd//etcd.conf ETCD_NAME=etcd01 ETCD_D

金庸武功之“碧血剑法”----squid做透明代理

一.试验目的:公司阿里云环境要求之开放一个代理服务器,其他服务器不允许有外网IP 二.环境拓扑: A:代理服务器:(利用squid做透明代理) (centos7.2) [[email protected] squid]# ip a eth0:10.30.204.122 eth1:116.62.XX.XX B:客户端服务器:(centos7.2) [[email protected] squid]# ip a eth0:10.30.204.90 三.试验环境准备(A,B都执行) yum  -y u

金庸武功之“黯然销魂掌”---zabbix实现微信报警

一.部署环境 系统:CentOS 6.5x64 最小化安装 Server:192.168.93.126 Client:192.168.93.125 二.基础软件包安装 在server安装基础软件包,这里的环境使用yum安装,如果使用源码安装也是可以的. yum -y install wget vim tree gcc gcc-c++ autoconf httpd php mysql mysql-server php-mysql httpd-manual mod_ssl mod_perl mod_

金庸武功之“九阳神功”--kubenetes集群的那些事儿

一.前言 首先,要解决的就是安装来源问题,由于长城防火墙的原因,我们要想办法自己找来源. Kubernetes 编译的各种发行版安装包来源于 Github 上的另一个叫 release 的项目,地址 点这里,把这个项目 clone 下来,由于本人是 Centos 用户,所以进入 rpm 目录,在安装好 docker 的机器上执行那个 docker-build.sh 脚本即可编译 rpm 包,最后会生成到当前目录的 output 目录下,截图如下 1.git clone https://githu

金庸武功之“易筋经”--docker pipework 设置容器IP在宿主机网段

一.主机环境及环境准备 宿主机IP:192.168.1.107 宿主机网关:192.168.1.1 容器IP:目的是设置为192.168.1.108 关闭Selinux 设置为网卡桥接模式 关闭防火墙 yum install bridge-utils -y yum install git -y 二.安装docker 增加repo tee /etc/yum.repos.d/docker.repo <<-'EOF' [dockerrepo] name=Docker Repository baseu

金庸武功之“乾坤大挪移”--kubenetes1.4容器集群搭建

1 初始化环境 关闭selinux 关闭selinux 关闭selinux 1.1 环境:centos7.2 节点 IP node-1 192.168.93.201 node-2 192.168.93.202 node-3 192.168.93.203 1.2 设置hostname hostnamectl --static set-hostname hostname IP hostname 192.168.93.201 kube.master 192.168.93.202 kube.salve1