环境描述:
[root@MySQL-M ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@MySQL-S ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
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> 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;
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+--------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+--------------
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
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:
主服务器操作:
mysql> change master to
-> master_host='192.168.10.20',
-> 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;
########################################################################
测试:
主服务器 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 |
+------+------+
6 rows in set (0.00 sec)
###########################################################
从库插入:
mysql> use test;
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';
mysql> flush privileges;
3、测试高可用性
1、通过Mysql客户端通过VIP连接,看是否连接成功。
2、停止master这台mysql服务,是否能正常切换过去,可通过ip addr命令来查看VIP在哪台服务器上。
3、可通过查看/var/log/messges日志,看出主备切换过程
4、master服务器故障恢复后,是否主动抢占资源,成为活动服务器。