mysql 主主复制(双主复制)+ 配置KEEPALIVED实现热备
binlog-do-db和replicate-do-db表示需要同步的数据库
binlog-ignore-db和replicate-ignore-db表示不需要同步的数据库
云端服务器为master配置如下:
[root@DB data]# egrep "server-id|log-bin|binlog-ignore-db|auto-increment-increment|auto-increment-offset|slave-skip-errors" /etc/my.cnf
log-bin=mysql-bin
binlog-ignore-db = mysql,information_schema
auto-increment-increment = 2
auto-increment-offset = 1
slave-skip-errors = all
server-id = 1
#server-id = 2
#log-bin=mysql-bin
先查看下log bin日志和pos值位置:
[root@DB data]# mysql -uroot -p -e "show master status;"
Enter password:
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000016 | 107 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
[root@DB data]# service mysqld restart
Shutting down MySQL. SUCCESS!
Starting MySQL... SUCCESS!
mysql> select user,password,host from user;
+--------+-------------------------------------------+-----------+
| user | password | host |
+--------+-------------------------------------------+-----------+
| root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | localhost |
| zabbix | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | % |
+--------+-------------------------------------------+-----------+
2 rows in set (0.05 sec) mysql> grant replication slave on *.* to 'rep'@'%' identified by 'replication';
Query OK, 0 rows affected (0.05 sec) mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec) mysql> change master to
-> master_host='192.168.1.99',
-> master_user='rep',
-> master_password='replication',
-> master_log_file='mysql-bin.000016',
-> master_log_pos=107;
Query OK, 0 rows affected (0.41 sec) mysql> select user,password,host from user;
+--------+-------------------------------------------+-----------+
| user | password | host |
+--------+-------------------------------------------+-----------+
| root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | localhost |
| rep | *D36660B5249B066D7AC5A1A14CECB71D36944CBC | % |
| zabbix | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | % |
+--------+-------------------------------------------+-----------+
3 rows in set (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
backup配置如下:
mysql中有自增长字段,在做数据库的主主同步时需要设置自增长的两个相关配置:auto_increment_offset和auto_increment_increment。
auto_increment_offset表示自增长字段从那个数开始,他的取值范围是1 ..
auto_increment_increment表示自增长字段每次递增的量,其默认值是1,取值范围是1 ..
在主主同步配置时,需要将两台服务器的auto_increment_increment增长量都配置为2,而要把auto_increment_offset分别配置为1和2.
mysql> use mysql;
Database changed
mysql> select user,host,password from user;
+--------+-----------+-------------------------------------------+
| user | host | password |
+--------+-----------+-------------------------------------------+
| root | localhost | *5E9021B1C46435835981A94A7087707B3465144B |
| master | % | *5E9021B1C46435835981A94A7087707B3465144B |
| rep | % | *5E9021B1C46435835981A94A7087707B3465144B |
+--------+-----------+-------------------------------------------+
3 rows in set (0.00 sec) mysql> grant replication slave on *.* to 'rep'@'%' identified by "3306";
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000023 | 184 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> change master to
-> master_host='192.168.1.66',
-> master_user='rep',
-> master_password='3306',
-> master_log_file='mysql-bin.000023',
-> master_log_pos=184;
Query OK, 0 rows affected (0.29 sec) mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果有如下报错及解决方案:
mysql> slave stop
-> ;
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> reset slave;
Query OK, 0 rows affected (0.18 sec) mysql> slave start;
Query OK, 0 rows affected (0.13 sec) mysql> show slave status;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@DB-S data]# egrep "server-id|log-bin|binlog-ignore-db|auto-increment-increment|auto-increment-offset|slave-skip-errors" /etc/my.cnf
log-bin=mysql-bin
server-id = 2
MASTER中新建张表;
mysql> use test;
Database changed
mysql> show tables;
Empty set (0.00 sec) mysql> create table user (number INT(10),name VARCHAR(255));
Query OK, 0 rows affected (0.17 sec) mysql> SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| user |
+----------------+
1 row in set (0.01 sec)
查看BACKUP是否同步:
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| user |
+----------------+
1 row in set (0.00 sec)
在backup中插入一条数据:
mysql> insert into user values(01,'zhangsan');
Query OK, 1 row affected (0.11 sec) mysql> select * from user;
+--------+----------+
| number | name |
+--------+----------+
| 1 | zhangsan |
+--------+----------+
1 row in set (0.00 sec)
查看MASTER是否同步成功:
mysql> select * from user;
+--------+----------+
| number | name |
+--------+----------+
| 1 | zhangsan |
+--------+----------+
1 row in set (0.00 sec)
同步成功!
在BACKUP端:
[root@DB ~]# yum install -y pcre-devel openssl-devel popt-devel #安装依赖包
[root@DB ~]# which wget
/usr/bin/wget
[root@DB ~]# wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
[root@DB ~]# ls
keepalived-1.2.7.tar.gz
[root@DB ~]# cd keepalived-1.2.7
[root@DB keepalived-1.2.7]# pwd
/root/keepalived-1.2.7
[root@DB keepalived-1.2.7]# ./configure --prefix=/usr/local/keepalived
[root@DB keepalived-1.2.7]# make && make install
[root@DB keepalived-1.2.7]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@DB keepalived-1.2.7]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@DB keepalived-1.2.7]# mkdir /etc/keepalived/
[root@DB keepalived-1.2.7]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@DB keepalived-1.2.7]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@DB keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs {
router_id MYSQL_HA#标识,双主相同
}
vrrp_script check_run{
script '/usr/local/mysql/mysql_check.sh'
interval 300
}
vrrp_instance VI_1 {
state BACKUP#两台都设置BACKUP
interface eth0
virtual_router_id 51#主备相同
priority 100#优先级,backup设置90(最关建点之一,同为backup时优先级别越高,就会主动去抢VIP,所以如果不想让这种情况出现加参数nopreempt)
advert_int 1#主备心跳检查间隔
nopreempt #不主动抢占资源,只在master这台优先级高的设置,backup不设置(当master恢复时,不会把VIP抢过来)
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.74 #vip
}
} virtual_server 192.168.1.74 3306 {
delay_loop 2
#lb_algo rr#LVS算法,用不到,我们就关闭了
#lb_kind NAT
#nat_mask 255.255.255.0
persistence_timeout 50#同一IP的连接60秒内被分配到同一台真实服务器
protocol TCP real_server 192.168.1.66 3306 { #检测本地mysql,backup也要写检测本地mysql
weight 3
notify_down /usr/local/keepalived/mysql.sh #当mysq服down时,执行此脚本,杀死keepalived实现切换
TCP_CHECK {
connect_timeout 3#连接超时
nb_get_retry 3#重试次数
delay_before_retry 3#重试间隔时间
}
}
[root@DB keepalived]# cat /usr/local/keepalived/mysql.sh
#!/bin/bash /usr/bin/pkill keepalived
backup端:
[root@DB-S ~]# 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 30
router_id MYSQL_HA
#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 {
192.168.1.74
}
} virtual_server 192.168.1.74 3306 {
delay_loop 2
#lb_algo rr
#lb_kind NAT
#nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP real_server 192.168.1.99 3306 {
weight 3
notify_down /usr/local/keepalived/mysql.sh
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
[root@DB ~]# chmod +x /usr/local/keepalived/mysql.sh
[root@DB ~]# /etc/init.d/keepalived start
# 授权两台DB服务器允许管理员登录,用于其它服务器登录测试!
mysql> grant all on *.* to 'admin'@'%' identified by '3306';
Query OK, 0 rows affected (0.00 sec) mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec) mysql> select user,host,password from user;
+--------+-----------+-------------------------------------------+
| user | host | password |
+--------+-----------+-------------------------------------------+
| U_R | % | *5E9021B1C46435835981A94A7087707B3465144B |
| root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| admin | % | *5E9021B1C46435835981A94A7087707B3465144B |
| rep | % | *D36660B5249B066D7AC5A1A14CECB71D36944CBC |
| zabbix | % | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+--------+-----------+-------------------------------------------+
5 rows in set (0.00 sec)
通过MYSQL客户端通过VIP连接,看是否成功!
[root@bass ~]# mysql -uadmin -h192.168.1.74 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 175
Server version: 5.5.40-log MySQL Community Server (GPL) Copyright (c) 2000, 2013, 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>
成功连接!!!!
可通MASTER或BACKUP的/var/log/messges日志,看出主备切换过程
[root@DB ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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_fast state UP qlen 1000
link/ether 52:54:00:94:a0:9f brd ff:ff:ff:ff:ff:ff
inet 192.168.1.66/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.74/32 scope global eth0
inet6 fe80::5054:ff:fe94:a09f/64 scope link
valid_lft forever preferred_lft forever
[root@DB-S ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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_fast state UP qlen 1000
link/ether 52:54:00:e4:6a:a4 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.99/24 brd 192.168.1.255 scope global eth0
inet6 fe80::5054:ff:fee4:6aa4/64 scope link
valid_lft forever preferred_lft forever
[root@DB ~]# service mysqld stop
Shutting down MySQL.... SUCCESS!
[root@DB ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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_fast state UP qlen 1000
link/ether 52:54:00:94:a0:9f brd ff:ff:ff:ff:ff:ff
inet 192.168.1.66/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.74/32 scope global eth0
inet6 fe80::5054:ff:fe94:a09f/64 scope link
valid_lft forever preferred_lft forever
[root@DB-S ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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_fast state UP qlen 1000
link/ether 52:54:00:e4:6a:a4 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.99/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.74/32 scope global eth0
inet6 fe80::5054:ff:fee4:6aa4/64 scope link
valid_lft forever preferred_lft forever
#vip 在BACKUP上出现
特别注意:mysql客户端不能在和双主服务器在一台上,否则VIP飘逸时,客户端连接会失败!
mysql 主主复制(双主复制)+ 配置KEEPALIVED实现热备的更多相关文章
- MySQL Replication, 主从和双主配置
MySQL Replication, 主从和双主配置 MySQL的Replication是一种多个MySQL的数据库做主从同步的方案,特点是异步,广泛用在各种对MySQL有更高性能,更高可靠性要求的场 ...
- Nginx+keepalived双机热备(主主模式)
之前已经介绍了Nginx+Keepalived双机热备的主从模式,今天在此基础上说下主主模式的配置. 由之前的配置信息可知:master机器(master-node):103.110.98.14/19 ...
- Nginx+keepalived 双机热备(主主模式)
之前已经介绍了Nginx+Keepalived双机热备的主从模式,今天在此基础上说下主主模式的配置. 由之前的配置信息可知:master机器(master-node):103.110.98.14/19 ...
- mysql传统主从、双主复制+keepalived配置步骤
mysql主从.主主复制(双主复制)配置步骤 一:MySQL复制: MySQL复制简介: 将master服务器中主数据库的ddl和dml操作通过二进制日志传到slaves服务器上,然后在master服 ...
- linux系统mysql主主复制(双主复制)
一.简介 在上一篇的主从复制中:http://www.cnblogs.com/lay2017/p/9043985.html 我们了解到,mysql通过master写日志,slave读取并执行日志内容从 ...
- mycat 1.6.6.1安装以及配置docker 安装mysql 5.7.24 双主多从读写分离主主切换
mycat和mysql的高可用参考如下两个图 简介:应用程序仅需要连接HAproxy或者mycat,后端服务器的读写分离由mycat进行控制,后端服务器数据的同步由MySQL主从同步进行控制. 服务器 ...
- mysql数据库之主从复制+双主--MMM
mysql复制:在主数据库中,前端用户每执行一个写操作/语句,都会在二进制日志中保存一个事件,把这个事件从mysql的服务器中3306端口发送给从服务器,从服务器把这个事件接受下来,接受下来先保存在本 ...
- MySQL自动化安装(双主多从读写分离)
shell #!/bin/bash # Create by # version 1.0 # // # # check out lockfile whether or not exist IsInput ...
- Nginx+keepalived双机热备(主主模式)
IP说明: master机器(master-node):10.0.0.5/172.16.1.5 VIP1:10.0.0.3slave机器(slave-node): 10.0.0.6/172.16. ...
随机推荐
- springmvc常用注解标签详解
1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...
- go tool proof
echo list | go tool pprof -alloc_space gateway http://10.2.1.93:8421/debug/pprof/heap > abc.log e ...
- [STL] lower_bound和upper_bound
STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...
- U盘10分钟安装linux系统
说来可能不信,10分钟包括创建U盘启动盘,用U盘启动,安装,不联网,不更新,不安装语言包,等装好系统再更新. Windows系统硬盘分区 如果你用的是Windows系统,现有的硬盘没有未分配的空间,需 ...
- [bug]The file ‘/xxx/xxx.aspx’ has not been pre-compiled, and cannot be requested
今天莫名奇妙的出现了这个问题,查找很多资料最后解决了. 发现编译的时候,少了一个dll,导致预编译失败. 参考资料 https://blogs.msdn.microsoft.com/asiatech/ ...
- GTP (GPRS隧道协议(GPRSTunnellingProtocol))
GTP (GPRS隧道协议(GPRSTunnellingProtocol)) GTP (GPRS隧道协议(GPRSTunnellingProtocol)) 编辑 本词条缺少信息栏.名片图,补充相关内容 ...
- 10.23lamp环境
前序: 查考文章:http://www.cnblogs.com/mchina/archive/2012/11/28/2778779.html http://www.centos.bz/2011/09/ ...
- [原] Android performClick无效,UI线程理解
原因 开发过程中遇到button.performClick()无效,原因是View.performClick()需要再UI线程中调用才会有效执行. 响应系统调用的方法(比如报告用户动作的onKeyDo ...
- odbc错误信息一览表
ODBC 错误信息 根据 X/Open 和 SQL Access Group SQL CAE 规范 (1992) 所进行的定义,SQLERROR 返回 SQLSTATE 值.SQLSTATE 值是包含 ...
- 二分图最大匹配的König定理及其证明
二分图最大匹配的K?nig定理及其证明 本文将是这一系列里最短的一篇,因为我只打算把K?nig定理证了,其它的废话一概没有. 以下五个问题我可能会在以后的文章里说,如果你现在很想知道的话,网上 ...