雙機熱備概念:
保持兩個數據庫的狀態自動同步。對任何一個數據庫的操作都自動應用到另外一個數據庫,始終保持兩個數據庫數據一致。
使用場景/解決問題
可以做災備,其中一個壞了可以切換到另一個。
可以做負載均衡,可以將請求分攤到其中任何一臺上,提高網站吞吐量。
msyql熱備工作原理圖
配置要求:
1.需要服務器兩臺A,B
2.服務器上安裝最好是相同版本的mysql數據庫(建議)
安裝步驟
[假定A服務器的ip爲10.1.2.131] A作爲主(master)
[假定B服務器的ip爲10.1.2.132] B作爲從(slave)
A主服務器的配置:
1.登陸A主數據庫 -》創建數據庫 -》 添加授權用戶
[root@10-1-2-131 ~]# mysql -uroot -p #進入MySQL控制檯
mysql>grant replication slave on *.* to 'user'@'10.1.2.132' identified by '10isp.com' with grant option; #授權用戶user只能從B從10.1.2.132這個IP訪問主服務器10.1.2.131上面的數據庫,並且只具有數據庫備份的權限
mysql>flush privileges; #刷新系統授權表
10.1.2.132是B從服務器的ip地址。 只允許B登錄,安全。
用戶名: user
密 碼: 10isp.com
用戶名和密碼在B上面要用。
2.在mysql的配置文件/etc/my.cnf中有添加如下參數。
[mysqld]
log-bin=mysql-bin #開啓主服務器的binarylog
server-id=1 #服務器id
#binlog-do-db=demo #需要備份的數據庫 如果備份多個數據庫就添加多條,不添加就代表全部數據庫備份
binlog-ignore-db=information_schema #忽略的數據庫 information_schema
#binlog-ignore-db=performance_schema #忽略的數據庫 performance_schema
#binlog-ignore-db=sys #忽略的數據庫 sys
binlog-ignore-db=mysql #忽略的數據庫 mysql
3.查看A主數據庫狀態
mysql>show master status
查詢結果顯示:
+----------------+--------+-------------+------------------------+
| File |Position| Binlog_Do_DB| Binlog_Ignore_DB |
+----------------+--------+-------------+------------------------+
|mysql-bin.000001| 154 | |mysql,information_schema|
+----------------+--------+-------------+------------------------+
以上的FIEL二進制文件和文件位置position 這兩項在配置從服務器時候要用
B從服務器的配置:
1.在mysql的配置文件/etc/my.cnf中有添加如下參數。
[mysqld]
log-bin=mysql-bin #開啓主服務器的binarylog
server-id=2 #從服務器id 如果是多機備份就注意這裏的id的指定
#replicate-do-db=demo #需要備份的數據庫 如果備份多個數據庫就添加多條,不添加就代表全部數據庫備份
replicate-ignore-db=information_schema #忽略的數據庫 information_schema
#replicate-ignore-db=performance_schema #忽略的數據庫 performance_schema
#replicate-ignore-db=sys #忽略的數據庫 sys
replicate-ignore-db=mysql #忽略的數據庫 mysql
2.重啓mysql服務
[root@10-1-2-132 ~]# /etc/init.d/mysqld restart
3.登陸B從數據庫-》配置主A的mysql用戶信息
[root@10-1-2-132 ~]# mysql -uroot -p #進入MySQL控制檯
mysql>change master to master_host='10.1.2.131',master_port=3306,master_user='user',master_password='10isp.com',master_log_file='mysql-bin.000001',master_log_pos=154;
在B數據庫上配置主數據庫的信息 IP:10.1.2.131 賬號:user 密碼:10isp.com
master_host : A主服務器的ip地址
master_log_file :A主服務器上的二進制文件明
master_user : A主服務器授權的用戶名
master_password : A主服務器授權的密碼
master_log_pos :二進制文件開始備備份的起始位置
以上參數配置A主的時候都有提到。
4.啓動B從服務器的兩條線程
mysql>start slave;
5.查詢B從的線程狀態
mysql>show slave status
以下爲B從的數據庫線程的日誌信息,
主要查看兩條標紅的線程是否開啓,如果兩條線程都YES 基本就完成了所有的配置
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.2.131
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 1067
Relay_Master_Log_File: mysql-bin.000001
SLAVE_IO_RUNNING: YES
SLAVE_SQL_RUNNING: YES
Replicate_Do_DB:
Replicate_Ignore_DB: information_schema,mysql
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: 1275
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: 7c681299-8e5b-11e8-925b-facd2dcb4a00
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:
以上是所有主從配置步驟,具體實施遇到問題可參考:http://py.10isp.com/?p=634
在主從配置中遇到的各種奇葩問題的彙總和解決方案
第一種
報錯: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
解決方法:
這個錯誤提示。即主從架構中使用了相同的UUID。檢查server_id系統變量,已經是不同的設置,那原因是?接下來爲具體描述。
master_mysql> show variables like 'server_id';
slave_mysql> show variables like 'server_id';
查看是不同的。
但是查看/mysql/data/auto.cnf發現裏面的UUID是哦相同的。原因是mysql是直接從節點1上拷貝過來而導致。
解決:mv /mysql/data/auto.cnf /mysql/data/auto.cnf.bak 重啓mysql解決 或者修改 server UUID