search
HomeDatabaseMysql Tutorial高可用的Mysql双机热备(Mysql_HA)

1.KeepAlived+Mysql 使用KeepAlived实现高可用的MYSQL_HA集群环境中,MYSQL为(Master/Master)主/主同步复制关系,保证MYSQL服务器数据的一致性,用KeepAlived提供虚拟IP,通过KeepAlived来进行故障监控,实现Mysql故障时自动切换。 布署环境拓朴如下: ?Mysq

1.KeepAlived+Mysql

使用KeepAlived实现高可用的MYSQL_HA集群环境中,MYSQL为(Master/Master)主/主同步复制关系,保证MYSQL服务器数据的一致性,用KeepAlived提供虚拟IP,通过KeepAlived来进行故障监控,实现Mysql故障时自动切换。

布署环境拓朴如下:

?Mysql?VIP :192.168.187.61

Master1:192.168.187.129

Master:192.168.187.132

OS 环境:Cent OS 5.9

Mysql版本:Mysql5.5.31


2.安装mysql

2.1.Mysql升级安装

因为CentOS的Mysql还是停留在5.0.19,而我们做Mysql之间的同步复制,Mysql版本至少要在Mysql5.1以上,所以要对其进行升级安装。

>>使用 yum安装, yum 可以帮你解决依赖于冲突

# rpm –Uvh ?http://repo.webtatic.com/yum/centos/5/latest.rpm?? //安装最新的mysql的yum源

# yum –y install Mysql55 MySQL55-* --enablerepo=webtatic?? //安装Mysql,--enablerepo参数是用来指定源

?

>>开启mysql服务

# service mysqld start ?//开启mysql服务

?

>>刚安装密码为空,设置root密码

# mysqladmin –u root password ‘1234’ ?//设置root密码?????

>>更改mysql配置文件

# cp /usr/share/mysql/my-medium.cnf? /etc/my.cnf? //在etc目录下建立mysql的配置文件my.cnf

# service mysqld restart//重启mysql服务

?

>>登陆Mysql

# mysql –u root –p 1234?? ?//设置root密码

Mysql>??? ?//登陆进了mysql

2.2.Mysql主/主配置

2.2.1 设置配置文件

Mysql是通过日志进行同步复制的,先建立日志文件

#touch /var/log/mysql/mysql-bin.log?? //建立日志文件

#chown mysql.mysql /var/log/mysql/mysql-bin.log ?//将日志文件的所属用户和用户组更改成mysql

在两台要进行备份的mysql服务器上的my.cnf文件进行配置如下(将下面的配置分别加入相关服务器的my.cnf):

Master1(192.168.187.129)

Master(192.168.187.132)

#主标服务标识号,必需唯一

server-id = 1

#因为MYSQL是基于二进制的日志来做同步的,每个日志文件大小为 1G

log-bin=/var/log/mysql/mysql-bin.log

#要同步的库名

binlog-do-db = test

#不记录日志的库,即不需要同步的库

binlog-ignore-db=mysql

#用从属服务器上的日志功能

log-slave-updates

#经过1日志写操作就把日志文件写入硬盘一次(对日志信息进行一次同步)。n=1是最安全的做法,但效率最低。默认设置是n=0。

sync_binlog=1

# auto_increment,控制自增列AUTO_INCREMENT的行为

用于MASTER-MASTER之间的复制,防止出现重复值,

auto_increment_increment=n有多少台服务器,n 就设置为多少,

auto_increment_offset=1设置步长,这里设置为1,这样Master的auto_increment字段产生的数值是:1, 3, 5, 7, …等奇数ID

auto_increment_offset=1

auto_increment_increment=2

#进行镜像处理的数据库

replicate-do-db = test

#不进行镜像处理的数据库

replicate-ignore-db= mysql

#主标服务标识号,必需唯一

server-id = 2

#因为MYSQL是基于二进制的日志来做同步的,每个日志文件大小为 1G

log-bin=/var/log/mysql/mysql-bin.log

#要同步的库名

binlog-do-db = test

#不记录日志的库,即不需要同步的库

binlog-ignore-db=mysql

#用从属服务器上的日志功能

log-slave-updates

#经过1日志写操作就把日志文件写入硬盘一次(对日志信息进行一次同步)。n=1是最安全的做法,但效率最低。默认设置是n=0。

sync_binlog=1

# auto_increment,控制自增列AUTO_INCREMENT的行为

用于MASTER-MASTER之间的复制,防止出现重复值,

auto_increment_increment=n有多少台服务器,n 就设置为多少,

auto_increment_offset=2设置步长,这里设置为2,这样Master的auto_increment字段产生的数值是:2, 4, 6, 8, …等奇数ID

auto_increment_offset=2

auto_increment_increment=2

#进行镜像处理的数据库

replicate-do-db = test

#不进行镜像处理的数据库

replicate-ignore-db= mysql

2.2.2查看配置情况

按上面的配置将两台服务器配置好以后,重新启动mysql服务,用showmaster status查看一下两台服务器的Master配置情况,可以看出已经配置成功,如下:

NO1:Master1(192.168.187.129)的情况

# mysql –u root –p 1234

mysql> show master status;

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

| File ????????????| Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000001|????? 107 | test???????? | mysql??????????? |

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

NO2:Master1(192.168.187.132)的情况

# mysql –u root –p 1234

mysql> show master status;

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

| File???????????? | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000001|????? 107 | test???????? | mysql??????????? |

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

2.2.3建立权限帐户,实现同步

a.创建账户并授予REPLICATION SLAVE权限

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY '1234'; ??//建立一个用户名为slave的帐户

mysql> FLUSH PRIVILEGES;?? //刷新相关权限表

因为进行双向复制,两边服务器都需要建立一个用于复制的的用户。两边可以复用上面的语句,用户名和密码可以自行进行修改。

b.同步设置

Master1(192.168.187.129)上操作如下:

mysql> stop slave; ?//停止slave

mysql>change master to master_host='192.168.187.132', master_user='slave', master_password='1234', master_log_file='mysql-bin.000001', master_log_pos=107;? //修改当前的Master的值,因为是互为备份,所以Master1(192.168.187.129)的master为Master2(192.168.187.132),Master2设置复制的用户名为slave,密码是1234,上面通过 show master status我们得知,log_file是mysql-bin.000001,postion是107。

mysql>start slave;??? //开始salve,开始同步

mysql>show slave status; ??//检测slave状态,如果Slave_IO_Running: Yes??? Slave_SQL_Running: Yes??? Seconds_Behind_Master: 0就OK了

?

Master2(192.168.187.132)上面操作如下:

mysql> stop slave;? //停止slave

mysql>change master to master_host='192.168.187.129', master_user='slave', master_password='1234', master_log_file='mysql-bin.000001', master_log_pos=107; ?//修改当前的Master的值,因为是互为备份,所以Master2(192.168.187.132)的master为Master2(192.168.187.129),Master1设置复制的用户名为slave,密码是1234,上面通过 show master status我们得知,log_file是mysql-bin.000001,postion是107。

mysql>start slave;??? //开始salve,开始同步

mysql>show slave status;? //检测slave状态,如果Slave_IO_Running: Yes??? Slave_SQL_Running: Yes??? Seconds_Behind_Master: 0就OK了

c.测试情况:

Step1:建一个测试表Test,两个字段,id与name字段,id字段为自增,两个服务器上面都是同样的结构,如下图:

Step2:我在Master1(192.168.187.129)表上执行一个insert语句,并进行查询,如下图:

Step3:在Master2(192.168.187.132)中查询,可以发现数据已经同步过来了,如下图:

?

3KeepAlived安装配置

3.1 KeepAlived的安装方法

?可参照“高可用的负载均衡配置方法(Haproxy+KeepAlived)” 5.1 中KeepAlived的安装方法

3.2将keepalived加入服务

可参照“高可用的负载均衡配置方法(Haproxy+KeepAlived)” 5.2 ?中将keepalived加入服务

3.3 KeepAlived的配置

安装好以后,对其进行配置如下:

有两台机器(MASTER1)所在的 192.168.187.129与(Master2) 192.168.187.132,用(VIP)192.168.187.61做虚拟IP。

在两台服各器中的/etc/keepalived文件夹中的keepalived.conf下进行配置:

Master1的设置?

192.168.187.129

global_defs {

?? router_id Mysql_HA  #当前节点名

}

vrrp_instance VI_1{

??? state BACKUP???????#两台配置节点均为BACKUP

interface eth0?????? #绑定虚拟IP的网络接口

virtual_router_id 51 #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组

priority 100???????? #节点的优先级,另一台优先级改低一点

acvert_int 1???????? #组播信息发送间隔,两个节点设置必须一样

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

authentication{????? #设置验证信息,两个节点必须一致

??? auth_type? PASS

??? auth_pass? 1111

}

Virtual_ipaddress{?? #指定虚拟IP, 两个节点设置必须一样

192.168.187.61

???? }

?? }

virtual_server 192.168.187.61 3306 { ??#linux虚拟服务器(LVS)配置

delay_loop 2??? ?#每个2秒检查一次real_server状态

lb_algo wrr ?????#LVS调度算法, rr|wrr|lc|wlc|lblc|sh|dh

lb_kind DR???? ?#LVS集群模式 , NAT|DR|TUN

persistence_timeout 60?? ?#会话保持时间

protocol TCP ???#使用的协议是TCP还是UDP

real_server 192.168.187.129 3306 {

weight 3 ??#权重

notify_down ?/usr/local/bin/mysql.sh?? ?#检测到服务down后执行的脚本

TCP_CHECK {

connect_timeout 10 ??#连接超时时间

nb_get_retry 3??????#重连次数

delay_before_retry 3?#重连间隔时间

connect_port 3306 ???#健康检查端口

}

}

?

Master2的设置?

192.168.187.132

global_defs {

?? router_id Mysql_HA  #当前节点名

}

vrrp_instance VI_1{

??? state BACKUP???????#两台配置节点均为BACKUP

interface eth0?????? #绑定虚拟IP的网络接口

virtual_router_id 51 #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组

priority 90???????? #节点的优先级,另一台优先级改低一点

acvert_int 1???????? #组播信息发送间隔,两个节点设置必须一样

authentication{????? #设置验证信息,两个节点必须一致

??? auth_type? PASS

??? auth_pass? 1111

}

Virtual_ipaddress{?? #指定虚拟IP, 两个节点设置必须一样

192.168.187.61

???? }

?? }

virtual_server 192.168.187.61 3306 { ??#linux虚拟服务器(LVS)配置

delay_loop 2??? ?#每个2秒检查一次real_server状态

lb_algo wrr ?????#LVS调度算法, rr|wrr|lc|wlc|lblc|sh|dh

lb_kind DR???? ?#LVS集群模式 , NAT|DR|TUN

persistence_timeout 60?? ?#会话保持时间

protocol TCP ???#使用的协议是TCP还是UDP

real_server 192.168.187.132 3306 {

weight 3 ??#权重

notify_down ??/usr/local/bin/mysql.sh?? ?#检测到服务down后执行的脚本

TCP_CHECK {

connect_timeout 10 ??#连接超时时间

nb_get_retry 3??????#重连次数

delay_before_retry 3?#重连间隔时间

connect_port 3306 ???#健康检查端口

}

}

脚本/usr/local/bin/mysql.sh

#vi? /usr/local/bin/mysql.sh

#!/bin/sh

killall keepalived

?

3.4 KeepAlived测试

可参照“高可用的负载均衡配置方法(Haproxy+KeepAlived)” 5.4 中KeepAlived测试

4.Mysql测试

Step1:打开三个服务器进行查看,刚开始三个都为空


Step2:在VIP(192.168.187.61)服务器中插入一条数据


Step3:再查看三个服务器中的数据都已经同步过来了


当关掉做为主机的192.168.187.129做为宕机处理,同样也不会出问题,虚拟IP由192.168.187.129漂移

到192.168.187.132上面。

?

5.安装时出现的问题及处理方法

NO1: Slave将无法链接到 Master情况

错误:Slave将无法链接到 Master??

原因:bind-address默认是127.0.0.1你必须更改它

解决办法:修改my.cnf,加上如下图红框所示的配置!


NO2: mysql error 1129 错误

错误:mysql 1129错误!如下图:

?????

原因:是因为mysql将ip连接阻塞了。

解决办法:登录到mysql数据库服务器端,使用命令:

#?mysqladmin -u root -p ?flush-hosts; ?

Enter password:

?

作者:xuejiazhi 发表于2013-5-17 17:36:01 原文链接

阅读:0 评论:0 查看评论

高可用的Mysql双机热备(Mysql_HA)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

MySQL BLOB : are there any limits?MySQL BLOB : are there any limits?May 08, 2025 am 12:22 AM

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

MySQL : What are the best tools to automate users creation?MySQL : What are the best tools to automate users creation?May 08, 2025 am 12:22 AM

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

MySQL: Can I search inside a blob?MySQL: Can I search inside a blob?May 08, 2025 am 12:20 AM

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software