MySQL主從配置

1、準備

一、主從數據庫的版本號最好一直(由於官方推薦這樣,最主要仍是怕版本不一致帶來的不兼容問題;)mysql

二、主從數據庫內數據保持一致,若不一致可將從數據庫的數據備份備份出來。在將從數據庫的數據全都刪除,並將主庫的數據所有導入進去sql

 

主數據庫:192.168.122.135數據庫

從數據庫:192.168.122.136bash

 

2、操做

  主數據庫操做

一、開啓binlog服務器

【mysqld】session

log-bin=mysql-bin
server-id=1#主從的server-id不能同樣

 二、啓動主庫less

systemctl start mysql  #用來建立同步的用戶

 三、登錄數據庫ide

mysql -uroot -p

 四、建立一個測試庫、表post

>create database game;
>use game;
>show tables;
>create table LOL( id int AUTO_INCREMENT PRIMARY KEY, name char(20), age int);
>insert into LOL(name,age) values('JS',12),('XYR',23);

 五、在主庫上建立供slave遠程鏈接的用戶並受權(用戶LM。slave庫ip。密碼)測試

>create user 'LM'@'192.168.122.136' identified by 'Www.1.com';

 六、授予複製權限

>grant replication slave on *.* to 'LM'@'192.168.122.136';

 七、刷新

>flush privileges;

 八、鎖表,禁止寫入,當前窗口不能退出,這時候開啓另外一個終端繼續操做

>flush table with read lock;

 九、新窗口操做,查看master狀態,記錄二進制文件名和位置

>show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |     1042 |              |                  |
+------------------+----------+--------------+------------------+

 十、把當前數據導出

# mysqldump -u root -p --all-databases > alldb.sql

 十一、解鎖查看binlog日誌位置,若是沒變化證實鎖定成功,從庫將從這個binlog日誌開始恢復

>unlock tables;
>show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |     1042 |              |                  |
+------------------+----------+--------------+------------------+

   從庫操做

一、導入數據庫

# mysql -u root -p < alldb.sql 

 二、修改配置文件

server-id=2  #設置server-id,必須惟一

三、重啓mysql,配置同步(須要主服務器主機名,登錄憑證,二進制文件的名稱和位置)

change master to master_host='192.168.122.135',
    -> master_user='LM',
    -> master_password='Www.1.com',
    -> master_log_file='mysql-bin.000003',
    -> master_log_pos=1024;

 四、啓動slave

>start slave;

 五、查看slave狀態

>show slave status\G;
若是
Slava_IO_Running:YES
Slave_SQL_Runing:YES 
便可

 

 3、錯誤解決

  Slave_IO_Running: No

因爲主服務器異外重啓, 致使從報錯, 錯誤以下:
show slave status錯誤:
mysql> show slave status\G
Master_Log_File: mysql-bin.000288
Read_Master_Log_Pos: 627806304
Relay_Log_File: mysql-relay-bin.000990
Relay_Log_Pos: 627806457
Relay_Master_Log_File: mysql-bin.000288
Slave_IO_Running: No
Slave_SQL_Running: Yes
Exec_Master_Log_Pos: 627806304
Relay_Log_Space: 627806663

   查看日誌

tail /data/mysql/mysql-error.log
111010 17:35:49 [ERROR] Error reading packet from server: Client requested master
 to start replication from impossible position ( server_errno=1236)
111010 17:35:49 [ERROR] Slave I/O: Got fatal error 1236 from master when reading data
from binary log: 'Client requested master to start replication from impossible
position', Error_code: 1236
111010 17:35:49 [Note] Slave I/O thread exiting, read up to log 'mysql-bin.000288',
position 627806304

   解決方法

錯誤依舊, 接下來登錄到主服務器查看binlog日誌.
先按照錯誤點的標記去主服務器日誌中查找:

[root@db1 ~]# mysqlbinlog --start-position=627655136 /data/mysql/binlog/mysql-bin.000288
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#111010 13:31:19 server id 4 end_log_pos 106 Start: binlog v 4, server v 5.1.45-log
created 111010 13:31:19
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
F1aTTg8EAAAAZgAAAGoAAAABAAQANS4xLjQ1LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC
'/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
沒有看到這個位置.

[root@db1 ~]# mysqlbinlog /data/mysql/binlog/mysql-bin.000288 > test.txt

less text.txt
看最後一部分
# at 627625495
#111010 16:35:46 server id 1 end_log_pos 627625631 Query thread_id=45613333
exec_time=32758 error_code=0
SET TIMESTAMP=1318289746/*!*/;
delete from freeshipping_bef_update where part='AR-4006WLM' and code=''
/*!*/;
# at 627625631
#111010 16:35:46 server id 1 end_log_pos 627625751 Query thread_id=45613333
exec_time=32758 error_code=0
SET TIMESTAMP=1318289746/*!*/;
delete from shippingFee_special where part='AR-4006WLM'
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
找到最接近錯誤標記627655136的一個position是627625631.

再回到slave機器上change master, 將postion指向這個位置.

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> change master to master_log_file='mysql-bin.000288',master_log_pos=627625631;
Query OK, 0 rows affected (0.06 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
再次查看

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Queueing master event to the relay log
Master_Host: 192.168.21.105
Master_User: rep
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000289
Read_Master_Log_Pos: 25433767
Relay_Log_File: mysql-relay-bin.000003
Relay_Log_Pos: 630
Relay_Master_Log_File: mysql-bin.000289
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
主從同步正常了, 一樣的方法修復其它slave機器.
相關文章
相關標籤/搜索