《pg+pgpool+Ubuntu 实现分布式流复制模式.docx》由会员分享,可在线阅读,更多相关《pg+pgpool+Ubuntu 实现分布式流复制模式.docx(14页珍藏版)》请在三一办公上搜索。
1、1.构架图本文档由postgreSQL专业群 深圳-GS初稿,PostgreSQL群beta 2015-01-18日修订主要工作有GS完成PGfSlave 1)PG Slave 2)Pgpgl进程PG(ma&ter)2. 主机宕机后构架图PG(New Master)APR REPgpoQl 程PG(Slave 2)PGfmaster)3. 恢复完成后构架图4.实际部署图5. 操作步骤1.安装pga) apt-get install postgresql-9.1b) : apt-get install postgresql-contrib-9.1c) : apt-get install libp
2、q-devd) : apt-get install postgresql-server-dev-9.1e) :配置系统用户postgres密码:pgf) :使用root用户,创建目录用来做后面的归档目录rootxelg-lLniix:mkdir /opt/archiverootxelg-liniLX:chown postgres :postgres /opt/archive/g) :进入postgres的home目录,vim .bash_profile添加环境变量,修改完成后使用 source .bash_profile 激活环境变量exp:rt PATHOSPATH:/usr/lib/pos
3、tgzesql/91/bin ezpozt PGPORT=5432export PGUSER=postgresrt PGPATH=/asr/lib/postgresql/9.1/bin expcrt PGDATA=/var/lib/postgresql/9.1/mainh) :配置数据库超级用户postgres密码:pgdb,并使用q推出SQL命令模式postgresyxelg-lLiinx:目 psqlpsql (9.1.13)Type help” for help.postgres=# alter user postgres password pgdb;ALTER ROLEpostgres
4、# q_postgrms多任1glinug:$ 口i) 进入postgres配置目录(下面目录只针对pg在ubuntu上的apt-get默认安装)postgres xelg-linux: -5 cd. /etc/postgresql./9.1/main/ postgres xelg-linux: /etc/postgresql/9. l/main.$ Is environment pg_hba conf postgresql conf pg_ctl.conf pg_ident.conf start.conf postgres xelg-linux:/etc/postgresql/9.l/mai
5、n$j)修改:pg_hba.conf:添加行:hostreplicationpostgres192.168.141.142/32trusthostreplicationpostgres192.168.141.140/32trusthostreplicationpostgres192.168.141.141/32trusthostreplicationpostgres1&2.1,68.141.142/32trey 1. SJmast.srhostreplicationpostgres192.160.141.140/32trut Sjfslave 1hostreplicationpostgres1
6、92.160.141.11/32trut #tslave 2k) 修改 postgresql.confi. 启用参数:listen_addressesii. 配置端口:port = 5432iii. 启用参数:wal_level = hot_standbyiv. 启用参数:archive_mode = onarchive_command = cp %p /opt/archivedir/%fv. 启用参数:hot_standby = onvi. 启用参数:max_wal_senders = 2l) 推出当前shell连接,重新登录系统用户postgres,确保环境变量生效m)创建配置文件post
7、gres.conf软连接文件到$PGDATA目录:ln /etc/postgresql/9.1/main/postgresql.conf /var/lib/postgresql/9.1/main/否则在使用pg_ctl start启动数据库是出现如下错误:postgreslxelg - virtualmachine:pg ctl startserver startingvlrtudl-Hacblne:S postgres cannot access the server conflguratlof tie /var/ltb/postgresql/g. 1/natn/pDEtgresql. co
8、nf :没有那个文件或目录n) 启动数据库(系统用户postgres下执行pg_ctl restart)20 L4-0-/7 09 : 5. CST LCG : roco: vod sm.rt shutdown201 1-0-27 09:1423 cel 丁心口: autovacuum K-unchf.r shutting down 201-O3 CST _OG: dazaliatEsystem i口 3hut down denestoppedserver 5tar?ingpotgrF&Klg- Li nux: /tc/po3gr3ql/9.1/minS 2014-04 -27 DE*: 19
9、 : 54 匚?;1? LCG : databmw: synt商shut cown 社 2014-04 -21 09: 1 9:.5? :st201-04-27 09; 19;54 GST LCG; database syslciT. it ready lu acjcljL CGiinccti cns2014 -04-27 09 :15:54 CS_ LCU: autovacuiLii Isuncher started2.配置ssh无密码连接(postgres)把B主机的公钥文件拷贝到A主机的authorized_keys,这样B主机可以ssh免密码登录A可以通过查看/var/log/secu
10、re来查看登录信息,这种方式为publickeya)安装 ssh 服务:apt-get install sshb)切换到系统用户postgres,执行ssh-keygen -t rsa命令,后续默认回车,并生成类似二维码的 东西。Generating public/prlvatc rsd key pair.Errtmr fi Le i n whn ch to eg th巳(/var/1 ib/pntgrql/ -id_ :directory T/var/lib/postgrql/.bsh1Enter pa.5sph.ra5e (empty tor no passpiir:Enter uame
11、paKbphxat; again:Your ide nt i t ic a t io n ha 5 been 5avel in /var/lib/posture 5 q_/ .ssh./ id_xss, Your pub J ic key h,-s been : aved in /va c/i ib/p():tgre:il/,sh/id r:; ,piib, Th.e ,ey tingeiprIet 15:On :c:d:c3:14:c :Til:29:xelg linuxThmrandomarl Linage is:+ RSA 2心:+IIIo oII . . o -II + + u - .
12、II三*SII = + . + .II0*u II- -O .II.-9.I0进入用户的home目录,用ls可以看到.ssh的文件夹,进入.ssh文件夹,查看文件:pcstgibxziLg-linux:/titc/pohtgrt3sql/9*l/itiain5 cdpcstgres&xelg-l inuix: -S u三 .53h pcstgroGK2L0-Linux:! id_ sa i ii_r m d. piiba) 使用 cat id_rsa.pub authorized_keys(#名称必须是 authorized_keys)pcstgre:xelg-i inux: /et(:/po
13、stgreql/3,1 /mains cd - /.s: authorized_keys 追加到 authorized_keys 里面j:cstgre7 5x.elg- irmx:. 5 5h$ sauthorized keys id rsa id xsa.puib known hosts nodel slavelpostgreszelg-Linux: -/ . ssti? cat nodel authorized._keys postgresxelg-Linux: -/ . ssti$ cat slavel authorized._k:eys uJrri ,n d) 测试无密码连接是否生效:
14、在第一次连接输入密码后,以后的连接都不用输入密码poGtgresselg-lin.u.x: -/ . ssh? gh 192.16S . 141.142Welcome 二口 Ubuntu 12.1.0 (UMU/Linux 3 * 3 - 0-L-/-generic 我8 6_6封)* Documertat ion: htzps : /help. libuntu. com/New rsleae T13.04 T avaiLafels.Run f do-rslease-upgrade to upgrade to it.Lat Logir: Sun Apr 27 09:32:07 2014 fro
15、m koLo Linux 2.local3.配置pg流复制模式(slave1,slave2)a) 使用 pg_ctl 命令,停止 slave1,slave2 的 PG 服务(slave)pL:5tyres3xfelg-liiLiix; /tc/pcstgrsql/S * 1/n.aliiS pg_ct 1 stop 2014-04-217 09; 21:39 CST LOG: received snar-z. ahutdo request I201-l-0-27 0:21:3S1 CST _OG: autovacuum Launcher shutting down waiting tier s
16、erver tc. shut dawn, . , .2014-04-2:7 0E?: 21:3? USl _OG: stiutt ing down2014-03-27 05:21:40 CST _UG: datibae system 12 shut lown donesr fjTnyjpedb)返回master服务器,执行如下命令postgresxelg-linux:/.ssh$ psqlpsql(9.1.13)Type help for help.postgres=# select pg_start_backup(stream_bak);pg_start_backup0/2000020(1
17、row)postgres=# qLinux:psql 9.1.13Type ,theLp,F for help.postgrsM select pg sts t rkup (F t.rsam_ba );pg_s tET t_ba c kuj?)/2000020(1 r-jw)postgrss=# cc) 使用命令 scp -r 9.1/ 192.168.141.140:和 scp -r 9.1/ 192.168.141.141:将备份文件分别 传送给 slave1 slave2 (master)d) 在master上面执行如下命令(master)postgresxelg-linux:$ psq
18、lpsql(9.1.13)Type help for help.postgres=# select pg_stop_backup();NOTICE: pg_stop_backup complete, all required WAL segments have been archivedpg_stop_backup0/2000168(1 row)postgres=#pcLgre1 g-1 _iiux: -5 pqlpql (9.1.L 3)Type pihGlpr for help.postgres= # select pg_stop_bajckup);NOTICE: pg_stop_back
19、up complete all required WAL SEgments have been archived pg_s top_b ackup0/2D0016B(1 row)e) 切换终端到 slavel 和 slave2,进入/9.1/main 目录,并执行 vi recovery.conf (slave1 slave2)cd 9.1/mann/ pGstgresfixelg-Linux:.1/main? pwd /var/Lib/postgre5ql/9.1/nainpcstgresGelg-Linux:-/9.1/main? vi recovery.conff) 添加行:编辑文件 $
20、PGDATA/recovery.confstandby_mode = onprimary_conninfo = host=192.168.141.142 port=5432 user二postgres password=pgdbrestore_command = scp 192.168.141.142:/opt/archive/%f %precovery_target_timeline=latesttrigger_file=/tmp/trigger_file0.:tanciby_inode = onprimary_conninfo = Thos=192. L6B. L4L. 142 port=
21、L25&r=pustcres 5war:i j::-jdbT fTtb刍 comnand = 1 scp 12. I C:-!. 141 . I 42 : /opt/AchJ %p rc cvc-ry_targeL_tiineline = 1 t rtri.qger_*i 1 n /tmp/tri gge十_fii iig)使用pg_ctl启动slave1,slave2数据库,并出现流复制成功连接到主节点,表示已经 成功配置了 pg的复制模式。pu3tgf?5xelq limix: -/9. L/mainS pg ctl :az ?i.artingpG5tgr:?3xlg-linux: -7
22、9 l/main Mid - M-K7 10 : j7 : 33t down in rccuv-ry 2014-: 4 27 1 j : : 29 匚三1:cp: /cpt/circhive/C JOO J02 . /ilstory: Lu Illi? ur201-0-27 10:07 : 4 宫l t.og:standby mW/upt/jirchivf?/C 300 jOC 0C jOCDOC 30C 30C G : No such,:ST LOG :syteni shudirectoryfile or cii rectory10:0-7:54CSTLOG:10:07:54CSTLOG:1
23、0:。7腴CSTLOG:10:07:54CSTLOG:2D14-04 27 201-0 rady tc r:ccept rid only connection record with z&ro length at C/5000C.I30,rep: /opt/iirchive/O JO; j0 lOt 00C DOt 30t JOO jj : No 3iich Lilc or iic?jtoryscp: /opt/Hrchive/00000002 + historyt No such film or directory2 j 1 4-0:-27 LC : 08 : 1 CST LGM: e二rr
24、mminq rapl iction successfully crji:nr?tr?d to primaryh) 测试:返回master节点,执行创建数据库命令,并在slave1,slave2上查看,是否已经 同时创建该数据库i) 测试:返回slave1,slave,执行创建数据库命令,出现如下提示:到现在PG流复制 已经成功配置crat.- databa: :-stir ;?0L4-C-27:29 CST 3-:RROR: cannot rxc-ute CREATE CArftEASE in m : rai-only -r-iv.smtion2014-: 4 27 Id: 12:29 :SL
25、 L5lATEMENJ: cr-dt- da 1 aba3- error: cannot execute create r-Tabase in rd-cnly transaction4.安装pgpool(执行pgpool的系统用户必须有ssh无密码 连接的权利,建议使用postgres用户安装pgpool)在 master执行命令./configure -with-pgsql=/var/lib/postgresql -with-pgsql-libdir=/usr/lib/postgresql/9.1/lib -with- pgsql-includedir=/usr/include/postgr
26、esql安装pgpool5.配置pgpool,实现主备自动切换(原master宕机,以 下统称M-1)a) 使用Root用户进入目录:/usr/local/etcb) 用户命令:pg_md5获取数据库用户postgres密码pgdb的MD5值rooLxelg-liniiz: /usr/Local/etc# pg_jn.d5 pgdb d463aa9c44e3ffto4027aaed7edd5a92c - 一一c)执行修改pcp.confd) 执行修改 pgpool.confe) 执行修改 poo_hba.conff) 按照pgpool.conf里面配置的failover_command参数,在
27、对应的目录下创建脚本文件g)参考下面内容:h)pcp.conf pgpool.confpool_hba.conf配置完成后,使用root用户创建文件夹:mkdir /var/run/pgpooli) 使用pgpool命令启动pgpoolrD3txlc-lirLUX: /var/lib/pJ5gre5ql# mkdir / var /run/pgpool Lg-1 inux: /var/lib/pogrpriql# pgpoolrootMdq-Linux: /var/lib/posTqrcsqlj) 切换系统用户到postgrs,执行命令psql -p 9999,看到如下界面是表示pgpool已
28、经安装OKpostgresxelg-linu.x: /etc/postgresql/9. l/main? psql -p 9999psq(9.3)Type 1Thelpv fox help.postgres=#6. 恢复并挂载宕机服务器a) 返回master服务器,切换到系统用户postgres,执行pg_ctl-m fast stoppD3tgrsxlg-llnuw!- 5 pg_ctl201d-Q4-3D21:47zL8 CST LOG:201J-D4-3D 21:J7:1S CST LOG:201d-04-3D 21:47:18 C5T LOG:2015-04-30 21:47:18 C
29、ST LOG;-iri fast stopreceived, fast shutdown request aborting any act l ve tr-a ns actions autovacuum launcher shutting down shutting downwaiting for server to shut dowi为刑-CN-30 21:47:18 CST LOG;2014-D4-30 21:47:18 CST FSTAL: donethe database system is shutting downdatabase system is shut downseiver
30、 stoppedoos ture g kg la-liniiK:Sb)切换到slavel,出现如下提示:首先提示主连接失去连接,然后开始启动本地编辑EK-3tJE63BK 1Q-llmiK?l/ffialTlS 201404-30scp; /pfc/H.tchive/OCiDflODOlQflCiOflO&OOOflCiGOSBs auch2C14-n4-3O 21137:34 CST IDA: invilid rscord Ismgth./opc/acckive/OCiOCiOOCilOCiCiOCiOOdCiOOCiOOSBi wo suchjcti: /opt/a.Ech.iw/OGDO
31、ODOS. hi stDr: Hd jui fiLe orCS7 FR.TAL5 Kjillcatlon twoil丽Ed 坷 prlnriary wrer file ar diiectary At a/3BCaOD7S file or dliect-or direcor2014-(14-30 21H7?5.5 CST LOS? trig於 20U-D4-30 21:37:55 OST IBTRL; couldIs Uiq scrrer ruiudng kp/ip eormectioMril f如rxU /trgp/tx ljer_CLlQnat conntrt ta the priruiy
32、server e could rot ccnacct to seivei: Connect iesn. refused i Aart 132. liiH-141-142 nd axcsptinq port S432?s: /qpt/a cclilvia /ODOODOOl OODOODOODOODOOHB Mo auch fl la ai dlxact-azy sep; /opc/a.cekivfr/OC!OOOD0100CiOCiOOOOOOCiOOSBi mo such lle or dlieccorc)切换到slave2,出现如下提示ZC 14-04-30 21e4S!:15 匚ST L
33、CC: r&D anm D/3BOQOa202011-0-30 21N8S15 C$1 LOGS last OWleted transaction 早耦 at L tlM 副 14-IH-3D 213f53B279234-0S wpM /crpt/fl.cchivt/OCDOOQOlOOCOOO&OOGOCQOSBs Ha aucli file ar dinctorysep: qpt/a,ECh.ivE /0DD0DD02. bi stDT: Ud zucti fl La or diEactar2C11-tn-30 21Nt3 csr LCK5; selected 世ew cinelitie
34、id 2 3CT: /opt/a.Ech.i,ipE-/ODDODDOL. hi story z No jsucti iile or direirtor2G14-04-30 2】=技山W CST LOS: 航Ctd戏 巽0ucb rile or directory rile ar cLiEtorjCET ESHSLe could not cctmect ta the pri-TLacy 5srver3 could not CDDEiect to serverz Cnne-iztLdd reEustd Is the server running ?n hast H192B1. 141,112*
35、and acceptioqTCP/IP confiectl-ons on port 5432?poatgresflx.elg-LLnux-:如 L/ma in$pc3 tgre303telg-Llnujti L/msin$pos tgre50x! Ig-LLnux:: -4 L/main$ pql帅ql 9,1,13)Type help-* lar 祯邛p&atgies-#taJ&LcNLd Intz*EROBtijjfECiE +11 cijf miini J、LIKE ir kqtab-Jlq平-日 i tKRipostgres-# create table test Cid inti;a
36、 read-only tfi3n.3e.ct.ianE?J0E: cajuiot execute 匚REKTE TABLE in.DO5txarB.5=d)回到master服务器,执行psql -p 9999,并创建table: testl,如下提示已经成功创建poslgrt5xelq-linux:psql -u 9999 psql (9.13) Type help for help.postgrpn=# postgres-poslgrt5-tablt testlid inL);CREATE TABLE postgres-e)到目前位置,已经完成主备机切换,以下步骤开始挂载原master为sl
37、ave21f) 进入 postgres 的 data 目录:/var/lib/postgresql/9.1/main, vim recovery.conf,内容如下:standby_mode = onprimary_conninfo = host=192.168.141.140 port=5432 user=postgres password=pgdbrestore_command = scp 192.168.141.140:/opt/archive/%f %precovery_target_timeline=latesttrigger_file=/tmp/trigger_file0s t a
38、ndby_mod.e = 1 on1primary_conninfo = 1 ho3t=192a 166.14110 port=5 432 U3er=po3tgrie5 ps3word=pgdlb1 restorQ_conunand = 1scp 192.168.141.lD:/opt/aichive/%f %prre cover y_t a rge t_t lune 1 ine=11 at e stptrigger file=,/trnp/triggrer fileDg)执行pg_ctl start,启动数据库,并执行psql, select * from test1,查看在原master
39、停止后创建的表test1是否恢复到该数据库上面,如下图所示:test1已经成功出现在原master (现 slave21)上面postgre-30xelg-11dux 】一/9 pg_ctl startserver starting postgresxelg-1inuM:-/9.1;main5 2014-04-30 22:10:51 CST LOG: 201-04-30 22:11:01 CST LOS: restored scp: /c-pt/archive/ODOOOOD3 history: No2013- D4-30 22:lli22 CST LM: restored. 201-04-30 22:11;22 CST LOG: enteringdatabase sy