首页 技术 正文
技术 2022年11月17日
0 收藏 698 点赞 3,440 浏览 2428 个字

  

主备查询

主备不会自动切换(即需要实现线上环境主数据库宕掉之后,从数据库能够自动切换为主数据库,需要借用第三方软件,例如heartbeat等)

(1)如何查看是primary还是standby

  •   方法1:查看进程

  主机 sender

  [wln@localhost data]$ ps -ef | grep wal
  wln      10421 10416  0 07:52 ?        00:00:00 postgres: wal writer process             
  wln      10691 10416  0 08:04 ?        00:00:00 postgres: wal sender process repluser 127.0.0.1(59226) streaming 0/6011BAC
  wln      10702  8927  0 08:05 pts/2    00:00:00 grep wal

  备机 receiver

  [wln@localhost data]$ ps -ef | grep wal
  wln      10421 10416  0 07:52 ?        00:00:00 postgres: wal writer process             
  wln      10690 10685  3 08:04 ?        00:00:03 postgres: wal receiver process   streaming 0/6011BAC
  wln      10702  8927  0 08:05 pts/2    00:00:00 grep wal

  •   方法2:

  主机:

  [wln@localhost data]$ pg_controldata | grep Database  
  Database system identifier:           6032728846531166188
  Database cluster state:               in production
  Database block size:                  8192

  备机:

  [wln@localhost standby]$ pg_controldata | grep Database
  Database system identifier:           6032728846531166188
  Database cluster state:               in archive recovery
  Database block size:                  8192

主备切换(方式1)

(1)备机切换为主机

  •   将主机停掉

  [wln@localhost data]$ pg_ctl stop
  waiting for server to shut down…. done
  server stopped

  •   在备机PGDATA对应目录下生成recovery.conf文件中指定的文件trigger_file = ‘/pgdata/9.1/data2/trigger_activestb’  (此时备机正常运行状态)

  touch  /pgdata/9.1/data2/trigger_activestb

  会看到 recovery.conf 文件变为 recovery.done

  •   查看原来备机日志:

  LOG:  trigger file found: /home/wln/standby/trigger_activestb
  LOG:  redo done at 0/6011C40
  LOG:  last completed transaction was at log time 2014-07-06 08:05:40.152147+08
  LOG:  selected new timeline ID: 2
  LOG:  archive recovery complete
  LOG:  database system is ready to accept connections
  LOG:  autovacuum launcher started

  •   登录原来的备机

  [postgres@postgres ~]$ psql  
  psql (9.1.0)
  Type “help” for help.

  You are now connected to database “postgres” as user “postgres”.
  postgres=# SHOW transaction_read_only
  postgres-# ;
  transaction_read_only
  ———————–
  off
  (1 row)

  可以看出现在原备机具有读写功能。即主备切换成功

  postgres=# \d
       List of relations
   Schema | Name | Type  | Owner 
  ——–+——+——-+——-
   public | t1   | table | wln
  (1 row)

  postgres=# create table t2(id int);
  CREATE TABLE

4、主备切换(方式2:实践过程中采用的方式)

  备机中recovery.conf中设置为:

  standby_mode = on

  recovery_target_timeline = ‘latest’

  primary_conninfo = ‘host=localhost port=5433 user=repluser password=password’

  #trigger_file = ‘/home/wln/standby/trigger_activestb’  #这个注释掉

  备机升为主机方式为:pg_ctl promote -D standbyPath

  会看到recovery.conf过几秒后变为recovery.done 表示升级为主机成功。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919