CentOS 7.4(內核版本3.10.0-693)
MySQL 5.7.19 # 安裝參考個人上一篇文章:http://www.javashuo.com/article/p-fhrnkwfx-ex.html
Keepalived 1.4.0 #將在下一篇文章介紹
JDK1.8_171(不用自帶OpenJDK)
DB1:192.168.200.180 # 這2臺主機爲VMware克隆,會有一個坑,後面介紹
DB2:192.168.200.181mysql
首先修改DB1主機的配置文件,在/etc/my.cnf文件中的[mysqld]段添加如下內容sql
[root@mysql01 ~]# vim /etc/my.cnf [mysqld] server-id = 111 # 2臺MySQL的ID不能相同,範圍0~255之間 log-bin=mysql-bin relay-log = mysql-relay-bin replicate-wild-ignore-table=mysql.% replicate-wild-ignore-table=test.% replicate-wild-ignore-table=information_schema.% # # Remove leading # and set to the amount of RAM for the most important data
而後修改DB2主機的配置文件數據庫
[root@mysql01 ~]# vim /etc/my.cnf [mysqld] server-id = 222 log-bin=mysql-bin relay-log = mysql-relay-bin replicate-wild-ignore-table=mysql.% replicate-wild-ignore-table=test.% replicate-wild-ignore-table=information_schema.% # # Remove leading # and set to the amount of RAM for the most important data
最後分別重啓DB1和DB2使配置生效:vim
service mysqld restart
首先在DB1的mysql庫中建立複製用戶:服務器
mysql> grant replication slave on *.* to '用戶名'@'192.168.200.181' identified by '密碼'; Query OK, 0 rows affected (0.04 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 271 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
而後在DB2的mysql庫中將DB1設爲本身的主服務器:ide
mysql> change master to \ -> master_host='192.168.200.180', -> master_user='用戶名', -> master_password='密碼', -> master_log_file='mysql-bin.000004', -> master_log_pos=271; Query OK, 0 rows affected (0.07 sec)
這裏須要注意master_log_file和master_log_pos兩個選項,這兩個選項的值是在DB1上經過「show master status」 查詢到的結果
接着在DB2上啓動slave服務測試
mysql> start slave; Query OK, 0 rows affected (0.01 sec)
下面查看DB2上slave的運行狀態rest
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.200.180 Master_User: chenghao Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000009 Read_Master_Log_Pos: 63008557 Relay_Log_File: mysql-relay-bin.000025 Relay_Log_Pos: 63008770 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: mysql.%,test.%,information_schema.% Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 63008557 Relay_Log_Space: 63009143 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: 111 Master_UUID: 46249b75-3685-11e8-be57-00505636ed4d # 本文的坑位置 Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.01 sec) mysql>
上面2個重點都爲yes,到這裏,從DB1到DB2的mysql同步複製已經完成。
接下來開始配置從DB2到DB1的mysql同步複製。
在DB2的mysql庫中建立複製用戶code
mysql> grant replication slave on *.* to '用戶名'@'192.168.200.180' identified by '密碼'; Query OK, 0 rows affected (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000005 | 271 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
而後在DB1的mysql庫中將DB2設爲本身的主服務器orm
mysql> change master to \ -> master_host='192.168.200.181', -> master_user='用戶名', -> master_password='密碼', -> master_log_file='mysql-bin.000005', -> master_log_pos=271; Query OK, 0 rows affected (0.07 sec)
最後,在DB1上啓動slave服務並查看狀態
mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.200.181 Master_User: chenghao Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000010 Read_Master_Log_Pos: 16985774 Relay_Log_File: mysql-relay-bin.000026 Relay_Log_Pos: 16985780 Relay_Master_Log_File: mysql-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: mysql.%,test.%,information_schema.% Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 16985774 Relay_Log_Space: 16985987 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: 222 Master_UUID: 85c9a8fc-3687-11e8-91bc-005056327a6d # 本文的坑位置 Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec) mysql>
上面2個重點都爲yes後,說明DB2到DB1的mysql同步複製已經配置好,到這裏這2臺MySQL的主從同步配置已經完成。
文章開頭提到了一個坑,這裏介紹一下:
若是2臺MySQL的主機是在VMware裏克隆的,2臺機器上面的Master_UUID是同樣的,會形成以前的主從配置失敗(SlaveIORunning或者SlaveSQLRunning狀態不爲yes),解決辦法:
在MySQL的主目錄:/var/lib/mysql目錄下,第一行會有一個auto開頭的一個文件,打開后里面有一串UUID,克隆的主機這2臺的UUID是如出一轍的,而後隨便找一臺,刪除這個文件,而後重啓數據庫,會自動生成
都配好後,建議重啓2臺數據庫,而後再登陸2臺數據庫,使用命令查看同步是否有問題(SlaveIORunning和SlaveSQLRunning都必須爲yes)
mysql> show slave status\G # 查看同步狀態
先查看2臺數據庫中默認的數據庫是否一致
DB1
mysql> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | mysql | | performance_schema | | sys | +---------------------+ 4 rows in set (0.00 sec) mysql>
DB2
mysql> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | mysql | | performance_schema | | sys | +---------------------+ 4 rows in set (0.00 sec) mysql>
能夠看到都是有默認的4個數據庫,在DB1上建立一個名爲test1的數據庫
mysql> create database test1; Query OK, 1 row affected (0.00 sec) mysql> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | mysql | | performance_schema | | sys | | test1 | +---------------------+ 5 rows in set (0.00 sec) mysql>
查詢DB2,發現一樣建立了一個test1數據庫
mysql> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | mysql | | performance_schema | | sys | | test1 | +---------------------+ 5 rows in set (0.00 sec) mysql>
而後可使用一樣方式在DB2數據庫建立一個數據庫test2,在分別查詢下(略過)
在DB2上面執行命令刪除test1數據庫命令:
mysql> drop database test1; Query OK, 0 rows affected (0.03 sec) mysql> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | mysql | | performance_schema | | sys | +---------------------+ 4 rows in set (0.01 sec) mysql>
而後查詢DB1,發現也已經同步刪除了
mysql> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | mysql | | performance_schema | | sys | +---------------------+ 4 rows in set (0.01 sec) mysql>
如今2個庫都只剩下4個默認數據庫,如今把DB1停掉
而後在DB2數據庫建立2個數據庫test1和test2,查看已經建立成功
mysql> create database test1; Query OK, 1 row affected (0.00 sec) mysql> create database test2; Query OK, 1 row affected (0.00 sec) mysql> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | mysql | | performance_schema | | sys | | test1 | | test2 | +---------------------+ 6 rows in set (0.00 sec) mysql>
這個時候啓動DB1,查看數據庫
[root@mysql01 ~]# service mysqld stop Stopping mysqld (via systemctl): [ 肯定 ] [root@mysql01 ~]# [root@mysql01 ~]# service mysqld start Starting mysqld (via systemctl): [ 肯定 ] [root@mysql01 ~]# [root@mysql01 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.19-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | mysql | | performance_schema | | sys | | test1 | | test2 | +---------------------+ 6 rows in set (0.00 sec) mysql>
已經同步過來了,用一樣方式,反過來故障測試一遍(略過)
經過以上模擬故障的方式,已經實現了MySQL的主從複製的配置,下一篇將介紹如何配置Keepalived實現VIP自動漂移實現MySQL同步複製的高可用