码迷,mamicode.com
首页 > 数据库 > 详细

MySQL主主高可用(keepalive)

时间:2017-07-22 13:16:03      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:登录   module   replica   linu   iptables   cti   报警   trie   +++   

 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

 修改密码

[root@mail ~]# cat /root/.mysql_secret

[root@mail ~]# mysql -uroot -pjBJIfBwD

mysql> set password for root@"localhost"=password("xzw12345");

mysql> quit;

[root@mail ~]# mysql -uroot -pxzw12345

mysql>

 

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

 

主主结构

master203

mysql   -uroot  -pxzw12345

mysql> grant  replication  slave  on  *.*   to   slaveuser@"%" 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   slaveuser@"%" 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   student@"%" 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关闭防火墙。

[root@mysql204 ~]#  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服务器网络参数

[root@mysql203 ~]# 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

[root@mysql203 ~]# 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. [root@web1 ~]# yum install -y gcc kernel-devel openssl-devel popt-devel
  2. [root@web1 ~]# tar -xzf keepalived-1.2.7.tar.gz
  3. [root@web1 ~]# cd keepalived-1.2.7
  4. [root@web1 keepalived-1.2.7]# ./configure --sysconf=/etc
  5. [root@web1 keepalived-1.2.7]# make && make install
  6. [root@web1 keepalived-1.2.7]# ln -s /usr/local/sbin/keepalived /sbin/
  7. [root@web1 keepalived-1.2.7]# chkconfig keepalived on
  8. [root@web2 ~]# yum install -y gcc kernel-devel openssl-devel popt-devel
  9. [root@web2 ~]# tar -xzf keepalived-1.2.7.tar.gz
  10. [root@web2 ~]# cd keepalived-1.2.7
  11. [root@web2 keepalived-1.2.7]# ./configure --sysconf=/etc
  12. [root@web2 keepalived-1.2.7]# make && make install
  13. [root@web2 keepalived-1.2.7]# ln -s /usr/local/sbin/keepalived /sbin/
  14. [root@web2 keepalived-1.2.7]# chkconfig keepalived on

步骤三:部署Keepalived服务

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

  1. [root@web1 ~]# vim /etc/keepalived/keepalived.conf
  2. global_defs {
  3. notification_email {
  4. admin@tarena.com.cn                //设置报警收件人邮箱
  5. }
  6. notification_email_from ka@localhost    //设置发件人
  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. [root@web1 ~]# vim /etc/keepalived/keepalived.conf
  2. global_defs {
  3. notification_email {
  4. admin@tarena.com.cn                //设置报警收件人邮箱
  5. }
  6. notification_email_from ka@localhost    //设置发件人
  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. [root@web1 ~]# service keepalived start
  2. [root@web2 ~]# service keepalived start

步骤四:测试

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

  1. [root@web1 ~]# ip addr show
  2. [root@web2 ~]# ip addr show

2) 客户端访问

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

MySQL主主高可用(keepalive)

标签:登录   module   replica   linu   iptables   cti   报警   trie   +++   

原文地址:http://www.cnblogs.com/linux985/p/7220764.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!