mysql的雙機熱備
mysql
雙機熱備,就是要保持兩個數據庫的狀態自動同步。對任何一個數據庫的操做都自動應用到另一個數據庫,始終保持兩個數據庫數據一致。nginx
這樣的作法好處在於:laravel
一、能夠作災備,其中一個壞了能夠切換到另外一個;sql
二、能夠作負載均衡,能夠請求分攤到其中任何一臺上,提升網站吞吐量。數據庫
這裏我使用的是mariadbvim
[root@nginx2 ~]# mysql -V服務器
mysql Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1負載均衡
一、在數據庫A上建立用戶並受權登陸ide
grant replication slave on *.* to 'rep'@'172.25.254.131' identified by '123456';網站
二、開啓A服務器的binarylog
vim /etc/my.cnf
log-bin = mysql-bin #開啓mysql的binlog日誌功能
binlog_format = mixed #binlog日誌格式
server-id = 1 #服務器id號
read-only=0 #關閉只讀,能夠讀寫
binlog-do-db=laravel #指定對db_nameA記錄二進制日誌
binlog-ignore-db=mysql #指定不對db_nameB記錄二進制日誌
#relay_log=mysql-relay-bin#開啓relay-log日誌,relay-log日誌記錄的是從服務器I/O線程將主服務器的二進制日誌讀取過來記錄到從服務器本地文件,而後SQL線程會讀取relay-log日誌的內容並應用到從服務器
三、登陸到B服務器上,開啓中繼
vim /etc/my.cnf
log-bin = mysql-bin #開啓mysql的binlog日誌功能
binlog_format = mixed #binlog日誌格式
server-id = 1 #服務器id號
read-only=0 #關閉只讀,能夠讀寫
binlog-do-db=laravel #指定對db_nameA記錄二進制日誌
binlog-ignore-db=mysql #指定不對db_nameB記錄二進制日誌
relay_log=mysql-relay-bin#開啓relay-log日誌,relay-log日誌記錄的是從服務器I/O線程將主服務器的二進制日誌讀取過來記錄到從服務器本地文件,而後SQL線程會讀取relay-log日誌的內容並應用到從服務器
四、在B服務器上開啓同步
CHANGE MASTER TO MASTER_HOST='172.25.254.134', MASTER_USER='rep', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000006', MASTER_LOG_POS=610;
五、重啓B服務器數據庫
systemctl restart mariadb
六、檢查同步的狀態
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.254.134
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 610
Relay_Log_File: mariadb-relay-bin.000003
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000006
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: 610
Relay_Log_Space: 825
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: 2
七、上述,這樣的只能實現服務器A改變同步到服務器B,是單向的。
要將服務器B的改變反回來也能夠同步到服務器A,只需將以上操做再來一次便可!