首页 技术 正文
技术 2022年11月12日
0 收藏 694 点赞 4,369 浏览 7047 个字

keepalive配置mysql自动故障转移

原创 2016年02月29日 02:16:52

  • 2640

本文先配置了一个双master环境,互为主从,然后通过Keepalive配置了一个虚拟IP,客户端通过虚拟IP连接master1,当master1宕机,自动切换到master2。一次只能连接其中一个master进行读写,所以是active-passive模式。

一 Mysql主主复制搭建

 

1.1 实验环境

两台机器事先都已经装好了mysql单实例。

keepalive配置mysql自动故障转移

二者的端口号需要保持一致,否则在最后用vip连接的时候,不能使用相同端口号连接。

1.2 实验步骤

 

1.2.1 修改配置文件

修改master1:

在[mysqld]下面添加:

server-id =  1

relay-log=/data/server/mysql_3307/binlog/ZabbixServer-relay-bin

relay-log-index=/data/server/mysql_3307/binlog/ZabbixServer-relay-bin.index

auto-increment-offset = 1

auto-increment-increment = 2

log-slave-updates=true

修改master2:

在[mysqld]下面添加:

server-id =  3

relay-log =/data/server/mysql/binlog/single-relay-bin

relay-log-index=/data/server/mysql/binlog/single-relay-bin.index

auto-increment-offset = 2

auto-increment-increment = 2

log-slave-updates=true

添加auto-increment-offset那两项,是为了避免在MySQL INSERT时主键冲突。

修改完后记得重启mysql

1.2.2 建复制用户

分别在两台mysql上执行

GRANT REPLICATION SLAVE ON *.* TO ‘RepUser’@’%’identified by ‘beijing’;

1.2.3 指向master

两台服务器均为新建立,且无其它写入操作,各服务器只需记录当前自己二进制日志文件及事件位置,以之作为另外的服务器复制起始位置即可。否则,需要先备份主库,在备库进行恢复,从而保持数据一致,然后再指向master。

Master1:

mysql> show master status;

+——————+———-+————–+——————+——————-+

| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+——————+———-+————–+——————+——————-+

| mysql-bin.000001 |      302|              |                  |                   |

+——————+———-+————–+——————+——————-+

1 row in set (0.00 sec)

Master2:

mysql> show master status;

+——————+———-+————–+——————+——————-+

| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+——————+———-+————–+——————+——————-+

| mysql-bin.000001 |      120 |              |                  |                   |

+——————+———-+————–+——————+——————-+

1 row in set (0.00 sec)

#Master1指向Master2

  1. CHANGE MASTER TO MASTER_USER=’RepUser’,MASTER_HOST=’192.168.1.21′,MASTER_PASSWORD=’beijing’,MASTER_PORT=3307,MASTER_LOG_FILE=’mysql-bin.000001′,MASTER_LOG_POS=120;

#Master2指向Master1

  1. CHANGE MASTER TO MASTER_USER=’RepUser’,MASTER_HOST=’192.168.1.22′,MASTER_PASSWORD=’beijing’, MASTER_PORT=3307,MASTER_LOG_FILE=’mysql-bin.000001′,MASTER_LOG_POS=302;

1.2.4 分别启动slave

start slave ;

确保show slave status

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

测试两边是否同步,略。

二:配置keepalived

 

1 keepalive安装

分别在master1,master2上安装keepalive

  1. yum install -y popt-devel
  2. cd /usr/local/src
  3. wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
  4. tar zxvf keepalived-1.2.2.tar.gz
  5. cd keepalived-1.2.2
  6. ./configure –prefix=/
  7. make
  8. make install

#假如在执行./configure –prefix=/时报错: OpenSSL is not properly installed on your system !!!Can notinclude OpenSSL headers files,则yum install openssl-devel -y

2 分别在master1,master2上新建检查mysql脚本

vi /root/check_mysql.sh

内容如下

  1. MYSQL=/usr/local/mysql/bin/mysql
  2. MYSQL_HOST=localhost
  3. MYSQL_USER=root
  4. MYSQL_PASSWORD=system@123
  5. $MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e “show status;” >/dev/null 2>&1
  6. #$mysqlclient –host=$host –port=$port –user=$user –password=$password  -e “show databases;” > /dev/null 2>&1
  7. if [ $? == 0 ]
  8. then
  9. echo ” $host mysql login successfully “
  10. exit 0
  11. else
  12. #echo ” $host mysql login faild”
  13. /etc/init.d/keepalived stop
  14. exit 2
  15. fi

chmod +x /root/check_mysql.sh

3 修改配置文件

vi /etc/keepalived/keepalived.conf

master1和master2配置文件内容相同。

内容:

  1. #ConfigurationFile for keepalived
  2. global_defs {
  3. notification_email {                        ######定义接受邮件的邮箱
  4. wangjj@hrloo.com
  5. }
  6. notification_email_from jiankong@staff.tuge.com    ######定义发送邮件的邮箱
  7. smtp_server mail.tuge.com
  8. smtp_connect_timeout 10
  9. }
  10. vrrp_script check_mysql {                   ######定义监控mysql的脚本
  11. script “/root/check_mysql.sh”
  12. interval 2                             ######监控时间间隔
  13. weight 2                               ######负载参数
  14. }
  15. vrrp_instance vrrptest {                 ######定义vrrptest实例
  16. state BACKUP              ######服务器状态
  17. interface eth0                      ######使用的接口
  18. virtual_router_id 51                ######虚拟路由的标志,一组lvs的虚拟路由标识必须相同,这样才能切换
  19. priority 150                        ######服务启动优先级,值越大,优先级越高,BACKUP 不能大于MASTER
  20. advert_int 1                        ######服务器之间的存活检查时间
  21. authentication {
  22. auth_type PASS                      ######认证类型
  23. auth_pass ufsoft       ######认证密码,一组lvs 服务器的认证密码必须一致
  24. }
  25. track_script {                              ######执行监控mysql进程的脚本
  26. check_mysql
  27. }
  28. virtual_ipaddress {                         ######虚拟IP地址
  29. 192.168.1.60
  30. }
  31. }

这里state不配置MASTER,且优先级一样,是期望在MASTER1宕机后再恢复时,不主动将MASTER状态抢过来,避免MySQL服务的波动。

由于不存在使用lvs进行负载均衡,不需要配置虚拟服务器virtual server,下同。

vi /etc/sysconfig/iptables

#注意,在两台机器上都要修改。

添加:

-A INPUT -d 192.168.1.60/32 -j ACCEPT

-A INPUT -d 224.0.0.18 -j ACCEPT #添加VRRP通讯支持

注意:第一行中的192.168.1.60需要改成你自己的vip。

service iptables restart

5 启动keepalived

在master1、master2上分别启动:
service keepalived start

分别执行ip addr命令,可以在其中一台机器上看到虚拟IP.如:

[root@slave1 keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_faststate UP qlen 1000
    link/ether 08:00:27:04:05:16 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.22/24 brd 192.168.1.255 scope global eth0
    inet 192.168.1.60/32 scope global eth0
    inet6 fe80::a00:27ff:fe04:516/64 scope link tentativedadfailed 
       valid_lft forever preferred_lft forever

说明虚拟vip连在了master1这台机器上。

如果自动只连接到了master2,关闭master2的keepalived,再启动,自动就连接到master1了。

现在都可以ping通虚拟ip了。

6 测试

停止master1服务器keepalived,检查VIP是否切换到master2服务器(用ip addr命令验证即可);

三 测试高可用环境是否配置成功

 

3.1 建允许远程访问的用户

在master1,master2创建允许远程访问的用户:

grant select,update,delete,insert on *.* to ‘dandan’ identified by ‘dandan’;

3.2 访问虚拟IP

用一台同网段的机器访问通过vip访问数据库:

mysql -u dandan-pdandan -h 192.168.1.60 –port 3307

停止master1服务器的mysql,VIP切换到了master2服务器。

在master2上查看:

  1. mysql> showprocesslist;
  2. +—-+————-+——————–+——+———+——+—————————————————————————–+——————+
  3. | Id | User        | Host               | db   | Command | Time | State                                                                      | Info             |
  4. +—-+————-+——————–+——+———+——+—————————————————————————–+——————+
  5. |  3 | root        | localhost          | dba | Query   |    0 | init                                                                       | show processlist |
  6. | 14 | systemuser |                    | NULL |Connect |  247 | Reconnecting after afailed master event read                               | NULL             |
  7. | 15 | systemuser |                    | NULL |Connect |  207 | Slave has read all relaylog; waiting for the slave I/O thread to update it | NULL             |
  8. | 90 |dandan      | 192.168.1.60:39995 |dba  | Sleep   |    8|                                                                            | NULL             |
  9. +—-+————-+——————–+——+———+——+—————————————————————————–+——————+

看到了dandan的连接信息。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,505
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844