使ç¨æ°æ®åºåæ¥çæ¹æ³è§£å³æ°æ®ä¼ è¾çé®é¢ï¼ä½å 为使ç¨mysql 5.5çæ¬æ¶ï¼è®¾ç½®ç主ä»å¤å¶å¨æ°æ®éè¾å¤§æè ç½ç»æ¥å¡çæ¶å延è¿ä¼æ´é«ï¼èä¸ç»è¿æ¥èµæï¼èçæ¬æ¯æ æ³ä»æ ¹æ¬ä¸æ¹åè¿ä¸ªé®é¢çãæè¿äºè§£äºMySQL 5.7çæ¬çç¹æ§ï¼ç¥éäº5.7çæ¬çåºäºç»æ交ç并è¡å¤å¶å¯ä»¥æ´å¤§çæ¹åè¿ä¸ªé®é¢ãæ¥ä¸æ¥å¯¹ç¸å ³çå 容è¿è¡è¯¦ç»çæ»ç»åæ¦æ¬ã
Â
ä¸ãMysql 5.6 åæ´ä½çæ¬ç主ä»å¤å¶
Â
      ï¼1ï¼ å¨MySQL 5.6ä¹åççæ¬éï¼æä¸ä¸ªçº¿ç¨åä¸ï¼é½æ¯å线ç¨ï¼Binlog Dumpï¼ä¸»ï¼ ----->IO Thread ï¼ä»ï¼ -----> SQL Threadï¼ä»ï¼ãå ¶ä¸IO Thread线ç¨è´è´£ä»ä¸»åºæ¿binlog并åå°relaylogï¼sql_thread 线ç¨è´è´£è¯»relaylog并æ§è¡ãå¤å¶åºç°å»¶è¿ä¸è¬åºå¨ä¸¤ä¸ªå°æ¹
         aãSQL线ç¨å¿ä¸è¿æ¥ï¼å¯è½éè¦åºç¨æ°æ®éè¾å¤§ï¼å¯è½åä»åºæ¬èº«çä¸äºæä½æéåèµæºçå²çªï¼ï¼
        è½ç¶ä¸»åºå¯ä»¥å¹¶ååï¼ä½Slave_SQL_Running线ç¨ä¸å¯ä»¥å¹¶ååï¼ä¸»è¦åå ï¼ã
         bãç½ç»æå¨å¯¼è´IO线ç¨å¤å¶å»¶è¿ï¼æ¬¡è¦åå ï¼ã
Â
      ï¼2ï¼ MySQLä»5.6å¼å§æäºå¤çº¿ç¨çæ¦å¿µï¼å¯ä»¥å¹¶åè¿åæ°æ®ï¼å³å¹¶è¡å¤å¶ææ¯ãå¤çº¿ç¨çæ路就æ¯æsql_thread åæåå线ç¨ï¼ç¶åç±ä¸ç»worker_threadæ¥è´è´£æ§è¡ãMySQL 5.6ä¸ï¼è®¾ç½®åæ°slave_parallel_workers = 4ï¼å³å¯æ4个SQL Threadï¼coordinator线ç¨ï¼æ¥è¿è¡å¹¶è¡å¤å¶ï¼å ¶ç¶æ为ï¼Waiting for an evant from Coordinatorãä½æ¯å ¶å¹¶è¡åªæ¯åºäºSchemaçï¼ä¹å°±æ¯åºäºåºçãå¦ææ°æ®åºå®ä¾ä¸åå¨å¤ä¸ªSchemaï¼è¿æ ·è®¾ç½®å¯¹äºSlaveå¤å¶çé度å¯ä»¥ææ¯è¾å¤§çæåãé常æ åµä¸ååºå¤è¡¨æ¯æ´å¸¸è§çä¸ç§æ å½¢ï¼æ以åºäºåºç并åä¸è¬æ¯æ²¡ä»ä¹ç¨çï¼ä¸è¿å ¶ä¹æä¸å®ä¼å¿ï¼
         对äºå¯ä»¥æ表ååçåºæ¯ï¼å¯ä»¥éè¿å°è¡¨è¿å°ä¸åçåºï¼æ¥åºç¨æ¤çç¥ï¼æå¯æä½æ§ã
Â
äºãMysql 5.7 ç主ä»å¤å¶
Â
      ï¼1ï¼æ°çæ¬å¢å äºä¸ç§ç±»åï¼åæäºä¸¤ç§ç±»å
       1ãDATABASE åºäºåºç并è¡å¤å¶ ï¼ æ¯ä¸ªæ°æ®åºå¯¹åºä¸ä¸ªå¤å¶çº¿ç¨ï¼5.6çæ¬å°±æäºï¼ç¶å¹¶åµï¼ï¼
       2ãLOGICAL_CLOCK åºäºç»æ交ç并è¡å¤å¶æ¹å¼ï¼åä¸ä¸ªæ°æ®åºä¸å¯ä»¥æå¤ä¸ªçº¿ç¨ï¼å¯¹å¤§å¤æ°æ°æ®åºæ´å®ç¨ï¼ã
         对äºç¬¬äºç§ç±»åï¼è®¾ç½®åæ°slave_parallel_workers>0并ä¸global.slave_parallel_typeï¼âLOGICAL_CLOCKâï¼å³å¯æ¯æä¸ä¸ªschemaä¸ï¼slave_parallel_workersä¸çworker线ç¨å¹¶åæ§è¡relay logä¸ä¸»åºæ交çäºå¡ã
       ï¼2ï¼ä»MySQLå®æ¹æ件çï¼å ¶å¹¶è¡å¤å¶çè¦å®ç°çç®æ æ¯æ¯æ表级ç并è¡å¤å¶åè¡çº§ç并è¡å¤å¶ï¼è¡çº§ç并è¡å¤å¶éè¿è§£æROWæ ¼å¼çäºè¿å¶æ¥å¿çæ¹å¼æ¥å®æã
       ï¼3ï¼5.7çæ¬åºäºç»æ交ç并è¡å¤å¶æ ¸å¿ææ³æ¯ï¼ä¸ä¸ªç»æ交çäºå¡é½æ¯å¯ä»¥å¹¶è¡åæ¾çï¼å 为è¿äºäºå¡é½å·²è¿å ¥å°äºå¡çprepareé¶æ®µï¼å说æäºå¡ä¹é´æ²¡æä»»ä½å²çªï¼å¦åå°±ä¸å¯è½æ交ï¼ã
Â
ä¸ãå ·ä½ç设置ä¸æä½
Â
1ãæä½æ¡ä»¶
     主æå¡å¨ï¼ï¼1ï¼ç³»ç»ï¼windows 7               ï¼2ï¼æ°æ®åºï¼MySQL 5.7.18
     ä»æå¡å¨ï¼èææºï¼ï¼ï¼1ï¼windows 7       ï¼2ï¼æ°æ®åºï¼mysql 5.7.18
2ã主æå¡å¨é ç½®
       ï¼1ï¼å¨ä¸»æå¡å¨ä¸å»ºç«éåæ¥çæ°æ®åºÂ   create database test; 并建ç«ä¸¤å¼ 表
Â
CREATE TABLE `backup_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) CHARACTER SET utf8 NOT NULL,
`sex` varchar(2) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
CREATE TABLE `user` ( Â
 `User_ID` int(50) NOT NULL, Â
 `User_Name` char(100) DEFAULT NULL, Â
 PRIMARY KEY (`User_ID`) Â
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Â
       ï¼2ï¼å¯¹äºwindowsç³»ç»ï¼ç´æ¥æé®windows+Ré®å¼¹åºè¿è¡çªå£ï¼è¾å ¥å°åC:\ProgramData\MySQL\MySQL Server 5.7ï¼æ¾å°my.inié ç½®æ件设置
[mysqld]
# å¼å¯log-binæ¥å¿
log-bin=mysql-bin
server-id=1
# æè¿éè¦å¤å¶å为testçæ°æ®åº
binlog-do-db=test
ç¶ååæ¾å°åæ°æè æ·»å åæ°è®¾ç½®å¦ä¸ï¼è¿ä¸¤ä¸ªåæ°æ§å¶çäºè¿å¶æ¥å¿å·æ°çé度ï¼å æä¸ä¸è¡¨ï¼ï¼
Â
innodb_flush_log_at_trx_commit=1
sync_binlog=1
        ï¼3ï¼ç¶årootç¨æ·ç»å½æ°æ®åºï¼æ°å»ºä¸ä¸ªç¨æ·å¹¶ææï¼æè¿é设置为testuserç¨æ·ï¼å¯ç ä¹æ¯testuserï¼IPæ¾å°ä¸»æå¡å¨çipåä¸ï¼
Â
CREATE USER 'testuser'@'192.168.0.%' IDENTIFIED BY 'testuser';
GRANT REPLICATION SLAVE ON *.* TO 'testuser'@'%';
         注é:å äº%åipå¿ é¡»è¦ç¨åå¼å·æ¬èµ·æ¥,%å¹é ä»»æã    Â
        ï¼4ï¼åå¼ä¸ä¸ªä¼è¯ï¼è¿æ¥mysql,æ§è¡ SHOW MASTER STATUS; æ¾ç¤ºå¦ä¸ï¼è®°ä½é£ä¸ªmysql-bin.000002åpositionçå¼
Â
Â
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 412 | test | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
Â
3ãä»æå¡å¨é ç½®
Â
       ï¼1ï¼ä»æå¡å¨çserver-id设置æ2ï¼ä¸åæ ·è¦å¨ç®å½ C:\ProgramData\MySQLä¸æ¾å°my.iniæ件æå¼è¿è¡è®¾ç½® ï¼ProgramDataæ件夹å¯è½æ¯éèçï¼ç´æ¥è¾å ¥å°åæ å°±è½æ¾å°ï¼ï¼åé¢ç设置å¯ç´æ¥åå¨server-idåé¢ï¼åæ ·æ¯å¨è¯¥ç®å½ä¸çmy.iniä¸
server-id=2 #è¯å«æå¡å¨çå¯ä¸å¼
replicate-do-db=test #è¦åæ¥çæ°æ®åº
replicate-do-table=test.bakeup_table #è¦åæ¥ç表ï¼æ¹æèªå·±çæ°æ®åºå表
replicate-do-table=test.user #è¦åæ¥ç第äºä¸ªè¡¨
       ï¼2ï¼å¨ä¸é¢è®¾ç½®çåæ°ä¹åç´§éä¸é¢çåæ°
skip-slave-start=true #è·³è¿slave线ç¨å¯å¨
read_only=ON #å¼å¯çåªè¯»æ¨¡å¼
relay-log=relay-bin
relay-log-index=relay-bin.index
       ï¼3ï¼é 置寻æ¾ä¸»æå¡å¨ï¼ç¶åå¯å¨ä»æå¡å¨ãå è¾å ¥start slaveï¼ç¶åæ§è¡å¦ä¸å½ä»¤ï¼
Â
CHANGE MASTER TO
MASTER_HOST='主æå¡å¨ip',
MASTER_USER='testuser', #æ°å»ºçç¨æ·
MASTER_PASSWORD='testuser', #æçç¨æ·å¯ç
MASTER_LOG_FILE='mysql-bin.000002', #ä¸å¾æ¥è¯¢åºçåæ¥æ件
MASTER_LOG_POS=412; #ä¸å¾æ¥è¯¢åºçåæ¥ç¹ï¼å³ï¼positionä¸çå¼ï¼
       ï¼4ï¼æ¥ä¸æ¥é ç½®ä»æå¡å¨ä¸å¹¶è¡å¤å¶çåæ°ï¼å¼å¯ Enhanced Multi-Threaded Slaveï¼
Â
       Aãmaster_info_repository
         å¼å¯MTSåè½åï¼å¡å¿ å°åæ°master_info_repostitory设置为TABLEï¼è¿æ ·æ§è½å¯ä»¥æ50%~80%çæåãè¿æ¯å 为并 è¡å¤å¶å¼å¯å对äºå master.infoè¿ä¸ªæ件çæ´æ°å°ä¼å¤§å¹ æåï¼èµæºçç«äºä¹ä¼å大ãå¨ä¹åInnoSQLççæ¬ä¸ï¼æ·»å äºåæ°æ¥æ§å¶å·æ° master.infoè¿ä¸ªæ件çé¢çï¼çè³å¯ä»¥ä¸å·æ°è¿ä¸ªæ件ãå 为å·æ°è¿ä¸ªæ件æ¯æ²¡æå¿ è¦çï¼å³æ ¹æ®master-info.logè¿ä¸ªæ件æ¢å¤æ¬èº«å°±æ¯ä¸å¯é çãå¨MySQL 5.7ä¸ï¼æ¨èå°master_info_repository设置为TABLEï¼æ¥åå°è¿é¨åçå¼éã
       Bãslave_parallel_workers
         è¥å°slave_parallel_workers设置为0ï¼åMySQL 5.7éå为åå线ç¨å¤å¶ï¼ä½å°slave_parallel_workers设置为1ï¼åSQL线ç¨åè½è½¬å为coordinator线ç¨ï¼ä½æ¯åªæ1 个worker线ç¨è¿è¡åæ¾ï¼ä¹æ¯å线ç¨å¤å¶ãç¶èï¼è¿ä¸¤ç§æ§è½å´åæä¸äºçåºå«ï¼å 为å¤äºä¸æ¬¡coordinator线ç¨ç转åï¼å æ¤ slave_parallel_workers=1çæ§è½åèæ¯0è¿è¦å·®ã
       Cãè¦å¼å¯Enhanced Multi-Threaded Slaveï¼åªéè¦è®¾ç½®å¦ä¸åæ°å³å¯ï¼
Â
Â
Â
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=16 #16为设置ç并å线ç¨ä¸ªæ°ï¼ä¹åæ ¹æ®é¡¹ç®å¯¹æ°æ®ä¼ è¾çå
·ä½è¦æ±åæ´æ¹
#ä¸ä¸ªschemaä¸ï¼slave_parallel_workersä¸çworker线ç¨å¹¶åæ§è¡relay logä¸ä¸»åºæ交çäºå¡
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON
         注ï¼åéslave-parallel-typeå¯ä»¥æ两个å¼
Â
         aãDATABASE 为é»è®¤å¼ï¼åºäºåºç并è¡å¤å¶æ¹å¼ï¼
         bãLOGICAL_CLOCKï¼åºäºç»æ交ç并è¡å¤å¶æ¹å¼
         slaveæºå¨çrelay logä¸ last_committedç¸åçäºå¡ï¼sequence_numä¸åï¼å¯ä»¥å¹¶åæ§è¡ã
       ï¼5ï¼æåä¿åmy.ini并è¿è¡services.mscï¼éå¯mysqlæå¡ã
       ï¼6ï¼åæ ·ï¼å¨ä»æå¡å¨ä¸å»ºç«ç¸ååç空æ°æ®åºtest以åç¸åç表ã
Â
åãé ç½®æ£æ¥åæµè¯
Â
1ãå¯å¨ä»æå¡å¨çmysqlå½ä»¤è¡çé¢
         æ§è¡ start slave;
         åæ¥çå ¶ç¶æï¼æ§è¡
         show slave status\G;
         ç»æå¦ä¸ï¼
Â
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.193
Master_User: testuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 412
Relay_Log_File: slave-relay-bin.000007
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes <<<--------------------------æ¤å¤å¯ä»¥çå°
Slave_SQL_Running: Yes <<<--------------------------è¿ä¸¤ä¸ªçº¿ç¨é½å¨è¿è¡
Replicate_Do_DB: test
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 412
Relay_Log_Space: 951
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: ed1d6bc3-51a6-11e7-a527-083e8e9a4d6f
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more up
dates <<<<<<<<<<<<<<<<<<---------------------------æ¤å¤å¯ä»¥çå°è¿ä¸ªçº¿ç¨æ£å¨çå¾
æ¥åæ°æ®
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
Â
2ãæ§è¡å½ä»¤show processlist;
       æ¥çä¸ä¸æ£å¨æ§è¡çå¾ æ¥æ¶æ°æ®çææ线ç¨ï¼ç»æå¦ä¸ï¼
         ææ线ç¨é½å¨çå¾ æ¥åæ°æ®ï¼è®¾ç½®æåï¼
3ãæ°æ®åºåæ¥æµè¯
         æ¥ä¸æ¥å°±å¯ä»¥å¨ä¸»æ°æ®åºä¸æ·»å è®°å½äºï¼ 为æ¹ä¾¿æ·»å æ°æ®ï¼è¿éæéç¨navicatæ¥æ·»å æ°æ®
         ï¼1ï¼å¨ä¸»æ°æ®åºä¸ç两个表ä¸é½æ·»å æ°æ®ï¼åæ¶ä¿åã
            Â
         ï¼2ï¼ç¶åå¨ä»æ°æ®åºä¸è¿è¡æ¥æ¾ï¼ç»æå¦ä¸ï¼
            Â
          è³æ¤ï¼æµè¯å®æï¼é ç½®æåï¼
Â
åèæç« ï¼http://dinglin.iteye.com/blog/2272079
                 http://sky66.blog.51cto.com/2439074/1688047/
                 http://www.linuxidc.com/linux/2014-05/101450.htmââLinuxå ¬ç¤¾
Â
Â
以ä¸å°±æ¯è¿ç¯æç« çå ¨é¨å 容äºï¼å¸ææ¬æçå 容对大家çå¦ä¹ æè å·¥ä½è½å¸¦æ¥ä¸å®ç帮å©ï¼å¦ææçé®å¤§å®¶å¯ä»¥çè¨äº¤æµï¼è°¢è°¢ï¼