準備環節:
主、從服務器 操作系統、mysql版本必須一致
一、環境
主機:
master操作系統:centos 5.5 64位
IP:192.168.3.28
MySQL版本:5.5.17
從機:
slave操作系統:centos 5.5 64位
IP:192.168.3.108
MySQL版本:5.7.17
二、master機和slave機的相關配置
1、創建一個複製用戶,具有replication slave 權限。
mysql>grant replication slave on *.* to
‘wxl’@’192.168.3.108′ identified by ‘123456’
2、修改master機器中mysql配置文件my.cnf,該文件在/etc目錄下
如果/etc/目錄下沒有my.cnf配置文件,請到/usr/share/mysql/找到my-medium*.cnf文件,拷貝到/etc/並改名爲my.cnf
命令如:# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
在[mysqld]配置段添加如下字段
log-bin = mysql-bin
log-bin-index = mysql-bin.index
binlog_format = row
server-id = 38
sync-binlog = 1
character-set-server = utf8
說明:
log-bin――設置二進制日誌文件的基本名;
log-bin-index設置二進制日誌索引文件名。
binlog_format――控制二進制日誌格式,進而控制了複製類型,三個可選值,STATEMENT:語句複製,類似Oracle的邏輯DG;
ROW:行復制,類似Oracle的物理DG;
MIXED:混和複製,默認選項。爲避免出現主從數據不一致,建議將其修改爲ROW。
server-id――爲服務器設置唯一ID,默認爲1,推薦取IP最後部分;
sync-binlog――默認爲0,爲保證不會丟失數據,需設置爲1,用於強制每次提交事務時,同步二進制日誌到磁盤上。
注:這幾個參數是必須的,其他更多參數請參考相關文檔
3、重啓master機的mysql服務:service mysqld restart
若Master庫可以停機,則可直接拷貝所有數據庫文件;若Master庫是在線生產庫,不能停機,則可採用多種方法對其進行完全備份,最好採用mysqldump,因爲它對所有存儲引擎均可使用。
――爲了獲取一個一致性的快照,需對所有表設置讀鎖
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
――獲取二進制日誌的偏移量
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 107 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
――備份數據庫
對於事務性引擎(如:InnoDB),可使用--single-transaction參數獲取一個一致性快照。
# mysqldump -uroot -proot123 --all-database -e --single-transaction --flush-logs --max_allowed_packet=1048576 --net_buffer_length=16384>/data/all_db.sql
對於MyISAM引擎,或多引擎混合的數據庫,可使用-l參數鎖定表,獲取一致性備份
# mysqldump -uroot --all-database -e -l --flush-logs --max_allowed_packet=1048576 --net_buffer_length=16384>/data/all_db.sql
注:因爲我們提前使用flush tables with read lock命令給所有表加了讀鎖,所以此處也不必使用-l參數再加鎖。
――備份完成後,將備份文件拷貝到Slave端
# scp all_db.sql
[email protected]:/data/
――恢復Master庫的寫操作
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
其實mysqldump還提供了一個--master-data參數,備份文件中包含了change master to語句,爲了清晰瞭解主從複製的過程,我們不採用這個參數。
二、從服務器
1、編輯my.cnf文件 在[mysqld]組添加以下設置
log-bin = /data/mysql/mysql-bin
binlog_format = row
log-slave-updates = 1
server-id = 108
relay-log = mysql-relay-bin
relay-log-index = mysql-relay-bin.index
read-only = 1
slave_net_timeout =10
說明:
1)relay-log――設置中繼日誌文件基本名;relay-log-index設置中繼日誌索引文件名。
2)read-only――設置Slave爲只讀,但具有super權限的用戶仍然可寫。
3)log-slave-updates――該參數用來控制Slave上的更新是否寫入二進制日誌,默認爲0;若Slave只作爲從服務器,則不必啓用;若Slave作爲其他服務器的Master,則需啓用,啓用時需和log-bin、binlog-format一起使用,這樣Slave從主庫讀取日誌並重做,然後記錄到自己的二進制日誌中。
4)slave_net_timeout――設置網絡超時時間,即多長時間測試一下主從是否連接,默認爲3600秒,即1小時,這個值在生產環境過大,我們將其修改爲10秒,即若主從中斷10秒,則觸發重新連接動作。
2、恢復slave
將Master庫的備份恢復到Slave庫,方法比較簡單,直接執行即可。
# mysql -uroot -proot123 </data/all_db.sql
說明:skip-slave-start作用是啓動從庫但不立即啓動從庫的複製進程,以便對從庫進行進一步的設置。
2、啓動從數據庫
service mysql restart
3、對從數據庫進行相應設置
#如果出現出現這個錯誤: ERROR 1201 (HY000): Could not initialize master!需停止slave進程
mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> reset slave;
Query OK, 0 rows affected,(0.00 sec)
mysql> change master to
-> master_host='192.168.3.28',
-> master_user='wxl',
-> master_password='123456',
-> master_log_file='mysql-bin.000003',
-> master_log_pos=107;
Query OK, 0 rows affected (0.01 sec)
4、啓動從服務器slave線程
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
執行show processlist命令顯示以下進程:
mysql> show processlist \G
*************************** 1. row ***************************
Id: 2
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 2. row ***************************
Id: 3
User: system user
Host:
db: NULL
Command: Connect
Time: 21
State: Waiting for master to send event
Info: NULL
*************************** 3. row ***************************
Id: 4
User: system user
Host:
db: NULL
Command: Connect
Time: 21
State: Slave has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
3 rows in set (0.00 sec)
5、查看slave線程狀態
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.3.28
Master_User: wxl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 107
Relay_Log_File: mysql-relay-bin.000014
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000009
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: 107
Relay_Log_Space: 452
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 28
1 row in set (0.00 sec)
說明:
Slave_IO_Running――此進程負責從Slave從Master上讀取binlog日誌,並寫入Slave上的中繼日誌。
Slave_SQL_Running――此進程負責讀取並執行中繼日誌中的binlog日誌。
這兩個進程的狀態需全部爲YES,只要有一個爲NO,則複製就會停止。
當Slave中Relay_Master_Log_File和Master_Log_File相同且Read_Master_Log_Pos和Exec_Master_Log_Pos完全相同時,則表明Slave和Master處於完全同步的狀態。
三、測試
1、在master庫創建一個測試表,並插入數據;
mysql> create table test3 as select * from test2;
2、然後在slave庫查看是否正確複製
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test1 |
| test2 |
| test3 |
+----------------+
3 rows in set (0.00 sec)
mysql> select * from test3;
+-----------+----------+
| user_name | user_pwd |
+-----------+----------+
| zhangsan | 123456 |
| lisi | 123456 |
| wangwu | 123456 |
| zhaoliu | 123456 |
| xiaoqian | 123456 |
+-----------+----------+
5 rows in set (0.00 sec)
說明:
1)可見從服務器已經正確複製了主服務器的數據,主從複製正確配置成功。
2)但是read-only參數沒有起作用,這是因爲我們是用root賬戶登陸的,它具有super權限,換成普通賬戶test,即未授予super或all權限的賬戶,測試如下:
mysql> use test
Database changed
mysql> select * from test;
+------+--------+
| id | name |
+------+--------+
| 6 | ffffff |
| 7 | gggggg |
| 8 | hhhhhh |
+------+--------+
3 rows in set (0.00 sec)
mysql> delete from test;
ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement
可見提示爲只讀,不能刪除。