主機環境說明。
master1: 10.8.1.11
master2: 10.8.1.12
版本資訊:
[root@m1 ~]# mysql -V
mysql Ver 14.14 Distrib 5.6.27, for Linux (x86_64) using EditLine wrapper
修改my.cf配置檔案,開啟bin-log功能,配置server-id。
[root@m1 ~]# more /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[mysqld]
server-id = 1
datadir = /var/lib/mysql
log_bin = /var/lib/mysql/bin-log
socket = /var/lib/mysql/mysql.sock
slave_net_timeout = 60
log-slave-updates
slave-skip-errors=all
skip-name-resolve
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#salve-net-timeout預設是3600秒,縮短時間是為了防止雙YES的假象
如果要指定同步或不同步哪些庫,可使用如下參數
#binlog-do-db=osyunweidb #需要同步的資料庫名,如果有多個資料庫,可重複此參數,每個資料庫一行
#binlog-ignore-db=mysql #不同步mysql系統資料庫
至于這些參數的說明具體看手冊。
紅色的部分非常重要,如果一個MASTER 挂掉的話,另外一個馬上接管。
紫紅色的部分指的是伺服器頻繁的重新整理日志。這個保證了在其中一台挂掉的話,日志重新整理到另外一台。進而保證了資料的同步
檢視指令 show variables like 'log_bin'; show variables like 'server_id';
mysql> show variables like 'log_bin';
+---------------+-------+
|Variable_name | Value |
|log_bin | ON |
1 rowin set (0.00 sec)
mysql>show variables like 'server_id';
|server_id | 1 |
mysql> grant replication slave on *.* to replication@'%'identified by '123456'; #授權該使用者對所有表都能進行複制
mysql>flush privileges; #重新整理權限
mysql>flush tables with read lock; #鎖定所有表,此時資料庫不能寫入資料
QueryOK, 0 rows affected (0.05 sec)
mysql>show master status; #檢視最新bin-log檔案及位置
+------------------------+------------+-------------------+-------------------------+
|File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
|mysql-bin.000001 | 26314 | | |
1row in set (0.00 sec)
由于退出目前mysql登陸視窗,鎖表功能就失效,需克隆一個會話進行全備。
#mysqldump-uroot -p -A -B > /tmp/mysql_bak_2015_11_17.sql
看下備份資料大小,确認備份成功。
[root@m1 ~]# ls -l mysql_bak_2015_11_17.sql
-rw-r--r--. 1 root root 645327 Nov 18 06:27 mysql_bak_2015_11_17.sql
[root@m1 ~]#
mysql>unlock tables;
或直接quit退出即可。
從庫開啟bin-log功能後,待會在主上在配置同步,互為主從就完成了。
[root@m2 ~]# vi /etc/my.cnf
server-id = 2
log_bin = /var/lib/mysql/mysql-bin
auto_increment_offset=2
#/etc/init.d/mysql restart
mysql>show variables like 'log_bin';
|log_bin | ON |
|server_id | 2 |
登陸mysql導入資料
mysql>source /root/mysql_bak_2015_11_17.sql
因為在從庫導入全備資料時,此時主庫與從庫的内容是一緻的,但是bin-log位置不一定一緻。
|File |Position | Binlog_Do_DB | Binlog_Ignore_DB |
|mysql-bin.000003 | 2328055 | | |
此處binlog檔案與位置狀态,是主庫在步驟4鎖表時show master status檢視的位置狀态。
CHANGE MASTER TO
MASTER_HOST='10.8.1.11',
MASTER_PORT=3306,
MASTER_USER='replication',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=26314;
使用start slave開啟同步功能,使用show slave status\G檢視同步是否成功
mysql>start slave;
QueryOK, 0 rows affected (0.00 sec)
mysql>show slave status\G #\G不按表格輸出
***************************1. row ***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 10.0.0.2
Master_User: replication
Master_Port: 8306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 136270
Relay_Log_File:mysqld-relay-bin.000002
Relay_Log_Pos: 72697
Relay_Master_Log_File: mysql-bin.000001
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: 98758
Relay_Log_Space: 110366
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: 622 #檢視主從同步延遲,延遲大則可能需要優化
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: 1
#sql線程與IO線程都是YES,slave配置成功。
由于從庫是全備導入,原先在主庫上配置的複制帳戶也同樣導入,是以這裡不用在從庫上新授權複制使用者。
從庫上的binlog檔案與位置狀态,是從庫在剛導入時show master status檢視到的位置狀态。
CHANGEMASTER TO
MASTER_HOST='10.8.1.12',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=2328055;
#修改相應資訊,直接把這些配置在mysql中粘貼即可。
mysql>show slave status\G
Master_Host: 172.16.0.2
Master_Port: 3306
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 107
Relay_Log_File:mysqld-relay-bin.000006
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes
Exec_Master_Log_Pos: 107
Relay_Log_Space: 556
Seconds_Behind_Master: 0
Master_Server_Id: 2
#IO線程與sql線程都是正常。
在兩台mysql各建立一個庫,看兩邊是否都能進行同步。
分别在主庫上執行 create database test01;
從庫上執行create database test02;
看兩台資料庫上執行show databases;,看是否都有test01表和test02表。
我的經過測試,雙主測試成功。
本文轉自 pgmia 51CTO部落格,原文連結:http://blog.51cto.com/heyiyi/1713971