mysql主主同步检查_mysql主主同步

实验环境:两台centos6.5

mysql1 ip:10.1.1.20

mysql2  ip:10.1.1.21

mysql vip:10.1.1.25

一、安装mysql,并打造主主同步

主主同步就是两台机器互为主的关系,在任何一台机器上写入都会同步。

安装mysql的过程不解释,yum就好啦

配置主主同步

1.配置 /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

log-bin=binlog  #开启binlog功能

log-bin-index=binlog.index

sync_binlog=0

server_id = 1    #两台机器不能重复,一个1 一个2 就好

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

2.分别在两台机器上配置同步账号

10.1.1.20机器上:

[root@localhost ~]# mysql

Welcome to the MySQL monitor. Commands end with; or \g.

Your MySQL connection id is 2

Server version: 5.0.77-log Sourcedistribution

Type 'help;' or '\h' for help. Type '\c' toclear the buffer.

mysql> GRANT replication slave ON *.* TO'ab'@'%' identified by '123';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

10.1.1.21机器上:

[root@localhost ~]# mysql

Welcome to the MySQL monitor. Commands end with; or \g.

Your MySQL connection id is 2

Server version: 5.0.77-log Sourcedistribution

Type 'help;' or '\h' for help. Type '\c' toclear the buffer.

mysql> GRANT replication slave ON *.* TO'ab'@'%' identified by '123';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

3.设置同步

10.1.1.20机器上:

mysql> flush tables with read lock;

mysql> show master status;

+---------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB |Binlog_Ignore_DB |

+---------------+----------+--------------+------------------+

| binlog.000003 | 365 | | |

+---------------+----------+--------------+------------------+

1 row in set (0.03 sec)

mysql> unlock tables;

Query OK, 0 rows affected (0.03 sec)

10.1.1.21机器上:

mysql> change master to master_host='10.1.1.20', master_port=3306, master_user='ab',master_password='123', master_log_file='binlog.000003',master_log_pos=365;

Query OK, 0 rows affected (0.06 sec)

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status \G  #执行这命令后 注意观察下面这两个参数,必须要都是yes才行

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

同样的 反过来做相同操作

10.1.1.21机器上:

mysql> flush tables with read lock;

mysql> show master status;

+---------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB |Binlog_Ignore_DB |

+---------------+----------+--------------+------------------+

| binlog.000004 | 207 | | |

+---------------+----------+--------------+------------------+

1 row in set (0.03 sec)

mysql> unlock tables;

Query OK, 0 rows affected (0.03 sec)

10.1.1.20机器上:

mysql> change master tomaster_host='10.1.1.21', master_port=3306, master_user='ab',master_password='123', master_log_file='binlog.000004',master_log_pos=207;

Query OK, 0 rows affected (0.06 sec)

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status \G  #执行这命令后 注意观察下面这两个参数,必须要都是yes才行

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

介此,主主同步打造完成,可以简单测试一下,分别在两个机器上写数据 看看会不会同步到另一台机器上

PS:如果报错  Slave_IO_Running: NO  可以检查同步的账号是否创建正常!

二、安装keepalived 并设置监控

keepalived是安装在两台MySQL服务器上的

首先安装keepalived 过程不解释就正常解压安装就好

安装后配置 vim /etc/keepalived/keepalived.conf 内容如下

10.1.1.20的配置文件

! 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 127.0.0.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_instance VI_1 {

state backup      #两台配置此处均是BACKUP

interface eth0

virtual_router_id 51

priority 100      #优先级,另一台改为90

advert_int 1

nopreempt          #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

10.1.1.25

}

}

virtual_server 10.1.1.25 3306 {

delay_loop 6

lb_algo wrr

lb_kind DR

persistence_timeout 50        #会话保持时间

protocol TCP

real_server 10.1.1.20 3306 {

weight 3

notify_down /tmp/nimei.sh    #检测到mysql服务挂了就执行这个脚本(脚本要自己写哈)

TCP_CHECK {

connect_timeout 10        #连接超时时间

nb_get_retry 3            #重连次数

delay_before_retry 3      #重连间隔时间

connect_port 3306        #健康检查端口

}

}

}

10.1.1.21 的配置文件

! 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 127.0.0.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_instance VI_1 {

state backup

interface eth0

virtual_router_id 51

priority 90

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

10.1.1.25

}

}

virtual_server 10.1.1.25 3306 {

delay_loop 6

lb_algo wrr

lb_kind DR

persistence_timeout 50

protocol TCP

real_server 10.1.1.21 3306 {

weight 3

notify_down /tmp/nimei.sh

TCP_CHECK {

connect_timeout 10

nb_get_retry 3

delay_before_retry 3

connect_port 3306

}

}

}

编写监控mysql服务是否挂了的脚本,按照上面配置文件的位置编写脚本。

vim /tmp/nimei.sh

#!/bin/sh

pkill keepalived

脚本很简单啊 就一句,目的是当keepalived检测到mysql服务挂了之后触发这个脚本,杀死keepalived进程,让另一台机器接管

好 修改后启动keeplived服务

介此整个集群搭建完成

三、测试

找一台机器用虚拟ip连接mysql

[root@localhost html]# mysql -uab  -h 10.1.1.25 -p123

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 736

Server version: 5.1.66-log Source distribution

Copyright (c) 2000, 2012, 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服务,看看还能不能连上!!

本文转自 linuxpp 51CTO博客,原文链接:http://blog.51cto.com/1439337369/1758790,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值