1. 程式人生 > >mysql5.7雙機主主熱備

mysql5.7雙機主主熱備

熱備這個東西必然消耗資源,對mysql應用的影響暫時還沒有研究,對mysql操作頻繁的場景下必然會有影響。
資料需要備份的請參考我的博文

mysql資料備份
先下載mysql的rpm安裝包。本文件預設是離線環境。
若無特殊說明,操作預設雙機都要進行。
首先下載rpm安裝包
mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar
解壓後安裝

rpm -ivh mysql-community-common-5.7.24-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.24-1.el7.x86_64.rpm 
rpm -ivh mysql-community-client-5.7.24-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.24-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-5.7.24-1.el7.x86_64.rpm

啟動程序

service mysqld start

檢視臨時密碼

grep 'temporary password' /var/log/mysqld.log

修改密碼

/usr/bin/mysql_secure_installation

若想修改密碼策略,請參考我的另一篇博文
Mysql修改密碼策略

#關防火牆

systemctl stop firewalld
systemctl disable firewalld

我們以10.0.0.1和10.0.0.2來作為兩臺主機的ip地址

1.修改兩臺機器的配置檔案

10.0.0.1的配置檔案為(資料目錄一致的話,改下host就能用)

[mysqld]
datadir=/var/lib/mysql
user=mysql
port = 3306
socket= /var/lib/mysql/mysql.sock
log-bin=mysql-bin
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
report-port=3306
report-host=10.0.0.1
#replicate-same-server-id #主要用於同時寫的情況
server_id = 1      #從寫 server_id = 2
log-slave-updates
slave-skip-errors=all
auto_increment_increment=2
auto_increment_offset=1     #從寫 auto_increment_offset = 2

10.0.0.2的配置檔案

datadir=/var/lib/mysql
user=mysql
port = 3306
socket= /var/lib/mysql/mysql.sock
log-bin=mysql-bin
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
report-port=3306
report-host=10.0.0.2
#replicate-same-server-id #主要用於同時寫的情況
server_id =2     
log-slave-updates
slave-skip-errors=all
auto_increment_increment=2
auto_increment_offset=2   

2.配置主機

重啟mysql服務

service mysqld restart
10.0.0.1上執行命令

grant replication slave on *.* to 'slave'@'10.0.0.2' identified by 'root';
show master status\G;

10.0.0.2上執行命令

grant replication slave on *.* to 'slave'@'10.0.0.1' identified by 'root';
show master status\G;

分別記錄兩臺機器的File和position值。
這裡假設10.0.0.1的顯示為如下

 File: mysql-bin.000001
 Position: 891
 Binlog_Do_DB: 
 Binlog_Ignore_DB:

這裡假設10.0.0.2的顯示為如下

 File: mysql-bin.000001
 Position: 892
 Binlog_Do_DB: 
 Binlog_Ignore_DB:

10.0.0.1上執行命令

change master  to master_host='10.0.0.2',master_user='slave',
master_password='root',master_log_file='mysql-bin.000001',
master_log_pos=892;
start slave;
show slave status\G;

10.0.0.2上執行命令

change master  to master_host='10.0.0.1',master_user='slave',
master_password='root',master_log_file='mysql-bin.000001',
master_log_pos=891;
start slave;
show slave status\G;

檢視兩邊的Slave_IO_Running和Slave_SQL_Running的狀態值,都為yes則配置完成。

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

再試一下,兩邊已經是同步的了。
可以通過修改配置檔案指定特定的資料庫。