標籤:資料庫主從
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的雙機熱備