一 服务器概述
1.1 服务资源介绍
本案例是使用192.168.152.136和192.168.152.140 这两台机器,服务详情见下表:
Ip | 数据库角色 | 用户 | Mysql的路径 |
192.168.152.136 | Master (slaver) | root | /usr/local/mysql-5.7.29 |
192.168.152.140 | Master (slaver) | root | /usr/local/mysql-5.7.29 |
192.168.152.130 | Vip(虚拟ip) | root/boc-123 |
192.168.152.136和192.168.152.140这台机器上的myql数据库隶属于root用户,启动mysql需要root用户来启动。
https://www.cnblogs.com/benjamin77/p/8682360.html
1.2 前提说明
192.168.152.136和192.168.152.140两台机器均以安装完成mysql数据库。
1.3 change master的作用
数据复制的关键操作是配置从服务器去连接主服务器进行数据复制,需要告知从服务器建立网络连接所有必要的信息。使用change master to语句即可完成该项工作,master_host指定主服务器主机名或IP地址,master_user为主服务器上创建的拥有复制权限的账户名称,master_password为该账户的密码,master_log_file指定主服务器二进制日志文件名称,master_log_pos为主服务器二进制日志当前记录的位置。start slave;开启从服务器功能进行主从连接,show slave status\G查看从服务器状态。
1.4 双机热备特点
1.特性:
1,至少需要两台服务器,其中一台为master始终提供服务,另外一台作为backup始终处于空闲状态,只有在主服务器挂掉的时候他就来帮忙了,这是典型的双击热备
2,能根据需求判断服务是否可用,在不可用的时候要即使切换
优缺点:
优点:数据同步非常简单,不像负载均衡对数据一致性要求非常高,实现起来相对复杂维护也颇为不便,双机热备用rsync就可以实现了操作和维护非常简单
缺点:服务器有点浪费,始终有一台处于空闲状态
2.Keepalived双机热备的应用场景
1,网站流量不高,压力不大,但是对服务器的可靠性要求极其高,例如实时在线OA系统,政府部门网站系统,医院实时报医系统,公安局在线报案系统,股市后台网站系统等等,他们的压力不是很大,但是对可靠性要求是非常高的
2,有钱没地方花的,典型的政府企业,公办学校等等
Linux 实现MySQL+Keepalive 高可用_IT黑旋风的博客-CSDN博客
1.5 知识扫盲
Mysql的双主顾名思义就是互为主备,利用keepalived实现 MySQL数据库的高可用,这个时候就需要 keepalived的非抢占模式。
安装mysql双主(被动)模式,实现数据的冗余备份。
安装keepalived nopreempt 模式,实现mysql数据库的故障切换。
二 搭建主主复制功能
2.1 在192.168.152.136上配置my.cnf内容
首先进入到 /etc/my.cnf 中配置如下内容: 机器号为ip的后一位数字,自增步长为2;开始位置为1;
[root@localhost etc]# vi my.cnf
server-id = 136 log-bin = mysql-bin sync_binlog = 1 binlog_checksum = none binlog_format = mixed auto-increment-increment = 2 auto-increment-offset = 1 slave-skip-errors = all |
其次重启mysql服务:
[root@localhost etc]# service mysql restart Shutting down MySQL............ SUCCESS! Starting MySQL. SUCCESS! [root@localhost etc]# |
最后,创建同步账号,锁表,同步配置,当前的binlog以及数据所在位置
创建用户名和密码: slave_zzcp/slave123
[root@localhost etc]# mysql -uroot -p mysql> create user 'slave_zzcp'@'192.168.1.%' identified by 'slave123'; Query OK, 0 rows affected (0.02 sec) mysql> grant replication slave,replication client on *.* to slave_zzcp@'192.168.152.%' identified by 'slave123'; Query OK, 0 rows affected, 1 warning (0.02 sec) mysql> flush privileges; Query OK, 0 rows affected (0.02 sec) mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec) mysql> show master status; |
截图所示:
2.2 在192.168.1.140上配置my.cnf内容
首先进入到 /etc/my.cnf 中配置如下内容: 机器号为ip的后一位数字,自增步长为2;开始位置为2;
[root@localhost etc]# vi my.cnf
#在140服务器上
server-id =140 log-bin = mysql-bin sync_binlog = 1 binlog_checksum = none binlog_format = mixed auto-increment-increment = 2 auto-increment-offset = 2 slave-skip-errors = all |
其次重启mysql服务:
[root@localhost etc]# service mysql restart Shutting down MySQL............ SUCCESS! Starting MySQL. SUCCESS! [root@localhost etc]# |
最后,创建同步账号,锁表,同步配置,当前的binlog以及数据所在位置
[root@localhost etc]# mysql -uroot -p mysql> create user 'slave_zzcp'@'192.168.1.%' identified by 'slave123'; Query OK, 0 rows affected (0.01 sec) mysql> grant replication slave,replication client on *.* to slave_zzcp@'192.168.152.%' identified by 'slave123'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 875 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) |
2.3 在192.168.152.136上将对方数据同步到本库中
//先解锁,将对方数据同步到自己的数据库中,其中master_host对方的主机;Master_user master_password 同步的账号密码,master_log_file,master_log_pos为上一步查看192.168.152.140上binlog的读取位置。
mysql> unlock tables; Query OK, 0 rows affected (0.00 sec) mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> change master to master_host='192.168.152.140',master_user='slave_zzcp',master_password='slave123',master_log_file='mysql-bin.000001',master_log_pos=875; Query OK, 0 rows affected, 2 warnings (0.05 sec) |
启动查看同步的状态,查看两个线程状态是否为YES
mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql> mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.152.140 Master_User: slave_zzcp Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 875 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 312 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: 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: 875 Relay_Log_Space: 515 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: 140 Master_UUID: 4bf0caea-09c9-11ed-ba52-000c29706359 Master_Info_File: /usr/local/mysql-5.7.29/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) mysql> |
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
只有这两个进程都为yes,才算配置ok!
2.4 在192.168.1.140上将对方数据同步到本库中
//先解锁,将对方数据同步到自己的数据库中,其中master_host对方的主机;Master_user master_password 同步的账号密码,master_log_file,master_log_pos为上一步查看192.168.152.136上binlog的读取位置。
mysql> unlock tables; Query OK, 0 rows affected (0.00 sec) mysql> stop slave; Query OK, 0 rows affected (0.01 sec) mysql> change master to master_host='192.168.152.136',master_user='slave_zzcp',master_password='slave123',master_log_file='mysql-bin.000001',master_log_pos=875; Query OK, 0 rows affected, 2 warnings (0.05 sec) mysql> start slave; Query OK, 0 rows affected (0.05 sec) mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.152.136 Master_User: slave_zzcp Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 875 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 312 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: 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: 875 Relay_Log_Space: 515 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: 136 Master_UUID: 4a741971-094c-11ed-bb5f-000c291822bd Master_Info_File: /usr/local/mysql-5.7.29/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) |
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
只有这两个进程都为yes,才算配置ok!
2.5 主主同步效果验证
2.5.1 在136上新增数据
mysql> use hd_test; Database changed mysql> show tables; +-------------------+ | Tables_in_hd_test | +-------------------+ | mtc_user | | test_ddd | +-------------------+ 2 rows in set (0.00 sec) mysql> insert into mtc_user values('xinxiang','xhqu','qwe123','haha','1882192343','1234@11','1',2,now(),'ljf','ts',now()); Query OK, 1 row affected (0.01 sec) mysql> select * from mtc_user where user_id='xinxiang'; +----------+--------------+---------------+-----------+------------+---------+------+-------+---------------------+----------------+-----------+---------------------+ | user_id | user_account | user_password | user_name | mobile | email | sex | state | create_date | create_persion | tenant_id | update_time | +----------+--------------+---------------+-----------+------------+---------+------+-------+---------------------+----------------+-----------+---------------------+ | xinxiang | xhqu | qwe123 | haha | 1882192343 | 1234@11 | 1 | 2 | 2022-07-23 14:46:44 | ljf | ts | 2022-07-23 14:46:44 | +----------+--------------+---------------+-----------+------------+---------+------+-------+---------------------+----------------+-----------+---------------------+ 1 row in set (0.00 sec) |
在140上进行查看
mysql> use hd_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 mtc_user where user_id='xinxiang'; +----------+--------------+---------------+-----------+------------+---------+------+-------+---------------------+----------------+-----------+---------------------+ | user_id | user_account | user_password | user_name | mobile | email | sex | state | create_date | create_persion | tenant_id | update_time | +----------+--------------+---------------+-----------+------------+---------+------+-------+---------------------+----------------+-----------+---------------------+ | xinxiang | xhqu | qwe123 | haha | 1882192343 | 1234@11 | 1 | 2 | 2022-07-23 14:46:44 | ljf | ts | 2022-07-23 14:46:44 | +----------+--------------+---------------+-----------+------------+---------+------+-------+---------------------+----------------+-----------+---------------------+ 1 row in set (0.00 sec) |
136和140可以看到已经实现了同步操作!
2.5.2 在140上新建数据库并插入数据
mysql> create database hongqi; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hd_test | | hongqi | | mysql | | performance_schema | | sys | +--------------------+ 6 rows in set (0.00 sec) mysql> use hongqi; Database changed mysql> create table tb_student(id int(10) primary key,uname varchar(255)); Query OK, 0 rows affected (0.05 sec) mysql> insert into tb_student values(12,'lisi'); Query OK, 1 row affected (0.01 sec) mysql> select * from tb_student; +----+-------+ | id | uname | +----+-------+ | 12 | lisi | +----+-------+ 1 row in set (0.00 sec) |
在136上进行查看
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hd_test | | hongqi | | mysql | | performance_schema | | sys | +--------------------+ 6 rows in set (0.00 sec) mysql> use hongqi; 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_student; +----+-------+ | id | uname | +----+-------+ | 12 | lisi | +----+-------+ 1 row in set (0.00 sec) mysql> a |
经过以上的操作,可以看出不管是在136还是140上进行新增操作,都能同步到对方数据库中,到此实现了我们主主复制的功能。
2.6 关于slaver启不起来的问题
当上次将master,slaver的机器关闭后,本次启动两台机器,分别将上面的mysql应用进行启动,发现重库无法进行启动,如下图所示:
show slave status \G
启动: start slave 提示:ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository
解决办法:
比较master ,slaver的binlog日志
136机器
Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 150 Relay_Log_File: localhost-relay-bin.000013 Relay_Log_Pos: 355 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes |
140机器
Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 150 Relay_Log_File: localhost-relay-bin.000011 Relay_Log_Pos: 355 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: No Slave_SQL_Running: No |
- 在my.cnf配置文件中添加如下项:
vi /etc/my.cnf |
2.重新同步链路信息
mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> reset slave; Query OK, 0 rows affected (0.02 sec) mysql> mysql> change master to master_host='192.168.152.136',master_user='slave_zzcp',master_password='slave123',master_log_file='mysql-bin.000003',master_log_pos=150; Query OK, 0 rows affected, 2 warnings (0.03 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G |
验证联通性:
136:
140上查看
在140上进行修改动作
在136上进行查看
https://www.dude6.com/article/26904.html
2.7 缺点和不足
当运行一段时间后,要是发现同步有问题,比如只能单向同步,双向同步失效。可以重新执行下上面的change master同步操作,只不过这样同步后,只能同步在此之后的更新数据。
缺点:服务器A进行关闭后,和服务器B将不同步,需要人工进行干预同步两个服务器binlog日志的消费位置,重新启动才能保持一致。
https://www.cndba.cn/dave/article/108022
三 搭建keepalived 实现高可用
3.1 在192.168.152.136安装keepalived
提示报错,原因在于keepalived需要一些依赖,openssl等组件
代码如下:
[root@localhost local]# cd keepalived-1.4.0/ [root@localhost keepalived-1.4.0]# ls aclocal.m4 AUTHOR ChangeLog configure CONTRIBUTORS depcomp genhash install-sh keepalived.spec.in Makefile.am missing snap ar-lib bin_install compile configure.ac COPYING doc INSTALL keepalived lib Makefile.in README.md TODO [root@localhost keepalived-1.4.0]# ./configure --prefix=/usr/local/keepalived checking for a BSD-compatible install... /usr/bin/install -c checking for openssl/ssl.h... no configure: error: !!! OpenSSL is not properly installed on your system. !!! !!! Can not include OpenSSL headers files. !!! |
3.1.1 keepalived的依赖插件的安装
1.解压
[root@localhost bigdata-software]# unzip keepalived.zip Archive: keepalived.zip |
2.命令编译安装,如果使用命令rpm -Uvh --force *.rpm在安装过程提示失败,则改用此命令:rpm -Uvh --force *.rpm --nodeps --force
[root@localhost keepalived]# rpm -Uvh --force *.rpm error: Failed dependencies: gcc = 4.8.5-28.el7 is needed by (installed) gcc-c++-4.8.5-28.el7.x86_64 [root@localhost keepalived]# rpm -Uvh --force *.rpm --nodeps --force Preparing... ################################# [100%] Updating / installing... 1:libgcc-4.8.5-39.el7 ################################# [ 2%] 2:glibc-common-2.17-292.el7 ################################# [ 4%] |
3.1.2 keepalived的安装
首先进入到/usr/local/src,执行命令
[root@localhost keepalived]# cd /usr/local/src [root@localhost src]# ls [root@localhost src]# tar -zxvf /root/bigdata-software/keepalived-1.4.0.tar.gz -C . |
其次进行安装
[root@localhost src]# cd keepalived-1.4.0/ [root@localhost keepalived-1.4.0]# ls aclocal.m4 AUTHOR ChangeLog configure CONTRIBUTORS depcomp genhash install-sh keepalived.spec.in Makefile.am missing snap ar-lib bin_install compile configure.ac COPYING doc INSTALL keepalived lib Makefile.in README.md TODO [root@localhost keepalived-1.4.0]# ./configure --prefix=/usr/local/keepalived [root@bogon keepalived-1.4.0]# make && make install Making all in lib make[1]: Entering directory `/usr/local/src/keepalived-1.4.0/lib' |
最后创建快捷方式
[root@localhost keepalived-1.4.0]# cp /usr/local/src/keepalived-1.4.0/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/ [root@localhost keepalived-1.4.0]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ [root@localhost keepalived-1.4.0]# mkdir /etc/keepalived/ [root@localhost keepalived-1.4.0]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ [root@localhost keepalived-1.4.0]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ [root@localhost keepalived-1.4.0]# echo "/etc/init.d/keepalived start" >> /etc/rc.local [root@localhost keepalived-1.4.0]# |
3.1.3 keepalived的keepalived.conf配置
[root@bogon keepalived-1.4.0]# cd /etc/keepalived [root@bogon keepalived]# ls keepalived.conf [root@bogon keepalived]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak [root@bogon keepalived]# ls keepalived.conf keepalived.conf.bak [root@bogon keepalived]# vim /etc/keepalived/keepalived.conf |
修改内容:
! Configuration File for keepalived global_defs { notification_email { ops@wangshibo.cn tech@wangshibo.cn } notification_email_from ops@wangshibo.cn smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MASTER-HA } vrrp_script chk_mysql_port { #检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等 script "/opt/chk_mysql.sh" #这里通过脚本监测 interval 2 #脚本执行间隔,每2s检测一次 weight -5 #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5 fall 2 #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间) rise 1 #检测1次成功就算成功。但不修改优先级 } vrrp_instance VI_1 { state MASTER interface ens33 #指定虚拟ip的网卡接口 mcast_src_ip 192.168.152.136 virtual_router_id 51 #路由器标识,MASTER和BACKUP必须是一致的 priority 101 #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级。这样MASTER故障恢复后,就可以将VIP资源再次抢回来 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.152.130 } track_script { chk_mysql_port } } |
3.1.4 脚本配置
[root@bogon keepalived]# vim /opt/chk_mysql.sh |
修改内容:
#!/bin/bash port=`netstat -lntup |grep 3306|wc -l` echo $port if [ $port -ne 1 ] then /etc/init.d/keepalived stop else echo "Mysql is running" fi |
赋予执行权限
[root@bogon keepalived]# chmod 755 /opt/chk_mysql.sh |
3.1.5 启动keeepalived
[root@bogon keepalived]# /etc/init.d/keepalived start Starting keepalived (via systemctl): [ OK ] [root@bogon keepalived]# /etc/init.d/keepalived status ● keepalived.service - LVS and VRRP High Availability Monitor Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2022-07-25 08:27:09 CST; 4s ago Process: 24336 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS) Main PID: 24337 (keepalived) Tasks: 1 CGroup: /system.slice/keepalived.service └─24337 /usr/local/keepalived/sbin/keepalived -D Jul 25 08:27:09 bogon Keepalived_vrrp[24339]: Registering Kernel netlink reflector Jul 25 08:27:09 bogon Keepalived_vrrp[24339]: Registering Kernel netlink command channel Jul 25 08:27:09 bogon Keepalived_vrrp[24339]: Registering gratuitous ARP shared channel Jul 25 08:27:09 bogon Keepalived_vrrp[24339]: Opening file '/etc/keepalived/keepalived.conf'. Jul 25 08:27:09 bogon Keepalived_vrrp[24339]: WARNING - default user 'keepalived_script' for script execution does not exist - please create. Jul 25 08:27:09 bogon Keepalived_vrrp[24339]: Cant find interface eth0 for vrrp_instance VI_1 !!! Jul 25 08:27:09 bogon Keepalived_vrrp[24339]: Default interface eth0 does not exist and no interface specified. Skipping static address 192.168.152.130. Jul 25 08:27:09 bogon Keepalived_vrrp[24339]: VRRP_Instance(VI_1) Unknown interface ! Jul 25 08:27:10 bogon Keepalived[24337]: Keepalived_vrrp exited with permanent error CONFIG. Terminating Jul 25 08:27:10 bogon Keepalived[24337]: Stopping |
效果截图:
3.2 在192.168.152.140安装keepalived
3.2.1 keepalived的依赖插件的安装
1.解压
[root@localhost bigdata-software]# unzip keepalived.zip Archive: keepalived.zip |
2.命令编译安装,如果使用命令rpm -Uvh --force *.rpm在安装过程提示失败,则改用此命令:rpm -Uvh --force *.rpm --nodeps --force
[root@localhost keepalived]# rpm -Uvh --force *.rpm error: Failed dependencies: gcc = 4.8.5-28.el7 is needed by (installed) gcc-c++-4.8.5-28.el7.x86_64 [root@localhost keepalived]# rpm -Uvh --force *.rpm --nodeps --force Preparing... ################################# [100%] Updating / installing... 1:libgcc-4.8.5-39.el7 ################################# [ 2%] 2:glibc-common-2.17-292.el7 ################################# [ 4%] |
3.2.2 keepalived的安装
首先进入到/usr/local/src,执行命令
[root@localhost keepalived]# cd /usr/local/src [root@localhost src]# ls [root@localhost src]# tar -zxvf /root/bigdata-software/keepalived-1.4.0.tar.gz -C . |
其次进行安装
[root@localhost src]# cd keepalived-1.4.0/ [root@localhost keepalived-1.4.0]# ls aclocal.m4 AUTHOR ChangeLog configure CONTRIBUTORS depcomp genhash install-sh keepalived.spec.in Makefile.am missing snap ar-lib bin_install compile configure.ac COPYING doc INSTALL keepalived lib Makefile.in README.md TODO [root@localhost keepalived-1.4.0]# ./configure --prefix=/usr/local/keepalived [root@bogon keepalived-1.4.0]# make && make install Making all in lib make[1]: Entering directory `/usr/local/src/keepalived-1.4.0/lib' |
最后创建快捷方式
[root@localhost keepalived-1.4.0]# cp /usr/local/src/keepalived-1.4.0/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/ [root@localhost keepalived-1.4.0]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ [root@localhost keepalived-1.4.0]# mkdir /etc/keepalived/ [root@localhost keepalived-1.4.0]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ [root@localhost keepalived-1.4.0]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ [root@localhost keepalived-1.4.0]# echo "/etc/init.d/keepalived start" >> /etc/rc.local [root@localhost keepalived-1.4.0]# |
3.2.3 keepalived的keepalived.conf配置
[root@bogon keepalived-1.4.0]# cd /etc/keepalived [root@bogon keepalived]# ls keepalived.conf [root@bogon keepalived]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak [root@bogon keepalived]# ls keepalived.conf keepalived.conf.bak [root@bogon keepalived]# vim /etc/keepalived/keepalived.conf |
修改内容:
! Configuration File for keepalived
global_defs { notification_email { ops@qq.com tech@qq.com }
notification_email_from ops@wangshibo.cn smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MASTER-HA }
vrrp_script chk_mysql_port { script "/opt/chk_mysql.sh" interval 2 weight -5 fall 2 rise 1 }
vrrp_instance VI_1 { state BACKUP interface ens33 mcast_src_ip 192.168.152.140 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.152.130 }
track_script { chk_mysql_port } } |
3.2.4 脚本配置
[root@bogon keepalived]# vim /opt/chk_mysql.sh |
修改内容:
#!/bin/bash port=`netstat -lntup |grep 3306|wc -l` echo $port if [ $port -ne 1 ] then /etc/init.d/keepalived stop else echo "Mysql is running" fi |
赋予执行权限
[root@bogon keepalived]# chmod 755 /opt/chk_mysql.sh |
3.2.5 启动keeepalived
[root@bogon keepalived]# /etc/init.d/keepalived start Starting keepalived (via systemctl): [ OK ] [root@bogon keepalived]# /etc/init.d/keepalived status ● keepalived.service - LVS and VRRP High Availability Monitor Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2022-07-25 08:27:09 CST; 4s ago Process: 24336 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS) Main PID: 24337 (keepalived) Tasks: 1 CGroup: /system.slice/keepalived.service └─24337 /usr/local/keepalived/sbin/keepalived -D Jul 25 08:27:09 bogon Keepalived_vrrp[24339]: Registering Kernel netlink reflector Jul 25 08:27:09 bogon Keepalived_vrrp[24339]: Registering Kernel netlink command channel Jul 25 08:27:09 bogon Keepalived_vrrp[24339]: Registering gratuitous ARP shared channel Jul 25 08:27:09 bogon Keepalived_vrrp[24339]: Opening file '/etc/keepalived/keepalived.conf'. Jul 25 08:27:09 bogon Keepalived_vrrp[24339]: WARNING - default user 'keepalived_script' for script execution does not exist - please create. Jul 25 08:27:09 bogon Keepalived_vrrp[24339]: Cant find interface eth0 for vrrp_instance VI_1 !!! Jul 25 08:27:09 bogon Keepalived_vrrp[24339]: Default interface eth0 does not exist and no interface specified. Skipping static address 192.168.152.130. Jul 25 08:27:09 bogon Keepalived_vrrp[24339]: VRRP_Instance(VI_1) Unknown interface ! Jul 25 08:27:10 bogon Keepalived[24337]: Keepalived_vrrp exited with permanent error CONFIG. Terminating Jul 25 08:27:10 bogon Keepalived[24337]: Stopping |
效果截图:
3.3 两台机器授权用户远程登录
master1和master2两台服务器都要授权允许root用户远程登录,用于在客户端登陆测试。
3.3.1 机器140上进行授权
[root@localhost keepalived]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 5.7.29-log MySQL Community Server (GPL) Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> grant all on *.* to root@'192.168.152.%' identified by "boc-123"; Query OK, 0 rows affected, 1 warning (0.02 sec) mysql> flush privileges; Query OK, 0 rows affected (0.02 sec) mysql> |
3.3.2 机器136上进行授权
[root@localhost keepalived]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 5.7.29-log MySQL Community Server (GPL) Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> grant all on *.* to root@'192.168.152.%' identified by "boc-123"; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.02 sec) mysql> |
3.4 高可用的验证
3.4.1 验证vip(虚拟ip)访问连通性
在navicate通过虚拟ip 192.168.152.130 root/boc-123 进行连接访问
在一台客户端连接这个虚拟ip
[root@localhost keepalived]# mysql -h 192.168.152.130 -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 11 Server version: 5.7.29-log MySQL Community Server (GPL) Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hd_test | | hongqi | | mysql | | performance_schema | | sys | +--------------------+ 6 rows in set (0.00 sec) mysql> |
通过以下配置可以看到:vip是在master1 192.168.152.136上的。使用"ip addr"命令查看vip切换情况
在136上查看ip: ip addr | grep 192.168
在140上查看ip
3.4.2 故障转义切换
现在模拟假如master1的mysql服务进行关闭或者挂掉后,vip虚拟的ip会切换到master2上。停止192.168.152.136机器上的mysql服务,根据配置中的脚本,mysql服务停了,keepalived也会停,从而vip资源将会切换到192.168.152.140机器上。(mysql服务没有起来的时候,keepalived服务也无法顺利启动!)
- 在136机器上,关闭mysql
[root@localhost opt]# ps -ef|grep mysql root 3172 1 0 14:42 pts/0 00:00:00 /bin/sh /usr/local/mysql-5.7.29/bin/mysqld_safe --user=root --datadir=/usr/local/mysql-5.7.29/data --pid-file=/usr/local/mysql-5.7.29/mysqlpid.pid root 3504 3172 0 14:42 pts/0 00:00:03 /usr/local/mysql-5.7.29/bin/mysqld --basedir=/usr/local/mysql-5.7.29 --datadir=/usr/local/mysql-5.7.29/data --plugin-dir=/usr/local/mysql-5.7.29/lib/plugin --user=root --log-error=/usr/local/mysql-5.7.29/logs/mysql-log.err --pid-file=/usr/local/mysql-5.7.29/mysqlpid.pid --port=3306 root 3573 2797 0 14:42 pts/0 00:00:00 mysql -uroot -p root 6938 4727 0 16:33 pts/1 00:00:00 grep --color=auto mysql [root@localhost opt]# service mysql stop Shutting down MySQL............ SUCCESS! [root@localhost opt]# ps -ef|grep mysql root 3573 2797 0 14:42 pts/0 00:00:00 mysql -uroot -p root 7060 4727 0 16:34 pts/1 00:00:00 grep --color=auto mysql [root@localhost opt]# ps -ef|grep mysql root 7074 4727 0 16:34 pts/1 00:00:00 grep --color=auto mysql [root@localhost opt]# ps -ef|grep keepalived root 7090 4727 0 16:34 pts/1 00:00:00 grep --color=auto keepalived [root@localhost opt]# ip addr | grep 192.168 inet 192.168.152.136/24 brd 192.168.152.255 scope global noprefixroute dynamic ens33 inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 [root@localhost opt]# netstat -lntup |grep 3306|wc -l 0 [root@localhost opt]# |
可以看到 mysql服务,keepalived服务的进程均不存在了,虚拟ip也没有在192.168.152.136上了。
- 在140上
[root@bogon opt]# ip addr | grep 192.168 inet 192.168.152.140/24 brd 192.168.152.255 scope global noprefixroute dynamic ens33 inet 192.168.152.130/32 scope global ens33 inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 [root@bogon opt]# ps -ef|grep mysql root 2748 1 0 14:42 pts/0 00:00:00 /bin/sh /usr/local/mysql-5.7.29/bin/mysqld_safe --user=root --datadir=/usr/local/mysql-5.7.29/data --pid-file=/usr/local/mysql-5.7.29/mysqlpid.pid root 3089 2748 0 14:42 pts/0 00:00:03 /usr/local/mysql-5.7.29/bin/mysqld --basedir=/usr/local/mysql-5.7.29 --datadir=/usr/local/mysql-5.7.29/data --plugin-dir=/usr/local/mysql-5.7.29/lib/plugin --user=root --log-error=/usr/local/mysql-5.7.29/logs/mysql-log.err --pid-file=/usr/local/mysql-5.7.29/mysqlpid.pid --port=3306 root 3128 2687 0 14:43 pts/0 00:00:00 mysql -uroot -p root 7009 3261 0 16:36 pts/1 00:00:00 grep --color=auto mysql [root@bogon opt]# ps -ef|grep keepalived root 4886 1 0 16:25 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D root 4887 4886 0 16:25 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D root 4888 4886 0 16:25 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D root 7035 3261 0 16:36 pts/1 00:00:00 grep --color=auto keepalived [root@bogon opt]# |
可以看到: mysql,keepalived的服务进程都在,且虚拟ip(192.168.152.130)和192.168.152.140 进行了绑定。
3.4.3 故障恢复
假设现在在192.168.152.136机器上将mysql,keepalived启动起来,发现虚拟ip(192.168.152.130)又切换到了192.168.152.136上,
注意:一定要先启动mysql服务,然后再启动keepalived服务。如果先启动keepalived服务,按照上面的配置,mysql没有起来,就会自动关闭keepalived。
[root@localhost opt]# service mysql start Starting MySQL. SUCCESS! [root@localhost opt]# ps -ef|grep mysql root 7447 1 0 16:54 pts/1 00:00:00 /bin/sh /usr/local/mysql-5.7.29/bin/mysqld_safe --user=root --datadir=/usr/local/mysql-5.7.29/data --pid-file=/usr/local/mysql-5.7.29/mysqlpid.pid root 7779 7447 3 16:54 pts/1 00:00:00 /usr/local/mysql-5.7.29/bin/mysqld --basedir=/usr/local/mysql-5.7.29 --datadir=/usr/local/mysql-5.7.29/data --plugin-dir=/usr/local/mysql-5.7.29/lib/plugin --user=root --log-error=/usr/local/mysql-5.7.29/logs/mysql-log.err --pid-file=/usr/local/mysql-5.7.29/mysqlpid.pid --port=3306 root 7818 4727 0 16:54 pts/1 00:00:00 grep --color=auto mysql [root@localhost opt]# /etc/init.d/keepalived start Starting keepalived (via systemctl): [ OK ] [root@localhost opt]# /etc/init.d/keepalived status ● keepalived.service - LVS and VRRP High Availability Monitor Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2022-08-01 16:54:40 CST; 4s ago Process: 7848 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS) Main PID: 7849 (keepalived) Tasks: 3 CGroup: /system.slice/keepalived.service ├─7849 /usr/local/keepalived/sbin/keepalived -D ├─7850 /usr/local/keepalived/sbin/keepalived -D └─7851 /usr/local/keepalived/sbin/keepalived -D Aug 01 16:54:40 localhost.localdomain Keepalived_vrrp[7851]: VRRP_Script(chk_mysql_port) succeeded Aug 01 16:54:41 localhost.localdomain Keepalived_vrrp[7851]: VRRP_Instance(VI_1) Transition to MASTER STATE Aug 01 16:54:42 localhost.localdomain Keepalived_vrrp[7851]: VRRP_Instance(VI_1) Entering MASTER STATE Aug 01 16:54:42 localhost.localdomain Keepalived_vrrp[7851]: VRRP_Instance(VI_1) setting protocol VIPs. Aug 01 16:54:42 localhost.localdomain Keepalived_vrrp[7851]: Sending gratuitous ARP on ens33 for 192.168.152.130 Aug 01 16:54:42 localhost.localdomain Keepalived_vrrp[7851]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.152.130 Aug 01 16:54:42 localhost.localdomain Keepalived_vrrp[7851]: Sending gratuitous ARP on ens33 for 192.168.152.130 Aug 01 16:54:42 localhost.localdomain Keepalived_vrrp[7851]: Sending gratuitous ARP on ens33 for 192.168.152.130 Aug 01 16:54:42 localhost.localdomain Keepalived_vrrp[7851]: Sending gratuitous ARP on ens33 for 192.168.152.130 Aug 01 16:54:42 localhost.localdomain Keepalived_vrrp[7851]: Sending gratuitous ARP on ens33 for 192.168.152.130 [root@localhost opt]# ip addr | grep 192.168 inet 192.168.152.136/24 brd 192.168.152.255 scope global noprefixroute dynamic ens33 inet 192.168.152.130/32 scope global ens33 inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 [root@localhost opt]# |
在140上进行查看
[root@bogon opt]# ip addr | grep 192.168 inet 192.168.152.140/24 brd 192.168.152.255 scope global noprefixroute dynamic ens33 inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 [root@bogon opt]# |
此时虚拟ip又切换到了192.168.152.136的主机上,以上在vip资源切换过程中,对于客户端连接mysql(使用vip连接)来说几乎是没有任何影响的。
四 My.cnf配置文件配置说明
4.1 配置高可用参数说明
log_slave_updates 同步数据时也写入日志,二进制记录id号,互为主从时时不会引起循环。建议开启方便实施日志恢复。 可选 binlog-do-db、binlog-ignore-db、replicate_do_db、replicate_ignore_db 在使用时应注意,若加了以上参数,则在操作数据库是要避免跨库操作(例:update test.table1 set...) 如设置 binlog_ignore_db=mysql 如设置 replicate_do_db=test 如设置 replicate_ignore_db=mysql 原因是设置binlog-do-db、binlog-ignore-db、replicate_do_db或replicate_ignore_db后,MySQL执行sql前检查的是当前默认数据库,所以跨库更新语句被忽略。 |