MySQL集羣

MySQL集羣

MySQL主從複製MySQL雙主複製

實現方式:基於GTID的多源複製(MySQL5.7版本纔有的特性)

                                            

192.168.1.94服務器中操作:

[root@server1 ~]# vim /etc/my.cnf

[mysqld]
#bin-log 開啓二進制日誌
server_id=94
log_bin=mysql-bin
## GTID
gtid_mode=ON
enforce_gtid_consistency=true

重啓MySQL使配置文件生效

[root@server1 ~]# systemctl restart mysqld
[root@server1 ~]# ss -antp | grep 3306 #確保端口啓動成功
LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=7697,fd=32))

mysql> show master status; #查看二進制日誌是否開啓
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> select @@server_uuid; #查看GTID的UUID
+--------------------------------------+
| @@server_uuid |
+--------------------------------------+
| e073964e-eba4-11e8-9eb4-000c2978fbe2 |
+--------------------------------------+
1 row in set (0.00 sec)

創建一個數據同步用戶

mysql> GRANT REPLICATION SLAVE ON *.* TO 'cmzw'@'192.168.1.%' IDENTIFIED BY 'Cmzw.2018';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> select User,Host from mysql.user;
+---------------+-------------+
| User | Host |
+---------------+-------------+
| cmzw |192.168.1.%|
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-------------+
4 rows in set (0.00 sec)

如果是已經使用過一段時間的數據庫,先把所有的數據入到數據庫中,本文中的數據庫是沒有進行使用。

導出數據

mysqldump --user=root --password=Long110427. --flush-logs --master-data=2 --lock-all-tables --all-databases > /tmp/aa.sql

scp /tmp/aa.sql 192.168.1.95:/tmp/aa.sql
scp /tmp/aa.sql 192.168.1.96:/tmp/aa.sql

導入數據

mysql> SOURCE /tmp/aa.sql

 

192.168.1.95服務器中的操作

開啓二進制日誌,啓動GTID。

[root@server2 ~]# vim /etc/my.cnf

[mysqld]
server_id=95
log_bin=mysql-bin
gtid_mode=on
enforce_gtid_consistency=true

重啓MySQL使配置文件生效

[root@server1 ~]# systemctl restart mysqld
[root@server1 ~]# ss -antp | grep 3306 #確保端口啓動成功
LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=7697,fd=32))

mysql> select @@server_uuid; #查看GTID的UUID
+--------------------------------------+
| @@server_uuid |
+--------------------------------------+
| e073964e-eba4-11e8-9eb4-000c2978fbe2 |
+--------------------------------------+
1 row in set (0.00 sec)

連接192.168.1.94服務器的數據庫

mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.1.94',
-> MASTER_USER='cmzw',
-> MASTER_PASSWORD='Cmzw.2018',
-> MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status \G; #確保slave中的IO線程和SQL線程是Yes(下面紅色的部分,如果不是請查看錯誤日誌)
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.94
Master_User: cmzw
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 447
Relay_Log_File: server2-relay-bin.000002
Relay_Log_Pos: 660
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: 447
Relay_Log_Space: 869
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: 94
Master_UUID: e073964e-eba4-11e8-9eb4-000c2978fbe2
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: e073964e-eba4-11e8-9eb4-000c2978fbe2:1
Executed_Gtid_Set: e073964e-eba4-11e8-9eb4-000c2978fbe2:1
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

 

返回192.168.1.94服務器進行連接192.168.1.95數據庫。(因爲是雙主複製)

注意:如果創建的用戶是不是以192.168.1.%的權限,還需要創建一個可以連接192.168.1.95的賬戶

連接192.168.1.95的數據庫

mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.1.95',
-> MASTER_USER='cmzw',
-> MASTER_PASSWORD='Cmzw.2018',
-> MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

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.1.95
Master_User: cmzw
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: server1-relay-bin.000002
Relay_Log_Pos: 367
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: 154
Relay_Log_Space: 576
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: 95
Master_UUID: d56c691c-eba7-11e8-84e0-000c292db05e
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: e073964e-eba4-11e8-9eb4-000c2978fbe2:1
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

這裏雙主複製已經算是成功了!

192.168.1.96服務器中操作(配置從服務器)

在MySQL的數據目錄中有兩個文件master.info relay_log.info 文件,要把這兩個文件寫入到表中

修改配置文件並重新啓動

[root@server3 ~]# vi /etc/my.cnf

[mysqld]
server_id=96
gtid_mode=ON
enforce_gtid_consistency=true
master_info_repository=TABLE
relay_log_info_repository=TABLE

[root@server3 ~]# systemctl restart mysqld
[root@server3 ~]# ss -antp |grep 3306
LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=74430,fd=30))

接下來進行做主從,192.168.1.96既是192.168.1.94的從服務器也是192.168.1.95的從服務器。

mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.1.94',
-> MASTER_USER='cmzw',
-> MASTER_PASSWORD='Cmzw.2018',
-> MASTER_AUTO_POSITION=1 FOR CHANNEL "master2";
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.1.95',
-> MASTER_USER='cmzw',
-> MASTER_PASSWORD='Cmzw.2018',
-> MASTER_AUTO_POSITION=1 FOR CHANNEL "master2";
Query OK, 0 rows affected, 2 warnings (0.01 sec)

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.1.94
Master_User: cmzw
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 447
Relay_Log_File: server3-relay-bin-master1.000005
Relay_Log_Pos: 454
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:

*************************** 2. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.95
Master_User: cmzw
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: server3-relay-bin-master2.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章