mysqld_multiã使ããMySQLãè¤æ°èµ·åãã¦ãMaster-Slaveæ§æã§Replicationãåãã¾ã§
çµç·¯
ä¸ã¤ã®éçºç¨ãµã¼ãã§mysqlã®replicationãå©ç¨ããscriptãä½æããªãã¦ã¯ãªããªããªããæ¦åï¼id:maroekunããæãã¦ããã£ã mysqld_multiãå©ç¨ãã¦ã¿ããã¨ã«ããã
mysqld_multiã¨ã¯
MySQL MySQL 4.1 リファレンスマニュアル 4.8.3 mysqld_multi(複数の MySQL サーバを管理するプログラム)
mysqld_multi ã¯ããã¾ãã¾ãª Unix ã½ã±ããããã³ TCP/IP ãã¼ãããªãã¹ã³ããè¤æ°ã® mysqld ããã»ã¹ã管çããããã®ããã°ã©ã ã§ãã
ããã¯ãä¸ã¤ã®mysqld ã§è¤æ°ããã»ã¹ãç«ã¡ä¸ãããã¨ãã§ãã¾ãã
ãã¡ãããè¤æ°ããã»ã¹éã§replicationãåããã¨ãå¯è½ã§ãã
è¨å®
/etc/my.cnf
ã®è¨å®ã¯ãããªæãã§ã
[mysqld_multi] mysqld = /usr/bin/mysqld_safe mysqladmin = /usr/bin/mysqladmin [mysqld] default-character-set = utf8 user=mysql [mysqld1] server-id = 1 port = 3306 datadir = /data/var/lib/mysql socket = /var/lib/mysql/mysql.sock pid-file = /var/run/mysqld/mysqld.pid [mysqld2] server-id = 2 port = 3307 <= netstat ã§ä½¿ããã¦ããªãportã§ãããã¨ã確èªãã¾ããã datadir = /var/db_multi/db <= èªåã§ç解ããããååãªããã©ãã«ä½ã£ã¦ãããã§ã socket = /var/db_multi/mysql.sock pid-file = /var/db_multi/mysqld.pid
åæèµ·åãããæ°ã ãã[mysqld*]ã追å ãã¾ãããã
å
±éã®è¨å®é
ç®ã¯ã[mysqld]ã«ããã°ãï½ã
[mysqld_multi]ã«ã¯ãmysqld_safeã¨mysqladminãè¨è¼ã
â»[mysqld1]ã¯æ¢ã«è¨å®æ¸ã¿ã§ç¨¼åãã¦ããã®ã§ã[mysqld2]ã®è¨å®ããã¾ã
dirä½ã£ã¦ããã¾ãããã
mkdir -P /var/db_multi/db
mysqlã¦ã¼ã¶ã«ãã¦
chown mysql:mysql /var/db_multi/
datadirã®åæåã¼
mysql_install_db --datadir=/var/db_multi/db --user=mysql
èµ·å
mysqld_multi start 1,2 ããã㯠mysqld_multi start 1-2
ã§èµ·åãã¾ã
mysqld_multi report
å©ãã°
Reporting MySQL servers MySQL server from group: mysqld1 is running MySQL server from group: mysqld2 is running
ãããªé¢¨ã«è¤æ°èµ·åã確èªã§ããã¯ãã
ps aux | grep mysql
ã§ã確èªã§ããããª
使ã£ã¦ã¿ã
[mysqld1]ã Master ã¨ãã¦å©ç¨ãã
[mysqld2]ã Slave ã¨ãã¦å©ç¨ãã¦ã¿ã¾ãã
ãããã¯Portçªå·ã§åºå¥ã§ããã®ã§ãPortãæå®ãã¦mysqlã³ãã³ããå®è¡ãã¾ã
Masterã¸
mysql -u user_name -P 3306 -h 127.0.0.1
Slaveã¸
mysql -u user_name -P 3307 -h 127.0.0.1
-h 127.0.0.1 ããããªãã¨Slaveã«ãã°ã¤ã³ã§ããªãã®ã§ã注æã
Replication
ãããããReplicationè¨å®ã
ã¨ã¯ãã£ã¦ããæ®éã®Replicationè¨å®ã¨åãã§ãã
mysqld_multiã ããã¨ãã£ã¦ãç¹å¥ãªæèã¯ãããªãã§ãã
1./etc/my.cnfã«è¿½å
/etc/my.cnf
[mysqld1] server-id = 1 port = 3306 datadir = /data/var/lib/mysql socket = /var/lib/mysql/mysql.sock pid-file = /var/run/mysqld/mysqld.pid sync_binlog=1 log-bin=mysql-bin log-bin-index=mysql-bin relay-log=relay-bin relay-log-index=relay-bin [mysqld2] server-id = 2 port = 3307 datadir = /var/db_multi/db/ socket = /var/db_multi/mysql.sock pid-file = /var/db_multi/mysqld.pid read_only log-slave-updates log-bin=mysql-bin log-bin-index=mysql-bin relay-log=relay-bin relay-log-index=relay-bin
ãã®ããã«ãbinlogã®è¨å®ãªã©ãReplicationã®æºåããã¾ãã
2.Replicationç¨Userã®ä½æ
Masterå´ã«ãReplicationç¨ã®Userãä½ã£ã¦ããã¾ããã
mysql> GRANT REPLICATION SLAVE ON *.* TO [email protected] IDENTIFIED BY 'password';
ã¦ã¼ã¶åã¯ããreplãã§ããå¿
è¦ã¯ããã¾ããã
æ¥ç¶å
IPã¯ãMasterã¨Slaveã¯åããªã®ã§ãæ®éã«ãµã¼ãIPãããã°åé¡ããã¾ããã
3.Masterãã¼ã¿ã®æ´æ°lock
ãã¼ã¿ãslaveã«ç§»ãã®ã§ãmasterã®æ´æ°ãlockãã¾ã
mysql> FLUSH TABLES WITH READ LOCK;
4.logã®posiã®ç¢ºèª
Masterã§ãshow master statusããå®è¡ãã¾ã
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 2283327 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.10 sec)
[File]ã¨[Position]ãã¡ã¢ãã¾ãããã
ãã¨ã§ä½¿ãã¾ãã
5.ãã¼ã¿ã®ã³ãã¼
ã§ã¯ãlockããã¾ã¾ãMasterãã¼ã¿ãSlaveã«ç§»ãã¾ããã
cp -aR /data/var/lib/mysql/* /var/db_multi/db/
6.masterã®unlock
ããã¾ã§ããããMasterã®æ´æ°Lockãæ»ãã¦ããã¾ããã
mysql> UNLOCK TABLES;
7.Slaveå´ã§Replicationã®ããã®ãã©ã¡ã¼ã¿è¨å®
mysql> CHANGE MASTER TO MASTER_HOST='10.0.0.0', <=ä»åã¯MasterãSlaveãåä¸ãµã¼ããªã®ã§èªåã®IP MASTER_USER='repl', <=2ã§ä½ã£ãUserå MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000003', <=4ã§è¨é²ãã[File]å MASTER_LOG_POS=2283327 ; <=4ã§è¨é²ãã[Position]
8.Replication start
mysql> start slave;
èµ·åãããã©ããã¯ã
mysql> show slave status;
ã§ç¢ºããã¾ãããã
Slave_IO_Running: Yes Slave_SQL_Running: Yes
ã§ããã°æ£å¸¸ã«åãã¦ãã¾ãã
ããã
[IO_Running]ã ãã No ã®å ´åã¯ãReplicationããã¾ãè¨å®ã§ãã¦ããªãå¯è½æ§ãã
[SQL_Running]ã ãã No ã®å ´åã¯ãä½ããã®SQLã«ããReplicationã¨ã©ã¼ã«ãªã£ã¦ããå¯è½æ§ãé«ãã§ã