mysql的雙機熱備
mysql的雙機熱備
雙機熱備,就是要保持兩個數據庫的狀態自動同步。對任何一個數據庫的操作都自動應用到另外一個數據庫,始終保持兩個數據庫數據一致。
這樣的做法好處在於:
1、可以做災備,其中一個壞了可以切換到另一個;
2、可以做負載均衡,可以請求分攤到其中任何一臺上,提高網站吞吐量。
這裏我使用的是mariadb
[[email protected] ~]# mysql -V
mysql Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1
1、在數據庫A上創建用戶並授權登錄
grant replication slave on *.* to ‘rep‘@‘172.25.254.131‘ identified by ‘123456‘;
2、開啟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日誌的內容並應用到從服務器
3、登錄到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日誌的內容並應用到從服務器
4、在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;
5、重啟B服務器數據庫
systemctl restart mariadb
6、檢查同步的狀態
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
7、上述,這樣的只能實現服務器A改變同步到服務器B,是單向的。
要將服務器B的改變反回來也可以同步到服務器A,只需將以上操作再來一次即可!
本文出自 “跛鱉千裏,貴在分享” 博客,請務必保留此出處http://chenxiaotao.blog.51cto.com/11430805/1959053
mysql的雙機熱備