1.簡介
MySQL作為世界上使用最為廣泛的數(shù)據(jù)庫之一,免費(fèi)是其原因之一。但不可忽略的是它本身的功能的確很強(qiáng)大。隨著技術(shù)的發(fā)展,在實(shí)際的生產(chǎn)環(huán)境中,由單臺MySQL數(shù)據(jù)庫服務(wù)器不能滿足實(shí)際的需求。此時(shí)數(shù)據(jù)庫集群就很好的解決了這個(gè)問題了。采用MySQL分布式集群,能夠搭建一個(gè)高并發(fā)、負(fù)載均衡的集群服務(wù)器(這篇博客暫時(shí)不涉及)。在此之前我們必須要保證每臺MySQL服務(wù)器里的數(shù)據(jù)同步。數(shù)據(jù)同步我們可以通過MySQL內(nèi)部配置就可以輕松完成,主要有主從復(fù)制和主主復(fù)制。

主從復(fù)制原理
2.環(huán)境說明
兩臺linux虛擬主機(jī)
Linux版本CentOS 7.2、MySQL 5.7
ip:192.168.178.198(主)、192.168.178.199(從)
3.主從復(fù)制
- 主服務(wù)器 開啟 log-bin
#log_bin
log-bin=/var/lib/mysql/mysql-bin
server-id=1
- 從服務(wù)器 設(shè)置server-id
server-id=2
- 開始構(gòu)建主從復(fù)制
主庫操作
mysql> CREATE USER 'repl1'@'%' IDENTIFIED BY '123456';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl1'@'192.168.178.198' IDENTIFIED BY '123456';
mysql> FLUSH PRIVILEGES;
mysql> SHOW master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 2723 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 2079 |
| mysql-bin.000002 | 177 |
| mysql-bin.000003 | 2723 |
+------------------+-----------+
3 rows in set (0.00 sec)
mysql> FLUSH TABLE WITH READ LOCK; # 全庫讀鎖
[root@localhost ~]# mysqldump -uroot -p123456 -S /var/lib/mysql/mysql.sock -A -B --events|gzip >/opt/rep.sql.gz # 數(shù)據(jù)量大
[root@localhost ~]# mysqldump -uroot -p123456 -S /var/lib/mysql/mysql.sock -A -B --events --master-data=2 >/opt/rep.sql
mysql> UNLOCK tables; # 解鎖
從庫先將主庫的全備數(shù)據(jù)同步
[root@localhost ~]# mysql-uroot -p123456 -S /var/lib/mysql/mysql.sock >/opt/rep.sql
下面進(jìn)行主從復(fù)制
mysql> CHANGE MASTER TO MASTER_HOST='192.168.178.198',MASTER_USER='repl1',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=2723;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)
# 查看 master.info
root@localhost ~]# cat /var/lib/mysql/master.info
25
mysql-bin.000003
2723
192.168.178.198
repl1
123456
3306
60
0
0
30.000
0
86400
0
查看主從復(fù)制是否配置成功, 當(dāng)看到Slave_IO_Running: YES、Slave_SQL_Running: YES才表明狀態(tài)正常
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.178.198
Master_User: repl1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 154
Relay_Log_File: localhost-relay-bin.000003
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000004
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: 744
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: 1
Master_UUID: f6e6d2d0-cae8-11e7-8def-000c29462b91
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)
- ok,現(xiàn)在主從復(fù)制的所有操作已經(jīng)完成了,現(xiàn)在在主庫進(jìn)行數(shù)據(jù)庫操作就會(huì)自動(dòng)同步到從庫