標籤:code errno 分析 common last bsp 分享 pos 日誌
環境:
Mater: CentOS7.1 5.5.52-MariaDB 192.168.108.133
Slave: CentOS7.1 5.5.52-MariaDB 192.168.108.140
1.匯出主服務資料,將主備初始資料同步
master:
//從master上匯出需要同步的資料庫資訊mysqldump -u*** -p*** --database test > test.sql//將master上的備份資訊傳輸到slave上scp /root/test.sql [email protected]:/opt/
slave:
//進入slave的資料庫mysql -u*** -p***//清空test資料庫drop database test//匯入master的test資料庫資訊source /opt/test.sql
2.配置master和slave上的mysql資料庫
master:
//修改master的my.cnf檔案vim /etc/my.cnf//master配置如下,在[mysqld]下添加如下配置#log-binserver-id = 1log_bin = master-binexpire_logs_days = 10max_binlog_size = 100Mbinlog-do_db = testbinlog_ignore_db = mysql//重啟mysql資料庫service mysqld restart//如果安裝的是mariadb可以重啟mariadbsystemctl restart mariadb.service
slave:
//修改slave的my.cnf檔案vim /etc/my.cnf//slave配置如下,在[mysqld]下添加如下配置server-id = 2//重啟mysql資料庫service mysqld restart//如果安裝的是mariadb可以重啟mariadbsystemctl restart mariadb.service
簡單說明一下參數配置,保證主備server-id唯一。在master上需要開啟mysql的binlog,log_bin=master_bin,指定binlog檔案的名稱。
3.建立一個複製使用者,具有replication slave 許可權,能保證slave能把master的資料同步過去
master:
grant replication slave on *.* to ‘replication‘@‘192.168.108.140‘ identified by ‘replication‘;
4.擷取master的binlog位置
master:
//進入mysql資料庫mysql -u*** -p***//設定讀鎖flush tables with read lock;//擷取mysql的binlog檔案資訊和位移量show master status;+-------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+-------------------+----------+--------------+------------------+| master-bin.000010 | 3713 | test | mysql |+-------------------+----------+--------------+------------------+1 row in set (0.00 sec)//解鎖unlock tables;
5.設定備端資料庫
//進入mysql資料庫mysql -u*** -p***//停止slavestop slave;//設定對應master的binlog資訊MariaDB [(none)]> change master to -> master_host=‘192.168.108.133‘, -> master_user=‘replication‘, -> master_password=‘replication‘, -> master_log_file=‘master-bin.000010‘, -> master_log_pos=3713;//啟動slavestart slave;
6.查看備端狀態
MariaDB [(none)]> show slave status\G;*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.108.133 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000010 Read_Master_Log_Pos: 3881 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 698 Relay_Master_Log_File: master-bin.000010 Slave_IO_Running: Yes Slave_SQL_Running: Yes 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: 0 Exec_Master_Log_Pos: 3881 Relay_Log_Space: 994 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: 0Master_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: 11 row in set (0.00 sec)ERROR: No query specified
如果:Slave_IO_Running: Yes,Slave_SQL_Running: Yes則為配置成功,配置錯誤重複上面操作即可。如果解決不了可通過查看mysql日誌分析處理。
vim /var/log/mariadb/mariadb.log
7.測試。其實測試沒啥好寫的,配置成功之後直接連到主從資料庫,在master上改變表、欄位、資料,slave會同步變化。
寫在最後:當時想的試一試能不能用mysql內建的功能做資料庫災備,後來發現mysql資料庫主從同步會有一些問題。第一個不好指令碼化的東西是在同步之前需保證兩邊的資料庫初始資訊一樣,因為備端配置的mysql-binlog位置只是當前主要資料庫資訊的位置,在該位置之前的資料只能通過人工匯入。第二個就是mysql主從同步時,只能進行資料庫的增量同步處理,不能進行全量同步;還有就是如果備端出現髒資料,多了一條資料,當主那邊新增一條主鍵相同的資料,則同步失敗。之後我會嘗試的能不能把這些操作指令碼化,發現mysql內建的同步功能限制性很大,並且手工幹預的東西太多了。
mysql資料庫主從同步