一、主備模式
也叫冷備模式,通俗點講,就是兩臺服務器都安裝了MySQL,一臺當作主服務器,一臺當作備服務器,備服務器會把主服務器的MySQL數據同步過來,當主服務器故障或者掛掉的時候可以讓備服務器頂上,也保證數據不會丟失
二、注意事項
如果是虛擬機直接拷貝的服務器,要注意修改MySQL的uuid值,兩臺是不可以一樣的
vi /usr/local/mysql/data/auto.cnf
# 隨便修改其中一個數字或字母就行
# 修改保存後記得重啓MySQL
另外一個還需要注意的就是防火牆,要把MySQL的端口添加到防火牆裏面,我這邊直接關閉了
# 查看防火牆狀態,開啓的話會有 active (running) 字樣
systemctl status firewalld.service
# 關閉放獲取,關閉的話會有 inactive (dead) 字樣
systemctl stop firewalld.service
三、開始搭建
1、環境
系統:CentOS 7.0
MySQL:5.7.29
主服務器IP:192.168.74.132
備服務器IP:192.168.74.140
2、創建庫 test
兩臺服務器分別創建數據庫test和一張表
# 創建庫
create database test;
# 創建表
use test;
create table tb_mobile( mobile VARCHAR(20) comment'手機號碼', time timestamp DEFAULT now() comment'時間' );
3、在主機創建同步用戶
# 創建用戶
grant replication slave on *.* to 'demo'@'192.168.74.140' identified by '1qaz2wsx';
# 立即生效
flush privileges;
4、在備機上用剛創建的demo用戶遠程登錄
# 在備機上遠程登錄主機的MySQL
mysql -h192.168.74.132 -udemo -p
5、修改主機的my.cnf文件
# ps:以前版本是沒有 !includedir /etc/my.cnf.d 這一行的
[client]
default-character-set=utf8
socket=/usr/local/mysql/data/mysql.sock
[mysqld]
# 唯一ID
server-id = 1
log-bin=mysql-bin
# 記錄日誌的數據庫
binlog-do-db = test
# 不記錄日誌的數據庫
binlog-ignore-db = mysql
port=3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/usr/local/mysql/mysql-log/error.log
pid-file=/usr/local/mysql/data/mysql.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
6、重啓MySQL
7、在主機上鎖定數據庫表
flush tables with read lock;
8、查看主機狀態
show master status\G;
# 結果如下,記錄一下 File 參數 和 Position 參數
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 154
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
Executed_Gtid_Set:
1 row in set (0.00 sec)
9、修改備服務器的my.cnf文件
[client]
default-character-set=utf8
socket=/usr/local/mysql/data/mysql.sock
[mysqld]
# 不能和主機一樣
server-id = 2
log-bin=mysql-bin
replicate-do-db = test
replicate-ignore-db = mysql,information_schema,performance_schema
port=3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/usr/local/mysql/mysql-log/error.log
pid-file=/usr/local/mysql/data/mysql.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
10、重啓備機MySQL服務
11、在備機進入MySQL執行 change master命令
# 先關閉 slave
stop slave;
# 設置連接
change master to master_host='192.168.74.132',master_user='demo',master_password='1qaz2wsx',master_log_file='mysql-bin.000001',master_log_pos=154;
# 再開啓 slave
start slave;
# 如果遇到報錯,先關閉
stop slave
# 重置 slave
reset slave
# 再重複前面 關閉-設置連接-開啓 的步驟
12、查看slave 的狀態
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.74.132
Master_User: demo
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
# 主要是這兩個 yes 代表通了
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
Replicate_Ignore_DB: mysql,information_schema,performance_schema
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: 531
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: be8b52d3-7f04-11ea-a454-000c29e3bb87
Master_Info_File: /usr/local/mysql/data/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)
四、開始同步測試
1、先在主機執行解鎖
unlock tables;
2、在主機添加數據
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql>
mysql>
mysql> insert into tb_mobile values('1111',now());
Query OK, 1 row affected (0.01 sec)
mysql> select * from tb_mobile;
+--------+---------------------+
| mobile | time |
+--------+---------------------+
| 1111 | 2020-04-16 22:06:46 |
+--------+---------------------+
1 row in set (0.00 sec)
3、在備機上查看同步數據
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from tb_mobile;
+--------+---------------------+
| mobile | time |
+--------+---------------------+
| 1111 | 2020-04-16 22:06:46 |
+--------+---------------------+
1 row in set (0.00 sec)
4、同步成功。結束