MySQL主主高可用(keepalive)

Posted 冥想心灵

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL主主高可用(keepalive)相关的知识,希望对你有一定的参考价值。

 2台新的虚拟机172.16.1.1、172.16.1.2  (配置yum )

安装数据库服务

其中 172.16.1.1、172.16.1.2运行数据库服务并设置数据库管理员从本机登录的密码是xzw12345 mysql-server.5.6

 tar -xvf MySQL-5.6.rpm.tar (RPM包安装)

rpm -Uvh MySQL-*.rpm

/etc/init.d/mysql  start  ; chkconfig  mysql   on

 修改密码

[[email protected] ~]# cat /root/.mysql_secret

[[email protected] ~]# mysql -uroot -pjBJIfBwD

mysql> set password for [email protected]"localhost"=password("xzw12345");

mysql> quit;

[[email protected] ~]# mysql -uroot -pxzw12345

mysql>

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

主主结构

master203

mysql   -uroot  -pxzw12345

mysql> grant  replication  slave  on  *.*   to   [email protected]"%" identified by  "123456";

mysql>quit;

 

vim /etc/my.cnf

[mysqld]

log-bin=master203

server_id=203

:wq

/etc/init.d/mysql  restart

ls  /var/lib/mysql/master203.*

 

mysql   -uroot  -pxzw12345

mysql>show  master  status;

 

master204

mysql   -uroot  -pxzw123

mysql> grant  replication  slave  on  *.*   to   [email protected]"%" identified by  "123456";

mysql>quit;

 

vim /etc/my.cnf

[mysqld]

log-bin=master204

server_id=204

:wq

/etc/init.d/mysql  restart

ls  /var/lib/mysql/master204.*

 

mysql   -uroot  -pxzw12345

mysql>show  master  status;

 

 

master204master203发起同步操作,指定相关master172.16.1.1的相关参数

mysql   -uroot  -pxzw12345

mysql>   change master  to   master_host="172.16.1.1",master_user="slaveuser",master_password="123456",master_log_file="master203.000001",master_log_pos=120;

mysql> start  slave;

mysql> show slave status\G;

               IO  yes

               SQL   yes

 

 

master203master204发起同步操作,指定相关master172.16.1.2的相关参数

mysql   -uroot  -pxzw12345 

mysql>   change master  to master_host="172.16.1.2",master_user="slaveuser",master_password="123456",master_log_file="master204.000001",master_log_pos=120;

mysql> start  slave;

mysql> show slave status\G;

               IO  yes

               SQL   yes

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 测试

1.master203

mysql  -uroot   -pxzw12345

mysql>  create  database  bbsdb;

mysql> grant all on  bbsdb.*  to   [email protected]"%" identified by "123";

mysql> quit;

 2.master204

mysql  -uroot   -pxzw12345

mysql>use bbsdb;

mysql> create  table  bbsdb.t1(id int);

mysql> quit;

 

3.在客户端访问(192.168.1.200:

ping  x.x.x.1

mysql  -hx.x.x.1  -ustudent   -p123

mysql>  insert into  bbsdb.t1  values(101);

 

master203、master204

mysql  -uroot  -pxzw12345

mysql > select * from  bbsdb.t1  ; #也能够查看到记录

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

 

 

本文为大家讲解的是mysql错误:Last_IO_Error: error connecting to master解决方法,感兴趣的同学参考下。

错误提示:

Slave_IO_Running: Connecting

Slave_SQL_Running: Yes

Last_IO_Errno: 2003

Last_IO_Error: error connecting to master ‘slaveuser@172.16.1.1:3306‘ - retry-time: 60 retries: 1

错误原因:

主数据库服务器没有关闭防火墙。

解决措施:

1、去主服务器:172.16.1.1关闭防火墙。

[[email protected] ~]#  service iptables stop;chkconfig iptables off

iptables: Setting chains to policy ACCEPT: filter          [  OK  ]

iptables: Flushing firewall rules:                         [  OK  ]

iptables: Unloading modules:                               [  OK  ]

2、去从服务器:stop slave;start slave;

mysql> stop slave;

Query OK, 0 rows affected (0.01 sec)

mysql> start slave;

Query OK, 0 rows affected (0.01 sec)

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

 Keepalived高可用服务器

 问题

两台Linux服务器(172.16.1.1、172.16.1.2),做MySQL服务器,并部署Keepalived高可用软件,一台作为客户端主机(192.168.1.200),实现如下功能:

  • 使用Keepalived实现MySQL服务器的高可用
  • Web服务器IP地址分别为172.16.1.1和172.16.1.2
  • Web服务器的漂移VIP地址为172.16.1.3
  • 客户端通过访问VIP地址访问MySQL服务

 

 步骤

实现此案例需要按照如下步骤进行。

步骤一:配置网络环境

1)设置MySQL服务器网络参数

[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=172.16.1.1
NETMASK=255.255.255.0
#DNS2=221.4.8.1
#GATEWAY=192.168.1.1
#DNS1=192.168.9.1
IPV6INIT=no
USERCTL=no

[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1

DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=172.16.1.2
NETMASK=255.255.255.0
#DNS2=221.4.8.1
#GATEWAY=192.168.1.1
#DNS1=192.168.9.1
IPV6INIT=no
USERCTL=no

 

步骤二:安装Keepalived软件

注意:两台MySQL服务器做相同的操作。

  1. [[email protected] ~]# yum install -y gcc kernel-devel openssl-devel popt-devel
  2. [[email protected] ~]# tar -xzf keepalived-1.2.7.tar.gz
  3. [[email protected] ~]# cd keepalived-1.2.7
  4. [[email protected] keepalived-1.2.7]# ./configure --sysconf=/etc
  5. [[email protected] keepalived-1.2.7]# make && make install
  6. [[email protected] keepalived-1.2.7]# ln -s /usr/local/sbin/keepalived /sbin/
  7. [[email protected] keepalived-1.2.7]# chkconfig keepalived on
  8. [[email protected] ~]# yum install -y gcc kernel-devel openssl-devel popt-devel
  9. [[email protected] ~]# tar -xzf keepalived-1.2.7.tar.gz
  10. [[email protected] ~]# cd keepalived-1.2.7
  11. [[email protected] keepalived-1.2.7]# ./configure --sysconf=/etc
  12. [[email protected] keepalived-1.2.7]# make && make install
  13. [[email protected] keepalived-1.2.7]# ln -s /usr/local/sbin/keepalived /sbin/
  14. [[email protected] keepalived-1.2.7]# chkconfig keepalived on

步骤三:部署Keepalived服务

1)修改web1服务器Keepalived配置文件

  1. [[email protected] ~]# vim /etc/keepalived/keepalived.conf
  2. global_defs {
  3. notification_email {
  4. [email protected].com.cn                //设置报警收件人邮箱
  5. }
  6. notification_email_from [email protected]    //设置发件人
  7. smtp_server 127.0.0.1                //定义邮件服务器
  8. smtp_connect_timeout 30
  9. router_id web1                        //设置路由ID号
  10. }
  11. vrrp_instance VI_1 {
  12. state MASTER                          //主服务器为MASTER
  13. interface eth0                        //定义网络接口
  14. virtual_router_id 50                    //主辅VRID号必须一致
  15. priority 100                     //服务器优先级
  16. advert_int 1
  17. authentication {
  18. auth_type pass
  19. auth_pass forlvs                      //主辅服务器密码必须一致
  20. }
  21. virtual_ipaddress { 172.16.0.1 }
  22. }

2)修改web2服务器Keepalived配置文件

  1. [[email protected] ~]# vim /etc/keepalived/keepalived.conf
  2. global_defs {
  3. notification_email {
  4. [email protected].com.cn                //设置报警收件人邮箱
  5. }
  6. notification_email_from [email protected]    //设置发件人
  7. smtp_server 127.0.0.1                //定义邮件服务器
  8. smtp_connect_timeout 30
  9. router_id web2                        //设置路由ID号
  10. }
  11. vrrp_instance VI_1 {
  12. state SLAVE                          //从服务器为SLAVE
  13. interface eth0                        //定义网络接口
  14. virtual_router_id 50                    //主辅VRID号必须一致
  15. priority 50                         //服务器优先级
  16. advert_int 1
  17. authentication {
  18. auth_type pass
  19. auth_pass forlvs                      //主辅服务器密码必须一致
  20. }
  21. virtual_ipaddress { 172.16.0.1 }
  22. }

3)启动服务

  1. [[email protected] ~]# service keepalived start
  2. [[email protected] ~]# service keepalived start

步骤四:测试

1)登录两台Web服务器查看VIP信息

  1. [[email protected] ~]# ip addr show
  2. [[email protected] ~]# ip addr show

2) 客户端访问

客户端使用curl命令连接http://172.16.0.1,查看Web页面;关闭Web1服务器的网卡,客户端再次访问http://172.16.0.1,验证是否可以正常访问服务。
























以上是关于MySQL主主高可用(keepalive)的主要内容,如果未能解决你的问题,请参考以下文章

MySQL+Keepalived实现主主高可用方案

mysql主主高可用方案

配置MySQL实现主主互备模式并利用keepalived实现双主高可用

MySQL集群之keepalived实现mysql双主高可用

memcached实现主主复制和keepalive高可用方式

haproxy+keepalive负载均衡环境部署(主主模式)