mysql主從同步的原理:
1、在master上開啟bin-log日志功能,記錄更新、插入、刪除的語句。
2、必須開啟三個線程,主上開啟io線程,從上開啟io線程和sql線程。
3、從上io線程去連接master,master通過io線程檢查有slave過來的請求,請求日志、postsion位置。
4、master將這些相應的日志返回給slave,slave自己去下載到本地的realy_log里面,寫入一個master-info日志記錄同步的點。
5、slave的sql線程檢查到realy-log日志有更新,然后在本地去exec執行。
一:
Slave I/O: error connecting to master 'backup@192.168.1.x:3306' - retry-time: 60 retries: 86400, Error_code: 1045
解決方法
從服務器上刪除掉所有的二進制日志文件,包括一個數據目錄下的master.info文件和hostname-relay-bin開頭的文件。
master.info::記錄了Mysql主服務器上的日志文件和記錄位置、連接的密碼。
或者是防火牆問題
二:出現錯誤提示
Error reading packet from server: File '/home/mysql/mysqlLog/log.000001' not found (Errcode: 2) ( server_errno=29)
解決方法:
由於主服務器運行了一段時間,產生了二進制文件,而slave是從log.000001開始讀取的,刪除主機二進制文件,包括log.index文件。
三:錯誤提示如下
Slave SQL: Error 'Table 'xxxx' doesn't exist' on query. Default database: 't591'. Query: 'INSERT INTO `xxxx`(type,post_id,browsenum)
SELECT type,post_id,browsenum FROM xxxx WHERE hitdate='20090209'', Error_code: 1146
解決方法:
由於slave沒有此table表,添加這個表,使用slave start 就可以繼續同步。
四:錯誤提示如下
Error 'Duplicate entry '1' for key 1' on query. Default database: 'movivi1'. Query: 'INSERT INTO `v1vid0_user_samename`
VALUES(null,1,'123','11','4545','123')'
Error 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
to use near '' at line 1' on query. Default database: 'club'. Query: 'INSERT INTO club.point_process ( GIVEID, GETID, POINT, CREATETIME, DEMO )
VALUES ( 0, 4971112, 5, '2010-12-19 16:29:28','
1 row in set (0.00 sec)
Mysql > Slave status\G;
顯示:Slave_SQL_Running 為 NO
解決方法:
Mysql > stop slave;
Mysql > set global sql_slave_skip_counter =1 ;
Mysql > start slave;
五:錯誤提示如下
show slave status\G;
Master_Log_File: mysql-bin.000029
Read_Master_Log_Pos: 3154083
Relay_Log_File: c7-relay-bin.000178
Relay_Log_Pos: 633
Relay_Master_Log_File: mysql-bin.000025
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB: club
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1594
Last_Error: Relay log read failure: Could not parse relay log event entry. The possible reasons are:
the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log),
the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log),
a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's
binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave.
Skip_Counter: 0
Exec_Master_Log_Pos: 1010663436
這個問題原因是,主數據庫突然停止或問題終止,更改了mysql-bin.xxx日志,slave服務器找不到這個文件,需要找到同步的點和日志文件,然后chage master即可。
解決方法:
change master to
master_host='IP',
master_user='同步帳號',
master_password='同步密碼',
master_port=3306,
master_log_file='mysql-bin.000025',
master_log_pos=1010663436;
六:錯誤提示如下
Error 'Unknown column 'qdir' in 'field list'' on query. Default database: 'club'. Query: 'insert into club.question_del (id, pid,
ques_name, givepoint, title, subject, subject_pid, createtime, approve, did, status, intime, order_d, endtime,banzhu_uid,
banzhu_uname,del_cause,qdir) select id, pid, ques_name, givepoint, title, subject, subject_pid, createtime, approve, did,
status, intime, order_d, endtime,'1521859','admin0523','無意義回復',qdir from club.question where id=7330212'
1 row in set (0.00 sec)
這個錯誤就說club.question_del 表里面沒有qdir這個字段 造成的加上就可以了~!
在主的mysql : 里面查詢 Desc club.question_del;
在 錯誤的從服務器上執行 : alter table question_del add qdir varchar(30) not null;
七:錯誤提示如下
Slave_IO_Running: NO
這個錯誤就是IO 進程沒連接上 ,想辦法連接上把 把與主的POS 號和文件一定要對,然后重新加載下數據。具體步驟:
slave stop;
change master to master_host='IP地址',master_user='club',master_password='mima ',master_log_file='mysqld-bin.000048',MASTER_LOG_POS=396549485;
注:master_log_file='mysqld-bin.000048',MASTER_LOG_POS=396549485;是從主的上面查出 來的 :show master status\G;
LOAD DATA FROM MASTER;
load data from master;
slave start;
八 ,mysql報錯如下:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.198
Master_User: rootclub
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000061
Read_Master_Log_Pos: 494107148
Relay_Log_File: xyxyclub-relay-bin.006609
Relay_Log_Pos: 1073365
Relay_Master_Log_File: mysql-bin.000061
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: club
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: 494107148
Relay_Log_Space: 1073365
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.00 sec)
ERROR:
No query specified #這里提示有錯誤,剛開始我以為數據庫的問題呢,經過查詢驗證,發現並不是數據庫的問題,而是我們操作的問題,
看下面操作。就沒有報錯誤吧。
mysql> mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.0.198
Master_User: rootclub
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000061
Read_Master_Log_Pos: 494248077
Relay_Log_File: xyxyclub-relay-bin.006609
Relay_Log_Pos: 1213077
Relay_Master_Log_File: mysql-bin.000061
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB: club
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: 494246860
Relay_Log_Space: 1214294
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: NULL
1 row in set (0.00 sec)
mysql>
解釋:
show slave status\G
\G就是結束 再加一個";" 就多余了,所以才報錯,這2個結束符號別重復使用就是...
九.mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.18
Master_User: tongbu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000022
Read_Master_Log_Pos: 106
Relay_Log_File: mysqld-relay-bin.000035
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000022
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 1
Exec_Master_Log_Pos: 106
Relay_Log_Space: 552
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1236
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
解決方法是在master端從新看一下master狀態
在master那邊,執行:
flush logs;
show master status;
記下File, Position。
在slave端,執行:
Slave stop
CHANGE MASTER TO MASTER_LOG_FILE='testdbbinlog.000008',MASTER_LOG_POS=107;
slave start;
show slave status \G
一切正常。