当前位置: 首页 > 工具软件 > Slony-I > 使用案例 >

Slony-I同步复制部署

袁鸿雪
2023-12-01


一、Slony-I环境搭建

1.Slony-I下载地址://www.slony.info/downloads/
2.编译安装:
./configure --prefix=/usr/local/slony  --with-pgconfigdir=/usr/local/slony/bin  --with-perltools  --with-pgpkglibdir=/usr/local/slony/pgpkg
make
make install

**ps:路径可自己指定,不需要和我一样**

二、Slony-I配置使用

1.slon_tools.conf配置

配置如下:

if ($ENV{"SLONYNODES"}) {    
require $ENV{"SLONYNODES"};
}else {
	$CLUSTER_NAME = 'cluster01';
	$PIDFILE_DIR = '/var/run/slony1';	#设置的目录保证必须存在,且有访问权限
	$LOGDIR = '/var/log/slony1';		
	$MASTERNODE = 1;
	$DEBUGLEVEL = 2;
	$SLEEP_TIME = 60;
	$WATCHDOG_VERBOSE = 0;
	#因为我只需要一个主节点和一个辅节点,因此把多余的注释掉    
	add_node(node     => 1,             
		host     => '10.9.40.200',             
		dbname   => 'UniMonDB',             
		port     => 5432,             
		user     => 'postgres',             
		password => 'leagsoft');
	add_node(node     => 2,             
		host     => '10.9.40.200',             
		dbname   => 'postgres',             
		port     => 5432,             
		user     => 'postgres',             
		password => 'leagsoft');
}
$SLONY_SETS = {
	"set1_name" => {
		"set_id" => 1,
		"table_id" => 1,
		"sequence_id" => 1,
		#设置配置复制集的配置项,主要配置要同步哪些表和序列,
		#第一部分是要复制哪些有主键的表,把需要同步表的加入其中
		"pkeyedtables" => ['test_table1',                       
				'test_table2',],
		#复制集的第二部分是没有主键,而又唯一键的表,
		#由于不使用这部分所以注释掉就可以了
		# "keyedtables" => {
		#           'table3' => 'index_on_table3',
		#           'table4' => 'index_on_table4',},
		#最后是配置同步哪些序列,由于不使用,注释掉
		#       "sequences" => ['sequence1',
		#                       'sequence2',],},
	#复制集2也不使用,同样注释掉
	#"set2" => {
	#       "set_id"       => 2,
	#       "table_id"     => 6,
	#       "sequence_id"  => 3,
	#       "pkeyedtables" => ["table6"],
	#       "keyedtables"  => {},
	#       "sequences"    => [], },
};
if ($ENV{"SLONYSET"}) {
    require $ENV{"SLONYSET"};
}
1;

提示1:根据自己需要复制的表的类型进行配置,我这里配置的是同步有主键的表
提示2:配置文件中添加的同步表在数据库中要确实存在
提示3:两个数据库中同步对应的表结构要保证一致

2.Slony-I同步启动

同步复制示例如下:

2.1.初始化Slony-I集群

[root@UniNID-6A8ADD bin]# ./slonik_init_cluster  
# INIT CLUSTER
cluster name = cluster01; 
node 1 admin conninfo='host=10.9.40.200 dbname=UniMonDB user=wwhlzzf port=5432 password=zcrlylw$2IO7'; 
node 2 admin conninfo='host=10.9.40.200 dbname=postgres user=postgres port=5432 password=leagsoft';  
init cluster (id = 1, comment = 'Node 1 - UniMonDB@10.9.40.200'); 
# STORE NODE  
store node (id = 2, event node = 1, comment = 'Node 2 - postgres@10.9.40.200');  
echo 'Set up replication nodes';
# STORE PATH  
echo 'Next: configure paths for each node/origin';  
store path (server = 1, client = 2, conninfo = 'host=10.9.40.200 dbname=UniMonDB user=wwhlzzf port=5432 password=zcrlylw$2IO7');  
store path (server = 2, client = 1, conninfo = 'host=10.9.40.200 dbname=postgres user=postgres port=5432 password=leagsoft');  
echo 'Replication nodes prepared';  
echo 'Please start a slon replication daemon for each node';

[root@UniNID-6A8ADD bin]# ./slonik_init_cluster | slonik
<stdin>:6: Possible unsupported PostgreSQL version (110200) 11.2, defaulting to 8.4 support
<stdin>:9: Possible unsupported PostgreSQL version (110200) 11.2, defaulting to 8.4 support
<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

2.2.启动所有节点的slon进程

[root@UniNID-6A8ADD bin]# ./slon_start 1
Invoke slon for node 1 - /usr/local/slony/bin/slon -p /var/run/slony1/cluster01_node1.pid -s 1000 -d2  cluster01 'host=10.9.40.200 dbname=UniMonDB user=postgres port=5432 password=leagsoft' > /var/log/slony1/node1/UniMonDB-2020-11-17.log 2>&1 &
Slon successfully started for cluster cluster01, node node1
PID [1123]
Start the watchdog process as well...

[root@UniNID-6A8ADD bin]# ./slon_start 2
Invoke slon for node 2 - /usr/local/slony/bin/slon -p /var/run/slony1/cluster01_node2.pid -s 1000 -d2  cluster01 'host=10.9.40.200 dbname=postgres user=postgres port=5432 password=leagsoft' > /var/log/slony1/node2/postgres-2020-11-17.log 2>&1 &
Slon successfully started for cluster cluster01, node node2
PID [3395]
Start the watchdog process as well...

2.3.在主节点创建复制集

[root@UniNID-6A8ADD bin]# ./slonik_create_set 1
cluster name = cluster01;
 node 1 admin conninfo='host=10.9.40.200 dbname=UniMonDB user=postgres port=5432 password=leagsoft';
 node 2 admin conninfo='host=10.9.40.200 dbname=postgres user=postgres port=5432 password=leagsoft';
# TABLE ADD KEY
# CREATE SET
    create set (id = 1, origin = 1, comment = 'Set 1 (set1_name) for cluster01');
# SET ADD TABLE
  echo 'Subscription set 1 (set1_name) created';
  echo 'Adding tables to the subscription set';
  set add table (set id = 1, origin = 1, id = 1,
                 full qualified name = 'public.test_table1',
                 comment = 'Table public.test_table1 with primary key');
  echo 'Add primary keyed table public.test_table1';
  set add table (set id = 1, origin = 1, id = 2,
                 full qualified name = 'public.test_table2',
                 comment = 'Table public.test_table2 with primary key');
  echo 'Add primary keyed table public.test_table2';
# SET ADD SEQUENCE
  echo 'Adding sequences to the subscription set';
  echo 'All tables added';

[root@UniNID-6A8ADD bin]# ./slonik_create_set 1 | slonik
<stdin>:11: Subscription set 1 (set1_name) created
<stdin>:12: Adding tables to the subscription set
<stdin>:16: Add primary keyed table public.test_table1
<stdin>:20: Add primary keyed table public.test_table2
<stdin>:23: Adding sequences to the subscription set
<stdin>:24: All tables added

2.4.创建复制订阅者

[root@UniNID-6A8ADD bin]# ./slonik_subscribe_set 1 2
cluster name = cluster01;
 node 1 admin conninfo='host=10.9.40.200 dbname=UniMonDB user=postgres port=5432 password=leagsoft';
 node 2 admin conninfo='host=10.9.40.200 dbname=postgres user=postgres port=5432 password=leagsoft';
    subscribe set (id = 1, provider = 1, receiver = 2, forward = yes);
  echo 'Subscribed nodes to set 1';

[root@UniNID-6A8ADD bin]# ./slonik_subscribe_set 1 2 | slonik
<stdin>:6: Subscribed nodes to set 1

同步部署到这就成功了,接下来可以在主节点数据库的表中增删数据,并查看辅节点中数据库中对应的表
Ps:同一个集群的初始化,创建只有第一次才需要在命令后面加上 | slonik,之后不需要再加了


其他命令:
./slon_status 1 查看节点1的进程状态
./slon_kill 结束slon进程

Slony-I复制的限制:
1.表要有主键,可以自动识别
2.没有主键要有唯一键
3.没有主键,要手动指定唯一键,没有唯一键,则不能复制表数据。

 类似资料: