免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 20644 | 回复: 8
打印 上一主题 下一主题

安装与slony配置 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-18 11:07 |只看该作者 |倒序浏览
以前在贴子里说要发的,但居然忘了发了。今天翻以前的贴子才看到。发一个完整版的。

功能:安装数据库,脚本实现自动建立、删除、重建slony.



postgresql 数据同步

slony1是基于postgresql的异步通知机制做的复制技术, 其同步速度非常快。  采用这个复制技术来做备份,呵呵,除了配置稍微复杂点,非常好用!   
   
1. 总体需求
1.1. 当前情况
随着软件系统复杂度的提高,分布式部署成为软件部署流行的方式。对于系统的结构,
程序和数据是支撑系统的两大要素。程序的分布式部署已经有很多很好的方案,在这里
我说一下数据的分布式部署。数据的分布式部署其实就是数据库的分布式部署。

1.2. 系统环境
在这里,我以下面的环境详细讲部署的过程。
主数据库服务器 (master)
RHEL3
ip 192.168.30.101
mask 255.255.255.0


从数据库服务器 (slave)
RHEL3
ip 192.168.30.102
mask 255.255.255.0
需要保证两台机器互连互通.

请到指定目的地下载指定的软件包:
db postgresql 8.2.4.tar.gz(http://www.postgresql.org/download/)
slony1 slony1-1.2.6.tar(http://www.postgresql.org/download/)
以上的网址是一个入口地址,请选择合适的正确的源程序包。

1.3. 系统安装
1.3.1 主数据库服务器
1.3.1.1 安装数据库
Groupadd postgres
Useradd postgres –g postgres –d /home/postgres
解压,命令 tar -xvzf postgresql 8.2.4.tar.gz
进入对应的 postgresql-8.2.4 目录 ,命令 :cd postgresql-8.2.4
./configure  --prefix=/usr/local/pgsql –localstatedir=/home/postgres/data
gmake,命令: gmake
注意,是 gmake
安装,命令 gmake install

为 postgres 用户增加环境参数如下:
Vi /home/postgres/.bash_profile

PGLIB=/usr/local/pgsql/lib
PGDATA=/test/spescso/data
PATH=$PATH:/usr/local/pgsql/bin
MANPATH=$MANPATH:/usr/local/pgsql/man
export PGLIB PGDATA PATH MANPATH

chown postgres.postgres /usr/local/pgsql –R

Su – postgres
Mkdir data

修改postgres配置文档
vi /home/postgre/data/postgresql.conf

listen_addresses = '*'     //配置监听地址


使用 postgres 创建 数据库集群
/usr/local/pgsql/bin/initdb  /home/postgres/data/
/usr/local/pgsql/bin/pg_ctl -D /home/postgres/data/  start
/usr/local/pgsql/bin/createuser  ssuser
/usr/local/pgsql/bin/createlang plpgsql template1

使用 postgres 创建 log 目录
mkdir /home/postgres/data/log

修改 /home/postgres/data/postgresql.conf 文件
主要配置日志文件
log_destination = 'stderr'
redirect_stderr = true
log_directory = '/test/spescso/data/log/'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'

修改 /test/spescso/data/pg_hba.conf,主要搞定双机相互认证问题,否则不能相互访问
# TYPE DATABASE USER CIDR-ADDRESS METHOD

# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 192.168.0.0/24 trust
# IPv6 local connections:
host all all ::1/128 trust
如果不能理解上面的意思,请仔细阅读 postgresql 数据库安全认证文档。


使用 postgres 用户后台启动 postmaser 数据库主进程
/usr/local/pgsql/bin/pg_ctl -D /home/postgres/data/ start


1.3.1.2 安装 slony1 数据同步工具(主从都需要安装)
解压,命令 tar -xvjf slony1-1.2.6.tar
注意,使用 -j 参数,该网站压缩包有点不一致。
进入对应的 slony1-1.2.6 目录 ,命令 :cd slony1-1.2.6
./configure  --with-pgsourcetree=<postgresql   源代码目录>
gmake,命令: gmake
注意,是 gmake
安装,命令 gmake install

1.3.2 从数据库服务器
和主数据库服务器的安装方式一致。(其实将主的直接COPY过来就可以的)

1.3.3 建立数据库和数据表
以下以在主数据库服务器上建立主数据库和数据表 test 为例见解,其他数据库和数据表请参考建立.
/usr/local/pgsql/bin/createdb  test
/usr/local/pgsql/bin/psql -f /home/hzh/share/sql.txt  -dtest -Ussuser

sql.txt 文件最好是 UTF-8 格式,特别是存在中文字符时)
例:sql.txt
CREATE TABLE tb_depart(Id int primary key,Name char();

在从数据库服务器上建立与主数据库服务器上相同的数据库test

1.4 配置同步
1.4.1. 主机配置
以下脚本都是在slave上的。
slony_0.sh文件内容如下:
  1. #!/bin/sh
  2. /usr/local/pgsql/bin/slonik << _END_
  3. #
  4. # Define cluster namespace and node connection information
  5. #
  6. #集群名称
  7. cluster name = testdb;
  8. # 定义复制节点
  9. node 1 admin conninfo = 'dbname=test host=192.168.30.101 port=5432 user=ssuser';
  10. node 2 admin conninfo = 'dbname=test host=localhost port=5432 user=ssuser';

  11. DROP SET (id=1, origin=1);
  12. uninstall node (id=1);
  13. uninstall node (id=2);
  14. echo 'Drop testdb set';

  15. _END_
复制代码

slony_1.sh文件内容如下:
  1. #!/bin/sh
  2. /usr/local/pgsql/bin/slonik << _END_
  3. cluster name = testdb;
  4. # 定义复制节点
  5. node 1 admin conninfo = 'dbname=test host=192.168.30.101 port=5432 user=ssuser';
  6. node 2 admin conninfo = 'dbname=test host=localhost port=5432 user=ssuser';
  7. echo 'Cluster defined, nodes identified';

  8. # 初始化集群,id从1开始
  9. init cluster (id=1, comment='Master Node');
  10. # 设置存储节点
  11. store node (id=2, comment='Slave Node');
  12. echo 'Nodes defined';
  13. # 设置存储路径
  14. store path (server=1, client=2, conninfo='dbname=test host=192.168.30.101 port=5432 user=ssuser');
  15. store path (server=2, client=1, conninfo='dbname=test host=localhost port=5432 user=ssuser');
  16. #设置侦听事件和订阅方向,复制中角色,主节点是原始提供者,从节点是接受者
  17. store listen (origin=1, provider = 1, receiver =2);
  18. store listen (origin=2, provider = 2, receiver =1);

  19. _END_
复制代码


slony_2.sh文件内容如下:
  1. #!/bin/sh
  2. /usr/local/pgsql/bin/slonik << _END_
  3. #
  4. # Define cluster namespace and node connection information
  5. #
  6. cluster name = testdb;
  7. node 1 admin conninfo = 'dbname=test host=192.168.30.101 port=5432 user=ssuser';
  8. node 2 admin conninfo = 'dbname=test host=localhost port=5432 user=ssuser';
  9. # 设置参与同步的数据表
  10. #先创建一个复制集,id也是从1开始
  11. #向自己的复制集种添加表,每个需要复制的表一条set命令
  12. #id从1开始,逐次递加,步进为1;
  13. #fully qualified name是表的全称:模式名.表名
  14. #这里的复制集id需要和前面创建的复制集id一致
  15. #假如某个表没有主键,但是有唯一键字,那么可以用key关键字
  16. #指定其为复制键字,如下面的key参数
  17. #set add table ( set id = 1, origin = 1,id = 4, fully qualified name = 'public.history',key = "column",comment = 'Table history' );
  18. #对于没有唯一列的表,需要这样处理,这一句放在 create set 的前面
  19. #table add key (node id = 1, fully qualified name = 'public.history');
  20. # 这样设置结果集
  21. #set add table (set id=1, origin=1, id=4, fully qualified name = 'public.history', #comment='history table', key = serial);

  22. create set (id=1, origin=1, comment='testdb tables');
  23. set add table ( set id=1, origin=1,id=1, fully qualified name='public.tb_depart',comment='Table tb_depart' );
  24. set add table ( set id=1, origin=1,id=2, fully qualified name='public.tb_user',comment='Table tb_user' );
  25. set add table ( set id=1, origin=1,id=3, fully qualified name='public.tb_manager',comment='Table tb_manager' );
  26. set add table ( set id=1, origin=1,id=4, fully qualified name='public.tb_test',comment='Table tb_test' );

  27. echo 'set 1 of testdb tables created';
  28. _END_
复制代码


slony_3.sh文件内容如下:
  1. #/bin/sh
  2. /usr/local/pgsql/bin/slon testdb "dbname=test host=192.168.30.101 port=5432 user=ssuser" > ~/slon_gb_1.out 2>&1 &
  3. /usr/local/pgsql/bin/slon testdb "dbname=test host=localhost port=5432 user=ssuser" > ~/slon_gb_2.out 2>&1 &

  4. /usr/local/pgsql/bin/slonik << _END_
  5. #
  6. # Define cluster namespace and node connection information
  7. #
  8. cluster name = testdb;
  9. #提供连接参数
  10. node 1 admin conninfo = 'dbname=test host=192.168.30.101 port=5432 user=ssuser';
  11. node 2 admin conninfo = 'dbname=test host=localhost port=5432 user=ssuser';
  12. # 提交订阅复制集
  13. subscribe set (id=1, provider=1, receiver=2, forward=no);
  14. echo 'set 1 of gb tables subscribed by node 2';

  15. _END_
复制代码

slony_main.sh文件内容如下:
  1. #!/bin/sh
  2. case $1 in
  3.         start)
  4.                 cd /home/postgre/slony
  5.                 sh slony_3.sh
  6.                 ;;
  7.         stop)
  8.                 killall -KILL slon
  9.                 ;;
  10.         rebuild)
  11.                 cd /home/postgre/slony
  12.                 killall -KILL slon
  13.                 sh slony_0.sh >> /dev/null 2>&1
  14.                 sh slony_1.sh
  15.                 sh slony_2.sh
  16.                 sh slony_3.sh
  17.                 ;;
  18.         *)
  19.                 echo "Please input start or stop or rebuild!!"
  20.                 ;;
  21. esac
复制代码


在从数据库服务器上执行slony_main.sh
可以建立同步,数据库更改后重建同步。
Rebuild错误处理
错误
[postgre@exper2 slony]$ ./slony_main.sh rebuild
<stdin>:4: Cluster defined, nodes identified
<stdin>:6: Error: namespace "_testdb" already exists in database of node 1
<stdin>:6: ERROR: no admin conninfo for node 158941536
<stdin>:8: PGRES_FATAL_ERROR select "_testdb".storeSet(1, 'testdb tables');  - ERROR:  duplicate key violates unique constraint "sl_set-pkey"
CONTEXT:  SQL statement "insert into "_testdb".sl_set (set_id, set_origin, set_comment) values ( $1 ,  $2 ,  $3 )"
PL/pgSQL function "storeset" line 13 at SQL statement
<stdin>:9: set 1 of gb tables subscribed by node 2

处理

#psql test
test=# drop SCHEMA _testdb CASCADE ;
VACUUM ANALYZE

或者再重新执行一次就可以了。。。

[ 本帖最后由 SkyBelieve 于 2008-1-18 11:11 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-07-03 12:29 |只看该作者
  1. slon: no process killed
  2. ERROR:  schema "_TWSDB" does not exist
  3. <stdin>:4: PGRES_FATAL_ERROR select "_TWSDB".dropSet(1);  - ERROR:  schema "_TWSDB" does not exist
  4. <stdin>:4: Cluster defined, nodes identified
  5. NOTICE:  type "_TWSDB.xxid" is not yet defined
  6. DETAIL:  Creating a shell type definition.
  7. NOTICE:  argument type _TWSDB.xxid is only a shell
  8. NOTICE:  type "_TWSDB.xxid_snapshot" is not yet defined
  9. DETAIL:  Creating a shell type definition.
  10. NOTICE:  argument type _TWSDB.xxid_snapshot is only a shell
  11. NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "sl_node-pkey" for table "sl_node"
  12. NOTICE:  CREATE TABLE will create implicit sequence "sl_nodelock_nl_conncnt_seq" for "serial" column "sl_nodelock.nl_conncnt"
  13. NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "sl_nodelock-pkey" for table "sl_nodelock"
  14. NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "sl_set-pkey" for table "sl_set"
  15. NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "sl_setsync-pkey" for table "sl_setsync"
  16. NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "sl_table-pkey" for table "sl_table"
  17. NOTICE:  CREATE TABLE / UNIQUE will create implicit index "sl_table_tab_reloid_key" for table "sl_table"
  18. NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "sl_trigger-pkey" for table "sl_trigger"
  19. NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "sl_sequence-pkey" for table "sl_sequence"
  20. NOTICE:  CREATE TABLE / UNIQUE will create implicit index "sl_sequence_seq_reloid_key" for table "sl_sequence"
  21. NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "sl_path-pkey" for table "sl_path"
  22. NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "sl_listen-pkey" for table "sl_listen"
  23. NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "sl_subscribe-pkey" for table "sl_subscribe"
  24. NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "sl_event-pkey" for table "sl_event"
  25. NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "sl_registry_pkey" for table "sl_registry"
  26. <stdin>:8: Nodes defined
  27. ERROR:  Slony-I: table "public"."providerurl" has no primary key
  28. <stdin>:14: PGRES_FATAL_ERROR select "_TWSDB".determineIdxnameUnique('public.providerurl', NULL);  - ERROR:  Slony-I: table "public"."providerurl" has no primary key
  29. <stdin>:6: set 1 of gb tables subscribed by node 2
复制代码


出现
  1. NOTICE:  type "_TWSDB.xxid" is not yet defined
复制代码
  1. ERROR:  Slony-I: table "public"."providerurl" has no primary key
  2. <stdin>:14: PGRES_FATAL_ERROR select "_TWSDB".determineIdxnameUnique('public.providerurl', NULL);  - ERROR:  Slony-I: table "public"."providerurl" has no primary key
复制代码


请问如何解决呢?

论坛徽章:
0
3 [报告]
发表于 2010-12-20 11:29 |只看该作者
回复 1# SkyBelieve


进入对应的 postgresql-8.2.4 目录 ,命令 :cd postgresql-8.2.4
./configure  --prefix=/usr/local/pgsql –localstatedir=/home/postgres/data
gmake,命令: gmake
注意,是 gmake
安装,命令 gmake install

为 postgres 用户增加环境参数如下:
Vi /home/postgres/.bash_profile

PGLIB=/usr/local/pgsql/lib
PGDATA=/test/spescso/data    --------------》你这里怎么指向了 /test/spescso/data,在configure的时候不所 /home/postgres/data吗?
PATH=$PATH:/usr/local/pgsql/bin
MANPATH=$MANPATH:/usr/local/pgsql/man
export PGLIB PGDATA PATH MANPATH

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
4 [报告]
发表于 2010-12-20 17:30 |只看该作者
我也没有成功配置过。

论坛徽章:
0
5 [报告]
发表于 2010-12-20 22:15 |只看该作者
本帖最后由 dwm123 于 2010-12-20 22:51 编辑

1.软件下载
db postgresql 8.2.4.tar.gz(http://www.postgresql.org/download/)
slony1 slony1-1.2.6.tar(http://www.postgresql.org/download/)

2 系统 centos 5.5 i386
主服务器:m-db
从服务器:s-db

3.源码安装postgresql(2台一样)

#Groupadd postgres
#Useradd postgres –g postgres –d /home/postgres

#cd postgresql-8.2.4

#./configure  --prefix=/usr/local/pgsql

#gmake
#gmake install
-----------

#mkdir -p /data
#chown -R postgres:postgres /data  --存放数据

# su - postgres
$ vi ~/.bash_profile  ---环境变量设置
PGDATA=/data
export PGDATA

$ source ~/.bash_profile
----------------------------------------
$ initdb --encoding=UTF-8 --no-locale -D /data

#mkdir -p  /data/archive

#vi /data/postgresql.conf  --当然这里面有很多项,大家可以自己设置。在此只是实验而已。

listen_addresses = '*'  --打开监听地址,
port=5432   


#vi /data/pg_hba.conf

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
host    all         all         192.168.1.0/24       password                        ---支持远程登录,这里允许192.168.1.0网段机器访问,利用密码教研。(注意一点,要给postgres用户设置个密码,否则登录拒绝)

使用 postgres 用户后台启动 postmaser 数据库主进程
#su - postgres
$/usr/local/pgsql/bin/pg_ctl -D /data start ( stop restart)   --启动数据库

给postgres用户增加密码,默认是没有密码的

#su - postgres
$/usr/local/pgsql/bin/psql template1
$alter user postgres with password 'newpassword';----‘ ’中是密码

#/usr/local/pgsql/bin/psql  -h  ip  -U postgres    来登录数据库看看

创建数据库 URT
# su - postgres

$ /usr/local/pgsql/bin/createdb URT;
$/usr/local/pgsql/bin/psql  -l     显示URT 是否创建成功

到此为止,数据库安装结束。2台设置一样。

-----------------------
安装slonyi
解压缩 slonyi

cd postgresql-8.2.4

./configure  --prefix=/usr/local/pgsql

安装slony1
slony1-2.0.3.tar.bz2

#cd /slony1-2.0.3
# ./configure --with-pgconfigdir=/usr/local/pgsql/bin  -----选择postgresql bin目录的pg_config
gmake all
sudo gmake install

如果没有错误,slony1安装结束,

下面是配置,  我是参考  http://www.91linux.com/html/arti ... 20090331/16321.html  文章,但是目前还没有配置成功,后续再写。


Slony Config

创建urt_replica_init.sh文件
--------
#!/bin/sh
SLONIK=/usr/local/pgsql/bin/slonik
CLUSTER=URT
SET_ID=1
MASTER=1
HOST1=m-db
DBNAME1=URT
SLONY_USER=postgres
SLAVE=2
HOST2=s-db
DBNAME2=URT
PGBENCH_USER=postgres
$SLONIK <<_EOF_
cluster name = $CLUSTER;
node $MASTER admin conninfo = 'dbname=$DBNAME1 host=$HOST1 user=$SLONY_USER';
node $SLAVE admin conninfo = 'dbname=$DBNAME2 host=$HOST2 user=$PGBENCH_USER';
init cluster ( id = $MASTER,comment = 'Primary Node' );
store node ( id = $SLAVE, comment = 'Slave Node' );
store path ( server = $MASTER, client = $SLAVE,conninfo = 'dbname=$DBNAME1 host=$HOST1 user=$SLONY_USER ');
store path ( server = $SLAVE, client = $MASTER,conninfo = 'dbname=$DBNAME2 host=$HOST2 user=$PGBENCH_USER ');
store listen ( origin = $MASTER, provider = 1, receiver = 2 );
store listen ( origin = $SLAVE, provider = 2, receiver = 1 );
create set ( id = $SET_ID, origin = $MASTER, comment = 'All pgbench tables' );
set add table ( set id = $SET_ID, origin = $MASTER,id = 1, fully qualified name = 'public.accounts',comment = 'Table accounts' );
_EOF_


在从服务器s-db之行的时候,报错,还在研究,如果大家有兴趣可以帮我分析一下,还有下面几个脚本,
[root@s-db tool]# ./urt_replica_init.sh.sh
<stdin>:5: Error: require EVENT NODE
<stdin>:5: Error: No admin conninfo provided for node -1

论坛徽章:
0
6 [报告]
发表于 2010-12-24 02:01 |只看该作者
store node ( id = $SLAVE, comment = 'Slave Node' );


改一下试试

store node ( id = $SLAVE, comment = 'Slave Node', event node=$MASTER );

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
7 [报告]
发表于 2010-12-24 22:31 |只看该作者
都说slony是异步的。不知道是否真假。

论坛徽章:
0
8 [报告]
发表于 2014-04-04 09:38 |只看该作者
回复 1# SkyBelieve

<stdin>:5: Error: require EVENT NODE
<stdin>:5: Error: No admin conninfo provided for node -1

这个错误是因为使用slonik命令不当造成的,slonik -version可以看到版本号。而实际上,需要单独安装slony1-bin ,安装成功后,再使用slonik -version 会发现版本号不同。实际使用的是后者。
安装成功后,问题可以解决 。


   

论坛徽章:
0
9 [报告]
发表于 2014-04-04 09:54 |只看该作者
欢迎一起交流,本人一直使用slony在线运营复制N多服务器。QQ348019870
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP