因生产需要对MySQL做高可用,同时,资源有限,因此采用双节点主从+keepalived方式实现高勇。另外因需要大批量部署MySQL集群,需要采用模板化部署,本方案采用将MySQL容器化,实现MySQL模板化配置部署。
部署环境及软件:centos7.6、两个节点、keepalived-2.2.7、MySQL5.7.30
mysql采用mysql官方基础镜像mysql5.7
主节点ip:192.168.112.121
从节点ip:192.168.112.122
在主从节点上分别部署mysql容器,启动脚本
主节点(192.168.112.121)my.cnf配置
[client]
port=3306
default-character-set=utf8
[mysqld]
port=3306
server-id=1
log-bin=mysql-bin
binlog_do_db=xxx_db
binlog_format=mixed
character-set-server=utf8
从节点(192.168.112.122)my.cnf配置
[client]
port=3306
default-character-set=utf8
[mysqld]
port=3306
server-id=2
log-bin=mysql-bin
binlog_do_db=xxx_db
binlog_format=mixed
character-set-server=utf8
主数据库192.168.112.121
#!/bin/bash
docker rm -f msyql-server-1 || true
BASE_DIR=/home/services/mysql/mysql-1
docker run -d -p 3306:3306 \
--name mysql-server-1 \
--privileged=true --user=mysql \
-e MYSQL_ROOT_PASSWORD=xxxx \
-v $BASE_DIR/conf/my.cnf:/etc/mysql/my.cnf \
-v $BASE_DIR/data:/var/lib/mysql \
mysql:5.7
从数据库192.168.112.122
#!/bin/bash
docker rm -f msyql-server-2 || true
BASE_DIR=/home/services/mysql/mysql-1
docker run -d -p 3306:3306 \
--name mysql-server-2 \
--privileged=true --user=mysql \
-e MYSQL_ROOT_PASSWORD=xxxx \
-v $BASE_DIR/conf/my.cnf:/etc/mysql/my.cnf \
-v $BASE_DIR/data:/var/lib/mysql \
mysql:5.7
启动数据库之后做数据库主从配置
登录主数据库创建主从同步账户slave_user,并查看master状态,记录下file、psition值,配置mysql从库时使用
操作步骤如下:
#授权slave_user复制权限
grant replication slave on *.* to slave_user@'%' identified BY '15dGt73@Adm';
flush PRIVILEGES;
# 需要登录验证账户及密码是否正确,如果能够正常登录,则继续后续步骤,否则需要排查原因并解决
mysql -h192.168.112.121 -uslave_user -p15dGt73@Adm
#查看master状态,找到master_log_file和MASTER_LOG_POS的值
show master status;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 2117| probe_db | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
# 选择从数据库执行
change master TO
MASTER_HOST='192.168.112.121',
master_port=3306,
master_user='slave_user',
master_password='15dGt73@Adm',
master_log_file='mysql-bin.000003',
MASTER_LOG_POS=2117;
验证从节点Slave_IO_Running、Slave_SQL_Running都为yes则主从配置成功
验证数据库主从同步是否生效,
第一组测试:在主库创建probe_db数据库,从库自动创建probe_db数据库,经验证从库自动创建;
第二组测试:数据库添加表,从库是否会自动创建数据表
在主数据库中执行创建数据库,如果从数据库也自动创建,则表示主从同步生效
CREATE TABLE `probe_db`.`ping_test_result` (
`id` INT NOT NULL,
`ip` VARCHAR(32) NOT NULL,
`package_loss_ratio` FLOAT NOT NULL)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_esperanto_ci;
从数据库自动创建表
第三组测试:从主数据插入一组数据,验证是否会将数据同步到从库;
INSERT INTO ping_test_result(id, ip, package_loss_ratio) VALUES(1, '8.8.8.8', 0.1);
INSERT INTO ping_test_result(id, ip, package_loss_ratio) VALUES(2, '8.8.8.8', 0.1);
INSERT INTO ping_test_result(id, ip, package_loss_ratio) VALUES(3, '8.8.8.8', 0.1);
检测从库也是自动生产3条数据,数据同步生效
第四组测试:往从库里插入一组数据,验证从库是否自动将数据同步到主库;
INSERT INTO ping_test_result(id, ip, package_loss_ratio) VALUES(4, '8.8.8.8', 0.1);
INSERT INTO ping_test_result(id, ip, package_loss_ratio) VALUES(5, '8.8.8.8', 0.1);
INSERT INTO ping_test_result(id, ip, package_loss_ratio) VALUES(6, '8.8.8.8', 0.1);
检测主库也是自动生产3条数据,数据同步生效
第五组测试:关闭主库,往从库里插入一组数据,验证从库在主库正常启动后是否自动将数据同步到主库;
INSERT INTO ping_test_result(id, ip, package_loss_ratio) VALUES(7, '8.8.8.8', 0.1);
INSERT INTO ping_test_result(id, ip, package_loss_ratio) VALUES(8, '8.8.8.8', 0.1);
INSERT INTO ping_test_result(id, ip, package_loss_ratio) VALUES(9, '8.8.8.8', 0.1);
到此主从同步配置完成,下面开始配置keepalived服务。
keepalived主要是需要将各种配置项配置正确
keepalived主节点:192.168.112.121
keepalived从节点:192.168.112.122
接下来是安装keepalived服务
安装必要的依赖包
yum -y install gcc openssl-devel popt-devel psmisc
cd /home/services/
wget https://www.keepalived.org/software/keepalived-2.2.7.tar.gz
tar -zxvf keepalived-2.2.7.tar.gz
cd keepalived-2.2.7
./configure --prefix=/opt/keepalived
make && make install
#存放配置文件并将keepalived启动脚本存在合适的位置
mkdir -p /etc/keepalived
cp keepalived/etc/keepalived/keepalived.conf.sample /etc/keepalived/keepalived.conf
cp keepalived/etc/init.d/keepalived /etc/init.d/
cp keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp bin/keepalived /usr/sbin/
在为主从节点做配置前,
1、需要查看网卡,主从节点的网络均是ens33
2、配置主从节点
3、创建/etc/keepalived/restart.sh
#!/bin/bash
#当mysql服务异常时,将keepalived应用停止,可以及时将vip切换到另一个节点上
killall keepalived
4、其他配置请参考外部资料
https://blog.csdn.net/D1179869625/article/details/126198495
主节点192.168.112.121配置文件如下:
!Confiquration File for keepalived
global defs {
notification email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 10.xx.xx.x
smtp_connect_timeout 30
router_id LVS_MASTER
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1{
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication{
auth_type PASS
auth_pass 1539
}
virtual_ip_address {
192.168.112.120
}
}
virtual_server 192.168.112.121 3306 {
delay_loop 6
1b_algo rr
1b_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.112.121 3306{
notify_down /etc/keepalived/restart.sh
TCP_CHECK {
connect_ip 192.168.112.121
connect_port 3306
connect_timeout 3
nb_get_retry 33
delay_before_retry 3
}
}
}
经验证,将virtual_server服务配置到对应3306端口,一旦数据库异常,将会做故障迁移到另一个数据库实例上。
从节点192.168.112.122配置
!Confiquration File for keepalived
global defs {
notification email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 10.xx.xx.x
smtp_connect_timeout 30
router_id LVS_SLAVE
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1{
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication{
auth_type PASS
auth_pass 1539
}
virtual_ip_address {
192.168.112.120
}
}
virtual_server 192.168.112.122 3306 {
delay_loop 6
1b_algo rr
1b_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.112.122 3306{
notify_down /etc/keepalived/restart.sh
TCP_CHECK {
connect_ip 192.168.112.122
connect_port 3306
connect_timeout 3
nb_get_retry 33
delay_before_retry 3
}
}
}
在两节点上配置keepalived服务为开机自启动并启动服务
systemctl enable keepalived && systemctl start keepalived
检测朱从节点服务状态为下图则表示服务正常运行,
测试一:通过vip(192.168.112.120)地址连接MySQL数据库,如果能正常连接则表示VIP生效;
测试二:关闭其中一个节点的keepalived服务,如果能正常连接则表示keepalived服务生效,否则异常,需要检查keepalived服务配置;
测试三:关闭其中一个数据库服务,如果能正常连接,则表示运行正常,否则异常,需要检查keepalived服务配置是否有误;
测试四:如果关闭主节点keepalived服务,并关闭从节点MySQL服务,如果无法访问,则表示正常。
至此整个高可用方案验证完成。