MySQL基於binlog實現主從複製,實現一主兩從
在移動網際網路開發中,為了減少資料庫單機的壓力、增加資料庫的併發訪問能力,同時大多數移動網際網路專案均是讀多寫少,鑑於此,將資料庫配置一主多從顯得尤為重要,下面我們將基於MySQL實現一主多從的配置。
準備工作
虛擬機器準備
準備裝有CentOS 7的虛擬機器兩臺,IP為192.168.0.10 與 192.168.0.11
IP可以根據自己的環境進行設定
MySQL安裝與啟動
rpm -ivh mysql57-community-release-el7-8.noarch.rpm 驗證repo源
yum install mysql-server 安裝MySQL
systemctl start mysqld 啟動MySQL
登入到MySQL
由於MySQL 5.7 版本之後對於root賬號有一個隨機密碼,可以通過 grep "password" /var/log/mysqld.log
獲得隨機密碼
mysql -u root -p 回車
貼上隨機密碼
更改密碼
由於root預設的隨機密碼不具有操作許可權,所以需要更改root的密碼之後才能對資料庫進行操作
set password for [email protected]=password(‘1qaz2wsx’); 設定密碼
如果需要設定簡單一點的密碼,可以選擇降低安全策略
set global validate_password_length=1
set global validate_password_policy=0
開放IP訪問
GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY ‘root’ WITH GRANT OPTION;
FLUSH PRIVILEGES;
使用MySQL中二進位制日誌實現主從同步的原理分析
MySQL實現主從同步的原理是 MySQL使用了3個執行緒來進行資料複製功能,主伺服器一個,從伺服器上兩個。當從伺服器發出start slave
主從配置
配置主伺服器
建立repl使用者
repl使用者主要用於資料複製
建立repl使用者並設定密碼為repl
CREATE USER repl IDENTIFIED BY 'repl';
為使用者repl使用者賦予REPLICATION SLAVE 許可權
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'repl' ;
修改my.cnf檔案
進入到/etc目錄,編輯my.cnf檔案,在 [mysqld]
區段下增加如下配置
[mysqld]
# enable mysql bin
log-bin=mysql-bin
# server id
server-id=10
重啟MySQL
systemctl restart mysqld
登入到MySQL,檢視狀態
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
配置從伺服器
修改my.cnf檔案
進入/etc目錄,編輯my.cnf檔案,在[mysqld]區段下增加如下配置
[mysqld]
# server id
server-id=11
relay-log=slave-relay-bin
relay-log-index=slave-relay-bin.index
read_only=1
重啟MySQL
systemctl restart mysqld
登入到MySQL,建立同步連線
mysql> CHANGE MASTER TO master_host='192.168.0.10',master_port=3306,master_user='repl',master_password='repl',master_log_file='mysql-bin.000001',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
啟動slave
START SLAVE;
檢視狀態
SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.10
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: slave-relay-bin.000016
Relay_Log_Pos: 320
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: 527
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: 10
Master_UUID: 217af41a-eeeb-11e7-8ed4-000c295dbc95
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)
\G 表示格式化輸出
Slave_IO_Running
與Slave_SQL_Running
均為 YES 表示主從配置成功另外的主機按照主機11從機配置即可
驗證
在主機192.168.0.10上建立資料庫,並建立user表驗證主從配置狀態
CREATE DATABASE test;
CREATE TABLE t_user( id INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50));
在主機192.168.0.11上登入到MySQL檢視資料庫時候存在
SHOW DATABASES;
USE test;
SHOW TABLES;
OK, 至此,關於MySQL的主從配置已經實現,如果閱讀中發現有什麼問題,歡迎指正!