MYSQL AAB架构
近日做了一次MYSQL 主主从架构的实验。功能实现:用三台服务器(master1,master2,slave)搭建主主从服务器。master1和master2主主复制,并且互为主备。当slave服务器在复制过程中,VIP所在的主服务器的MYSQL、网络或者系统等出现故障,导致从服务器slave无法从该服务器复制时,系统自动把从服务器指向的主的IP转移到另外一台服务器上。从而实现了故障转移。本次实验,使用了mysql5.6后的新功能gtid保持主主复制的一致性。
一:实验环境
IP
mysql
keepalived
192.168.1.197
mysql主
master
192.168.1.198
mysql主
backup
192.168.1.199
slave
192.168.1.50
系统
Centos63 64位
MYSQL软件版本
mysql-5.7.4-m14-linux-glibc2.5-x86_64.tar.gz
keepalived软件版本
keepalived-1.2.6.tar.gz
二:系统架构
三:原理
1.Keepalived原理 :
keepalived也是模块化设计,不同模块复杂不同的功能,下面是keepalived的组件
core check vrrp libipfwc libipvs-2.4 libipvs-2.6
core:是keepalived的核心,复杂主进程的启动和维护,全局配置文件的加载解析等
check:负责healthchecker(健康检查),包括了各种健康检查方式,以及对应的配置的解析包括LVS的配置解析
vrrp:VRRPD子进程,VRRPD子进程就是来实现VRRP协议的
libipfwc:iptables(ipchains)库,配置LVS会用到
libipvs*:配置LVS会用到
注意,keepalived和LVS完全是两码事,只不过他们各负其责相互配合而已
keepalived启动后会有三个进程
父进程:内存管理,子进程管理等等
子进程:VRRP子进程
子进程:healthchecker子进程
有图可知,两个子进程都被系统WatchDog看管,两个子进程各自复杂自己的事,healthchecker子进程复杂检查各自服务器的健康程度,例如HTTP,LVS等等,如果healthchecker子进程检查到MASTER上服务不可用了,就会通知本机上的兄弟VRRP子进程,让他删除通告,并且去掉虚拟IP,转换为BACKUP状态.
2.replication原理
从高层来看,复制分成三步:
(1) master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events);
(2) slave将master的binary log events拷贝到它的中继日志(relay log);
(3) slave重做中继日志中的事件,将改变反映它自己的数据。
下图描述了这一过程:
该过程的第一部分就是master记录二进制日志。在每个事务更新数据完成之前,master在二日志记录这些改变。MySQL将事务串行的写入二进制日志,即使事务中的语句都是交叉执行的。在事件写入二进制日志完成后,master通知存储引擎提交事务。
下一步就是slave将master的binary log拷贝到它自己的中继日志。首先,slave开始一个工作线程——I/O线程。I/O线程在master上打开一个普通的连接,然后开始binlog dump process。Binlog dump process从master的二进制日志中读取事件,如果已经跟上master,它会睡眠并等待master产生新的事件。I/O线程将这些事件写入中继日志。
SQL slave thread处理该过程的最后一步。SQL线程从中继日志读取事件,更新slave的数据,使其与master中的数据一致。只要该线程与I/O线程保持一致,中继日志通常会位于OS的缓存中,所以中继日志的开销很小。
此外,在master中也有一个工作线程:和其它MySQL的连接一样,slave在master中打开一个连接也会使得master开始一个线程。复制过程有一个很重要的限制——复制在slave上是串行化的,也就是说master上的并行更新操作不能在slave上并行操作。
四:安装
分别在三台服务器上安装mysql
(一)、安装mysql
1.创建mysql账户和组groupadd mysqluseradd -r -g mysql mysql2.在这里我使用的是编译过的安装包,只需要解压就行,不再需要重新编译
tar zxf mysql-5.7.4-m14-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
3.改名
mv mysql-5.7.4-m14-linux-glibc2.5-x86_64 mysql
4.初始化
./usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql
5.匿名登录修改密码
./usr/local/mysql/bin/mysqld_safe --skip-grant-tables &mysqlupdate mysql.user set password=password(‘sa‘) ;flush privileges;exit
6.停止mysql 服务
pkill mysql
7.复制MYSQL文件到指定位置
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
8.配置MYSQL为自启动
chkconfig --add mysqld
chkconfig mysqld on
9.启动MYSQL
service mysqld start
10.为MYSQL添加复制账户
grant replication all on *.* to ‘replicate‘@‘192.168.1.197‘ identified by ‘replicate‘;
flush privileges;
11.重置MYSQL的master和slave
reset master;
stop slave;
reset slave;
12.设置MYSQL的gtid值
set global gtid_purged=‘82ee3730-a135-11e4-8b94-b4b52fbacca8:1-2000‘;
13.修改MYSQL的复制对象
在198上
change master to master_host=‘192.168.1.197‘,master_user=‘replicate‘,master_password=‘replicate‘,master_auto_position= 1;
在197上
change master to master_host=‘192.168.1.198‘,master_user=‘replicate‘,master_password=‘replicate‘,master_auto_position= 1;
在199上
change master to master_host=‘192.168.1.50‘,master_user=‘replicate‘,master_password=‘replicate‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=191;
14.启动slave
start slave;
15.my.cnf 的配置
197:/etc/my.cnf
[mysqld]server-id = 197log_bin = /data/mysql/log_bin/mysql-bin.logdatadir = /data/mysql/databasedir = /usr/local/mysqlreplicate_do_db = testreplicate_ignore_db = mysql,information_schema,performance_schemamax_connections = 2000expire_logs_days = 100max_binlog_size = 1024Mbinlog_format =rowlog-slave-updates=truegtid-mode=onenforce-gtid-consistency=truemaster-info-repository=TABLErelay-log-info-repository=TABLEsync-master-info=1slave-parallel-workers=2binlog-checksum=CRC32master-verify-checksum=1binlog-rows-query-log_events=1sync_binlog=1tmp_table_size = 256Mmax_heap_table_size = 256Mkey_buffer_size = 256Mquery_cache_type=1query-cache-size = 256Mread_rnd_buffer_size = 20Mread_buffer_size=20Mcharacter-set-server = utf8[client]default-character-set = utf8198:/etc/my.cnf
[mysqld]server-id = 198#启用二进制日志,这是保证复制功能的基本前提log_bin = /data/mysql/log_bin/mysql-bin.logdatadir = /data/mysql/databasedir = /usr/local/mysqlmax_connections = 2000replicate_do_db = testreplicate_ignore_db = mysql,information_schema,performance_schemaexpire_logs_days = 100max_binlog_size = 1024Mbinlog_format = row #二进制日志的格式#用于启动GTID及满足附属的其它需求log-slave-updates=truegtid-mode=onenforce-gtid-consistency=true#可用于实现在崩溃时保证二进制及从服务器安全的功能master-info-repository=TABLErelay-log-info-repository=TABLE#启用可确保无信息丢失sync-master-info=1slave-parallel-workers=2 #设定从服务器的SQL线程数;0表示关闭多线程复制功能#启用复制有关的所有校验功能binlog-checksum=CRC32master-verify-checksum=1slave-sql-verify-checksum=1#启用之可用于在二进制日志记录事件相关的信息,可降低故障排除的复杂度binlog-rows-query-log_events=1sync_binlog=1tmp_table_size = 256Mmax_heap_table_size = 256Mkey_buffer_size = 256Mquery_cache_type=1query-cache-size = 256Mread_rnd_buffer_size = 20Mread_buffer_size=20Mcharacter-set-server = utf8[client]default-character-set=utf8199:/etc/my.cnf
[mysqld]server-id = 199#启用二进制日志,这是保证复制功能的基本前提log_bin = /data/mysql/log_bin/mysql-bin.logdatadir = /data/mysql/datamax_connections = 2000#replicate-do-db=super#replicate-do-db=dianxinbinlog_ignore_db = mysqlbinlog_ignore_db = information_schemabinlog_ignore_db = performance_schemareplicate_do_db = testreplicate_ignore_db = mysqlreplicate_ignore_db = information_schemareplicate_ignore_db = performance_schemaexpire_logs_days = 100max_binlog_size = 1024M#二进制日志的格式binlog_format = row#用于启动GTID及满足附属的其它需求log-slave-updates=truegtid-mode=onenforce-gtid-consistency=true#可用于实现在崩溃时保证二进制及从服务器安全的功能master-info-repository=TABLErelay-log-info-repository=TABLE#启用可确保无信息丢失sync-master-info=1#设定从服务器的SQL线程数;0表示关闭多线程复制功能slave-parallel-workers=2#启用复制有关的所有校验功能binlog-checksum=CRC32master-verify-checksum=1slave-sql-verify-checksum=1#启用之可用于在二进制日志记录事件相关的信息,可降低故障排除的复杂度binlog-rows-query-log_events=1sync_binlog=1tmp_table_size = 256Mkey_buffer_size = 256Mquery_cache_type=1query-cache-size = 256Mread_rnd_buffer_size = 20Mread_buffer_size=20Mcharacter-set-server = utf8[client]default-character-set = utf8
(二)、安装keepalived
1. 软件安装
# tar zxf keepalived-1.2.6.tar.gz#cd keepalived-1.2.6
#./configure --prefix=/usr/local/keepalived
#cp keepalived/etc/init.d/keepalived.sysconfig /etc/sysconfig/keepalived
#cp keepalived/etc/init.d/keepalived.rh.init /etc/init.d/keepalived
#chmod +x /etc/init.d/keepalived
#ln /usr/local/keepalived/sbin/keepalived /sbin/keepalived
#chkconfig --add keepalived
#cp -a keepalived/etc/keepalived/ /etc/
#service keepalived start
#service keepalived stop
2.配置文件
master:不使用脚本,直接使用端口检测MYSQL是否在运行
[[email protected] ~]# cat /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {notification_email {[email protected][email protected][email protected]}notification_email_from [email protected]smtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_DEVEL}vrrp_script check_run {script "</dev/tcp/127.0.0.1/3306"interval 1weight 2}vrrp_instance VI_1 {state MASTERinterface eth0virtual_router_id 52priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {check_run}virtual_ipaddress {192.168.1.50/24}}
backup
! Configuration File for keepalived
global_defs {notification_email {[email protected][email protected][email protected]}notification_email_from [email protected]smtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_DEVEL}vrrp_script check_run {script "</dev/tcp/127.0.0.1/3306"interval 1weight 2}vrrp_instance VI_1 {state BACKUPinterface eth0virtual_router_id 52priority 99advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {check_run}virtual_ipaddress {192.168.1.50/24}}
五:测试
1.停止197的MYSQL服务,发现vip漂移至198,199的replication运行正常
2.启动197的MYSQL服务,发现VIP飘移会197,199的replication运行正常