環境描述:
[root@MySQL-M ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@MySQL-S ~]# cat /etc/redhat-release
IP 規劃:
MySQL-M 192.168.10.10
MySQL-S 192.168.10.20
配置兩服務伺服器雙主:
MySQL 安裝:
[root@MySQL-M ~]# yum install mysql-server mysql -y
[root@MySQL-M ~]# systemctl mysqld start
編輯 /etc/my.cnf 配置檔案,增加以下内容。設定 server-id。
[mysqld]
server-id = 1 #backup這台設定2。
log-bin = mysql-bin
binlog-ignore-db = mysql,information_schema #忽略寫入binlog日志的庫。
auto-increment-increment = 2 #字段變化增量值。
auto-increment-offset = 1 #初始字段ID為1。
slave-skip-errors = all #忽略所有複制産生的錯誤 。
檢視 主伺服器 log bin 日志和pos 值位置。
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
| mysql-bin.000001 | 6383 | | | |
1 row in set (0.00 sec)
MySQL-M 配置如下:
主伺服器授權:
mysql> grant replication client,replication slave on . to repl@'172.20.2.%' identified by 'repl';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
第二台主伺服器授權:
第二台主伺服器操作:
mysql> change master to
-> master_host='192.168.10.10',
-> master_user='repl',
-> master_password='repl',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=1559;
Query OK, 0 rows affected, 2 warnings (0.05 sec)
mysql> show slave status;
啟動同步功能:
mysql> start slave;
##############################################################
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+--------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+--------------
mysql> show slave status \G
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 172.20.2.46
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1559
Relay_Log_File: mysql-relay.000003
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
主伺服器操作:
-> master_host='192.168.10.20',
###########################################################
########################################################################
測試:
主伺服器 10.10:
mysql> create database test;
mysql> show databases;
+--------------------+
| Database |
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
| wangbo |
6 rows in set (0.00 sec)
mysql> use test;
mysql> create table t1 ( id int, age int );
mysql> insert into t1 values (1,10),(2,20),(3,30);
mysql> select * from t1;
+------+------+
| id | age |
| 1 | 20 |
| 2 | 30 |
| 3 | 30 |
從庫插入:
mysql> insert into t1 values (4,40),(5,50),(6,60);
可以看到已經成功同步過去,同樣在backup插入到user表資料,一樣同步過去,雙主就做成功了。
安裝 keepalived :
[root@MySQL-M ~]#yum -y install keepalived
[root@master ~]# vi /etc/keepalived/keepalived.conf
! Configuration File forkeepalived
global_defs {
notification_email {br/>[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_HA #辨別,雙主相同
vrrp_instance VI_1 {
state BACKUP #兩台都設定BACKUP
interface eth0
virtual_router_id 51 #主備相同
priority 100 #優先級,backup設定90
advert_int 1
nopreempt #不主動搶占資源,隻在master這台優先級高的設定,backup不設定
authentication {
auth_type PASS
auth_pass 1111
virtual_ipaddress {
192.168.10.100
virtual_server 192.168.10.100 3306 {
delay_loop 2
#lb_algo rr #LVS算法,用不到,我們就關閉了
#lb_kind DR #LVS模式,如果不關閉,備用伺服器不能通過VIP連接配接主MySQL
persistence_timeout 50 #同一IP的連接配接60秒内被配置設定到同一台真實伺服器
protocol TCP
real_server 192.168.10.10 3306 {本地mysql,backup也要寫檢測本地mysql
weight 3
notify_down /usr/local/keepalived/mysql.sh #當mysq服down時,執行此腳本,殺死keepalived實作切換
TCP_CHECK {
connect_timeout 3 #連接配接逾時
nb_get_retry 3 #重試次數
delay_before_retry 3 #重試間隔時間
[root@master ~]# vi /usr/local/keepalived/mysql.sh
#!/bin/bash
pkill keepalived
[root@master ~]# chmod +x /usr/local/keepalived/mysql.sh
[root@master ~]# /etc/init.d/keepalived start
注:
#backup伺服器隻修改priority為90、nopreempt不設定、real_server設定本地IP。
#授權兩台Mysql伺服器允許root遠端登入,用于在其他伺服器登陸測試!
mysql> grant all on . to' root'@'192.168.10.%' identified by 'oldboy123';
3、測試高可用性
1、通過Mysql用戶端通過VIP連接配接,看是否連接配接成功。
2、停止master這台mysql服務,是否能正常切換過去,可通過ip addr指令來檢視VIP在哪台伺服器上。
3、可通過檢視/var/log/messges日志,看出主備切換過程
4、master伺服器故障恢複後,是否主動搶占資源,成為活動伺服器。