PostgreSQL::Slony

postgresでのレプリケーション

今回は、postgresのレプリケーションの勉強を行うに当たり、このブログのDBを2台のサーバで、同期を取ることにしました。

今回の教科書は、PCWEB(http://pcweb.mycom.co.jp/column/yetanother/030/)です

材料:

  サーバ:1号機(SuseLinux:10.8.136.37)~このサーバ

  ソフト:

   postgresql-8.1.2(インストール済み)

   Slony-1.1.5

 サーバ:2号機(SuseLinux:10.8.136.16)~別途用意しました(昔の1号機)

  ソフト:

   postgresql-8.1.2

   Slony-1.1.5

 DB:

   mt32

  Table:対象は、以下の通り

  1. mt32=# \dt
  2. List of relations
  3. Schema | Name | Type | Owner
  4. --------+-----------------+-------+----------
  5. public | mt_author | table | postgres
  6. public | mt_blog | table | postgres
  7. public | mt_category | table | postgres
  8. public | mt_comment | table | postgres
  9. public | mt_config | table | postgres
  10. public | mt_entry | table | postgres
  11. public | mt_fileinfo | table | postgres
  12. public | mt_ipbanlist | table | postgres
  13. public | mt_log | table | postgres
  14. public | mt_notification | table | postgres
  15. public | mt_permission | table | postgres
  16. public | mt_placement | table | postgres
  17. public | mt_plugindata | table | postgres
  18. public | mt_session | table | postgres
  19. public | mt_tbping | table | postgres
  20. public | mt_template | table | postgres
  21. public | mt_templatemap | table | postgres
  22. public | mt_trackback | table | postgres

■インストール

  2号機にpostgresql-8.1.2をインストールします

  Download: http://www.postgresql.org/

  1. # cd /usr/local/src
  2. # wget http://wwwmaster.postgresql.org/redir?ftp%3A%2F%2Fftp2.jp.postgresql.org%2Fpub%2Fpostgresql%2Fsource%2Fv8.1.2%2Fpostgresql-8.1.2.tar.gz
  3. # tar zxvf postgresql-8.1.2.tar.gz
  4. # cd postgresql-8.1.2
  5. # useradd postgres
  6. # su - postgres
  7. $ ./configure --enable-multibyte=UTF-8 --without-readline
  8. $ make
  9. $ make install
  10. $ cd /usr/local/pgsql
  11. $ mkdir data
  12. $ initdb -no-locale
  13. $ createdb --encoding=UTF-8 mt32

 次に1号機から、DBを持ってきます。

  1. $ pg_dump -C -f mt32.dmp mt32
  2. $ scp mt32.dmp root@10.8.136.16:/usr/local/src/
  3.  
  4. </block>
  5.  
  6.  そして、2号機で取り込みます。
  7.  
  8.  
  9. <blockcode language='bash'>
  10.  
  11. $ psql mt32 < mt32.dmp

 DBがちゃんと取り込めていることを確認しら、slonyのインストールです

 Download: http://gborg.postgresql.org/project/slony1/projdisplay.php

  1. # cd /usr/local/src
  2. # wget http://developer.postgresql.org/~wieck/slony1/download/slony1-1.1.5.tar.bz2
  3. # bzip2 -d slony1-1.1.5.tar.bz2
  4. # su - postgres
  5. $ ./configure --with-pgsourcedir=../postgresql-8.1.2
  6. $ make
  7. $make install

 これでインストール完了です

 

  続いて、設定を行います。

  ここでは、1号機をマスター、2号機をスレーブとして扱います。

 

  まずは、両方にSlonyで必要な手続き言語を登録します。

  1. $ createlang plpgsql mt32

 続いて、slonikコマンド経由で、マスターの設定を行います。

  以下のファイルを作成して、実行します。

  1. #!/bin/sh
  2. CLUSTERNAME=mt32
  3. DB1=mt32
  4. DB2=mt32
  5. HOST1=10.8.136.37
  6. HOST2=10.8.136.16
  7. RUSER=postgres
  8. slonik <<_EOF_
  9. cluster name = $CLUSTERNAME;
  10. node 1 admin conninfo = 'dbname=$DB1 host=$HOST1 user=$RUSER';
  11. node 2 admin conninfo = 'dbname=$DB2 host=$HOST2 user=$RUSER';
  12. init cluster ( id=1, comment = 'Master');
  13. create set (id=1, origin=1, comment='All tables');
  14. set add table (set id=1, origin=1, id=1, fully qualified name = 'public.mt_author', comment='mt_author');
  15. set add table (set id=1, origin=1, id=2, fully qualified name = 'public.mt_blog', comment='mt_blog');
  16. set add table (set id=1, origin=1, id=3, fully qualified name = 'public.mt_category', comment='mt_category');
  17. set add table (set id=1, origin=1, id=4, fully qualified name = 'public.mt_comment', comment='mt_comment');
  18. set add table (set id=1, origin=1, id=5, fully qualified name = 'public.mt_config', comment='mt_config');
  19. set add table (set id=1, origin=1, id=6, fully qualified name = 'public.mt_entry', comment='mt_entry');
  20. set add table (set id=1, origin=1, id=7, fully qualified name = 'public.mt_fileinfo', comment='mt_fileinfo');
  21. set add table (set id=1, origin=1, id=8, fully qualified name = 'public.mt_ipbanlist', comment='mt_ipbanlist');
  22. set add table (set id=1, origin=1, id=9, fully qualified name = 'public.mt_log', comment='mt_log');
  23. set add table (set id=1, origin=1, id=10, fully qualified name = 'public.mt_notification', comment='mt_notification');
  24. set add table (set id=1, origin=1, id=11, fully qualified name = 'public.mt_permission', comment='mt_permission');
  25. set add table (set id=1, origin=1, id=12, fully qualified name = 'public.mt_placement', comment='mt_placement');
  26. set add table (set id=1, origin=1, id=13, fully qualified name = 'public.mt_plugindata', comment='mt_plugindata');
  27. set add table (set id=1, origin=1, id=14, fully qualified name = 'public.mt_session', comment='mt_session');
  28. set add table (set id=1, origin=1, id=15, fully qualified name = 'public.mt_tbping', comment='mt_tbping');
  29. set add table (set id=1, origin=1, id=16, fully qualified name = 'public.mt_template', comment='mt_template');
  30. set add table (set id=1, origin=1, id=17, fully qualified name = 'public.mt_templatemap', comment='mt_templatemap');
  31. set add table (set id=1, origin=1, id=18, fully qualified name = 'public.mt_trackback', comment='mt_trackback');
  32. store node (id=2, comment = 'Slave');
  33. store path (server = 1, client = 2, conninfo='dbname=$DB1 host=$HOST1 user=$RUSER');
  34. store path (server = 2, client = 1, conninfo='dbname=$DB2 host=$HOST2 user=$RUSER');
  35. store listen (origin=1, provider = 1, receiver =2);
  36. store listen (origin=2, provider = 2, receiver =1);
  37. _EOF_

 マスター上でレプリケーションデーモンを起動します

  1. $ slon mt32 "dbname=mt32 user=postgres host=10.8.136.37" &

 続いて、スレーブ上でレプリケーションデーモンを起動します

  1. $ slon mt32 "dbname=mt32 user=postgres host=10.8.136.16" &

 これで、同期の準備が整いましたので、後は、スレーブ側で、同期の受けを作ります。

  マスターと同じように下記のファイルを作成して実行します。

  ※実行すると同時に同期が始まります。

  1. #!/bin/sh
  2. CLUSTERNAME=mt32
  3. DB1=mt32
  4. DB2=mt32
  5. HOST1=10.8.136.37
  6. HOST2=10.8.136.16
  7. RUSER=postgres
  8. slonik <<_EOF_
  9. cluster name = $CLUSTERNAME;
  10. node 1 admin conninfo = 'dbname=$DB1 host=$HOST1 user=$RUSER';
  11. node 2 admin conninfo = 'dbname=$DB2 host=$HOST2 user=$RUSER';
  12. subscribe set ( id = 1, provider = 1, receiver = 2, forward = no);
  13. _EOF_

Tags for PostgreSQL::Slony


■お知らせ
鯖の電気代が2万/月を超えました。新しい鯖を購入して電気代を下げるようにしましたが、ご協力いただける方がいらっしゃいましたら、よろしくお願いします。
カンパしなくても、鯖は存続しますので、WebMoney余ってるよ~という方だけ、よろしくお願いしますm(__)m
WebMoney ぷちカンパ
カスタム検索