ºìÁªLinuxÃÅ»§
Linux°ïÖú

Mysql µÄÊý¾Ý¿â¸´Öƹ¦ÄÜ (Replication Database)

·¢²¼Ê±¼ä:2006-09-18 00:25:04À´Ô´:ºìÁª×÷Õß:shale
Mysql Êý¾Ý¿âÏàÐÅ´ó¼ÒÒѾ­Í¶ÈëÁËÉú²úʹÓ᣺ܶàÈ˶¼½«ËûºÍ PHP ¼¯³ÉÔÚ Apache ÖУ¬ÎªWebSite ·þÎñ¡£µÄÈ·£¬ËûÃÇÔÚWebSite ÖеÄÓ¦ÓñȽ϶࣬¶øÇÒPhpMyAdmin ÓÖÊÇÒ»¸öPHP+Mysql µÄ×îºÃÓ¦ÓÃÀý×Ó¡£
ÄÇôMysql Äܲ»ÄÜʵÏÖÁ½¸öϵͳ֮¼äͨ¹ýTCP/IPÈ¥¸´ÖÆÊý¾Ý¿â£¿Äܲ»ÄÜʵÏÖʵʱ¸´ÖÆÄØ£¿Ò²¾ÍÊÇ˵Äܲ»ÄÜʵÏÖͬ²½£¨Synchronization£©µÄÎÊÌâ¡£ÏȸÅÀ¨½éÉÜÒ»ÏÂMysql µÄReplication Database¹¦ÄÜ¡£
¸´ÖÆ£¨Replication£©ÀàËÆÓÚ¿½±´Êý¾Ý¿âµ½Áíһ̨·þÎñÆ÷ÉÏ£¬µ«ËüÊÇͨ¹ý¶¨ÒåMaster ºÍSlaveµÄ¹ØϵȥʵʱµØ±£Ö¤Á½¸öÊý¾Ý¿âµÄÍêȫͬ²½¡£Õâ¸ö¹¦ÄÜÔÚMysqlµÄ3.23°æÖпªÊ¼³öÏÖ¡£
ÏÂÃæ´ó¼ÒÒ»ÆðÀ´²âÊÔÒ»ÏÂMysqlµÄReplication ¹¦ÄÜ¡£
×÷ÕßµÄƽ̨ÊÇ£º
Master£ºMysql 3.23.53-log on FreeBSD 4.7 Release IP:192.168.10.100
Slave£º Mysql 3.23.56-log on FreeBSD 4.8 Stable IP:192.168.10.200 1¡¢Master »úÆ÷ÉèÖÃȨÏÞ£¬¸³ÓèSlave Relication ȨÀû£¬²¢´ò°üҪͬ²½µÄÊý¾Ý¿â½á¹¹¡£
MasterBSD# pwd
/usr/local/mysql/bin
MasterBSD#./mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 3.23.53-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> GRANT FILE ON *.* TO replication@192.168.10.200 IDENTIFIED BY ¡®repplication¡¯;
(¸³Óè192.168.10.200Ò²¾ÍÊÇSlave »úÆ÷ÓÐFileȨÏÞ)
È»ºó´ò°üÒª¸´ÖƵÄÊý¾Ý¿â
MasterBSD# cd var
MasterBSD# tar czvf repdatabase.tar.gz repdatabase
ÕâÑù£¬ÎÒÃǵĵ½Ò»¸örepdatabaseÊý¾Ý¿âµÄ´ò°üÎļþrepdatabase.tar.gz2ÉèÖÃÖ÷·þÎñÆ÷MasterµÄmy.cnf£¬Æô¶¯Mysql·þÎñ
MasterBSD# vi /etc/my.cnfÔÚ[mysqld]Ìí¼Ó»òÐÞ¸ÄÒÔϵÄ
[mysqld]
log-bin
server-id=1
sql-bin-update-same
binlog-do-db= repdatabaseÕë¶Ôrepdatabase ¿â×öreplication ¹¦ÄÜ
È»ºó°ÑMasterÖ÷·þÎñÆ÷µÄMysqlÖØÆô¡£
MasterBSD# /usr/local/mysql/bin/mysqladmin -u root -p shutdown
MasterBSD# /usr/local/mysql/bin/safe_mysqld --user=mysql &3¡¢½¨Á¢SlaveÊý¾Ý¿â
¸Õ²ÅÎÒÃÇÔÚMasterÖдò°üÁËrepdatabase.tar.gz£¬ËüµÄ×÷ÓþÍÊÇÒªÔÚSlave»Ö¸´³ÉÒ»ÑùµÄÊý¾Ý¿â¡£ÏÈ°ÑMaster µÄrepdatabase.tar.gzÎļþ´«µ½Slave»úÆ÷ÖÐÈ¥¡£È»ºó
SlaveBSD# tar zxvf repdatabase.tar.gz -C /usr/local/mysql/var/4¡¢ÐÞ¸ÄSlave·þÎñÆ÷µÄmy.cnf
SlaveBSD# vi /etc/my.cnfÔÚ[mysqld]Ìí¼Ó»òÐÞ¸ÄÒÔϵÄ
master-host=192.168.10.100
master-user=replication
master-password=replication
master-port=3306
server-id=2
master-connect-retry=60
replicate-do-db=reldatabase ¡¡¡¡¡¡
[Òª¸üеÄÊý¾Ý¿â]
log-slave-updates5¡¢ÖØÆô¶¯SlaveµÄslave start¡£
SlaveBSD# /usr/local/mysql/bin/mysqladmin -u root -p shutdown
SlaveBSD# /usr/local/mysql/bin/safe_mysqld --user=mysql &6¡¢²âÊÔ
Ïȼì²âÁ½¸öMysqlÊý¾Ý¿âÖеÄrepdatabaseÊÇ·ñÕý³£¡£
Õý³£Çé¿öÓ¦¸ÃÊÇMasterºÍSlave ÖеÄMysql ¶¼ÓÐÏàͬµÄrepdatabase Êý¾Ý¿â£¬²¢ÇÒÀïÃæµÄÊý¾Ý¶¼Ò»Ñù¡£
È»ºóÎÒÃDzâÊÔreplication ¹¦ÄÜÊÇ·ñÆðÓá£
ÔÚMasterÖеÄrepdatabasÊý¾Ý¿âÌí¼ÓÒ»±ÊÊý¾Ý£º
MasterBSD# /usr/local/mysql/bin/mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12 to server version: 3.23.53-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use repdatabase;
Database changed
mysql> INSERT INTO `rep_table` ( `name` , `num` , `selectd ` ) VALUES ('test1', '4321', 'Y');
Query OK, 1 row affected (0.00 sec)
mysql>
ÔÚSlaveµÄÊý¾Ý¿âÖÐÓ¦¸ÃÒ²»áͬÑùÓÐÕâÑùÒ»ÌõÊý¾Ý
SlaveBSD# /usr/local/mysql/bin/mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17 to server version: 3.23.56-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> select * from repdatabase.rep_table;
+--------+-----------+------------+
| name | num | selectd |
+--------+-----------+------------+
| aaa | 44444 | N |
| ddd | 111112222 | N |
| insert | 1234 | N |
| test | 12345 | N |
| test1 | 4321 | Y | ¡¡¡¡¡¡
ÕâÒ»ÐоÍÊÇMaster²åÈëµÄʱºòSlave ͬ²½µÃ»ØÀ´µÄÊý¾Ý¡£
+--------+-----------+------------+
5 rows in set (0.01 sec)
mysql>
µ½´Ë£¬ÎÒÃǵÄÁ½¸öÊý¾Ý¿âreplication ¹¦ÄÜʵÑé³É¹¦¡£
7¡¢»¥Îªreplication
ÔÚMysql µÄÎĵµ×ÊÁÏÖÐÒ²Ö¸³öÁË£¬Ò»Ì¨Mysql»úÆ÷ͬÑù¿ÉÒÔ×÷ΪMasterÒ²¿ÉÒÔ×÷ΪSlaveµÄ£¬Ò²¿ÉÒÔ»¥ÏàreplicationÊý¾Ý¡£
8¡¢Ó¦ÓÃ
replication¿ÉÒÔÓÃÔÚÄÇ·½ÃæÄØ£¿ÎÒµÄÏë·¨ÊÇ£¬Ò»Ì¨MysqlÉú²ú»úÆ÷ÔÚÌṩ·±Ã¦µÄSQL²éѯ£¬±ÈÈç˵ÊǹÉÊеIJéѯ£¬ÄÇËü½ö½öÊÇ×÷Ϊ²éѯ¶øÒÑ¡£ÄÇôÎÒÃǾͿÉÒÔͨ¹ýÁ½Ì¨»úÆ÷£¬Ìṩ²éѯµÄ»úÆ÷ΪSlave£¬ÄÇôÊý¾Ý¼ÈëµÄ»úÆ÷ÊÇMaster£¬Í¨¹ýË«Íø¿¨È¥½øÐУ¬Çë¿´ÏÂͼ£º

ÕâÑùµÄºÃ´¦ÊÇÏÔ¶øÒ×¼û£¬Slave×÷Ϊ´óÁ¿SQL²éѯµÄ·þÎñÆ÷£¬·±Ã¦¡¢ÈÎÎñ´ó£¡¶øÇÒ¶Ô×ÅInternet£¬ÆÆ»µ¿ÉÄÜÐÔÒ²´ó£¡Òò´Ë£¬ÒýÈëMysqlµÄreplicationÎÒÃÇ¿ÉÒÔ·½±ã¡¢°²È«µØ¹ÜÀíÊý¾Ý¿â£¡
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ