postgresql-9

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取

checkpoint_segments = 16

archive_mode = on

max_wal_senders = 3
wal_keep_segments = 16


2、创建复制用户



CREATE USER repuser REPLICATION LOGIN CONNECTION LIMIT 4 ENCRYPTED PASSWORD ‘123’;


3、设置 pg\_hba.conf访问权限,添加以下



IPv4 local connections:

host all all 192.153.167.137/24 md5
#host replication postgres ::1/128 md5
host replication repuser 192.153.167.137/24 md5


4、启动库并查看表空间



/etc/init.d/postgresql-9.2 restart

cyyun=# \db
List of tablespaces
Name | Owner | Location
------------±---------±------------------
fastspace | postgres | /data1/tablespace
pg_default | postgres |
pg_global | postgres |
(3 rows)


5、在master上查看数据目录,先查看表空间目录和数据目录,因为这些目录需要在备库主机上手工创建



echo $PGDATA
/opt/PostgreSQL/9.2/data


6、在slave库上面创建目录并且授权



mkdir -p /data/psql

mkdir -p /data1/tablespace

chown postgres. /data/psql

chown postgres. /data1/tablespace

chmod 700 /data/psql


7、在slave上面创建.pgpass



touch .pgpass

chmod 600 .pgpass

postgres@jxnc1:~$ cat .pgpass
192.153.167.136:3500:replication:repuser:123


8、先删除从库的data目录  
 使用 pg\_basebackup 生成备库



postgres@jxnc1:~$ pg_basebackup -D /usr/local/pgsql/data -Fp -Xs -v -P -h 192.168.163.182 -p 5432 -U repuser
transaction log start point: 0/3000020
pg_basebackup: starting background WAL receiver
25769/25769 kB (100%), 2/2 tablespaces
transaction log end point: 0/30000E0
pg_basebackup: waiting for background process to finish streaming…
pg_basebackup: base backup completed


9、设置从库 postgresql.conf



hot_standby = on


10、创建 recovery.conf



postgres@jxnc1:~$ cat /data/psql/recovery.conf|tail -5
#---------------------------------------------------------------------------

standby_mode = on
primary_conninfo = ‘host=192.153.167.136 port=3500 user=repuser’
trigger_file = ‘/data/psql/postgresql.trigger.3500’


11、修改启动脚本 /etc/init.d/postgresql-9.2里对应的data和log目录改成新的目录/data/psql下的


12、启动



/etc/init.d/postgresql-9.2 start


13、查看slave进程及log



postgres$ ps aux |grep postgres
postgres 19904 0.0 0.0 37280 1476 pts/0 S 17:09 0:00 su - postgres
postgres 19905 1.0 0.0 21252 3864 pts/0 S 17:09 0:00 -su
postgres 20002 0.2 0.0 90124 8080 pts/0 S 17:10 0:00 /opt/PostgreSQL/9.2/bin/postgres -D /data/psql
postgres 20003 0.0 0.0 56380 1220 ? Ss 17:10 0:00 postgres: logger process
postgres 20004 0.1 0.0 90156 1864 ? Ss 17:10 0:00 postgres: startup process recovering 000000010000000000000004
postgres 20005 0.0 0.0 90124 1436 ? Ss 17:10 0:00 postgres: checkpointer process
postgres 20006 0.1 0.0 90124 1440 ? Ss 17:10 0:00 postgres: writer process
postgres 20007 0.0 0.0 58524 1304 ? Ss 17:10 0:00 postgres: stats collector process
postgres 20008 0.5 0.0 105324 2788 ? Ss 17:10 0:00 postgres: wal receiver process streaming 0/4000280
postgres 20073 0.0 0.0 15268 1220 pts/0 R+ 17:10 0:00 ps aux
postgres 20074 0.0 0.0 7844 936 pts/0 S+ 17:10 0:00 grep --color=auto postgres



postgres@jxnc1:/data/psql$ tailf pg_log/postgresql-2015-02-03_171003.log

2015-02-03 17:10:03 CST LOG: database system was interrupted; last known up at 2015-02-03 16:49:25 CST
2015-02-03 17:10:03 CST LOG: creating missing WAL directory “pg_xlog/archive_status”
2015-02-03 17:10:03 CST LOG: entering standby mode
2015-02-03 17:10:03 CST LOG: redo starts at 0/3000020
2015-02-03 17:10:03 CST LOG: consistent recovery state reached at 0/30000E0
2015-02-03 17:10:03 CST LOG: database system is ready to accept read only connections
2015-02-03 17:10:04 CST LOG: streaming replication successfully connected to primary


14、查看master进程



root@jxnc1:/data1/tablespace/PG_9.2_201204301# ps aux |grep postgres
postgres 14446 0.0 0.0 90736 8096 pts/1 S 16:14 0:00 /opt/PostgreSQL/9.2/bin/postgres -D /opt/PostgreSQL/9.2/data
postgres 14447 0.0 0.0 57072 1188 ? Ss 16:14 0:00 postgres: logger process
postgres 14449 0.0 0.0 90860 2296 ? Ss 16:14 0:00 postgres: checkpointer process
postgres 14450 0.0 0.0 90736 1384 ? Ss 16:14 0:00 postgres: writer process
postgres 14451 0.0 0.0 90736 1380 ? Ss 16:14 0:00 postgres: wal writer process
postgres 14452 0.0 0.0 91552 2636 ? Ss 16:14 0:00 postgres: autovacuum launcher process
postgres 14453 0.0 0.0 59168 1212 ? Ss 16:14 0:00 postgres: archiver process last was 000000010000000000000003
postgres 14454 0.0 0.0 59328 1460 ? Ss 16:14 0:00 postgres: stats collector process
postgres 18964 0.0 0.0 64152 1976 pts/1 S 16:36 0:00 su - postgres
postgres 18965 0.0 0.0 29852 7940 pts/1 S 16:36 0:00 -su
postgres 20912 0.0 0.0 15512 1464 pts/1 S+ 16:43 0:00 /bin/bash /opt/PostgreSQL/9.2/bin/psql cyyun
postgres 20914 0.0 0.0 55216 2936 pts/1 S+ 16:43 0:00 /opt/PostgreSQL/9.2/bin/psql.bin cyyun
postgres 20947 0.0 0.0 92764 6068 ? Ss 16:43 0:00 postgres: postgres cyyun [local] idle
postgres 27512 0.0 0.0 91568 2836 ? Ss 17:09 0:00 postgres: wal sender process repuser 59.53.67.37(44583) streaming 0/4000828
root 28200 0.0 0.0 10424 932 pts/0 S+ 17:14 0:00 grep --color=auto postgres


到此主从搭建完成。


常见报错 :


1、postgres@ubuntu:~$ pg\_basebackup -D ?/usr/local/pgsql/data -Fp -Xs -v -P -h 192.168.163.182 -p 5432 -U repuser?  
 pg\_basebackup: could not connect to server: could not connect to server: Connection refused  
 Is the server running on host “192.168.163.182” and accepting  
 TCP/IP connections on port 5432?  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190928174820626.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwOTA3OTc3,size_16,color_FFFFFF,t_70)


解决办法:  
 在主从数据库上面查看日志cat logfile  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190928174640590.png)  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190928174655473.png)  
 前面添加#,注释掉,127.0.0.1表示本机  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190928223030475.png)  
 查看本机的端口号,然后kill-9 端口号。重新启动数据库服务。  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190928223430330.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwOTA3OTc3,size_16,color_FFFFFF,t_70)


## 扩展


## 生产环境在线数据库重新初始化和重新做主从


**一主库操作步骤**  
 1、先检查系统是否已设置好中文包,方法如下:



echo $LANG;echo $LANGUAGE
zh_CN.UTF-8
zh_CN:zh


2、停止web服务器和dumpall 数据库库  
 停止web:



/etc/init.d/tomcat6 stop


备份数据库:



su - postgres
pg_dumpall > db.date -d today +%Y%m%d%H%M%S.out


3、找一台测试服务器,测试备份的数据导入数据库是否有问题


新建其它库名进行导入测试



createdb dbname
psql postgres -f db.date -d today +%Y%m%d%H%M%S.out


4、如有问题,排查问题,具体问具体解决


5、停止主库来备份数据库目录



/opt/PostgreSQL/9.2/bin/postgres -D /data1/pgsql -m fast stop
cd /data1/
mv pgsql pgsqlbk
mkdir pgsql


6、初始化数据目录



/opt/PostgreSQL/9.2/bin/initdb -D /data1/pgsql --locale=zh_CN.UTF8

(Success. You can now start the database server using:
/opt/PostgreSQL/9.2/bin/postgres -D /data1/pgsql or
/opt/PostgreSQL/9.2/bin/pg_ctl -D /data1/pgsql -l logfile start)


7、测试启动数据库



/opt/PostgreSQL/9.2/bin/postgres -D /data1/pgsql start


8、停止数据库备份配置文件和表空间



/opt/PostgreSQL/9.2/bin/postgres -D /data1/pgsql stop
mv pgsql/postgresql.conf pgsql/postgresql.conf.bk
mv pgsql/pg_hba.conf pgsql/pg_hba.conf.bk
mv pgsql/pg_tblspc pgsql/pg_tblspc.bk


9、拷贝源备份的postgresql.conf、pg\_hba.conf和表空间文件到新库pgsql中



cp pgsqlbk/postgresql.conf pgsql/
cp pgsqlbk/pg_hba.conf pgsql/
cp -r pgsqlbk/pg_tblspc pgsql/


10、启动数据库



/opt/PostgreSQL/9.2/bin/postgres -D /data1/pgsql start


11、导入备份的库



su - postgres
createdb dbname
psql -U postgres -d dbname -f db.date -d today +%Y%m%d%H%M%S.out


11、验证数据是否完整。


![img](https://img-blog.csdnimg.cn/img_convert/5808e24d53c55384f01eedb7d4298fc9.png)
![img](https://img-blog.csdnimg.cn/img_convert/29e90124b6d38beec7cbb72026752ef5.png)
![img](https://img-blog.csdnimg.cn/img_convert/228c2fcfe5959478c4fbe08520cfa63f.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618631832)**

S`.out 

11、验证数据是否完整。

[外链图片转存中…(img-FsF6FBzn-1715815375397)]
[外链图片转存中…(img-EpxeVX92-1715815375398)]
[外链图片转存中…(img-QZAcDagX-1715815375398)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取

  • 19
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值