Àí½âMySQL¡ª¡ª¸´ÖÆ(Replication)
ÔÎÄ£º http://www.cnblogs.com/hustcat/archive/2009/12/19/1627525.html
1¡¢¸´ÖƸÅÊö
1.1¡¢¸´Öƽâ¾öµÄÎÊÌâ
Êý¾Ý¸´ÖƼ¼ÊõÓÐÒÔÏÂһЩÌص㣺
(1) Êý¾Ý·Ö²¼
(2) ¸ºÔØƽºâ(load balancing)
(3) ±¸·Ý
(4) ¸ß¿ÉÓÃÐÔ(high availability)ºÍÈÝ´í
1.2¡¢¸´ÖÆÈçºÎ¹¤×÷
´Ó¸ß²ãÀ´¿´£¬¸´ÖÆ·Ö³ÉÈý²½£º
(1) master½«¸Ä±ä¼Ç¼µ½¶þ½øÖÆÈÕÖ¾(binary log)ÖУ¨ÕâЩ¼Ç¼½Ð×ö¶þ½øÖÆÈÕ־ʼþ£¬binary log events£©£»
(2) slave½«masterµÄbinary log events¿½±´µ½ËüµÄÖмÌÈÕÖ¾(relay log)£»
(3) slaveÖØ×öÖмÌÈÕÖ¾ÖеÄʼþ£¬½«¸Ä±ä·´Ó³Ëü×Ô¼ºµÄÊý¾Ý¡£
ÏÂͼÃèÊöÁËÕâÒ»¹ý³Ì£º
¸Ã¹ý³ÌµÄµÚÒ»²¿·Ö¾ÍÊÇmaster¼Ç¼¶þ½øÖÆÈÕÖ¾¡£ÔÚÿ¸öÊÂÎñ¸üÐÂÊý¾ÝÍê³É֮ǰ£¬masterÔÚ¶þÈÕÖ¾¼Ç¼ÕâЩ¸Ä±ä¡£MySQL½«ÊÂÎñ´®ÐеÄдÈë¶þ½øÖÆÈÕÖ¾£¬¼´Ê¹ÊÂÎñÖеÄÓï¾ä¶¼Êǽ»²æÖ´Ðеġ£ÔÚʼþдÈë¶þ½øÖÆÈÕÖ¾Íê³Éºó£¬master֪ͨ´æ´¢ÒýÇæÌá½»ÊÂÎñ¡£
ÏÂÒ»²½¾ÍÊÇslave½«masterµÄbinary log¿½±´µ½Ëü×Ô¼ºµÄÖмÌÈÕÖ¾¡£Ê×ÏÈ£¬slave¿ªÊ¼Ò»¸ö¹¤×÷Ï̡߳ª¡ªI/OÏ̡߳£I/OÏß³ÌÔÚmasterÉÏ´ò¿ªÒ»¸öÆÕͨµÄÁ¬½Ó£¬È»ºó¿ªÊ¼binlog dump process¡£Binlog dump process´ÓmasterµÄ¶þ½øÖÆÈÕÖ¾ÖжÁȡʼþ£¬Èç¹ûÒѾ¸úÉÏmaster£¬Ëü»á˯Ãß²¢µÈ´ýmaster²úÉúеÄʼþ¡£I/OÏ߳̽«ÕâЩʼþдÈëÖмÌÈÕÖ¾¡£
SQL slave thread´¦Àí¸Ã¹ý³ÌµÄ×îºóÒ»²½¡£SQLÏ̴߳ÓÖмÌÈÕÖ¾¶Áȡʼþ£¬¸üÐÂslaveµÄÊý¾Ý£¬Ê¹ÆäÓëmasterÖеÄÊý¾ÝÒ»Ö¡£Ö»Òª¸ÃÏß³ÌÓëI/OÏ̱߳£³ÖÒ»Ö£¬ÖмÌÈÕ־ͨ³£»áλÓÚOSµÄ»º´æÖУ¬ËùÒÔÖмÌÈÕÖ¾µÄ¿ªÏúºÜС¡£
´ËÍ⣬ÔÚmasterÖÐÒ²ÓÐÒ»¸ö¹¤×÷Ị̈߳ººÍÆäËüMySQLµÄÁ¬½ÓÒ»Ñù£¬slaveÔÚmasterÖдò¿ªÒ»¸öÁ¬½ÓÒ²»áʹµÃmaster¿ªÊ¼Ò»¸öÏ̡߳£¸´Öƹý³ÌÓÐÒ»¸öºÜÖØÒªµÄÏÞÖÆ¡ª¡ª¸´ÖÆÔÚslaveÉÏÊÇ´®Ðл¯µÄ£¬Ò²¾ÍÊÇ˵masterÉϵIJ¢ÐиüвÙ×÷²»ÄÜÔÚslaveÉϲ¢ÐвÙ×÷¡£
2¡¢ÌåÑéMySQL¸´ÖÆ
MySQL¿ªÊ¼¸´ÖÆÊǺܼòµ¥µÄ¹ý³Ì£¬²»¹ý£¬¸ù¾ÝÌض¨µÄÓ¦Óó¡¾°£¬¶¼»áÔÚ»ù±¾µÄ²½ÖèÉÏÓÐһЩ±ä»¯¡£×î¼òµ¥µÄ³¡¾°¾ÍÊÇÒ»¸öа²×°µÄmasterºÍslave£¬´Ó¸ß²ãÀ´¿´£¬Õû¸ö¹ý³ÌÈçÏ£º
(1)ÔÚÿ¸ö·þÎñÆ÷ÉÏ´´½¨Ò»¸ö¸´ÖÆÕʺţ»
(2)ÅäÖÃmasterºÍslave£»
(3)SlaveÁ¬½Ómaster¿ªÊ¼¸´ÖÆ¡£
2.1¡¢´´½¨¸´ÖÆÕʺÅ
ÿ¸öslaveʹÓñê×¼µÄMySQLÓû§ÃûºÍÃÜÂëÁ¬½Ómaster¡£½øÐи´ÖƲÙ×÷µÄÓû§»áÊÚÓèREPLICATION SLAVEȨÏÞ¡£Óû§ÃûµÄÃÜÂ붼»á´æ´¢ÔÚÎı¾Îļþmaster.infoÖС£¼ÙÈ磬ÄãÏë´´½¨replÓû§£¬ÈçÏ£º
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.*
-> TO repl@'192.168.0.%' IDENTIFIED BY 'p4ssword';
2.2¡¢ÅäÖÃmaster
½ÓÏÂÀ´¶Ômaster½øÐÐÅäÖ㬰üÀ¨´ò¿ª¶þ½øÖÆÈÕÖ¾£¬Ö¸¶¨Î¨Ò»µÄservr ID¡£ÀýÈ磬ÔÚÅäÖÃÎļþ¼ÓÈëÈçÏÂÖµ£º
[mysqld]
log-bin=mysql-bin
server-id=10
ÖØÆômaster£¬ÔËÐÐSHOW MASTER STATUS£¬Êä³öÈçÏ£º
2.3¡¢ÅäÖÃslave
SlaveµÄÅäÖÃÓëmasterÀàËÆ£¬ÄãͬÑùÐèÒªÖØÆôslaveµÄMySQL¡£ÈçÏ£º
log_bin = mysql-bin
server_id = 2
relay_log = mysql-relay-bin
log_slave_updates = 1
read_only = 1
server_idÊDZØÐëµÄ£¬¶øÇÒΨһ¡£slaveûÓбØÒª¿ªÆô¶þ½øÖÆÈÕÖ¾£¬µ«ÊÇÔÚһЩÇé¿öÏ£¬±ØÐëÉèÖã¬ÀýÈ磬Èç¹ûslaveΪÆäËüslaveµÄmaster£¬±ØÐëÉèÖÃbin_log¡£ÔÚÕâÀÎÒÃÇ¿ªÆôÁ˶þ½øÖÆÈÕÖ¾£¬¶øÇÒÏÔʾµÄÃüÃû(ĬÈÏÃû³ÆΪhostname£¬µ«ÊÇ£¬Èç¹ûhostname¸Ä±äÔò»á³öÏÖÎÊÌâ)¡£
relay_logÅäÖÃÖмÌÈÕÖ¾£¬log_slave_updates±íʾslave½«¸´ÖÆʼþд½ø×Ô¼ºµÄ¶þ½øÖÆÈÕÖ¾(ºóÃæ»á¿´µ½ËüµÄÓô¦)¡£
ÓÐЩÈË¿ªÆôÁËslaveµÄ¶þ½øÖÆÈÕÖ¾£¬È´Ã»ÓÐÉèÖÃlog_slave_updates£¬È»ºó²é¿´slaveµÄÊý¾ÝÊÇ·ñ¸Ä±ä£¬ÕâÊÇÒ»ÖÖ´íÎóµÄÅäÖá£ËùÒÔ£¬¾¡Á¿Ê¹ÓÃread_only£¬Ëü·ÀÖ¹¸Ä±äÊý¾Ý(³ýÁËÌØÊâµÄÏß³Ì)¡£µ«ÊÇ£¬read_only²¢ÊǺÜʵÓã¬ÌرðÊÇÄÇЩÐèÒªÔÚslaveÉÏ´´½¨±íµÄÓ¦Óá£
2.4¡¢Æô¶¯slave
½ÓÏÂÀ´¾ÍÊÇÈÃslaveÁ¬½Ómaster£¬²¢¿ªÊ¼ÖØ×ömaster¶þ½øÖÆÈÕÖ¾ÖеÄʼþ¡£Äã²»Ó¦¸ÃÓÃÅäÖÃÎļþ½øÐиòÙ×÷£¬¶øÓ¦¸ÃʹÓÃCHANGE MASTER TOÓï¾ä£¬¸ÃÓï¾ä¿ÉÒÔÍêÈ«È¡´ú¶ÔÅäÖÃÎļþµÄÐ޸ģ¬¶øÇÒËü¿ÉÒÔΪslaveÖ¸¶¨²»Í¬µÄmaster£¬¶ø²»ÐèҪֹͣ·þÎñÆ÷¡£ÈçÏ£º
mysql> CHANGE MASTER TO MASTER_HOST='server1',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='p4ssword',
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=0;
MASTER_LOG_POSµÄֵΪ0£¬ÒòΪËüÊÇÈÕÖ¾µÄ¿ªÊ¼Î»Öá£È»ºó£¬Äã¿ÉÒÔÓÃSHOW SLAVE STATUSÓï¾ä²é¿´slaveµÄÉèÖÃÊÇ·ñÕýÈ·£º
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: server1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 4
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
...omitted...
Seconds_Behind_Master: NULL
Slave_IO_State, Slave_IO_Running, ºÍSlave_SQL_Running±íÃ÷slave»¹Ã»ÓпªÊ¼¸´Öƹý³Ì¡£ÈÕÖ¾µÄλÖÃΪ4¶ø²»ÊÇ0£¬ÕâÊÇÒòΪ0Ö»ÊÇÈÕÖ¾ÎļþµÄ¿ªÊ¼Î»Ö㬲¢²»ÊÇÈÕ־λÖá£Êµ¼ÊÉÏ£¬MySQLÖªµÀµÄµÚÒ»¸öʼþµÄλÖÃÊÇ4¡£
ΪÁË¿ªÊ¼¸´ÖÆ£¬Äã¿ÉÒÔÔËÐУº
mysql> START SLAVE;
ÔËÐÐSHOW SLAVE STATUS²é¿´Êä³ö½á¹û£º
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: server1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 164
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 164
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...omitted...
Seconds_Behind_Master: 0
×¢Ò⣬slaveµÄI/OºÍSQLÏ̶߳¼ÒѾ¿ªÊ¼ÔËÐУ¬¶øÇÒSeconds_Behind_Master²»ÔÙÊÇNULL¡£ÈÕÖ¾µÄλÖÃÔö¼ÓÁË£¬Òâζ×ÅһЩʼþ±»»ñÈ¡²¢Ö´ÐÐÁË¡£Èç¹ûÄãÔÚmasterÉϽøÐÐÐ޸ģ¬Äã¿ÉÒÔÔÚslaveÉÏ¿´µ½¸÷ÖÖÈÕÖ¾ÎļþµÄλÖõı仯£¬Í¬Ñù£¬ÄãÒ²¿ÉÒÔ¿´µ½Êý¾Ý¿âÖÐÊý¾ÝµÄ±ä»¯¡£
Äã¿É²é¿´masterºÍslaveÉÏÏ̵߳Ä״̬¡£ÔÚmasterÉÏ£¬Äã¿ÉÒÔ¿´µ½slaveµÄI/OÏ̴߳´½¨µÄÁ¬½Ó£º
mysql> show processlist \G *************************** 1. row *************************** Id: 1 User: root Host: localhost:2096 db: test Command: Query Time: 0 State: NULL Info: show processlist *************************** 2. row *************************** Id: 2 User: repl Host: localhost:2144 db: NULL Command: Binlog Dump Time: 1838 State: Has sent all binlog to slave; waiting for binlog to be updated Info: NULL 2 rows in set (0.00 sec) |
ÐÐ2Ϊ´¦ÀíslaveµÄI/OÏ̵߳ÄÁ¬½Ó¡£
ÔÚslaveÉÏÔËÐиÃÓï¾ä£º
mysql> show processlist \G *************************** 1. row *************************** Id: 1 User: system user Host: db: NULL Command: Connect Time: 2291 State: Waiting for master to send event Info: NULL *************************** 2. row *************************** Id: 2 User: system user Host: db: NULL Command: Connect Time: 1852 State: Has read all relay log; waiting for the slave I/O thread to update it Info: NULL *************************** 3. row *************************** Id: 5 User: root Host: localhost:2152 db: test Command: Query Time: 0 State: NULL Info: show processlist 3 rows in set (0.00 sec) |
ÐÐ1ΪI/OÏß³Ì״̬£¬ÐÐ2ΪSQLÏß³Ì״̬¡£
2.5¡¢´ÓÁíÒ»¸ömaster³õʼ»¯slave
Ç°ÃæÌÖÂ۵ļÙÉèÄãÊÇа²×°µÄmasterºÍslave£¬ËùÒÔ£¬slaveÓëmasterÓÐÏàͬµÄÊý¾Ý¡£µ«ÊÇ£¬´ó¶àÊýÇé¿öÈ´²»ÊÇÕâÑùµÄ£¬ÀýÈ磬ÄãµÄmaster¿ÉÄÜÒѾÔËÐкܾÃÁË£¬¶øÄãÏë¶Ôа²×°µÄslave½øÐÐÊý¾Ýͬ²½£¬ÉõÖÁËüûÓÐmasterµÄÊý¾Ý¡£
´Ëʱ£¬Óм¸ÖÖ·½·¨¿ÉÒÔʹslave´ÓÁíÒ»¸ö·þÎñ¿ªÊ¼£¬ÀýÈ磬´Ómaster¿½±´Êý¾Ý£¬´ÓÁíÒ»¸öslave¿Ë¡£¬´Ó×î½üµÄ±¸·Ý¿ªÊ¼Ò»¸öslave¡£SlaveÓëmasterͬ²½Ê±£¬ÐèÒªÈýÑù¶«Î÷£º
(1)masterµÄij¸öʱ¿ÌµÄÊý¾Ý¿ìÕÕ£»
(2)masterµ±Ç°µÄÈÕÖ¾Îļþ¡¢ÒÔ¼°Éú³É¿ìÕÕʱµÄ×Ö½ÚÆ«ÒÆ¡£ÕâÁ½¸öÖµ¿ÉÒÔ½Ð×öÈÕÖ¾Îļþ×ø±ê(log file coordinate)£¬ÒòΪËüÃÇÈ·¶¨ÁËÒ»¸ö¶þ½øÖÆÈÕÖ¾µÄλÖã¬Äã¿ÉÒÔÓÃSHOW MASTER STATUSÃüÁîÕÒµ½ÈÕÖ¾ÎļþµÄ×ø±ê£»
(3)masterµÄ¶þ½øÖÆÈÕÖ¾Îļþ¡£
¿ÉÒÔͨ¹ýÒÔϼ¸Öз½·¨À´¿Ë¡һ¸öslave£º
(1) À俽±´(cold copy)
Í£Ö¹master£¬½«masterµÄÎļþ¿½±´µ½slave£»È»ºóÖØÆômaster¡£È±µãºÜÃ÷ÏÔ¡£
(2) ÈÈ¿½±´(warm copy)
Èç¹ûÄã½öʹÓÃMyISAM±í£¬Äã¿ÉÒÔʹÓÃmysqlhotcopy¿½±´£¬¼´Ê¹·þÎñÆ÷ÕýÔÚÔËÐС£
(3) ʹÓÃmysqldump
ʹÓÃmysqldumpÀ´µÃµ½Ò»¸öÊý¾Ý¿ìÕÕ¿É·ÖΪÒÔϼ¸²½£º
<1>Ëø±í£ºÈç¹ûÄ㻹ûÓÐËø±í£¬ÄãÓ¦¸Ã¶Ô±í¼ÓËø£¬·ÀÖ¹ÆäËüÁ¬½ÓÐÞ¸ÄÊý¾Ý¿â£¬·ñÔò£¬ÄãµÃµ½µÄÊý¾Ý¿ÉÒÔÊDz»Ò»Öµġ£ÈçÏ£º
mysql> FLUSH TABLES WITH READ LOCK;
<2>ÔÚÁíÒ»¸öÁ¬½ÓÓÃmysqldump´´½¨Ò»¸öÄãÏë½øÐи´ÖƵÄÊý¾Ý¿âµÄת´¢£º
shell> mysqldump --all-databases --lock-all-tables >dbdump.db
<3>¶Ô±íÊÍ·ÅËø¡£
mysql> UNLOCK TABLES;
3¡¢ÉîÈ븴ÖÆ
ÒѾÌÖÂÛÁ˹ØÓÚ¸´ÖƵÄһЩ»ù±¾¶«Î÷£¬ÏÂÃæÉîÈëÌÖÂÛһϸ´ÖÆ¡£
3.1¡¢»ùÓÚÓï¾äµÄ¸´ÖÆ(Statement-Based Replication)
MySQL 5.0¼°Ö®Ç°µÄ°æ±¾½öÖ§³Ö»ùÓÚÓï¾äµÄ¸´ÖÆ£¨Ò²½Ð×öÂß¼¸´ÖÆ£¬logical replication£©£¬ÕâÔÚÊý¾Ý¿â²¢²»³£¼û¡£master¼Ç¼Ï¸ıäÊý¾ÝµÄ²éѯ£¬È»ºó£¬slave´ÓÖмÌÈÕÖ¾ÖжÁȡʼþ£¬²¢Ö´ÐÐËü£¬ÕâЩSQLÓï¾äÓëmasterÖ´ÐеÄÓï¾äÒ»Ñù¡£
ÕâÖÖ·½Ê½µÄÓŵã¾ÍÊÇʵÏÖ¼òµ¥¡£´ËÍ⣬»ùÓÚÓï¾äµÄ¸´ÖƵĶþ½øÖÆÈÕÖ¾¿ÉÒԺܺõĽøÐÐѹËõ£¬¶øÇÒÈÕÖ¾µÄÊý¾ÝÁ¿Ò²½ÏС£¬Õ¼Óôø¿íÉÙ¡ª¡ªÀýÈ磬һ¸ö¸üÐÂGBµÄÊý¾ÝµÄ²éѯ½öÐèÒª¼¸Ê®¸ö×ֽڵĶþ½øÖÆÈÕÖ¾¡£¶ømysqlbinlog¶ÔÓÚ»ùÓÚÓï¾äµÄÈÕÖ¾´¦ÀíÊ®·Ö·½±ã¡£
µ«ÊÇ£¬»ùÓÚÓï¾äµÄ¸´ÖƲ¢²»ÊÇÏñËü¿´ÆðÀ´ÄÇô¼òµ¥£¬ÒòΪһЩ²éѯÓï¾äÒÀÀµÓÚmasterµÄÌض¨Ìõ¼þ£¬ÀýÈ磬masterÓëslave¿ÉÄÜÓв»Í¬µÄʱ¼ä¡£ËùÒÔ£¬MySQLµÄ¶þ½øÖÆÈÕÖ¾µÄ¸ñʽ²»½ö½öÊDzéѯÓï¾ä£¬»¹°üÀ¨Ò»Ð©ÔªÊý¾ÝÐÅÏ¢£¬ÀýÈ磬µ±Ç°µÄʱ¼ä´Á¡£¼´Ê¹Èç´Ë£¬»¹ÊÇÓÐһЩÓï¾ä£¬±ÈÈ磬CURRENT USERº¯Êý£¬²»ÄÜÕýÈ·µÄ½øÐи´ÖÆ¡£´ËÍ⣬´æ´¢¹ý³ÌºÍ´¥·¢Æ÷Ò²ÊÇÒ»¸öÎÊÌâ¡£
ÁíÍâÒ»¸öÎÊÌâ¾ÍÊÇ»ùÓÚÓï¾äµÄ¸´ÖƱØÐëÊÇ´®Ðл¯µÄ¡£ÕâÒªÇó´óÁ¿ÌØÊâµÄ´úÂ룬ÅäÖã¬ÀýÈçInnoDBµÄnext-keyËøµÈ¡£²¢²»ÊÇËùÓеĴ洢ÒýÇ涼֧³Ö»ùÓÚÓï¾äµÄ¸´ÖÆ¡£
3.2¡¢»ùÓڼǼµÄ¸´ÖÆ(Row-Based Replication)
MySQLÔö¼Ó»ùÓڼǼµÄ¸´ÖÆ£¬ÔÚ¶þ½øÖÆÈÕÖ¾ÖмǼÏÂʵ¼ÊÊý¾ÝµÄ¸Ä±ä£¬ÕâÓëÆäËüһЩDBMSµÄʵÏÖ·½Ê½ÀàËÆ¡£ÕâÖÖ·½Ê½ÓÐÓŵ㣬ҲÓÐȱµã¡£Óŵã¾ÍÊÇ¿ÉÒÔ¶ÔÈκÎÓï¾ä¶¼ÄÜÕýÈ·¹¤×÷£¬Ò»Ð©Óï¾äµÄЧÂʸü¸ß¡£Ö÷ÒªµÄȱµã¾ÍÊǶþ½øÖÆÈÕÖ¾¿ÉÄÜ»áºÜ´ó£¬¶øÇÒ²»Ö±¹Û£¬ËùÒÔ£¬Äã²»ÄÜʹÓÃmysqlbinlogÀ´²é¿´¶þ½øÖÆÈÕÖ¾¡£
¶ÔÓÚһЩÓï¾ä£¬»ùÓڼǼµÄ¸´ÖÆÄܹ»¸üÓÐЧµÄ¹¤×÷£¬È磺
mysql> INSERT INTO summary_table(col1, col2, sum_col3)
-> SELECT col1, col2, sum(col3)
-> FROM enormous_table
-> GROUP BY col1, col2;
¼ÙÉ裬ֻÓÐÈýÖÖΨһµÄcol1ºÍcol2µÄ×éºÏ£¬µ«ÊÇ£¬¸Ã²éѯ»áɨÃèÔ±íµÄÐí¶àÐУ¬È´½ö·µ»ØÈýÌõ¼Ç¼¡£´Ëʱ£¬»ùÓڼǼµÄ¸´ÖÆЧÂʸü¸ß¡£
ÁíÒ»·½Ã棬ÏÂÃæµÄÓï¾ä£¬»ùÓÚÓï¾äµÄ¸´ÖƸüÓÐЧ£º
mysql> UPDATE enormous_table SET col1 = 0;
´ËʱʹÓûùÓڼǼµÄ¸´ÖÆ´ú¼Û»á·Ç³£¸ß¡£ÓÉÓÚÁ½ÖÖ·½Ê½²»ÄܶÔËùÓÐÇé¿ö¶¼ÄܺܺõĴ¦Àí£¬ËùÒÔ£¬MySQL 5.1Ö§³ÖÔÚ»ùÓÚÓï¾äµÄ¸´ÖƺͻùÓڼǼµÄ¸´ÖÆ֮ǰ¶¯Ì¬½»»»¡£Äã¿ÉÒÔͨ¹ýÉèÖÃsession±äÁ¿binlog_formatÀ´½øÐпØÖÆ¡£
3.3¡¢¸´ÖÆÏà¹ØµÄÎļþ
³ýÁ˶þ½øÖÆÈÕÖ¾ºÍÖмÌÈÕÖ¾ÎļþÍ⣬»¹ÓÐÆäËüһЩÓ븴ÖÆÏà¹ØµÄÎļþ¡£ÈçÏ£º
(1)mysql-bin.index
·þÎñÆ÷Ò»µ©¿ªÆô¶þ½øÖÆÈÕÖ¾£¬»á²úÉúÒ»¸öÓë¶þÈÕÖ¾ÎļþͬÃû£¬µ«ÊÇÒÔ.index½áβµÄÎļþ¡£ËüÓÃÓÚ¸ú×Ù´ÅÅÌÉÏ´æÔÚÄÄЩ¶þ½øÖÆÈÕÖ¾Îļþ¡£MySQLÓÃËüÀ´¶¨Î»¶þ½øÖÆÈÕÖ¾Îļþ¡£ËüµÄÄÚÈÝÈçÏÂ(ÎҵĻúÆ÷ÉÏ)£º
(2)mysql-relay-bin.index
¸ÃÎļþµÄ¹¦ÄÜÓëmysql-bin.indexÀàËÆ£¬µ«ÊÇËüÊÇÕë¶ÔÖмÌÈÕÖ¾£¬¶ø²»ÊǶþ½øÖÆÈÕÖ¾¡£ÄÚÈÝÈçÏ£º
.\mysql-02-relay-bin.000017
.\mysql-02-relay-bin.000018
(3)master.info
±£´æmasterµÄÏà¹ØÐÅÏ¢¡£²»ÒªÉ¾³ýËü£¬·ñÔò£¬slaveÖØÆôºó²»ÄÜÁ¬½Ómaster¡£ÄÚÈÝÈçÏÂ(ÎҵĻúÆ÷ÉÏ)£º
I/OÏ̸߳üÐÂmaster.infoÎļþ£¬ÄÚÈÝÈçÏÂ(ÎҵĻúÆ÷ÉÏ)£º
.\mysql-02-relay-bin.000019 254 mysql-01-bin.000010 286 0 52813 |
(4)relay-log.info
°üº¬slaveÖе±Ç°¶þ½øÖÆÈÕÖ¾ºÍÖмÌÈÕÖ¾µÄÐÅÏ¢¡£
3.4¡¢·¢Ë͸´ÖÆʼþµ½ÆäËüslave
µ±ÉèÖÃlog_slave_updatesʱ£¬Äã¿ÉÒÔÈÃslave°çÑÝÆäËüslaveµÄmaster¡£´Ëʱ£¬slave°ÑSQLÏß³ÌÖ´ÐеÄʼþд½øÐÐ×Ô¼ºµÄ¶þ½øÖÆÈÕÖ¾(binary log)£¬È»ºó£¬ËüµÄslave¿ÉÒÔ»ñÈ¡ÕâЩʼþ²¢Ö´ÐÐËü¡£ÈçÏ£º
3.5¡¢¸´ÖƹýÂË(Replication Filters)
¸´ÖƹýÂË¿ÉÒÔÈÃÄãÖ»¸´ÖÆ·þÎñÆ÷ÖеÄÒ»²¿·ÖÊý¾Ý£¬ÓÐÁ½ÖÖ¸´ÖƹýÂË£ºÔÚmasterÉϹýÂ˶þ½øÖÆÈÕÖ¾ÖеÄʼþ£»ÔÚslaveÉϹýÂËÖмÌÈÕÖ¾ÖеÄʼþ¡£ÈçÏ£º
4¡¢¸´ÖƵij£ÓÃÍØÆ˽ṹ
¸´ÖƵÄÌåϵ½á¹¹ÓÐÒÔÏÂһЩ»ù±¾ÔÔò£º
(1) ÿ¸öslaveÖ»ÄÜÓÐÒ»¸ömaster£»
(2) ÿ¸öslaveÖ»ÄÜÓÐÒ»¸öΨһµÄ·þÎñÆ÷ID£»
(3) ÿ¸ömaster¿ÉÒÔÓкܶàslave£»
(4) Èç¹ûÄãÉèÖÃlog_slave_updates£¬slave¿ÉÒÔÊÇÆäËüslaveµÄmaster£¬´Ó¶øÀ©É¢masterµÄ¸üС£
MySQL²»Ö§³Ö¶àÖ÷·þÎñÆ÷¸´ÖÆ(Multimaster Replication)¡ª¡ª¼´Ò»¸öslave¿ÉÒÔÓжà¸ömaster¡£µ«ÊÇ£¬Í¨¹ýһЩ¼òµ¥µÄ×éºÏ£¬ÎÒÃÇÈ´¿ÉÒÔ½¨Á¢Áé»î¶øÇ¿´óµÄ¸´ÖÆÌåϵ½á¹¹¡£
4.1¡¢µ¥Ò»masterºÍ¶àslave
ÓÉÒ»¸ömasterºÍÒ»¸öslave×é³É¸´ÖÆϵͳÊÇ×î¼òµ¥µÄÇé¿ö¡£SlaveÖ®¼ä²¢²»Ï໥ͨÐÅ£¬Ö»ÄÜÓëmaster½øÐÐͨÐÅ¡£ÈçÏ£º
Èç¹ûд²Ù×÷½ÏÉÙ£¬¶ø¶Á²Ù×÷ºÜʱ£¬¿ÉÒÔ²ÉÈ¡ÕâÖֽṹ¡£Äã¿ÉÒÔ½«¶Á²Ù×÷·Ö²¼µ½ÆäËüµÄslave£¬´Ó¶ø¼õСmasterµÄѹÁ¦¡£µ«ÊÇ£¬µ±slaveÔö¼Óµ½Ò»¶¨ÊýÁ¿Ê±£¬slave¶ÔmasterµÄ¸ºÔØÒÔ¼°ÍøÂç´ø¿í¶¼»á³ÉΪһ¸öÑÏÖصÄÎÊÌâ¡£
ÕâÖֽṹËäÈ»¼òµ¥£¬µ«ÊÇ£¬ËüÈ´·Ç³£Áé»î£¬×ã¹»Âú×ã´ó¶àÊýÓ¦ÓÃÐèÇó¡£Ò»Ð©½¨Ò飺
(1) ²»Í¬µÄslave°çÑݲ»Í¬µÄ×÷ÓÃ(ÀýÈçʹÓò»Í¬µÄË÷Òý£¬»òÕß²»Í¬µÄ´æ´¢ÒýÇæ)£»
(2) ÓÃÒ»¸öslave×÷Ϊ±¸ÓÃmaster£¬Ö»½øÐи´ÖÆ£»
(3) ÓÃÒ»¸öÔ¶³ÌµÄslave£¬ÓÃÓÚÔÖÄѻָ´£»
4.2¡¢Ö÷¶¯Ä£Ê½µÄMaster-Master(Master-Master in Active-Active Mode)
Master-Master¸´ÖƵÄÁ½Ì¨·þÎñÆ÷£¬¼ÈÊÇmaster£¬ÓÖÊÇÁíһ̨·þÎñÆ÷µÄslave¡£Èçͼ£º
Ö÷¶¯µÄMaster-Master¸´ÖÆÓÐһЩÌØÊâµÄÓô¦¡£ÀýÈ磬µØÀíÉÏ·Ö²¼µÄÁ½¸ö²¿·Ö¶¼ÐèÒª×Ô¼ºµÄ¿ÉдµÄÊý¾Ý¸±±¾¡£ÕâÖֽṹ×î´óµÄÎÊÌâ¾ÍÊǸüгåÍ»¡£¼ÙÉèÒ»¸ö±íÖ»ÓÐÒ»ÐÐ(Ò»ÁÐ)µÄÊý¾Ý£¬ÆäֵΪ1£¬Èç¹ûÁ½¸ö·þÎñÆ÷·Ö±ðͬʱִÐÐÈçÏÂÓï¾ä£º
ÔÚµÚÒ»¸ö·þÎñÆ÷ÉÏÖ´ÐУº
mysql> UPDATE tbl SET col=col + 1;
ÔÚµÚ¶þ¸ö·þÎñÆ÷ÉÏÖ´ÐУº
mysql> UPDATE tbl SET col=col * 2;
ÄÇô½á¹ûÊǶàÉÙÄØ£¿Ò»Ì¨·þÎñÆ÷ÊÇ4£¬ÁíÒ»¸ö·þÎñÆ÷ÊÇ3£¬µ«ÊÇ£¬Õâ²¢²»»á²úÉú´íÎó¡£
ʵ¼ÊÉÏ£¬MySQL²¢²»Ö§³ÖÆäËüһЩDBMSÖ§³ÖµÄ¶àÖ÷·þÎñÆ÷¸´ÖÆ(Multimaster Replication)£¬ÕâÊÇMySQLµÄ¸´Öƹ¦ÄܴܺóµÄÒ»¸öÏÞÖÆ(¶àÖ÷·þÎñÆ÷µÄÄѵãÔÚÓÚ½â¾ö¸üгåÍ»)£¬µ«ÊÇ£¬Èç¹ûÄãʵÔÚÓÐÕâÖÖÐèÇó£¬Äã¿ÉÒÔ²ÉÓÃMySQL Cluster£¬ÒÔ¼°½«ClusterºÍReplication½áºÏÆðÀ´£¬¿ÉÒÔ½¨Á¢Ç¿´óµÄ¸ßÐÔÄܵÄÊý¾Ý¿âƽ̨¡£µ«ÊÇ£¬¿ÉÒÔͨ¹ýÆäËüһЩ·½Ê½À´Ä£ÄâÕâÖÖ¶àÖ÷·þÎñÆ÷µÄ¸´ÖÆ¡£
4.3¡¢Ö÷¶¯-±»¶¯Ä£Ê½µÄMaster-Master(Master-Master in Active-Passive Mode)
ÕâÊÇmaster-master½á¹¹±ä»¯¶øÀ´µÄ£¬Ëü±ÜÃâÁËM-MµÄȱµã£¬Êµ¼ÊÉÏ£¬ÕâÊÇÒ»ÖÖ¾ßÓÐÈÝ´íºÍ¸ß¿ÉÓÃÐÔµÄϵͳ¡£ËüµÄ²»Í¬µãÔÚÓÚÆäÖÐÒ»¸ö·þÎñÖ»ÄܽøÐÐÖ»¶Á²Ù×÷¡£Èçͼ£º
4.4¡¢´ø´Ó·þÎñÆ÷µÄMaster-Master½á¹¹(Master-Master with Slaves)
ÕâÖֽṹµÄÓŵã¾ÍÊÇÌṩÁËÈßÓà¡£ÔÚµØÀíÉÏ·Ö²¼µÄ¸´Öƽṹ£¬Ëü²»´æÔÚµ¥Ò»½Úµã¹ÊÕÏÎÊÌ⣬¶øÇÒ»¹¿ÉÒÔ½«¶ÁÃܼ¯Ð͵ÄÇëÇó·Åµ½slaveÉÏ¡£
Ö÷Òª²Î¿¼£º¡¶High Performance MySQL¡·
ÍƼöÐÅÏ¢
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
ÆÀÂÛ