您好,登錄后才能下訂單哦!
1. 準備工作
(本文永久地址:http://woymk.blog.51cto.com/10000269/1922786)
配置環境:centos6.9,mysql5.7
先安裝、配置好兩臺MySQL服務器
server1 IP:192.168.1.1
server2 IP:192.168.1.2
mysql的安裝請參考之前的文章:
LAMP環境搭建(centos6.9+apache2.4+mysql5.7+php7.1)
2. 在server1上操作
vi /etc/my.cnf
修改或添加下面這幾行:
server-id=1
log-bin=mysql-bin # 啟用二進制日志
auto-increment-increment = 2 #每次增長2
auto-increment-offset = 1 #設置自動增長的字段的偏移量
兩個可選參數(2選1):
binlog-do-db=db1,db2 #需要同步的庫
binlog-ignore-db=db1,db2 #忽略不同步的庫
保存后重啟
/etc/init.d/mysqld restart
運行mysql客戶端
mysql -uroot -p
創建同步賬號
grant replication slave on *.* to 'repl2'@'192.168.1.2' identified by '1234';
flush privileges;
鎖表,不讓數據寫入
flush tables with read lock;
show master status;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 613 | | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
記錄下二進制日志文件名和位置
備份數據庫
在mysql1上執行
mysqldump -uroot -p db1 > back.sql;
把備份好的數據拷貝到從服務器
scp back.sql 192.168.1.2:~
3. 在server2操作
vi /etc/my.cnf
修改或增加:
server-id=2 #這個數值不能和主一樣
log-bin=mysql-bin # 啟用二進制日志
auto-increment-increment = 2 #每次增長2
auto-increment-offset = 2 #設置自動增長的字段的偏移量
可選參數(2選1,這兩個參數設置成和主一樣):
replicate-do-db=db1,db2
replicate-ignore-db=db1,db2
保存后重啟
/etc/init.d/mysqld restart
把在server1上備份的數據導入server2
在server2上執行
[root@server2 ~]# mysqldump -uroot -p db1 < back.sql
運行mysql客戶端
mysql -uroot -p
創建同步賬號
grant replication slave on *.* to 'repl1'@'192.168.1.1' identified by '1234';
flush privileges;
show master status;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 613 | | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
記錄下二進制日志文件名和位置
執行以下命令
stop slave;
change master to master_host='192.168.1.1',master_user='repl2',master_password='1234',master_log_file='mysql-bin.000001',master_log_pos=613;
(master_log_file和master_log_pos填上剛才記錄下的二進制日志文件名和位置)
start slave;
查看從服務器的狀態:
show slave status\G
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.1
Master_User: repl2
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 613
Relay_Log_File: server2-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
4. 回到server1上操作
運行mysql客戶端
mysql -uroot -p
取消server1上數據庫的鎖定
mysql> unlock tables;
執行以下命令
stop slave;
change master to master_host='192.168.1.2',master_user='repl1',master_password='1234',master_log_file='mysql-bin.000001',master_log_pos=613;
(master_log_file和master_log_pos填上剛才記錄下的二進制日志文件名和位置)
start slave;
查看從服務器的狀態:
show slave status\G
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.2
Master_User: repl1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 613
Relay_Log_File: server1-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
5. 主主復制測試
經測試,主主復制配置成功。
具體過程略,請自行請參考上一篇 mysql5.7主從配置 里的方法。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。