一、主主模式
也叫熱備模式,通俗點講,就是兩臺服務器都安裝了MySQL,這兩臺服務器互爲主備,A機器產生數據會同步到B機器,同理,B機器產生數據也會同步到A機器。
二、注意事項
如果是虛擬機直接拷貝的服務器,要注意修改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
A服務器IP:192.168.74.132
B服務器IP:192.168.74.140
2、創建庫 test
A/B兩臺服務器分別創建數據庫 test 和一張表 tb_mobile
# 創建庫
create database test;
# 創建表
use test;
create table tb_mobile( mobile VARCHAR(20) comment'手機號碼', time timestamp DEFAULT now() comment'時間' );
3、在A服務器創建同步用戶
# 創建用戶
grant replication slave on *.* to 'demo'@'192.168.74.140' identified by '1qaz2wsx';
# 立即生效
flush privileges;
4、在B服務器創建同步用戶
# 創建用戶
grant replication slave on *.* to 'demo'@'192.168.74.132' identified by '1qaz2wsx';
# 立即生效
flush privileges;
5、在A/B服務器上分別用剛創建的demo用戶遠程登錄
# 在A服務器上遠程登錄主機的MySQL
mysql -h192.168.74.140 -udemo -p
# 在B服務器上遠程登錄主機的MySQL
mysql -h192.168.74.132 -udemo -p
6、修改A服務器的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
# 打開binlog,打開該選項纔可以通過I/O寫到Slave的relay-log,也是可以進行replication的前提。
# MYSQL是基於二進制的日誌來做同步的,每個日誌文件默認大小爲 1G
log-bin=mysql-bin
# 表示需要記錄二進制日誌的數據庫。如果有多個數據可以用逗號分隔,或者使用多個binlog-do-dg選項
binlog-do-db = test
# 不需要記錄二進制日誌的數據庫,如果有多個數據庫可用逗號分隔,或者使用多binglog-ignore-db選項
binlog-ignore-db = mysql
# 用從屬服務器上的日誌功能
# 配置從庫上的更新操作是否寫入二進制文件,如果這臺從庫,還要做其他從庫的主庫,那麼就需要打這個參數,以便從庫的從庫能夠進行日誌同步
log-slave-updates
# 日誌寫操作就把日誌文件寫入硬盤一次(對日誌信息進行一次同步)。n=1是最安全的做法,但效率最低。默認設置是n=0
# 詳細解釋查看文章最下面
sync_binlog = 1
# auto_increment,控制自增列AUTO_INCREMENT的行爲
# 用於MASTER-MASTER之間的複製,防止出現重複值
# auto_increment_offset=1設置步長,這裏設置爲1,這樣Master的auto_increment字段產生的數值是:1, 3, 5, 7, …等奇數ID
# 設置2就是2、4、6、8等
auto_increment_offset = 1
# auto_increment_increment=n有多少臺服務器,n就設置爲多少
auto_increment_increment = 2
# 進行鏡像處理的數據庫
# 表示需要同步的數據庫,如果有多個數據可用逗號分隔,或者使用多個replicate-do-db選項
replicate-do-db = test
# 不進行鏡像處理的數據庫
# 表示不需要同步的數據庫,如果有多個數據庫可用逗號分隔,或者使用多個replicate-ignore-db選項
replicate-ignore-db = mysql,information_schema
# MySQL端口
port=3306
# MySQL安裝目錄
basedir=/usr/local/mysql
# MySQL數據存放目錄
datadir=/usr/local/mysql/data
# socket 文件是在 Unix/Linux 環境下才有的,用戶在 Unix/Linux 環境下客戶端連接可以不通過 TCP/IP 網絡而直接使用 Unix Socket 來連接MySQL
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]
# MySQL的錯誤日誌文件
log-error=/usr/local/mysql/mysql-log/error.log
# pid file 是 mysqld 應用程序在 Unix/Linux 環境下的一個進程文件,和許多其他Unix/Linux 服務端程序一樣,存放着自己的進程 id
pid-file=/usr/local/mysql/data/mysql.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
7、修改B服務器的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
binlog-do-db = test
binlog-ignore-db = mysql
log-slave-updates
sync_binlog = 1
auto_increment_offset = 2
auto_increment_increment = 2
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
8、分別重啓A/B兩臺服務器的MySQL
9、在A/B兩臺服務器上執行鎖定數據庫表操作,防止加入新的數據
flush tables with read lock;
10、查看A/B兩臺服務器上的 master 狀態
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)
11、分別在A/B兩臺服務器上執行 change master to 指令指定同步位置
# 在A服務器上執行
# 先關閉 slave
stop slave;
# 設置連接
change master to master_host='192.168.74.140',master_user='demo',master_password='1qaz2wsx',master_log_file='mysql-bin.000001',master_log_pos=154;
# 再開啓 slave
start slave;
--------------------------------------------------------------------------------
# 在B服務器上執行
# 先關閉 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、分別在A/B服務器上查看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)
13、分別在A/B兩臺服務器上解鎖
unlock tables;
四、開始同步測試
1、分別在A/B服務器上添加數據
# 這裏只寫一個,剩下的自己寫
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)
2、分別在A/B兩臺服務器上查看數據有沒有同步過來
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)
3、同步成功,結束
五、名詞解釋
1、Sync_binlog=1 Or N
Sync_binlog的默認值是0,這種模式下,MySQL不會同步到磁盤中去。這樣的話,Mysql依賴操作系統來刷新二進制日誌binary log,就像操作系統刷新其他文件的機制一樣。因此如果操作系統或機器(不僅僅是Mysql服務器)崩潰,有可能binlog中最後的語句丟失了。要想防止這種情況,可以使用sync_binlog全局變量,使binlog在每N次binlog寫入後與硬盤同步。當sync_binlog變量設置爲1是最安全的,因爲在crash崩潰的情況下,你的二進制日誌binary log只有可能丟失最多一個語句或者一個事務。但是,這也是最慢的一種方式(除非磁盤有使用帶蓄電池後備電源的緩存cache,使得同步到磁盤的操作非常快)。
即使sync_binlog設置爲1,出現崩潰時,也有可能表內容和binlog內容之間存在不一致性。如果使用InnoDB表,Mysql服務器處理COMMIT語句,它將整個事務寫入binlog並將事務提交到InnoDB中。如果在兩次操作之間出現崩潰,重啓時,事務被InnoDB回滾,但仍然存在binlog中。可以用-innodb-safe-binlog選項來增加InnoDB表內容和binlog之間的一致性。(註釋:在Mysql 5.1版本中不需要-innodb-safe-binlog;由於引入了XA事務支持,該選項作廢了),該選項可以提供更大程度的安全,使每個事務的binlog(sync_binlog=1)和(默認情況爲真)InnoDB日誌與硬盤同步,該選項的效果是崩潰後重啓時,在滾回事務後,Mysql服務器從binlog剪切回滾的InnoDB事務。這樣可以確保binlog反饋InnoDB表的確切數據等,並使從服務器保持與主服務器保持同步(不接收回滾的語句)。
2、Auto_increment_offset和Auto_increment_increment
Auto_increment_increment和auto_increment_offset用於主-主服務器(master-to-master)複製,並可以用來控制AUTO_INCREMENT列的操作。兩個變量均可以設置爲全局或局部變量,並且假定每個值都可以爲1到65,535之間的整數值。將其中一個變量設置爲0會使該變量爲1。
這兩個變量影響AUTO_INCREMENT列的方式:auto_increment_increment控制列中的值的增量值,auto_increment_offset確定AUTO_INCREMENT列值的起點。
如果auto_increment_offset的值大於auto_increment_increment的值,則auto_increment_offset的值被忽略。例如:表內已有一些數據,就會用現在已有的最大自增值做爲初始值。
3、Master-connect-retry
master-connect-retry=n表示從服務器與主服務器的連接沒有成功,則等待n秒(s)後再進行管理方式(默認設置是60s)。如果從服務器存在mater.info文件,它將忽略些選項。
4、參考文檔
# 解釋 Replication 概念
https://blog.csdn.net/francis123580/article/details/80809596
# 參考的雙機搭建文章和名詞解釋
https://www.cnblogs.com/jianmingyuan/p/10903682.html