环境:

DB1:centos6.8、mysql5.5、192.168.2.204  hostname:bogon

DB2:centos6.8、mysql5.5、192.168.2.205  hostname:localhost.localdomain

vip:192.168.2.33

一、先配置DB1和DB2的双主热备

1、分别在DB1和DB2上安装mysql,我这里是用的ansible自动部署

[root@www ansible]# ansible-playbook lnmp.yml 

PLAY [new] *********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.2.205]
ok: [192.168.2.204] TASK [mysql : Create backup folder] ********************************************
ok: [192.168.2.204]
ok: [192.168.2.205] TASK [mysql : create log folder] ***********************************************
changed: [192.168.2.204]
changed: [192.168.2.205] TASK [mysql : copy mysql_tar_gz to client] *************************************
changed: [192.168.2.204]
changed: [192.168.2.205] TASK [mysql : copy install_script to client] ***********************************
changed: [192.168.2.204]
changed: [192.168.2.205] TASK [mysql : copy my.cnf to /data/backup] *************************************
changed: [192.168.2.204]
changed: [192.168.2.205] TASK [mysql : install mysql] ***************************************************
changed: [192.168.2.204]
changed: [192.168.2.205] PLAY RECAP *********************************************************************
192.168.2.204 : ok= changed= unreachable= failed=
192.168.2.205 : ok= changed= unreachable= failed=

2、修改mysql的配置文件

首先修改DB1主机的配置文件,在/etc/my.cnf文件中的[mysqld]段添加以下内容

[root@bogon ~]# vim /etc/my.cnf
server-id = 1    #节点标示,主从节点不能相同,必须全局唯一
log-bin=mysql-bin  #开启mysql的binlog日志功能
relay-log = mysql-relay-bin   #开启relay-log日志,relay-log日志记录的是从服务器I/O线程将主服务器的二进制日志读取过来记录到从服务器本地文件,然后SQL线程会读取relay-log日志的内容并应用到从服务器
replicate-wild-ignore-table=mysql.%  #复制过滤选项
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%

然后修改DB2主机的配置文件,

[root@localhost ~]# vim /etc/my.cnf
server-id =
log-bin=mysql-bin
relay-log = mysql-relay-bin
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%

最后分别重启DB1和DB2使配置生效

3、创建复制用户并授权

注:在执行主主互备之前要保证两台server上数据一致

首先在DB1的mysql库中创建复制用户

mysql> grant replication slave on *.* to 'repl_user'@'192.168.2.205' identified by 'repl_passwd';
Query OK, rows affected (0.04 sec) mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin. | | | |
+------------------+----------+--------------+------------------+
row in set (0.00 sec)

然后在DB2的mysql库中将DB1设为自己的主服务器

mysql> change master to \
-> master_host='192.168.2.204',
-> master_user='repl_user',
-> master_password='repl_passwd',
-> master_log_file='mysql-bin.000004',
-> master_log_pos=;
Query OK, rows affected (0.07 sec)

这里需要注意master_log_file和master_log_pos两个选项,这两个选项的值是在DB1上通过“show master status” 查询到的结果

接着在DB2上启动slave服务

mysql> start slave;
Query OK, rows affected (0.01 sec)

下面查看DB2上slave的运行状态,

mysql> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.204
Master_User: repl_user
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: mysql-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
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: mysql.%,test.%,information_schema.%  #跳过的表
Last_Errno:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
row in set (0.00 sec)

到这里,从DB1到DB2的mysql主从复制已经完成。接下来开始配置从DB2到DB1的mysql主从复制

在DB2的mysql库中创建复制用户

mysql> grant replication slave on *.* to 'repl_user'@'192.168.2.204' identified by 'repl_passwd';
Query OK, rows affected (0.00 sec) mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin. | | | |
+------------------+----------+--------------+------------------+
row in set (0.00 sec)

然后在DB1的mysql库中将DB2设为自己的主服务器

mysql> change master to \
-> master_host='192.168.2.205',
-> master_user='repl_user',
-> master_password='repl_passwd',
-> master_log_file='mysql-bin.000005',
-> master_log_pos=;
Query OK, rows affected (0.07 sec)

最后,在DB1上启动slave服务

mysql> start slave;
Query OK, rows affected (0.01 sec)

查看DB1上slave的运行状态

mysql> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.205
Master_User: repl_user
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: mysql-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
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: mysql.%,test.%,information_schema.%
Last_Errno:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
row in set (0.00 sec)

二、配置keepalived实现mysql双主高可用

1、安装keepalived

[root@bogon src]# tar zxf keepalived-1.2..tar.gz
[root@bogon src]# cd keepalived-1.2.
[root@bogon keepalived-1.2.]# ./configure --sysconf=/etc --with-kernel-dir=/lib/modules/2.6.-642.3..el6.x86_64/
[root@bogon keepalived-1.2.]# make && make install
[root@bogon keepalived-1.2.]# ln -s /usr/local/sbin/keepalived /sbin/
[root@bogon keepalived-1.2.]# chkconfig --add keepalived
[root@bogon keepalived-1.2.]# chkconfig --level keepalived on
[root@bogon keepalived-1.2.24]# yum  -y install ipvsadm ####之前没安装ipvsadm,导致 keepalived配置中lvs配置部分不生效,其中定义的notify_down 字段死活不生效,查了好久在发现是没安装ipvsadm导致的,泪奔!!!
[root@bogon keepalived-1.2.24]# ipvsadm

2、配置keepalived

DB1上keepalived.conf配置为

[root@bogon keepalived-1.2.]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval
vrrp_gna_interval
} vrrp_instance HA_1 {
state BACKUP    #在DB1和DB2上均配置为BACKUP
interface eth1
virtual_router_id
priority
advert_int
nopreempt    #不抢占模式,只有优先级高的机器上设置即可,优先级低的机器可不设置
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.2.33
}
} virtual_server 192.168.2.33 {
delay_loop
lb_algo wrr
lb_kind DR
persistence_timeout 60  #会话保持时间 
protocol TCP
real_server 192.168.2.204 {
weight
notify_down /root/shutdown.sh  #检测到服务down后执行的脚本 
TCP_CHECK {
connect_timeout 10  #连接超时时间
nb_get_retry 3    #重连次数
delay_before_retry 3   #重连间隔时间  
connect_port 3306     #健康检查端口
}
}
}

DB2上keepalived.conf配置为

[root@localhost keepalived-1.2.]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval
vrrp_gna_interval
} vrrp_instance HA_1 {
state BACKUP
interface eth1
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.2.33
}
} virtual_server 192.168.2.33 {
delay_loop
lb_algo wrr
lb_kind DR
persistence_timeout
protocol TCP
real_server 192.168.2.205 {
weight
notify_down /root/shutdown.sh
TCP_CHECK {
connect_timeout
nb_get_retry
delay_before_retry
connect_port
}
}
}

编写检测服务down后所要执行的脚本shutdown.sh

[root@bogon ~]# cat /root/shtdown.sh
#!/bin/bash
killall keepalived

注:此脚本是上面配置文件notify_down选项所用到的,keepalived使用notify_down选项来检查real_server的服务状态,当发现real_server服务故障时,便触发此脚本;我们可以看到,脚本就一个命令,通过killall keepalived强制杀死keepalived进程,从而实现了MySQL故障自动转移。另外,我们不用担心两个MySQL会同时提供数据更新操作,因为每台MySQL上的keepalived的配置里面只有本机MySQL的IP+VIP,而不是两台MySQL的IP+VIP

启动keepalived并查看日志

[root@bogon keepalived-1.2.]# chmod  /etc/init.d/keepalived
[root@bogon keepalived-1.2.]# service keepalived start
正在启动 keepalived: [确定]
[root@bogon keepalived-1.2.]# tail -f /var/log/messages
Oct :: bogon Keepalived_vrrp[]: Sending gratuitous ARP on eth1 for 192.168.2.33
Oct :: bogon Keepalived_vrrp[]: Sending gratuitous ARP on eth1 for 192.168.2.33
Oct :: bogon Keepalived_vrrp[]: Sending gratuitous ARP on eth1 for 192.168.2.33
Oct :: bogon Keepalived_vrrp[]: Sending gratuitous ARP on eth1 for 192.168.2.33
Oct :: bogon Keepalived_vrrp[]: Sending gratuitous ARP on eth1 for 192.168.2.33
Oct :: bogon Keepalived_vrrp[]: VRRP_Instance(HA_1) Sending/queueing gratuitous ARPs on eth1 for 192.168.2.33
Oct :: bogon Keepalived_vrrp[]: Sending gratuitous ARP on eth1 for 192.168.2.33
Oct :: bogon Keepalived_vrrp[]: Sending gratuitous ARP on eth1 for 192.168.2.33
Oct :: bogon Keepalived_vrrp[]: Sending gratuitous ARP on eth1 for 192.168.2.33
Oct :: bogon Keepalived_vrrp[]: Sending gratuitous ARP on eth1 for 192.168.2.33

三、测试功能

1、在远程客户端通过vip登陆测试

[root@www ansible]# mysql -h 192.168.2.33 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.5.-log Source distribution Copyright (c) , , 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>

mysql> show variables like "%hostname%"
  -> ;
  +---------------+-------+
  | Variable_name | Value |
  +---------------+-------+
  | hostname | bogon |
  +---------------+-------+
  1 row in set (0.00 sec)

从sql输出结果看,可以通过vip登陆,并且登陆了DB1服务器

2、创建一个数据库,然后在这个库重创建一个表,并插入数据

mysql> create database repldb;
Query OK, row affected (0.02 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| repldb |
| test |
+--------------------+
rows in set (0.06 sec) mysql> use repldb;
Database changed
mysql> create table repl_table(id int,email varchar(),password varchar() not null);
Query OK, rows affected (0.03 sec) mysql> show tables;
+------------------+
| Tables_in_repldb |
+------------------+
| repl_table |
+------------------+
row in set (0.01 sec) mysql> insert into repl_table(id,email,password) values(,"master@163.com","qweasd");
Query OK, row affected (0.00 sec)

登陆DB2主机的mysql,可数据是否复制成功

mysql> show variables like "%hostname%";
+---------------+-----------------------+
| Variable_name | Value |
+---------------+-----------------------+
| hostname | localhost.localdomain |
+---------------+-----------------------+
row in set (0.01 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| repldb |
| test |
+--------------------+
rows in set (0.05 sec) mysql> use repldb;
Database changed
mysql> show tables;
+------------------+
| Tables_in_repldb |
+------------------+
| repl_table |
+------------------+
row in set (0.00 sec) mysql> select * from repl_table;
+------+----------------+----------+
| id | email | password |
+------+----------------+----------+
| | master@.com | qweasd |
+------+----------------+----------+
row in set (0.08 sec)

3、停止DB1主机上的mysql,查看故障是否自动转移

[root@bogon ~]# service mysqld stop
Shutting down MySQL.. SUCCESS!

登陆192.168.2.33查看:

mysql> show variables like "%hostname%";
ERROR (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:
Current database: repldb +---------------+-----------------------+
| Variable_name | Value |
+---------------+-----------------------+
| hostname | localhost.localdomain |
+---------------+-----------------------+
row in set (0.01 sec)

可以看到现在登陆的是DB2 故障自动切换成功

接着,插入数据看DB1是否能复制

mysql> insert into repl_table(id,email,password) values(,"slave@163.com","qweasd");
Query OK, row affected (0.06 sec) mysql> use repldb;
Database changed
mysql> select * from repl_table;
+------+----------------+----------+
| id | email | password |
+------+----------------+----------+
| | master@.com | qweasd |
| | slave@.com | qweasd |
+------+----------------+----------+
rows in set (0.00 sec)

登陆DB1查看表数据

[root@bogon ~]# service mysqld start
Starting MySQL. SUCCESS!
[root@bogon ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.5.-log Source distribution Copyright (c) , , 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> use repldb;
Database changed
mysql> select * from repl_table;
+------+----------------+----------+
| id | email | password |
+------+----------------+----------+
| | master@.com | qweasd |
| | slave@.com | qweasd |
+------+----------------+----------+
rows in set (0.02 sec)

复制成功!

到此全部完成!!!

keepalived+mysql实现双主高可用的更多相关文章

  1. 基于Keepalived实现LVS双主高可用集群

    Reference:  https://mp.weixin.qq.com/s?src=3&timestamp=1512896424&ver=1&signature=L1C7us ...

  2. nginx+keepalived实现nginx双主高可用的负载均衡

    http://kling.blog.51cto.com/3320545/1253474 一.前言: 在互联网上面,网站为用户提供原始的内容访问,同时为用户提供交互操作.提供稳定可靠的服务,可以给用户带 ...

  3. MySQL集群(四)之keepalived实现mysql双主高可用

    前面大家介绍了主从.主主复制以及他们的中间件mysql-proxy的使用,这一篇给大家介绍的是keepalived的搭建与使用! 一.keepalived简介 1.1.keepalived介绍 Kee ...

  4. 基于keepalived搭建mysql双主高可用

    目录 概述 环境准备 keepalived搭建 mysql搭建 mysql双主搭建 mysql双主高可用搭建 概述 传统(不借助中间件)的数据库主从搭建,如果主节点挂掉了,从节点只能读取无法写入,只能 ...

  5. MariaDB+Keepalived双主高可用配置MySQL-HA

    利用keepalived构建高可用MySQL-HA,保证两台MySQL数据的一致性,然后用keepalived实现虚拟VIP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换. ...

  6. Nginx(haproxy)+keepalived+Tomcat双主高可用负载均衡

    周末的时候一个正在学Linux的朋友问我,高可用怎么玩?我和他微信了将近三个小时,把Nginx和haproxy双主高可用教给他了,今天突然想把这个给写进博客里,供给那些正在学习Linux系统的朋友们, ...

  7. haproxy+keepalive双主高可用实现负载均衡

    转载自https://blog.51cto.com/3381847248/1977073 前面我已经介绍了haproxy结合keepalive做简单的双主高可用,如果不清楚的话,可以去我的上一 篇博客 ...

  8. 使用Keepalived实现MySQL双主高可用

    MySQL双主配置 环境准备: OS: CentOS7 master:192.168.1.10 backup:192.168.1.20 VIP:192.168.1.30 一.安装MySQL数据库. 在 ...

  9. MySQL5.7 利用keepalived来实现mysql双主高可用方案的详细过程

    Reference:  http://blog.csdn.net/mchdba/article/details/51377989 服务器准备 Keepalived:192.168.13.15 Keep ...

随机推荐

  1. python 脚本中使用了第三方openpyxl 打包程序运行提示ImportError:cannot import name __version__

    最近写了一个脚本,脚本中使用了第三方openpyxl(openpyxl是使用 pip install openpyxl 下载的),先是使用py2exe打包程序,打包过程中提示很多文件没有包含,在没有仔 ...

  2. 2016-08-16: 检测函数是否存在的C++模板

    #include <iostream> struct Hello { ; } }; struct Generic {}; // SFINAE test template <typen ...

  3. jq封装的tab切换

    function tab(a,b,c){ $(a).on(c,function(){ $(this).addClass('active').siblings().removeClass('active ...

  4. OC Runtime

    OC 是面向运行时的语言.Runtime就是系统在运行的时候的一些机制,其中最主要的是消息发送机制.OC语言与其他语言(如C语言)在函数(方法)的调用有很大的不同.C语言,函数的调用在编译的时候就已经 ...

  5. Windows程序设再读笔记01-起步

    1.从程序员角度看,统一的界面意味着编程人员可以使用windows自带的例程来构建许多的功能,例如菜单,对话框等.只用几行代码就可以实现很多复杂的功能.但是这同时也增加了一些限制,使得做出一个个性化的 ...

  6. 关于angularJS绑定数据时自动转义html标签

    关于angularJS绑定数据时自动转义html标签 angularJS在进行数据绑定时默认是会以文本的形式输出,也就是对你数据中的html标签不进行转义照单全收,这样提高了安全性,防止了html标签 ...

  7. 移动端rem页面详谈

    rem布局是移动端常见的布局之一,也是较为成熟的方案.接下来就详细说以下rem布局的实际操作. 1.首先加<meta />标签,设置视口的大小,不多说. <meta name=&qu ...

  8. oracle 客户端单独配置

    本文目的是在CentOS 5.3上安装Oracle 11.2 instant client来访问远端的Oracle 10.2数据库,笔者测试通过,应该也适用于Redhat Linux 5.x     ...

  9. SpringMVC学习笔记(六)

    一.SpringMVC文件的上传 1.1.需要导入两个jar包 1.2在SpringMVC配置文件中加入 <!-- upload settings --> <bean id=&quo ...

  10. LDO和DC-DC器件的区别

    DCDC的意思是直流变(到)直流(不同直流电源值的转换),只要符合这个定义都可以叫DCDC转换器,包括LDO.但是一般的说法是把直流变(到)直流由开关方式实现的器件叫DCDC.      LDO 是低 ...