Mysql實現主從同步


根據網上眾多參考案例,繼續在VM虛擬機里實現MySQL主從同步功能。步驟如下:
* 首先明確下環境
主庫本地windows ip192.168.0.103
從庫虛擬機mysql5.6 ip192.168.0.128
1 主服務器的配置
1.1 先建立要同步的數據庫users,建表語句不說了。
1.2 創建一個具有replication slave用戶。
mysql>grant replication slave on *.* to 'slave'@'192.168.0.128' identified by '123456';
mysql>flush privileges; 刷新權限或重啟服務。
1.3 編輯my.cnf文件
vi /etc/my.cnf
添加server-id=103
開啟log-bin二進制日志文件(Mysql需要有/var/lib/mysql/目錄的讀寫權限【可通過chown -R mysql:mysql /var/lib/mysql命令進行更改】)
log-bin=/var/lib/mysql/mysql-bin
#指定絕對路徑,否者會出現mysql運行show master status;時無法查看日志情況
mysql> show master status;
Empty set (0.00 sec)
mysql> show binary logs;
ERROR 1381 (HY000): You are not using binary logging
其他擴展配置項: 
binlog-do-db=mysql1 #需要備份的數據庫名,如果備份多個數據庫,重復設置這個選項 即可
binlog-ignore-db=mysql2 #不需要備份的數據庫名,如果備份多個數據庫,重復設置這 個選項即可
log-slave-updates=1 #這個參數一定要加上,否則不會給更新的記錄些到二進制文件 里
slave-skip-errors=1 #是跳過錯誤,繼續執行復制操作(可選)
1.4 重啟mysql數據庫
service mysqld restart
1.5 設置讀鎖
mysql>flush tables with read lock;
1 .6 得到binlog日志文件名和偏移量(此處記住File名稱和Position值,后面slave服務器配置時需要用到)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 713 | | |
+------------------+----------+--------------+------------------+
row in set (0.00 sec)
1.7 備份要同步的數據庫
mysqldump users > users.sql;
1.8 解鎖
mysql>unlock tables;
2 從服務器的配置
先將備份下來的sql導入到從庫中。
2.1 編輯my.cnf文件
vi /etc/my.cnf
添加 server-id=128
2.2 重啟從數據庫
service mysqld restart
2.3 對從數據庫進行相應設置
此處要注意logfile的名稱和position的值,其余host、user和password為主數據庫設置的賬號和密碼
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to
   -> master_host='192.168.0.103',
   -> master_user='slave',
   -> master_password='123456',
   -> master_log_file='mysql-bin.000001',
   -> master_log_pos=713;
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
查看從庫狀態:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.107
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1079
Relay_Log_File: mysqld-relay-bin.000004
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes 重點關注這兩值,如果為no,重做上述步驟,重新設置。
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: 1079
Relay_Log_Space: 407
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:
row in set (0.00 sec)
ERROR:
No query specified
3 測試
上述項配置完以后可查看master和slave上線程的狀態。在master上,你可以看到slave的I/O線程創建的連接:在master上輸入show processlist\G;
mysql> show processlist\G;
*************************** 1. row ***************************
Id: 4
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 2. row ***************************
Id: 19
User: repl
Host: 192.168.0.109:42337
db: NULL
Command: Binlog Dump
Time: 183
State: Has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
rows in set (0.00 sec)
 
ERROR:
No query specified
 
在主庫上添加數據,檢驗從庫變化。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM