主ä»æ°æ®åºåæ¥ å¯ä»¥åèå¦ä¸ï¼
ä¸ã æ¦è¿°
ã MySQLä»3.23.15çæ¬ä»¥åæä¾æ°æ®åºå¤å¶ï¼replicationï¼åè½ï¼å©ç¨è¯¥åè½å¯ä»¥å®ç°ä¸¤ä¸ªæ°æ®åºåæ¥ã主ä»æ¨¡å¼ãäºç¸å¤ä»½æ¨¡å¼çåè½ãæ¬æ档主è¦éè¿°äºå¦ä½å¨linuxç³»ç»ä¸å©ç¨mysqlçreplicationè¿è¡åæºçå¤çé
ç½®ã
äºã ç¯å¢
æä½ç³»ç»ï¼Linux 2.6.23.1-42.fc8 # SMPï¼ä¸å®è£
XENï¼
Mysqlçæ¬ï¼5.0.45-4.fc8
设å¤ç¯å¢ï¼PCï¼æè
èææºï¼ä¸¤å°
ä¸ã é
ç½®
æ°æ®åºåæ¥å¤å¶åè½ç设置é½å¨MySQLçé
ç½®æ件ä¸ä½ç°ï¼MySQLçé
ç½®æ件ï¼ä¸è¬æ¯my.cnfï¼ï¼å¨æ¬ç¯å¢ä¸ä¸º/etc/my.cnfã
3.1 设置ç¯å¢ï¼
IPç设置ï¼
Aä¸»æº IPï¼10.10.0.119
Maskï¼255.255.0.0
Bä¸»æº IPï¼10.10.8.112
Maskï¼255.255.0.0
å¨IP设置å®æ以åï¼éè¦ç¡®å®ä¸¤ä¸»æºçé²ç«å¢ç¡®å®å·²ç»å
³éãå¯ä»¥ä½¿ç¨å½ä»¤service iptables statusæ¥çé²ç«å¢ç¶æãå¦æé²ç«å¢ç¶æ为ä»å¨è¿è¡ã使ç¨service iptables stopæ¥åç¨é²ç«å¢ãå¦ææ³å¯å¨å
³éé²ç«å¢ï¼å¯ä»¥ä½¿ç¨setupå½ä»¤æ¥ç¦ç¨æå®å¶ã
æç»ä»¥ä¸¤å°ä¸»æºå¯ä»¥ç¸äºpingé为佳ã
3.2 é
ç½®A主(master) Bä»(slave)模å¼
3.2.1 é
置A 为master
ããå¢å ä¸ä¸ªç¨æ·åæ¥ä½¿ç¨çå¸å·ï¼
GRANT FILE ON *.* TO âbackupâ@'10.10.8.112' IDENTIFIED BY â1234â;
GRANTREPLICATION SLAVE ON *.* TO âbackupâ@'10.10.8.112' IDENTIFIED BY â1234â;
èµäº10.10.8.112ä¹å°±æ¯Slaveæºå¨æFileæéï¼åªèµäºSlaveæºå¨æFileæéè¿ä¸è¡ï¼è¿è¦ç»å®REPLICATION SLAVEçæéæå¯ä»¥ã
ãå¢å ä¸ä¸ªæ°æ®åºä½ä¸ºåæ¥æ°æ®åºï¼
create database test;
ãå建ä¸ä¸ªè¡¨ç»æï¼
create table mytest (username varchar(20),password varchar(20));
ãä¿®æ¹é
ç½®æ件ï¼
ä¿®æ¹Aç/etc/my.cnfæ件ï¼å¨my.cnfé
置项ä¸å å
¥ä¸é¢é
ç½®ï¼
server-id = 1 #Serveræ è¯
log-bin
binlog-do-db=test #æå®éè¦æ¥å¿çæ°æ®åº
ãéèµ·æ°æ®åºæå¡ï¼
service mysqld restart
æ¥çserver-idï¼
show variable like âserver_idâï¼
å®ä¾ï¼
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
1 row in set (0.00 sec)
ãç¨show master status/Gå½ä»¤çæ¥å¿æ
åµã
æ£å¸¸ä¸ºï¼
mysql> show master status/G
*************************** 1. row ***************************
File: mysqld-bin.000002
Position: 198
Binlog_Do_DB: test,test
Binlog_Ignore_DB:
1 row in set (0.08 sec)
3.2.2 é
置B 为slave
ãå¢å ä¸ä¸ªæ°æ®åºä½ä¸ºåæ¥æ°æ®åºï¼
create database test;
ãå建ä¸ä¸ªè¡¨ç»æï¼
create table mytest (username varchar(20),password varchar(20));
ãä¿®æ¹é
ç½®æ件ï¼
ä¿®æ¹Bç/etc/my.cnfæ件ï¼å¨my.cnfé
置项ä¸å å
¥ä¸é¢é
ç½®ï¼
server-id=2
master-host=10.10. 0.119
master-user=backup #åæ¥ç¨æ·å¸å·
master-password=1234
master-port=3306
master-connect-retry=60 #é¢è®¾éè¯é´éç§
replicate-do-db=test #åè¯slaveåªåbackupæ°æ®åºçæ´æ°
ãéèµ·æ°æ®åºæå¡ï¼
service mysqld restart
æ¥çserver-idï¼
show variables like âserver_idâï¼
å®ä¾ï¼
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 2 |
+---------------+-------+
1 row in set (0.00 sec)
ãç¨show slave status/Gå½ä»¤çæ¥å¿æ
åµã
æ£å¸¸ä¸ºï¼
mysql> show slave status/G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.10.0.119
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysqld-bin.000001
Read_Master_Log_Pos: 98
Relay_Log_File: mysqld-relay-bin.000003
Relay_Log_Pos: 236
Relay_Master_Log_File: mysqld-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test,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: 98
Relay_Log_Space: 236
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
1 row in set (0.01 sec)
3.2.3 éªè¯é
ç½®
åå«ä½¿ç¨insert, delete , updateå¨A主æºè¿è¡å¢å æ¹æ¥æ°æ®åºï¼æ¥çB主æºçæ°æ®åºæ¯å¦ä¸A主æºä¸è´ï¼è¥ä¸è´ï¼åé
ç½®æåã
3.3åæºäºå¤æ¨¡å¼
å¦æå¨A主æºå å
¥slave设置ï¼å¨B主æºå å
¥master设置ï¼åå¯ä»¥åB->Açåæ¥ã
ãå¨A主æºçé
ç½®æä»¶ä¸ mysqldé
置项å å
¥ä»¥ä¸è®¾ç½®ï¼
master-host=10.10.8.112
master-user=backup
master-password=1234
replicate-do-db=test
master-connect-retry=10
ãå¨Bçé
ç½®æä»¶ä¸ mysqldé
置项å å
¥ä»¥ä¸è®¾ç½®ï¼
log-bin
binlog-do-db=test
ãã注æï¼å½æé误产çæ¶ï¼*.erræ¥å¿æ件åæ¥ç线ç¨éåºï¼å½çº æ£é误åï¼è¦è®©åæ¥æºå¶è¿è¡å·¥ä½ï¼è¿è¡slave startã
éèµ·AãBæºå¨ï¼åå¯ä»¥å®ç°ååççå¤ä»½ã
åã 常è§é®é¢å解å³
ãSlaveæºå¨çæéé®é¢ï¼ä¸ä½è¦ç»slaveæºå¨Fileæéï¼è¿è¦ç»å®REPLICATION SLAVEçæéã
ãå¨ä¿®æ¹å®Slaveæºå¨/etc/my.cnfä¹åï¼slaveæºå¨çmysqlæå¡å¯å¨ä¹åï¼è®°å¾è¦å é¤æmaster.info
ãå¨show master statusæçshow slave statusä¸æ£å¸¸æ¶ï¼çç.erræ¯ææ ·è¯´çã
ãSlaveä¸MysqlçReplicationå·¥ä½æ两个线ç¨, I/O threadåSQL threadãI/Oçä½ç¨æ¯ä»master 3306端å£ä¸æå®çbinlogåè¿æ¥(masterå¨è¢«ä¿®æ¹äºä»»ä½å
容ä¹å,å°±ä¼æä¿®æ¹äºä»ä¹åå°èªå·±çbinlogçå¾
slaveæ´æ°),ç¶ååå°æ¬å°çrelay-log,èSQL threadåæ¯å»è¯»æ¬å°çrelay-log,åæå®è½¬æ¢ææ¬Mysqlæè½ç解çè¯å¥ï¼äºæ¯åæ¥å°±è¿æ ·ä¸æ¥ä¸æ¥çå®æ.å³å®I/O threadçæ¯/var/lib/mysql/master.info,èå³å®SQL threadçæ¯/var/lib/mysql/relay-log.info.
ãå¯å¨slaveï¼å½ä»¤ç¨start slaveï¼éæ°å¯å¨ç¨restart slave
温馨提示:答案为网友推荐,仅供参考