1. 技术资料
http://www.linuxidc.com/Linux/2015-06/118767.htm
http://blog.csdn.net/mchdba/article/details/51377989
http://blog.sina.com.cn/s/blog_4f9fc6e10102w6xy.html
推荐 http://www.cnblogs.com/lyhabc/p/3838764.html
2.
可靠的数据:数据不能丢失
可用的服务:服务器不能宕机
可用性的技术方案和可用性概率:
要达到99.9%:使用MYSQL复制技术
要达到99.99%:使用MYSQL NDB 集群和虚拟化技术
要达到99.999%:使用shared-nothing架构的GEO-REPLICATION和NDB集群技术
一年计不同可用性允许宕机时间:
90% 36天
99% 3.65天
99.9% 8.76小时
99.99% 52分钟
99.999% 5.26分钟
99.9999% 31.5秒
3. 各种高可用方案
3.1 mysql主从复制 单活
3.2 双主 单活
3.3 双主 配 SAN存储 单活
3.4 DRBD双主配DRBD 单活
3.5 NDB cluster
3.6 第三方Tungsten软件
3.7 网易INNOSQL
4. MHA: Master High Availability Manager and Tools for MySQL
Perl语言写的脚本管理工具,仅适用MySQL Replicaiton二层环境,维持master高可用。
至少需要三台机器来做。
http://www.2cto.com/database/201407/319748.html
5. 另一组解决方案
http://bbs.chinaunix.net/thread-3769165-1-1.html
5.1 lvs+keepalived+mysql 单点写入主主同步
5.2 lvs+keepalived+mysql 单点写入读负载均衡主主同步
5.3 heartbeat高可用主主同步
5.4 heartbeat+drbd+mysql
5.5 mmm方案
6. 实战5.1的解决方案
http://www.111cn.net/database/mysql/65579.htm
6.1 服务器配置
mysql1: ttt, 10.168.1.111
mysql2: zzz, 10.168.1.222
用户名和密码 root:tiigggg
6.2 在两个数据都创建测试用户和测试库
create user 'usertest'@'%' identified by '123456';
create database testdb;
grant all privileges on testdb.* to 'usertest'@'%';
这帐号只能远程登录。
create user 'usertest'@'localhost' identified by '123456';
grant all privileges on testdb.* to 'usertest'@'localhost';
这帐号只能本地登录。
需要创建内网ip登录账户
在ttt上执行
create user 'usertest'@'10.168.1.222' identified by '123456';
grant all privileges on testdb.* to 'usertest'@'10.168.1.222';
grant replication slave on *.* to 'usertest'@'10.168.1.222' identified by '123456';
flush privileges;
在zzz上执行
create user 'usertest'@'10.168.1.111' identified by '123456';
grant all privileges on testdb.* to 'usertest'@'10.168.1.111';
grant replication slave on *.* to 'usertest'@'10.168.1.111' identified by '123456';
flush privileges;
6.3 修改ttt的mysql配置/etc/my.cnf的[mysqld]的配置如下
#主主同步配置
log-bin=mysql-bin
server-id=1
expire-logs-days=100
replicate-do-db=testdb
binlog-ignore-db=mysql
binlog-ignore-db=infomation_schema
auto-increment-increment=2
auto-increment-offset=1
6.4 修改zzz的mysql配置/etc/my.cnf的[mysqld]的配置如下
#主主同步配置
log-bin=mysql-bin
server-id=2
expire-logs-days=100
replicate-do-db=testdb
binlog-ignore-db=mysql
binlog-ignore-db=infomation_schema
auto-increment-increment=2
auto-increment-offset=2
二都只有server-id不同和 auto-increment- offset不同
auto-increment-offset是用来设定数据库中自动增长的起点的,这两服务器都设定了一次自动增长值2,所以它们的起点必须得不同,这样才能避免两台服务器数据同步时出现主键冲突
replicate-do-db 指定同步的数据库,我们只在两台服务器间同步usertest库
另:auto-increment-increment的值应设为整个结构中服务器的总数,本案例用到两台服务器,所以值设为2
6.5 重启这这两个数据库
service mysqld restart
6.5 互告bin-log信息
在zzz上执行 show master status
在ttt上执行 show master status
能看到各自的master信息。
在ttt上执行如下语句
change master to master_host='10.168.1.222',master_user='usertest',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=525;
也就是说,让ttt的master指向了zzz的相关配置
在zzz上执行如下语句
change master to master_host='10.168.1.111',master_user='usertest',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=106;
也就是说,让ttt的master指向了zzz的相关配置
然后,在两台服务器上都执行statr slave
然后,在两台服务器上都执行show slave status \G
如果在两个mysql都看到两个yes,则表明成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
6.6 测试同步效果
6.6.1 同步表
在zzz下执行
use testdb;
create table t1(
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
);
此时,在ttt的testdb下也出现了t1表,表明同步成功。
6.6.2 同步记录
在zzz下插入记录
insert into t1 (title) values('tom');
此时,去ttt检查数据库,能看到这个id为2的记录。
然后,再去ttt插入记录
insert into t1(title) values('sam');
然后就在zzz的t1表也出现了id为3的记录。
这就表明记录是可以同步了。
至此,主主同步设置成功。
6.7 vip的漂移问题
现在有了两个mysql实例,两个ip地址。对外,只能有一个ip地址,这个ip地址上绑定了keepalived和heartbeat等这样的服务,当一台mysql宕机是能自动切换到另一台。
现在的问题是,阿里云的内网地址是固定的,不能随意在一个机器上再加一个虚拟的内网ip。
如果是自己的私有网络,这是没问题的,因为内网ip地址随便设置,但对阿里云来说,不能随便设置内网ip地址,可能会有冲突。
关于这一块的文档有
https://bbs.aliyun.com/read/300778.html
https://yq.aliyun.com/articles/7596?spm=5176.bbsr300778.0.0.DfpiJ7
这里都提到了需要使用私网高可用虚拟IP(Private High-Availability Virtual IP Address,简称HaVip)。
必需开通专有网络,才有havip,文档在此
https://yq.aliyun.com/articles/7603
开通专有网路了
https://help.aliyun.com/document_detail/27710.html
控制台
这个文档描述如何在阿里云用专有网络实现havip这个东西
http://www.cnhejia.cn/aliyun-vpc-keepalived-ha/
http://www.linuxidc.com/Linux/2015-06/118767.htm
http://blog.csdn.net/mchdba/article/details/51377989
http://blog.sina.com.cn/s/blog_4f9fc6e10102w6xy.html
推荐 http://www.cnblogs.com/lyhabc/p/3838764.html
2.
可靠的数据:数据不能丢失
可用的服务:服务器不能宕机
可用性的技术方案和可用性概率:
要达到99.9%:使用MYSQL复制技术
要达到99.99%:使用MYSQL NDB 集群和虚拟化技术
要达到99.999%:使用shared-nothing架构的GEO-REPLICATION和NDB集群技术
一年计不同可用性允许宕机时间:
90% 36天
99% 3.65天
99.9% 8.76小时
99.99% 52分钟
99.999% 5.26分钟
99.9999% 31.5秒
3. 各种高可用方案
3.1 mysql主从复制 单活
3.2 双主 单活
3.3 双主 配 SAN存储 单活
3.4 DRBD双主配DRBD 单活
3.5 NDB cluster
3.6 第三方Tungsten软件
3.7 网易INNOSQL
4. MHA: Master High Availability Manager and Tools for MySQL
Perl语言写的脚本管理工具,仅适用MySQL Replicaiton二层环境,维持master高可用。
至少需要三台机器来做。
http://www.2cto.com/database/201407/319748.html
5. 另一组解决方案
http://bbs.chinaunix.net/thread-3769165-1-1.html
5.1 lvs+keepalived+mysql 单点写入主主同步
5.2 lvs+keepalived+mysql 单点写入读负载均衡主主同步
5.3 heartbeat高可用主主同步
5.4 heartbeat+drbd+mysql
5.5 mmm方案
6. 实战5.1的解决方案
http://www.111cn.net/database/mysql/65579.htm
6.1 服务器配置
mysql1: ttt, 10.168.1.111
mysql2: zzz, 10.168.1.222
用户名和密码 root:tiigggg
6.2 在两个数据都创建测试用户和测试库
create user 'usertest'@'%' identified by '123456';
create database testdb;
grant all privileges on testdb.* to 'usertest'@'%';
这帐号只能远程登录。
create user 'usertest'@'localhost' identified by '123456';
grant all privileges on testdb.* to 'usertest'@'localhost';
这帐号只能本地登录。
需要创建内网ip登录账户
在ttt上执行
create user 'usertest'@'10.168.1.222' identified by '123456';
grant all privileges on testdb.* to 'usertest'@'10.168.1.222';
grant replication slave on *.* to 'usertest'@'10.168.1.222' identified by '123456';
flush privileges;
在zzz上执行
create user 'usertest'@'10.168.1.111' identified by '123456';
grant all privileges on testdb.* to 'usertest'@'10.168.1.111';
grant replication slave on *.* to 'usertest'@'10.168.1.111' identified by '123456';
flush privileges;
6.3 修改ttt的mysql配置/etc/my.cnf的[mysqld]的配置如下
#主主同步配置
log-bin=mysql-bin
server-id=1
expire-logs-days=100
replicate-do-db=testdb
binlog-ignore-db=mysql
binlog-ignore-db=infomation_schema
auto-increment-increment=2
auto-increment-offset=1
6.4 修改zzz的mysql配置/etc/my.cnf的[mysqld]的配置如下
#主主同步配置
log-bin=mysql-bin
server-id=2
expire-logs-days=100
replicate-do-db=testdb
binlog-ignore-db=mysql
binlog-ignore-db=infomation_schema
auto-increment-increment=2
auto-increment-offset=2
二都只有server-id不同和 auto-increment- offset不同
auto-increment-offset是用来设定数据库中自动增长的起点的,这两服务器都设定了一次自动增长值2,所以它们的起点必须得不同,这样才能避免两台服务器数据同步时出现主键冲突
replicate-do-db 指定同步的数据库,我们只在两台服务器间同步usertest库
另:auto-increment-increment的值应设为整个结构中服务器的总数,本案例用到两台服务器,所以值设为2
6.5 重启这这两个数据库
service mysqld restart
6.5 互告bin-log信息
在zzz上执行 show master status
在ttt上执行 show master status
能看到各自的master信息。
在ttt上执行如下语句
change master to master_host='10.168.1.222',master_user='usertest',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=525;
也就是说,让ttt的master指向了zzz的相关配置
在zzz上执行如下语句
change master to master_host='10.168.1.111',master_user='usertest',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=106;
也就是说,让ttt的master指向了zzz的相关配置
然后,在两台服务器上都执行statr slave
然后,在两台服务器上都执行show slave status \G
如果在两个mysql都看到两个yes,则表明成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
6.6 测试同步效果
6.6.1 同步表
在zzz下执行
use testdb;
create table t1(
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
);
此时,在ttt的testdb下也出现了t1表,表明同步成功。
6.6.2 同步记录
在zzz下插入记录
insert into t1 (title) values('tom');
此时,去ttt检查数据库,能看到这个id为2的记录。
然后,再去ttt插入记录
insert into t1(title) values('sam');
然后就在zzz的t1表也出现了id为3的记录。
这就表明记录是可以同步了。
至此,主主同步设置成功。
6.7 vip的漂移问题
现在有了两个mysql实例,两个ip地址。对外,只能有一个ip地址,这个ip地址上绑定了keepalived和heartbeat等这样的服务,当一台mysql宕机是能自动切换到另一台。
现在的问题是,阿里云的内网地址是固定的,不能随意在一个机器上再加一个虚拟的内网ip。
如果是自己的私有网络,这是没问题的,因为内网ip地址随便设置,但对阿里云来说,不能随便设置内网ip地址,可能会有冲突。
关于这一块的文档有
https://bbs.aliyun.com/read/300778.html
https://yq.aliyun.com/articles/7596?spm=5176.bbsr300778.0.0.DfpiJ7
这里都提到了需要使用私网高可用虚拟IP(Private High-Availability Virtual IP Address,简称HaVip)。
必需开通专有网络,才有havip,文档在此
https://yq.aliyun.com/articles/7603
开通专有网路了
https://help.aliyun.com/document_detail/27710.html
控制台
这个文档描述如何在阿里云用专有网络实现havip这个东西
http://www.cnhejia.cn/aliyun-vpc-keepalived-ha/
开通专有网络,搞两台机器搭建mysql,为了给web提供服务,为了速度,也需要把web后端也放在专有网络,总之,一切都放到专有网络解决。
7. 结论
如果自己配,需要lvs+keepalived+主主同步,比较繁琐。
如果不是敏感数据,建议直接买阿里云的rds双机热备。