Slony-I是一個「一主多備」的複製系統,支持級聯複製(ascading)和失效轉移failover.html
slony-I的系統分析:https://wiki.postgresql.org/wiki/Slonynode
slony-I安裝配置使用linux
系統要求:http://slony.info/documentation/requirements.html
sql
使用限制:http://slony.info/documentation/2.2/limitations.htmlshell
配置與安裝:http://slony.info/documentation/administration.html#INSTALLATION數據庫
使用配置:http://slony.info/documentation/tutorial.html#FIRSTDBcentos
主庫:centos linux 32bit虛擬機,ip爲192.168.100.240服務器
PostgreSQL9.2.13ide
Slony-I 2.2函數
備庫: centos linux 32bit虛擬機, ip爲192.168.100.241
PostgreSQL9.2.13
Slony-I 2.2
在主庫和備庫都,進行源碼編譯、安裝、配置PostgreSQL數據庫以下:
源碼編譯安裝PostgbreSQL9.2。
安裝目錄:/opt/pgsql2
編譯安裝過程請參考:PostgreSQL在Linux下的源碼編譯安裝
源碼安裝完畢後,要配置pg_hba.conf和postgresql.conf確保主、備庫能夠遠程訪問。
注意:PG版本要必須是Slony支持的版本,詳見 http://slony.info/documentation/requirements.html 。
在主庫和備庫都,執行:
su - postgres export CLUSTERNAME=lyy_cluster1 export MASTERDBNAME=masterdb export SLAVEDBNAME=slavedb export MASTERHOST=192.168.13.128 export SLAVEHOST=192.168.100.236 export REPLICATIONUSER=postgres
注意:
1.REPLICATIONUSER一般是PostgreSQL數據庫的超級用戶。
2.修改MASTERHOST和SLAVEHOST時,儘可能不要使用localhost,由於可能會致使錯誤:ERROR remoteListenThread_1: db_getLocalNodeId() returned 2 - wrong database?。
在主庫和備庫都,根據環境變量來建立相應對象:
--在主服務器中 cd /opt/pgsql2/bin ./createuser -p 5432 -U postgres -SRD -P $PGBENCHUSER --若已建立則無需再建立 input password for new user :(此處輸入新用戶的密碼便可) input password again:(此處輸入新用戶的密碼便可) password:(此處輸入超級用戶postgres的密碼便可) ./createdb -p 5432 -U postgres -O $PGBENCHUSER -h $MASTERHOST $MASTERDBNAME password:(此處輸入超級用戶postgres的密碼便可)
--在備用服務器中 cd /opt/pgsql2/bin ./createuser -p 5432 -U postgres -SRD -P $PGBENCHUSER --若已建立則無需再建立 input password for new user :(此處輸入新用戶的密碼便可) input password again:(此處輸入新用戶的密碼便可) password:(此處輸入超級用戶postgres的密碼便可) ./createdb -p 5432 -U postgres -O $PGBENCHUSER -h $SLAVEHOST $SLAVEDBNAME password:(此處輸入超級用戶postgres的密碼便可)
--在主服務器,建立要同步的數據表(數據表必須有主鍵或者惟一鍵,才能經過slony-i實現數據同步) [postgres@localhost bin]$ ./psql -U $PGBENCHUSER -h $MASTERHOST -d $MASTERDBNAME psql (9.2.13) Type "help" for help.. masterdb=# CREATE TABLE lyy(id int primary key, name varchar); CREATE TABLE
Slony-I 須要數據庫有 pl/pgSQL 過程語言,若是模板數據 template1已經安裝了pl/pgSQL,那麼新建的$MASTERDBNAME也就也有了pl/pgSQL,若是已經存在了,則不須要執行如下語句:
在主庫和備庫都執行:
--在bin目錄下 ./createlang -h $MASTERHOST plpgsql $MASTERDBNAME
當備庫Slony-I 訂閱主庫以後,Slony-I不能自動從主庫拷貝表定義到備庫,因此咱們須要把表定義從主庫導入備庫,咱們經過 pg_dump來實現表定義的主、備同步:
--在主服務器中執行pg_dump,將表定義備份到備庫中 --在bin目錄下 [postgres@localhost bin]$ ./pg_dump -s -U postgres -h 192.168.100.240 masterdb | ./psql -U postgres -h 192.168.100.241 slavedb
在主庫和備庫都,按照如下步驟安裝slony-i。
slony下載地址:http://slony.info/downloads/2.2/source/
下載並解壓slony-2.2,而後進行編譯配置安裝,要確保每一步安裝正確後再進行下一步。
cd ../slony-2.2 ./configure --with-pgconfigdir=/opt/pgsql944/bin --with-perltools gmake all gamke install
注意:若是slony-I後面的配置過程使用altperl腳本,則configure時必須指定--with-perltools。
Configuring the Database For Replication.
Creating the configuration tables, stored procedures, triggers and configuration is all done through the slonik tool. It is a specialized scripting aid that mostly calls stored procedures in the master/slave (node) databases.
The example that follows uses slonik directly (or embedded directly into scripts). This is not necessarily the most pleasant way to get started; there exist tools for building slonik scripts under the tools directory, including:
Section 6.1.1 - a set of Perl scripts that build slonik scripts based on a single slon_tools.conf file.
Section 6.1.2 - a shell script (e.g. - works with Bash) which, based either on self-contained configuration or on shell environment variables, generates a set of slonik scripts to configure a whole cluster.
如下兩種方法省略詳細介紹,詳見文檔http://slony.info/documentation/tutorial.html#FIRSTDB。
方法一:Using slonik Command Directly
方法二:Using the altperl Scripts
擴展至:http://file:///C:/Users/Yuanyuan/Desktop/slony1-2.2.4/doc/adminguide/additionalutils.html#ALTPERL
在主庫配置slony cluster
在主庫,建立並執行slony_setup.sh文件,實現slony cluster的配置:
[postgres@localhost data]$ vi slony_setup.sh
CLUSTERNAME=lyy_cluster1 MASTERDBNAME=masterdb SLAVEDBNAME=slavedb MASTERHOST=192.168.100.240 SLAVEHOST=192.168.100.241 REPLICATIONUSER=postgres /opt/pgsql92/bin/slonik <<_EOF_ #-- # define the namespace the replication system # uses in our example it is slony_example #-- cluster name = $CLUSTERNAME; #-- # admin conninfo's are used by slonik to connect to # the nodes one for eachnode on each side of the cluster, # the syntax is that of PQconnectdb in # the C-API # -- node 1 admin conninfo = 'dbname=$MASTERDBNAME \ host=$MASTERHOST user=$REPLICATIONUSER'; node 2 admin conninfo = 'dbname=$SLAVEDBNAME \ host=$SLAVEHOST user=$REPLICATIONUSER'; #-- # init the first node. Its id MUST be 1. This creates # the schema _$CLUSTERNAME containing all replication # system specific database objects. #-- init cluster ( id=1, comment = 'Master Node'); #-- # Slony-I organizes tables into sets. The smallest unit # a node can subscribe is a set. The master or origin of # the set is node 1. #-- create set (id=1, origin=1, comment='All masterdb tables'); set add table (set id=1, origin=1, id=1, fully qualified name = 'public.lyy', comment='lyy table'); # set add sequence (set id=1, origin = 1, id = 1, # fully qualified name = 'public.t1_id_seq', # comment = 't1 id sequence'); #-- # Create the second node (the slave) tell the 2 nodes how # to connect to each other and how they should listen for events. #-- store node (id=2, comment = 'Slave Node', event node=1); store path (server = 1, client = 2, conninfo='dbname=$MASTERDBNAME \ host=$MASTERHOST user=$REPLICATIONUSER'); store path (server = 2, client = 1, conninfo='dbname=$SLAVEDBNAME \ host=$SLAVEHOST user=$REPLICATIONUSER'); _EOF_
[postgres@localhost data]$sh slony_setup.sh
註釋:本步執行完畢後,初始化完畢一個名爲lyy_cluster1的slony集羣。
並相應的產生一個名爲_lyy_cluster1的模式,裏面含有slony運行所需的配置表、序列、函數、觸發器等(主要是經過slony-i安裝過程當中在/opt/pgsql92/share下生成的slony1_base.2.2.4.sql和slony1_funcs.2.2.4.sql)。
還會在同步表lyy下建立相應的觸發器。
在主庫:
masterdb=# \d lyy Table "public.lyy" Column | Type | Modifiers --------+-------------------+----------- id | integer | not null name | character varying | Indexes: "lyy_pkey" PRIMARY KEY, btree (id) Triggers: _lyy_cluster1_logtrigger AFTER INSERT OR DELETE OR UPDATE ON lyy FOR EACH ROW EXECUTE PROCEDURE _lyy_cluster1.logtrigger('_lyy_cluster1', '1', 'k') _lyy_cluster1_truncatetrigger BEFORE TRUNCATE ON lyy FOR EACH STATEMENT EXECUTE PROCEDURE _lyy_cluster1.log_truncate('1') _lyy_cluster_logtrigger AFTER INSERT OR DELETE OR UPDATE ON lyy FOR EACH ROW EXECUTE PROCEDURE _lyy_cluster.logtrigger('_lyy_cluster', '1', 'k') _lyy_cluster_truncatetrigger BEFORE TRUNCATE ON lyy FOR EACH STATEMENT EXECUTE PROCEDURE _lyy_cluster.log_truncate('1') Disabled triggers: _lyy_cluster1_denyaccess BEFORE INSERT OR DELETE OR UPDATE ON lyy FOR EACH ROW EXECUTE PROCEDURE _lyy_cluster1.denyaccess('_lyy_cluster1') _lyy_cluster1_truncatedeny BEFORE TRUNCATE ON lyy FOR EACH STATEMENT EXECUTE PROCEDURE _lyy_cluster1.deny_truncate() _lyy_cluster_denyaccess BEFORE INSERT OR DELETE OR UPDATE ON lyy FOR EACH ROW EXECUTE PROCEDURE _lyy_cluster.denyaccess('_lyy_cluster') _lyy_cluster_truncatedeny BEFORE TRUNCATE ON lyy FOR EACH STATEMENT EXECUTE PROCEDURE _lyy_cluster.deny_truncate()
在備庫:
slavedb=# \d lyy Table "public.lyy" Column | Type | Modifiers --------+-------------------+----------- id | integer | not null name | character varying | Indexes: "lyy_pkey" PRIMARY KEY, btree (id) Triggers: _lyy_cluster1_denyaccess BEFORE INSERT OR DELETE OR UPDATE ON lyy FOR EACH ROW EXECUTE PROCEDURE _lyy_cluster1.denyaccess('_lyy_cluster1') _lyy_cluster1_truncatedeny BEFORE TRUNCATE ON lyy FOR EACH STATEMENT EXECUTE PROCEDURE _lyy_cluster1.deny_truncate() Disabled triggers: _lyy_cluster1_logtrigger AFTER INSERT OR DELETE OR UPDATE ON lyy FOR EACH ROW EXECUTE PROCEDURE _lyy_cluster1.logtrigger('_lyy_cluster1', '1', 'k') _lyy_cluster1_truncatetrigger BEFORE TRUNCATE ON lyy FOR EACH STATEMENT EXECUTE PROCEDURE _lyy_cluster1.log_truncate('1')
在主庫啓動主庫節點及監視器
在主庫,執行如下命令啓動slon daemon:
[postgres@localhost bin]$./slon lyy_cluster1 "dbname=masterdb user=postgres host=192.168.100.240"&
執行當前命令的終端會不斷地返回檢測信息,因此該終端不要關閉也不要再執行其餘操做。
在備庫啓動備庫節點及監視器
在備庫。執行如下命令啓動slon deamon:
[postgres@localhost bin]$./slon slony_example "dbname=slavedb user=postgres host=192.168.100.241" &
執行當前命令的終端會不斷地返回檢測信息,因此該終端不要關閉也不要再執行其餘操做。
在主庫執行備庫訂閱主庫
在主庫,執行訂閱過程, 經過腳本文件subscribe_set.sh來執行訂閱過程:
[postgres@localhost data]$ vi subscribe_set.sh
CLUSTERNAME=lyy_cluster1 MASTERDBNAME=masterdb SLAVEDBNAME=slavedb MASTERHOST=192.168.100.240 SLAVEHOST=192.168.100.241 REPLICATIONUSER=postgres /opt/pgsql92/bin/slonik <<_EOF_ # ---- # This defines which namespace the replication system uses # ---- cluster name = $CLUSTERNAME; # ---- # Admin conninfo's are used by the slonik program to connect # to the node databases. So these are the PQconnectdb arguments # that connect from the administrators workstation (where # slonik is executed). # ---- node 1 admin conninfo = 'dbname=$MASTERDBNAME host=$MASTERHOST \ user=$REPLICATIONUSER'; node 2 admin conninfo = 'dbname=$SLAVEDBNAME host=$SLAVEHOST \ user=$REPLICATIONUSER'; # ---- # Node 2 subscribes set 1 # ---- subscribe set ( id = 1, provider = 1, receiver = 2, forward = no); _EOF_
[postgres@localhost data]$sh subscribe_set.sh
目前,本次數據庫的slony的同步複製已經配置完畢。
驗證slony-I同步複製生效
在主庫,向表lyy插入數據:
[postgres@localhost bin]$ ./psql -U postgres -d masterdb psql (9.2.13) Type "help" for help. masterdb=# insert into lyy values(1,'lyy'); INSERT 0 1
在備庫,查詢表lyy中的數據狀況:
postgres@localhost bin]$ ./psql -U postgres -d slavedb psql (9.2.13) Type "help" for help. slavedb=# select * from lyy; id | name ----+------ (0 rows) slavedb=# select * from lyy; id | name ----+------ 1 | lyy (1 row)
能夠在主庫執行增刪操做,而後在備庫執行查詢操做,進行比對。
注意:slony-I 同步複製的備庫是不能對同步的表數據進行修改的:
slavedb=# insert into test values(4,'fff'); ERROR: Slony-I: Table test is replicated and cannot be modified on a subscriber node - role=0
配置slony_tool.conf並初始化slony集羣
默認狀況下,slony的配置文件樣本已默認安裝至/usr/local/etc/slon_tools.conf-sample。
cd /usr/local/etc --複製一個slon_tools.conf-sample,命名爲slon_tools.conf [root@localhost etc]#cp slon_tools.conf-sample /usr/local/etc/slon_tools.conf --開始編輯slon_tools.conf [root@localhost etc]#vi slon_tools.conf
配置詳情:
#修改CLUSTER_NAME爲前面咱們設置的 # The name of the replication cluster. This will be used to # create a schema named _$CLUSTER_NAME in the database which will # contain Slony-related data. $CLUSTER_NAME = 'lyy_cluster1'; #slony的pid文件目錄。若是沒有,則根據提示建立,並授予要求的權限。 # The directory where Slony store PID files. This # directory will need to be writable by the user that invokes # Slony. $PIDFILE_DIR = '/var/run/slony1'; #日誌文件存儲目錄,若是沒有,則修改配置或者根據配置建立目錄。 # The directory where Slony should record log messages. This # directory will need to be writable by the user that invokes # Slony. $LOGDIR = '/var/log/slony1'; #修改節點信息爲咱們的主、備節點的信息,樣本中提供的多出的節點能夠註釋或者刪除掉。 # Include add_node lines for each node in the cluster. Be sure to # use host names that will resolve properly on all nodes # (i.e. only use 'localhost' if all nodes are on the same host). # Also, note that the user must be a superuser account. add_node(node => 1, host => '162.168.100.240', dbname => 'masterdb', port => 5432, user => 'postgres', password => 'postgres'); add_node(node => 2, host => '192.168.100.241', dbname => 'slavedb', port => 5432, user => 'postgres', password => 'postgres'); #修改要同步的表或者序列 # This array contains a list of tables that already have primary keys. "pkeyedtables" => [ 'public.lyy', ], #若是沒有序列和無主鍵表須要同步,則將如下示例註釋掉。 # For tables that have unique not null keys, but no primary # key, enter their names and indexes here. #lyy comment this # "keyedtables" => { # 'table3' => 'index_on_table3', # 'table4' => 'index_on_table4', # }, # Sequences that need to be replicated should be entered here. # lyy comment this # "sequences" => ['sequence1', # 'sequence2', # ], },
#在主庫,初始化slony cluster [root@localhost etc]# slonik_init_cluster | /opt/pgsql92/bin/slonik <stdin>:10: Set up replication nodes <stdin>:13: Next: configure paths for each node/origin <stdin>:16: Replication nodes prepared <stdin>:17: Please start a slon replication daemon for each node
註釋:一樣的,本步執行完畢後,初始化完畢一個名爲lyy_cluster1的slony集羣。
並相應的產生一個名爲_lyy_cluster1的模式,裏面含有slony運行所需的配置表、序列、函數、觸發器等(主要是經過slony-i安裝過程當中在/opt/pgsql92/share下生成的slony1_base.2.2.4.sql和slony1_funcs.2.2.4.sql)。
啓動slon並進行數據集合訂閱:
# 在主庫,啓動節點1的slon [root@localhost etc]# slon_start 1 Invoke slon for node 1 - /opt/pgsql92/bin//slon -p /var/run/slony1/lyy_cluster2_node1.pid -s 1000 -d2 lyy_cluster1 'host=192.168.100.240 dbname=master user=postgres port=5432 password=postgres' > /var/log/slony1/node1/master-2015-09-15.log 2>&1 & Slon successfully started for cluster lyy_cluster1, node node1 PID [7839] Start the watchdog process as well...
# 在備庫,啓動節點2的slon [root@localhost etc]# slon_start 2 Invoke slon for node 2 - /opt/pgsql92/bin//slon -p /var/run/slony1/cluster1_node2.pid -s 1000 -d2 cluster1 'host=192.168.100.241 dbname=slavedb user=postgres port=5432 password=postgres' > /var/log/slony1/node2/slavedb-2015-09-15.log 2>&1 & Slon successfully started for cluster lyy_cluster1, node node2 PID [7613] Start the watchdog process as well...
# 在主庫,建立數據集合 (此處 1 是一個 set 集合號) [root@localhost etc]# slonik_create_set 1 | /opt/pgsql92/bin/slonik <stdin>:11: Subscription set 1 (set1_name) created <stdin>:12: Adding tables to the subscription set <stdin>:16: Add primary keyed table public.lyy <stdin>:19: Adding sequences to the subscription set <stdin>:20: All tables added
# 在主庫,訂閱 集合1 到 節點2 (1= set ID, 2= node ID) [root@localhost etc]# slonik_subscribe_set 1 2 | /opt/pgsql92/bin/slonik <stdin>:6: Subscribed nodes to set 1
目前,本次數據庫的slony的同步複製已經配置完畢。
驗證slony-I同步複製生效
在主庫,向表lyy插入數據:
[postgres@localhost bin]$ ./psql -U postgres -d masterdb psql (9.2.13) Type "help" for help. masterdb=# insert into lyy values(1,'lyy'); INSERT 0 1
在備庫,查詢表lyy中的數據狀況:
postgres@localhost bin]$ ./psql -U postgres -d slavedb psql (9.2.13) Type "help" for help. slavedb=# select * from lyy; id | name ----+------ (0 rows) slavedb=# select * from lyy; id | name ----+------ 1 | lyy (1 row)
能夠在主庫執行增刪操做,而後在備庫執行查詢操做,進行比對。
5.Slony-I的其餘操做
slony的switchover操做(也就是把主節點改爲從節點,從節點升級爲主節點):
slonik_move_set set1 node1 node2 | /opt/pgsql92/bin/slonik
slony的failver操做:
slonik_failover node1 node2 | /opt/pgsql92/bin/slonik
轉至:http://my.oschina.net/liuyuanyuangogo/blog/507936
Slony-I只能同步如下內容:
1.表數據(不能同步DDL,表必須含有主鍵或者惟一鍵)
2.序列
Slony-I不能自動同步如下內容:
1.對大對象(BLOBS)的變動
2.對DDL(數據定義語句)的變動
3.對用戶和角色的變動
這些使用限制的主要緣由是:slony-I是經過觸發器收集變動狀況的,而觸發器不可以捕獲定義和大對象的修改。對於DDL的變動,slony-I提供了SLONIK EXECUTE SCRIPT命令來執行DDL的SQL腳本,但slony-I不會自動執行,你得手動組織變動的DDL sql語句並用SLONIK EXECUTE SCRIPT(固然這也能夠經過直接到備庫執行sql語句來實現定義修改)。
另外,若是表定義中with oid,那麼原庫中的表記錄oid的取值不能同步爲相同的值。
建議:若是您沒法接受以上使用限制,那您值得嘗試一下PostgrSQL 8.0以後的PITR(Point in Time Recovery),PITR對於遠程節點是基於WAL日誌的。
http://www.cnblogs.com/gaojian/p/3196244.html
http://blog.chinaunix.net/uid-15145533-id-2775796.html
也能夠只用pgbench來測試slony-i的數據同步。
pgbench使用:http://www.postgresql.org/docs/9.2/static/pgbench.html