GreenPlum数据库Master节点Standby同步与切换
本文档主要演示GreenPlum数据库的Master节点的Standby在异常后的数据同步及Master节点故障后,Standby的切换。在操作前通过gpstate命令查看Standby的状态是正常的,数据也在同步状态。
[gpadmin@mdw ~]$ gpstate -f 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:-Starting gpstate with args: -f 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:-local Greenplum Version: 'postgres (Greenplum Database) 4.3.6.2 build 1' 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:-master Greenplum Version: 'PostgreSQL 8.2.15 (Greenplum Database 4.3.6.2 build 1) on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.4.2 compiled on Nov 12 2015 23:50:28' 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:-Obtaining Segment details from master... 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:-Standby master details 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:----------------------- 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:- Standby address = mdw-std 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:- Standby data directory = /gpdb/gpdata/master/gpseg-1 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:- Standby port = 5432 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:- Standby PID = 3139 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:- Standby status = Standby host passive 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:----------------------------------------- 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:--pg_stat_replication 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:----------------------------------------- 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:--WAL Sender State: streaming 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:--Sync state: sync 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:--Sent Location: 0/C203088 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:--Flush Location: 0/C203088 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:--Replay Location: 0/C203088 20160227:17:00:24:019032 gpstate:mdw:gpadmin-[INFO]:-----------------------------------------
然后直接关闭Standby所在的主机,模拟Standby故障,通过gpstate命令可以查看到Standby已经连接失败。
[gpadmin@mdw ~]$ gpstate -f 20160227:17:01:14:019099 gpstate:mdw:gpadmin-[INFO]:-Starting gpstate with args: -f 20160227:17:01:14:019099 gpstate:mdw:gpadmin-[INFO]:-local Greenplum Version: 'postgres (Greenplum Database) 4.3.6.2 build 1' 20160227:17:01:14:019099 gpstate:mdw:gpadmin-[INFO]:-master Greenplum Version: 'PostgreSQL 8.2.15 (Greenplum Database 4.3.6.2 build 1) on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.4.2 compiled on Nov 12 2015 23:50:28' 20160227:17:01:14:019099 gpstate:mdw:gpadmin-[INFO]:-Obtaining Segment details from master... 20160227:17:01:32:019099 gpstate:mdw:gpadmin-[INFO]:-Standby master details 20160227:17:01:32:019099 gpstate:mdw:gpadmin-[INFO]:----------------------- 20160227:17:01:32:019099 gpstate:mdw:gpadmin-[INFO]:- Standby address = mdw-std 20160227:17:01:32:019099 gpstate:mdw:gpadmin-[INFO]:- Standby data directory = /gpdb/gpdata/master/gpseg-1 20160227:17:01:32:019099 gpstate:mdw:gpadmin-[INFO]:- Standby port = 5432 20160227:17:01:32:019099 gpstate:mdw:gpadmin-[WARNING]:-Standby PID = Error getting status from host mdw-std <<<<<<<< 20160227:17:01:32:019099 gpstate:mdw:gpadmin-[WARNING]:-Standby status = Status could not be determined <<<<<<<< 20160227:17:01:32:019099 gpstate:mdw:gpadmin-[INFO]:----------------------------------------- 20160227:17:01:32:019099 gpstate:mdw:gpadmin-[INFO]:--pg_stat_replication 20160227:17:01:32:019099 gpstate:mdw:gpadmin-[INFO]:----------------------------------------- 20160227:17:01:32:019099 gpstate:mdw:gpadmin-[INFO]:--WAL Sender State: streaming 20160227:17:01:32:019099 gpstate:mdw:gpadmin-[INFO]:--Sync state: sync 20160227:17:01:32:019099 gpstate:mdw:gpadmin-[INFO]:--Sent Location: 0/C203088 20160227:17:01:32:019099 gpstate:mdw:gpadmin-[INFO]:--Flush Location: 0/C203088 20160227:17:01:32:019099 gpstate:mdw:gpadmin-[INFO]:--Replay Location: 0/C203088 20160227:17:01:32:019099 gpstate:mdw:gpadmin-[INFO]:-----------------------------------------
同路Master节点,创建一张表,模拟Master节点数据变化。
dbdream=> \c You are now connected to database "dbdream" as user "dbdream". dbdream=> \d List of relations Schema | Name | Type | Owner | Storage --------+----------------------+-------+---------+---------- public | med_ord_pgm_d | table | dbdream | heap public | med_ord_pgm_d_err | table | gpadmin | heap public | med_ord_pgm_d_ext | table | gpadmin | external public | med_ord_pgm_d_unload | table | gpadmin | external public | ord_pay | table | dbdream | heap public | ord_pay_errs | table | gpadmin | heap (6 rows) dbdream=> create table ord_pay_s(like ord_pay); NOTICE: Table doesn't have 'distributed by' clause, defaulting to distribution columns from LIKE table CREATE TABLE dbdream=> \d List of relations Schema | Name | Type | Owner | Storage --------+----------------------+-------+---------+---------- public | med_ord_pgm_d | table | dbdream | heap public | med_ord_pgm_d_err | table | gpadmin | heap public | med_ord_pgm_d_ext | table | gpadmin | external public | med_ord_pgm_d_unload | table | gpadmin | external public | ord_pay | table | dbdream | heap public | ord_pay_errs | table | gpadmin | heap public | ord_pay_s | table | dbdream | heap (7 rows)
启动Standby的主机,启动Standby,发现Standby已经自动同步了数据。
[gpadmin@mdw ~]$ gpstate -f 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:-Starting gpstate with args: -f 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:-local Greenplum Version: 'postgres (Greenplum Database) 4.3.6.2 build 1' 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:-master Greenplum Version: 'PostgreSQL 8.2.15 (Greenplum Database 4.3.6.2 build 1) on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.4.2 compiled on Nov 12 2015 23:50:28' 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:-Obtaining Segment details from master... 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:-Standby master details 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:----------------------- 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:- Standby address = mdw-std 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:- Standby data directory = /gpdb/gpdata/master/gpseg-1 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:- Standby port = 5432 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:- Standby PID = 3621 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:- Standby status = Standby host passive 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:-------------------------------------- 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:--pg_stat_replication 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:-------------------------------------- 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:--WAL Sender State: streaming 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:--Sync state: sync 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:--Sent Location: 0/C269D88 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:--Flush Location: 0/C269D88 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:--Replay Location: 0/C269D88 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:--------------------------------------
如果Standby数据不能自动同步,那么就需要重新初始化Standby的数据,也就是手动全量同步数据,因为Master只存数据库的元数据,通常都很小,这个过程很快即可完成,但是需要将Master数据库启动到Utility模式,否则会遇到下面的错误。
[gpadmin@mdw ~]$ gpinitstandby -n 20160227:17:17:52:002836 gpinitstandby:mdw:gpadmin-[INFO]:-Standy master is already up and running.
使用gpstart –m只将Master节点启动到Utility模式。
[gpadmin@mdw ~]$ gpstop –a [gpadmin@mdw ~]$ gpstart -m 20160227:17:24:21:003056 gpstart:mdw:gpadmin-[INFO]:-Starting gpstart with args: -m 20160227:17:24:21:003056 gpstart:mdw:gpadmin-[INFO]:-Gathering information and validating the environment... 20160227:17:24:21:003056 gpstart:mdw:gpadmin-[INFO]:-Greenplum Binary Version: 'postgres (Greenplum Database) 4.3.6.2 build 1' 20160227:17:24:21:003056 gpstart:mdw:gpadmin-[INFO]:-Greenplum Catalog Version: '201310150' 20160227:17:24:21:003056 gpstart:mdw:gpadmin-[WARNING]:-**************************************** 20160227:17:24:21:003056 gpstart:mdw:gpadmin-[WARNING]:-Master-only start requested in a configuration with a standby master. 20160227:17:24:21:003056 gpstart:mdw:gpadmin-[WARNING]:-This is advisable only under the direct supervision of Greenplum support. 20160227:17:24:21:003056 gpstart:mdw:gpadmin-[WARNING]:-This mode of operation is not supported in a production environment and 20160227:17:24:21:003056 gpstart:mdw:gpadmin-[WARNING]:-may lead to a split-brain condition and possible unrecoverable data loss. 20160227:17:24:21:003056 gpstart:mdw:gpadmin-[WARNING]:-************************************* Continue with master-only startup Yy|Nn (default=N):
此处需要输入Y确认。
> y 20160227:17:24:23:003056 gpstart:mdw:gpadmin-[INFO]:-Starting Master instance in admin mode 20160227:17:24:24:003056 gpstart:mdw:gpadmin-[INFO]:-Obtaining Greenplum Master catalog information 20160227:17:24:24:003056 gpstart:mdw:gpadmin-[INFO]:-Obtaining Segment details from master... 20160227:17:24:24:003056 gpstart:mdw:gpadmin-[INFO]:-Setting new master era 20160227:17:24:24:003056 gpstart:mdw:gpadmin-[INFO]:-Master Started...
将Master启动到Utility模式后,使用gpinitstandby来重新初始化Standby的数据。
[gpadmin@mdw ~]$ gpinitstandby -n 20160227:17:24:49:003161 gpinitstandby:mdw:gpadmin-[INFO]:-Starting standby master 20160227:17:24:49:003161 gpinitstandby:mdw:gpadmin-[INFO]:-Checking if standby master is running on host: mdw-std in directory: /gpdb/gpdata/master/gpseg-1 20160227:17:24:50:003161 gpinitstandby:mdw:gpadmin-[INFO]:-Successfully started standby master
重启数据库,Standby的状态就会正常。
[gpadmin@mdw ~]$ gpstop –a [gpadmin@mdw ~]$ gpstart –a [gpadmin@mdw ~]$ gpstate -f 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:-Starting gpstate with args: -f 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:-local Greenplum Version: 'postgres (Greenplum Database) 4.3.6.2 build 1' 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:-master Greenplum Version: 'PostgreSQL 8.2.15 (Greenplum Database 4.3.6.2 build 1) on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.4.2 compiled on Nov 12 2015 23:50:28' 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:-Obtaining Segment details from master... 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:-Standby master details 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:----------------------- 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:- Standby address = mdw-std 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:- Standby data directory = /gpdb/gpdata/master/gpseg-1 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:- Standby port = 5432 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:- Standby PID = 3621 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:- Standby status = Standby host passive 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:----------------------------------------- 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:--pg_stat_replication 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:----------------------------------------- 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:--WAL Sender State: streaming 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:--Sync state: sync 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:--Sent Location: 0/C269D88 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:--Flush Location: 0/C269D88 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:--Replay Location: 0/C269D88 20160227:17:28:23:003476 gpstate:mdw:gpadmin-[INFO]:-----------------------------------------
下面模拟Master节点故障,切换到Standby的情况。直接关闭Master节点主机的电源,模拟Master节点故障。可以通过gpactivatestandby命令将Standby切换成Master,这个命令依赖几个环境变量,下面的测试会一一介绍。
MASTER_DATA_DIRECTORY环境变量,在切换Standby时,必须设置MASTER_DATA_DIRECTORY环境变量,直接在命令后面写上直接路径也是不行的,如下:
[gpadmin@mdw-std ~]$ gpactivatestandby -d /gpdb/gpdata/master/gpseg-1 20160227:17:33:04:003762 gpactivatestandby:mdw-std:gpadmin-[CRITICAL]:-MASTER_DATA_DIRECTORY environment variable not set. [gpadmin@mdw-std ~]$ gpactivatestandby -d /gpdb/gpdata/master/gpseg-1 20160227:17:33:42:003781 gpactivatestandby:mdw-std:gpadmin-[CRITICAL]:-MASTER_DATA_DIRECTORY environment variable not set.
编辑profile文件,增加MASTER_DATA_DIRECTORY环境变量后使之生效。
[gpadmin@mdw-std ~]$ vi .bash_profile source /gpdb/app/greenplum_path.sh export MASTER_DATA_DIRECTORY=/gpdb/gpdata/master/gpseg-1 export PGDATABASE=dbdream [gpadmin@mdw-std ~]$ . .bash_profile
PGPORT环境变量,同样也需要设置PGPORT环境变量。
[gpadmin@mdw-std ~]$ gpactivatestandby -d $MASTER_DATA_DIRECTORY 20160227:17:34:28:003809 gpactivatestandby:mdw-std:gpadmin-[CRITICAL]:-PGPORT environment variable not set.
编辑profile文件,增加PGPORT环境变量,使之生效。
[gpadmin@mdw-std ~]$ vi .bash_profile source /gpdb/app/greenplum_path.sh export MASTER_DATA_DIRECTORY=/gpdb/gpdata/master/gpseg-1 export PGDATABASE=dbdream export PGPORT=5432 [gpadmin@mdw-std ~]$ . .bash_profile
此时即可切换Standby了。
[gpadmin@mdw-std ~]$ gpactivatestandby -d $MASTER_DATA_DIRECTORY 20160227:17:35:02:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:--------------------- 20160227:17:35:02:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Standby data directory = /gpdb/gpdata/master/gpseg-1 20160227:17:35:02:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Standby port = 5432 20160227:17:35:02:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Standby running = yes 20160227:17:35:02:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Force standby activation = no 20160227:17:35:02:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:--------------------- Do you want to continue with standby master activation? Yy|Nn (default=N):
输入Y确定切换。
> y 20160227:17:35:04:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-found standby postmaster process 20160227:17:35:04:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Updating transaction files filespace flat files... 20160227:17:35:04:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Updating temporary files filespace flat files... 20160227:17:35:04:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Promoting standby... 20160227:17:35:04:003839 gpactivatestandby:mdw-std:gpadmin-[DEBUG]:-Waiting for connection... 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Standby master is promoted 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Reading current configuration... 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[DEBUG]:-Connecting to dbname=dbdream 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Writing the gp_dbid file - /gpdb/gpdata/master/gpseg-1/gp_dbid... 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-But found an already existing file. 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Hence removed that existing file. 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Creating a new file... 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Wrote dbid: 1 to the file. 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Now marking it as read only... 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Verifying the file... 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:------------------- 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-The activation of the standby master has completed successfully. 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-mdw-std is now the new primary master. 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-You will need to update your user access mechanism to reflect 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-the change of master hostname. 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Do not re-start the failed master while the fail-over master is 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-operational, this could result in database corruption! 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-MASTER_DATA_DIRECTORY is now /gpdb/gpdata/master/gpseg-1 if 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-this has changed as a result of the standby master activation, remember 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-to change this in any startup scripts etc, that may be configured 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-to set this value. 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-MASTER_PORT is now 5432, if this has changed, you 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-may need to make additional configuration changes to allow access 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-to the Greenplum instance. 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Refer to the Administrator Guide for instructions on how to re-activate 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-the master to its previous state once it becomes available. 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-Query planner statistics must be updated on all databases 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-following standby master activation. 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-When convenient, run ANALYZE against all user databases. 20160227:17:35:06:003839 gpactivatestandby:mdw-std:gpadmin-[INFO]:-----------------------
切换完成,查看数据库的状态。
[gpadmin@mdw-std ~]$ gpstate -s 20160227:17:36:33:003954 gpstate:mdw-std:gpadmin-[INFO]:-Starting gpstate with args: -s 20160227:17:36:33:003954 gpstate:mdw-std:gpadmin-[INFO]:-local Greenplum Version: 'postgres (Greenplum Database) 4.3.6.2 build 1' 20160227:17:36:33:003954 gpstate:mdw-std:gpadmin-[INFO]:-master Greenplum Version: 'PostgreSQL 8.2.15 (Greenplum Database 4.3.6.2 build 1) on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.4.2 compiled on Nov 12 2015 23:50:28' 20160227:17:36:33:003954 gpstate:mdw-std:gpadmin-[INFO]:-Obtaining Segment details from master... 20160227:17:36:33:003954 gpstate:mdw-std:gpadmin-[INFO]:-Gathering data from segments... . 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:----------------------------------------------------- 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:--Master Configuration & Status 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:----------------------------------------------------- 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Master host = mdw-std 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Master postgres process ID = 3621 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Master data directory = /gpdb/gpdata/master/gpseg-1 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Master port = 5432 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Master current role = dispatch 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Greenplum initsystem version = 4.3.6.2 build 1 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Greenplum current version = PostgreSQL 8.2.15 (Greenplum Database 4.3.6.2 build 1) on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.4.2 compiled on Nov 12 2015 23:50:28 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Postgres version = 8.2.15 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Master standby = No master standby configured 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:--------------------------------- 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:-Segment Instance Status Report 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:--------------------------------- 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Segment Info 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Hostname = sdw1 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Address = sdw1 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Datadir = /gpdb/gpdata/primary/gpseg0 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Port = 40000 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Status 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- PID = 2670 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Configuration reports status as = Up 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Database status = Up 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:--------------------------- 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Segment Info 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Hostname = sdw2 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Address = sdw2 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Datadir = /gpdb/gpdata/primary/gpseg1 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Port = 40000 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Status 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- PID = 2431 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Configuration reports status as = Up 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Database status = Up 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:--------------------------- 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Segment Info 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Hostname = sdw3 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Address = sdw3 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Datadir = /gpdb/gpdata/primary/gpseg2 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Port = 40000 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Status 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- PID = 4427 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Configuration reports status as = Up 20160227:17:36:34:003954 gpstate:mdw-std:gpadmin-[INFO]:- Database status = Up
此时Standby已经切换成Master,原因的Master节点已经被踢出了集群,切换后的数据库显示的是No master standby configured状态,通过gpstate –f参数也可以看到新的集群已经没有了Standby,Standby状态变成了Standby master instance not configured状态。
[gpadmin@mdw-std ~]$ gpstate -f 20160301:16:29:07:022510 gpstate:mdw-std:gpadmin-[INFO]:-Starting gpstate with args: -f 20160301:16:29:07:022510 gpstate:mdw-std:gpadmin-[INFO]:-local Greenplum Version: 'postgres (Greenplum Database) 4.3.6.2 build 1' 20160301:16:29:07:022510 gpstate:mdw-std:gpadmin-[INFO]:-master Greenplum Version: 'PostgreSQL 8.2.15 (Greenplum Database 4.3.6.2 build 1) on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.4.2 compiled on Nov 12 2015 23:50:28' 20160301:16:29:07:022510 gpstate:mdw-std:gpadmin-[INFO]:-Obtaining Segment details from master... 20160301:16:29:07:022510 gpstate:mdw-std:gpadmin-[INFO]:-Standby master instance not configured 20160301:16:29:07:022510 gpstate:mdw-std:gpadmin-[INFO]:------------------------- 20160301:16:29:07:022510 gpstate:mdw-std:gpadmin-[INFO]:--pg_stat_replication 20160301:16:29:07:022510 gpstate:mdw-std:gpadmin-[INFO]:------------------------- 20160301:16:29:07:022510 gpstate:mdw-std:gpadmin-[INFO]:-No entries found. 20160301:16:29:07:022510 gpstate:mdw-std:gpadmin-[INFO]:-------------------------
也就是gpactivatestandby切换相当于ORACLE的failover。登录到数据库,查询gp_segment_configuration基础表也可以看出源Master已经被踢出了集群,Standby已经变成了新的Master。
[gpadmin@mdw-std ~]$ psql psql (8.2.15) Type "help" for help. dbdream=# select * from gp_segment_configuration; dbid | content | role | preferred_role | mode | status | port | hostname | address | replication_port | san_mounts ------+---------+------+----------------+------+--------+-------+----------+---------+------------------+-------- 2 | 0 | p | p | s | u | 40000 | sdw1 | sdw1 | | 3 | 1 | p | p | s | u | 40000 | sdw2 | sdw2 | | 4 | 2 | p | p | s | u | 40000 | sdw3 | sdw3 | | 1 | -1 | p | p | s | u | 5432 | mdw-std | mdw-std | | (4 rows)
下面查看下,在切换之前建的那张表是否可以查询到。
dbdream=# \d List of relations Schema | Name | Type | Owner | Storage --------+----------------------+-------+---------+---------- public | med_ord_pgm_d | table | dbdream | heap public | med_ord_pgm_d_err | table | gpadmin | heap public | med_ord_pgm_d_ext | table | gpadmin | external public | med_ord_pgm_d_unload | table | gpadmin | external public | ord_pay | table | dbdream | heap public | ord_pay_errs | table | gpadmin | heap public | ord_pay_s | table | dbdream | heap (7 rows)
在切换前创建的ord_pay_s表可以查到,切换后,如果要访问数据库,需要连接Standby主机的IP地址,源Master已经被踢出了集群环境。
如果还要使用源Master节点的主机做Master,需要在源Master主机上搭建新Master的Standby,然后再切换回去即可。