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

Ҳ̸ mysql Êý¾Ý¿âµÄͬ²½ÎÊÌâ

·¢²¼Ê±¼ä:2006-05-16 00:55:05À´Ô´:ºìÁª×÷Õß:ÑàÄÏÌì
תÌù£º

ÍøÉÏÓв»ÉÙ¹ØÓÚ mysql Êý¾Ý¿â·þÎñÆ÷µÄÊý¾Ýͬ²½ÎÄÕ£¬ÆäʵֻÓжþλÅóÓÑ·¢±íµÄÎÄÕ£¬ÆäËû¶¼ÊÇת³­¡£
¶øÕâЩÎÄÕÂÖУ¬½ö¶ÔÊý¾Ýͬ²½¹ý³ÌµÄ²Ù×÷×÷ÁËÒ»¸ö¼Ç¼£¬¶øûÓаѲÙ×÷·½·¨£¬Ô­ÀíÓëһЩ¹ý³ÌÖгöÏÖµÄÎÊÌâ˵Ã÷Çå³þ¡£

ÒòΪ¹¤×÷ÐèÒª£¬ÎÒ¹«Ë¾ÐèÒª¶ÔÃÀ¹ú·þÎñÆ÷É쵀 mysql Êý¾Ý¿âͬ²½µ½Ïã¸Û·þÎñÆ÷ÉÏ£¬½ö¶Ô²¿·ÖÒµÎñÏà¹Ø±í½øÐÐͬ²½£¬¶øͬʱÓÖÐèÒª°ÑÏã¸Û·þÎñÆ÷ÉϵIJúÆ·Êý¾Ýͬ²½µ½ÃÀ¹ú·þÎñÆ÷ÉÏ£¬ÕâÊÇͬһ¸öÊý¾Ý¿â£¬²»Í¬±í table µÄË«Ïòͬ²½£¬ÎÒÔÚÉèÖùý³ÌÖУ¬²Î¿¼ÁËÍøÉϵÄÖÐÎÄÎÄÕÂÓë msql.com ÍøÕ¾ÉϵÄÔÚÏßÎĵµ£¬¶à´Î³É°Ü£¬Ò²ËãÊÇ°Ñ mysql µÄÕâÑùͬ²½¸ã¸öÃ÷°×¡£

ÏÂÃæ¾ÍÊÇһЩ¼Ç¼ÎÄÕ£¬Ï£Íû¿ÉÒÔ˵Ã÷һЩÄÚÔÚÔ­Àí£¬Ò»Ð©³ö´íÏÖÏóÓë´¦Àí·½·¨¡£

mysql µÄÊý¾Ýͬ²½£¬ÔÚ mysql ¹Ù·½ÍøÕ¾ÎĵµÉÏ£¬½Ð replication ×ÖÃæÊÇÖØ×÷µÄÒâ˼£¬ÒâÒë¾ÍÊÇͬ²½ÁË¡£
Æäʵ£¬mysql µÄͬ²½£¬²¢²»ÊÇʹÓÃͬ²½ sync Õâ¸öµ¥´Ê¶øÊÇÓÃÖØ×÷ replication ,ºÜ׼ȷ±íÃ÷ÁË mysql Êý¾Ý¿â²Ù×÷µÄʵÖÊ£¬ÊÇ×÷ͬÑùµÄ²Ù×÷£¬»ò½ÐÖØ×÷ͬÑùµÄ²Ù×÷£¬ÒÔ±£³ÖÖ÷Êý¾Ý¿â·þÎñÆ÷ master Óë ´ÓÊô·þÎñÆ÷ slave Ö®ÑùµÄÊý¾Ý±£³ÖÒ»Ö¡£replication ¾ÍÊÇÓÐÖظ´£¬ÖØ×÷µÄÒâ˼¡£

mysql ΪÁËʵÏÖ replication ±ØÐë´ò¿ª bin-log ÏҲÊÇ´ò¿ª¶þ½øÖÆµÄ mysql ÈÕÖ¾¼Ç¼ѡÏî¡£

mysql µÄ bin log ¶þ½øÖÆÈÕÖ¾£¬¿ÉÒԼǼËùÓÐÓ°Ïìµ½Êý¾Ý¿â±íÖд洢¼Ç¼ÄÚÈÝµÄ sql ²Ù×÷£¬Èç insert / update / delete ²Ù×÷£¬¶ø²»¼Ç¼ select ÕâÑùµÄ²Ù×÷¡£Òò´Ë£¬ÎÒÃÇ¿ÉÒÔͨ¹ý¶þ½øÖÆÈÕÖ¾°Ñijһʱ¼ä¶ÎÄÚ¶ªÊ§µÄÊý¾Ý¿ÉÒÔ»Ö¸´µ½Êý¾Ý¿âÖУ¬( Èç¹û¶þ½øÖÆÈÕÖ¾ÖмǼµÄÈÕÖ¾Ï°üº­Êý¾Ý¿â±íÖÐËùÓÐÊý¾Ý£¬ÄÇô£¬¾Í¿ÉÒÔ»Ö¸´±¾µØÊý¾Ý¿âµÄÈ«²¿Êý¾ÝÁË )

¶øÕâ¸ö¶þ½øÖÆÈÕÖ¾£¬Èç¹ûÓÃ×÷Ô¶³ÌÊý¾Ý¿â»Ö¸´£¬ÄǾÍÊÇ replication ÁË¡£
Õâ¾ÍÊÇʹÓà replication ¶ø²»Óà sync µÄÔ­Òò¡£
ÕâÒ²ÊÇΪʲôҪÉèÖà bin-log = Õâ¸öÑ¡ÏîµÄÔ­Òò¡£

ÔÚͬ²½¹ý³ÌÖУ¬×îÖØÒªµÄͬ²½²ÎÕÕÎ¾ÍÊÇͬ²½Ê¹ÓÃÄÇÒ»¸ö¶þ½øÖÆÈÕÖ¾Îļþ£¬´ÓÄÇÒ»Ìõ¼Ç¼¿ªÊ¼Í¬²½¡£
ÏÂÃ棬ÎÒÃÇÀ´½²½â×îºËÐĵĹý³Ì


ÉÏÃæͬ²½µÄµ¥´ÊÊÇ synchronization £¬ºÇºÇ¼òд sync £¬Ò²ÊÇ linux ÏÂÒ»¸öÖØÒª²Ù×÷

ÏÂÃæµÄ¹Ù·½Îĵµ£¬ËµÃ÷ÈçºÎ°²×°ÓëÉèÖà mysql ͬ²½²Ù×÷£¬µ±È»£¬Ó¢ÎÄ°æ
http://dev.mysql.com/doc/mysql/en/Replication_HOWTO.html

ÏÖÔÚ£¬ÎÒÃÇÓÃÖÐÎĸ´ÊöһϹý³Ì

1. Ê×ÏÈ£¬ÄãÓ¦¸ÃÓÐÁ½¸ö»òÁ½¸öÒÔÉ쵀 mysql Êý¾Ý¿â·þÎñÆ÷£¬°æ±¾×îºÃÊÇÔÚ 3.3 ÒÔÉÏ ( µ±È»À²£¬Á½¸ö·þÎñÆ÷²»Ò»¶¨ÊÇÁ½Ì¨»úÆ÷£¬Ò»Ì¨»úÆ÷ÉÏ°²×°Á½¸ö mysql ·þÎñÊÇ¿ÉÒԵģ¬Í¬Ê±£¬Èç¹ûÄã¶Ô mysql replication Ô­ÀíÊ®·Ö¾«Í¨µÄ»°£¬ÄãÉõÖÁ¿ÉÒÔÔÚÒ»¸ö mysql ·þÎñµÄÁ½¸ö²»Í¬Êý¾Ý¿â database Ö®¼ä×÷ͬ²½£¬¿´ÓÐûÓÐÐèÒªÁË)¡¡
˵Ã÷£º ÕâÁ½¸ö·þÎñÆ÷Ò»°ãÉèÖÃÒ»¸öΪ Ö÷·þÎñÆ÷£¬»ò½ÐÔ´·þÎñÆ÷£¬ master mysql server, Áíһ̨»òÆäËû¶ą̀¾ÍÊÇ replication slave ͬ²½´Ó·þÎñÆ÷ÁË¡£Ò»Ì¨ slave Ó붹̀ slave ÉèÖ÷½·¨ÊÇÒ»ÑùµÄ£¬ÕâÑùÄã¾Í¿ÉÒÔ×÷ÀàËÆÊý¾Ý¿â¼¯ÈºÁË

2. ÉèÖÿɷÃÎÊ mysql Õʺţ¬²Ù×÷ÒÔÓ¢ÎÄΪ׼
˵Ã÷£º 2.1 mysql ÕʺÅÒ»°ãÉèÖÃΪÏÞ¶¨ ip ·ÃÎÊ£¬ÒÔ±£ÕÏ°²È«ÐÔ
¡¡¡¡2.2 mysql ÕʺÅÒ»°ãÔÚ master Óë slave ÉèÖÃΪÏàͬÕʺţ¬Í¬Ê±ÊÇÔ¶³Ì¿É·ÃÎÊ
¡¡¡¡2.3 Ìرð×¢Ò⣬Èç¹ûÄãÓà linux / unix ²Ù×÷ϵͳ£¬ÄÇÒ»¶¨Òª×¢ÒâһϷÀ»ðǽ firewall
¡¡¡¡¡¡ÓÐûÓÐÏÞÖÆ mysql Ô¶³Ì·ÃÎÊ£¬Èç¹ûÊÇ£¬×îºÃÊÇ´ò¿ªÔ¶³Ì·ÃÎʶ˿ڣ¬²¢×÷ºÃ·ÃÎÊ ip ÏÞÖÆ
¡¡¡¡2.4 ÓÉÓÚ my.cnf ÖÐÒªÃ÷ÎÄ´æ´¢ mysql ÕʺÅÃÜÂ룬Çë×¢Òâ±£»¤ my.cnf ²»ÈÃÆäËûÓû§·ÃÎʵ½
¡¡¡¡¡¡( ¿´À´ÒªÏò mysql ˵Ã÷ÏÂÒÔºóÓÃÃÜÂë´æ passwd )

3. µÚÈý²½£¬µ±È»ÊÇÉèÖÃÁ½¸ö·þÎñÆ÷Ҫͬ²½µÄÊý¾Ý¿âΪͬÑùµÄÊý¾Ý¿âÁË
ÕâÀïÓÐһЩ¼¼ÇÉ£¬°üÀ¨ mysql µÄ sql Ö¸Áî˵Ã÷ÏÂ
3.1 ·½·¨Ö®Ò»£¬¾ÍÊÇÓ¢ÎÄ˵Ã÷ÖÐ˵µÄ£¬ÏÈËø¶¨Êý¾Ý¿â¶Áд¹¦ÄÜ ( Æäʵ×îºÃÊÇÍ£Ö¹ mysqld ·þÎñ£¬ÔÙ×÷±¸·Ý) È»ºóÓà tar ±¸·ÝÊý¾Ý¿âĿ¼£¬×ªµ½ slave ·þÎñÆ÷ÏàͬÊý¾ÝĿ¼ÖÐ

3.2 ·½·¨Ö®¶þ£¬Ê¹Óà mysql studio ÕâÑùµÄʵÓù¤¾ß£¬Ö±½ÓʹÓà mysql studio µÄ backup database ¹¤¾ß°ÑÊý¾Ý¿âͬ²½

3.3 ÒÔÉÏÁ½ÖÖ·½·¨¶¼ÊÇ master Êý¾Ý¿âÖÐÓв»ÉÙÊý¾Ý¼Ç¼£¬°´ÉÏÁ½ ÖÖ·½·¨µÃµ½ master Óë slave ÓÐÏàͬÊý¾Ý¿âÓëÊý¾Ý¼Ç¼¡£ ¶øµÚÈýÖÖ·½·¨£¬ÔòÊÇÊʺÏÓÚн¨Êý¾Ý¿âµÄÇé¿ö£¬ÌرðÊʺÏÓÚ master Óë slave ÔÚ my.cnf ÒѾ­ÉèÖúà replication ¹Øϵ £¨µ« δָ¶¨ database ͬ²½Êý¾Ý¿â ) µÄÇé¿ö: Õâ¾ÍÊÇʹÓá¡mysql µÄ sql Óï¾ä load table from master Óë load data from master;
load table from master ¿ÉÒÔ´Ó master Êý¾Ý¿â°Ñ±í½á¹¹¸´ÖƵ½ slave Êý¾Ý¿âÖУ¬ÕâÑù¿ÉÒÔ½¨Á¢Í¬²½µÄ±í¡£
load data from master ÊÇ´Ó master Êý¾Ý¿â°ÑÊý¾Ýµ¼Èëµ½ slave Êý¾Ý±íÖУ¬Ìõ¼þÊÇ master ´ÓÒ»¿ªÊ¼°²×°ÔËÐоÍʹÓÃÁË bin-log ²ÎÊý¶ø±£´æÓжþ½øÖÆÈÕÖ¾

4. ½ÓÏÂÀ´¾ÍÊÇÅäÖà master Óë slave µÄ my.cnf Îļþ£¬Ê¹µÃ replcation ÄÜÓкÏÊʵÄÆô¶¯²ÎÊýÒÔÖ§³ÖÊý¾Ýͬ²½
¼¼ÇÉ£º Èç¹ûÄãʹÓà win2k µÄ mysql ÄÇô£¬Äã¿ÉÒÔÏÂÔØ mysql.com ³öµÄ¹Ù·½ mysql administrator ʵÓóÌÐò£¬Ö±½ÓÔÚ mysql administrator Öм´¿ÉÅäÖà master Óë slave £¬Í¬Ê±Ò²¿ÉÒÔÅäÖà query-cache Ó´

ÔÚ master µÄ my.cnf ( Èç¹ûÊÇ win32 ÄǾÍÊÇ my.ini ) Ôö¼Ó
[mysqld]
log-bin =
server-id=1

×¢Ò⣬ÉÏÃæµÄ log-bin = ÖÐµÄ µÈºÅ = ÊDz»¿ÉÉÙµÄ

ÔÚ slave µÄ my.cnf ÐÞ¸Ä
[mysqld]
server-id=2 # Èç¹ûÓжà¸ö slave ¾Í¸ÄΪ²»Öظ´µÄ id ¾ÍºÃ£¬ÔÚmysql 4.1 ÖУ¬Õâ¸öÒѾ­È¡ÏûÁË
master-host=10.10.10.22
master-user=backup #ͬ²½Óû§ÕʺÅ
master-password=1234
master-port=3306
master-connect-retry=60 #Ô¤ÉèÖØÊÔ¼ä¸ô60Ãë
replicate-do-db=test # ¸æËßslaveÖ»×ö test Êý¾Ý¿âµÄ¸üÐÂ
bin-log =


5. ¼ì²é master Óë slave ÅäÖÃ״̬£¬Ê¹Óà show master status; Óë show slave status; ÕâÁ½¸ö SQL Ö¸Áî¼´¿ÉÔÚ master Óë slave ²é¿´ÅäÖÃ״̬¡£ÕâÀïÓÐÁ½¸ö״̬±äÁ¿Ê®·ÖÖØÒª

mysql > SHOW MASTER STATUS;
+---------------+----------+--------------+------------------+
| File¡¡¡¡ | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| mysql-bin.003 | 73¡¡¡¡| test¡¡¡¡ | manual,mysql¡¡ |
+---------------+----------+--------------+------------------+

×¢Òâ File ÊÇ mysql-bin.003 ¶ø position ÊÇ 73
Õâ¸öÎļþÊÇ¶Ô test Õâ¸ö Êý¾Ý¿âµÄ¶þ½øÖÆÈÕÖ¾¼Ç¼£¬¼Ç¼Êý¾Ý±ä»¯µÄµ±Ç°¼Ç¼ÌõÊýÊÇ 73


Ç°ÃæÎÒÃÇ˵¹ý£¬¶þ½øÖÆÈÕÖ¾¼Ç¼×Åij¸öÊý¾Ý¿âËùÓÐÊý¾Ý¼Ç¼±ä»¯µÄ sql ²Ù×÷Óï¾ä£¬Èç insert / update / delete µÈ£¬ÕýÊÇÒòΪÈç´Ë£¬ÔÚ slave ͬ²½ mysql Êý¾Ý¿â²Ù×÷ʱ£¬ÆäʵÊǶÁÈ¡master ÖÐÕâ¸ö mysql-bin.003 ¶þ½øÖÆÈÕÖ¾ÖÐµÄ sql ²Ù×÷£¬Í¬ÔÚ slave ÖÐÖ´ÐÐÕâЩ sql ²Ù×÷£¬ËùÒÔ£¬Í¬²½³É¹¦ÓÐÒÔϼ¸¸öÌõ¼þ
1. master Óë slave ÓÐÏàͬµÄÊý¾Ý¿â±í½á¹¹£¬×îºÃ database name Ò²Ò»Ñù ( ¿ÉÒÔÉèÖÃΪ²»Ò»ÑùµÄ database name)
2. master Óë slave ÓÐÏàͬµÄ³õʼÊý¾Ý¼Ç¼£¬±£Ö¤Í¬²½²Ù×÷¿ªÊ¼ºóÁ½ÕßÊý¾ÝÒ»ÖÂ
3. master ±ØÐëʹÓà bin-log ¶þ½øÖÆÈÕÖ¾¼Ç¼ ( ÍƼö slave ҲʹÓà bin-log )
4. slave ÊÇ´Ó master µÄ bin-log ÊǶÁÈ¡ sql ¼Ç¼À´Í¬²½£¬ËùÒÔ£¬´ÓÄÄÒ»Ìõ log ¿ªÊ¼¶ÁÈ¡ºÜÖØÒª ( ÏÂÃæµÄµÚ 7 Ìõ²Ù×÷£¬¾ÍÊDZ£Ö¤ slave ÄÜÓë master ±£³ÖÏàͬµÄ¼Ç¼¶ÁÈ¡£¬²¢¶ÁÈ¡ÕýÈ·µÄ bin-log ÈÕÖ¾Îļþ )

6.

7. ÔÚ slave Ö´ÐÐÏÂÃæ sql ²Ù×÷

mysql> CHANGE MASTER TO
->¡¡ MASTER_HOST='master_host_name',
->¡¡ MASTER_USER='replication_user_name',
->¡¡ MASTER_PASSWORD='replication_password',
->¡¡ MASTER_LOG_FILE='recorded_log_file_name',
->¡¡ MASTER_LOG_POS=recorded_log_position;

ÕâÀ°Ñ ÉÏÃæµÄ redcorded_log_file_name ¸ÄΪ mysql-bin.003 ¶ø recorded_log_position ¸ÄΪ 73 £¨Ìرð×¢ÒâÓ´£¬ÕâÊǹØϵ¹Ø¼ü£©


×îºó£¬²âÊÔһϰÉ


linux Ï嵀 mysql Óë win2k Ï嵀 mysql ×÷ replication »áÓÐʲô¼¼ÇÉ:


1. ×¢Òâ linux ÏÂÓдóСдÇø·Ö£¬¶ø win2k ÏÂûÓÐ
ËùÒÔ×îºÃʹÓà mysqldump ·½·¨À´ÈÃÁ½¸öÊý¾Ý¿â³õʼ»¯Í¬²½£¬¶ø²»ÄÜʹÓà tar ·½Ê½Ö±½Ó copy Êý¾Ý¿â±íÎļþ
ʹÓà mysql studio À´×÷ database backup Ò²²»´í

2. ×¢Òâ win2k Ï嵀 mysql ÅäÖÃÎļþÊÇ c:\my.ini
Äã¿ÉÒÔʹÓà mysql administrator 0.9 Õâ¸öͼÐͽçÃæµÄʵÓù¤¾ßÀ´ÉèÖÃ

3. Èç¹û win2k Ï嵀 mysql Ϊ master , Ò»¶¨Òª×¢Òâ show master status ÖÐÏÔʾ³öÀ´µÄ½á¹û
4. win2k Ï嵀 mysql Ϊ slave ʱ£¬×¢Òâ²»Òª°Ñͬ²½Ê±¼äÖÜÆÚÉèÖÃÌ«³¤¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 1 ÌõÆÀÂÛ

  1. redapp ÓÚ 2006-08-01 00:33:41·¢±í:

    ѧϰÁ˲»ÉÙ£¬Ð»Ð»