mysql高可用MHA+Atlas读写分离
Posted rm580036
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql高可用MHA+Atlas读写分离相关的知识,希望对你有一定的参考价值。
1、mysql MHA介绍
MHA ( Master High Availability )目前在MySQL高可用方面是一个相对成熟的解决方案 ,它由日本DeNA公司
youshimaton (现就职于Facebook公司)开发,是-套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用
软件。在MySQL故障切换过程中, MHA能做到在0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过
程中, MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用。
github地址: https://github.com/yoshinorim
**该软件由两部分组成: MHA Manager (管理节点)和MHA Node (数据节点)。**MHA Manager可以单独部署在一台
独立的机器上管理多个master-slave集群,也可以部署在一台slave节点 上。MHA Node运行在每台MySQL服务器上,
MHA Manager会定时探测集群中的master节点,当master出现故障时, 它可以自动将最新数据的slave提升为新的
master ,然后将所有其他的slave重新指向新的master。整个故障转移过程对应用程序完全透明。
在MHA自动故障切换过程中, MHA试图从宕机的主服务器上保存二进制日志 ,最大程度的保证数据的不丢失,但这并
不总是可行的。例如,如果主服务器硬件故障或无法通过ssh访问, MHA没法保存二进制日志 ,只进行故障转移而丢失了
最新的数据。**使用MySQL 5.5的半同步复制,可以大大降低数据丢失的风险。MHA可以与半同步复制结合起来。**如果只
有一个slave已经收到了最新的二进制日志, MHA可以将最新的二进制日志应用于其他所有的slave服务器.上,因此可以保
证所有节点的数据一致性。
目前MHA主要支持一主多从的架构,**要搭建MHA,要求一个复制集群中必须最少有三 台数据库服务器,**一主二从, 即一
台充当master ,一台充当备用master ,另外一台充当从库,因为至少需要三台服务器,出于机器成本的考虑,淘宝也在
该基础上进行了改造,目前淘宝TMHA已经支持一注- -从。(出自 :《深入浅出MySQL(第二版)》 )
官方介绍: https://code.google.com/p/mysql-master-ha/
下图展示了如何通过MHA Manager管理多组主从复制。
可以将MHA工作原理总结为如下:
(1 )从宕机崩溃的master保存:进制日志事件( binlog events );
(2)识别含有最新更新的slave ;
(3)应用差异的中继日志( relay log )到其他的slave ;
(4)应用从master保存的进制日志事件( binlog events) ;
(5)提升- -个slave为新的master ;
(6)使其他的slave连接新的masteri进行复制;
MHA软件由两部分组成, Manager工具包和Node工具包,具体的说明如下
Manager工具包主要包括以下几个工具:
masterha_ .check. ssh #检查MHA的SSH配置状况 masterha .check. rep1 #检查MySQL复制状况 masterha manger #启动MHA masterha_ .check_ status #检测当前MHA运行状态 masterha master. .monitor #检测master是否宕机 masterha master- switch #控制故障转移(自动或者手动) masterha .conf_ host #添加或删余配置的server信息
Node工具包(这些工具通常由MHA Manager的脚本触发,无需人为操作)主要包括以下几个工具: .
save_ binary. _logs #保存和复制master的二进制日志 apply- diff. relay_logs #识别差异的中继日志事件并将其差异的事件应用于其他的slave filter_ mysqlbinlog #去除不必要的ROLLBACK事件(MHA已不再使用这个工具) purge. relay- .1ogs #清除中继日志(不会阻塞SQL线程)
2、安装部署
2.1、准备环境
主机名 | 内网ip | 外网ip | 角色 |
c81 | 10.0.0.81 | 172.16.1.81 | master |
c82 | 10.0.0.82 | 172.16.1.82 | slave(备用master) |
c83 | 10.0.0.83 | 172.16.1.83 | slave |
c84 | 10.0.0.84 | 172.16.1.84 | mha+atlas |
10.0.0.89 | VIP |
1、同步时间,四台都做 echo "*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1">>/var/spool/cron/root 2、关闭防火墙和selinux,四台都做 systemctl stop firewalld.service #临时关闭 systemctl disable firewalld.service #永久关闭 setenforce 0 #临时关闭 sed -i ‘/^SELINUX/s#enforcing#disabled#g‘ /etc/selinux/config #永久关闭 3、hosts解析,四台都做 cat >>/etc/hosts<<EOF 10.0.0.81 c81 10.0.0.82 c82 10.0.0.83 c83 10.0.0.84 c84 EOF 4、ssh免密登录,四台都做 #!/bin/bash yum -y install sshpass &> /dev/null read -p "请输入服务器密码:" passwd UserName=root IP="10.0.0." #创建密钥 ssh-keygen -t dsa -f ~/.ssh/id_dsa -P "" &>/dev/null #分发公钥 for i in 81 82 83 84 #这里的要改成自己机子的ip do sshpass -p "$passwd" ssh-copy-id -i ~/.ssh/id_dsa.pub -p 22 -o StrictHostKeyChecking=no $UserName@$IP$i &>/dev/null done
2.2、配置mysql主从
2.2.1、安装yum repo
前三台装 wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-community-release-el7-5.noarch.rpm yum install mysql-server mysql -y 第四台装 wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-community-release-el7-5.noarch.rpm yum install mysql -y
2.2.2、启动mysql
前三台执行以下命令 1、启动mysql systemctl start mysql 2、修改密码 update mysql.user set password=password(‘123456‘) where user=‘root‘ and host=‘localhost‘; flush privileges; 3、配置主从 c81,master操作 server-id=1 log-bin=mysql-bin #禁止mysql自动删除relaylog工能 relay_log_purge = 0 #mysql5.6已上的特性,开启gtid,必须主从全开 gtid_mode = on enforce_gtid_consistency = 1 log_slave_updates = 1 c82,slave(备用master)操作 server-id=2 log-bin=mysql-bin #禁止mysql自动删除relaylog工能 relay_log_purge = 0 #mysql5.6已上的特性,开启gtid,必须主从全开 gtid_mode = on enforce_gtid_consistency = 1 log_slave_updates = 1 c83,slave操作 server-id=3 log-bin=mysql-bin #禁止mysql自动删除relaylog工能 relay_log_purge = 0 #mysql5.6已上的特性,开启gtid,必须主从全开 gtid_mode = on enforce_gtid_consistency = 1 log_slave_updates = 1 重启mysql systemctl restart mysql 创建同步用户,三台都操作 grant replication slave on *.* to ‘rep‘@‘10.0.0.%‘ identified by ‘123456‘; flush privileges; 在c81,master操作 [root@c81 ~]# mysql -p123456 1、查看mysql主库的master状态 mysql> show master statusG *************************** 1. row *************************** File: mysql-bin.000001 Position: 530 mysql> show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql-bin.000001 | 530 | | | e9a0b767-9c25-11ea-a267-000c29913c03:1-2 | +------------------+----------+--------------+------------------+------------------------------------------+ 2、查看GTID状态 mysql> show global variables like ‘%gtid%‘; +---------------------------------+------------------------------------------+ | Variable_name | Value | +---------------------------------+------------------------------------------+ | binlog_gtid_simple_recovery | OFF | | enforce_gtid_consistency | ON | | gtid_executed | e9a0b767-9c25-11ea-a267-000c29913c03:1-2 | | gtid_mode | ON | | gtid_owned | | | gtid_purged | | | simplified_binlog_gtid_recovery | OFF | +---------------------------------+------------------------------------------+ c82,slave(备用master)操作 [root@c82 ~]# mysql -p123456 1、关闭从服务器的复制功能 stop slave; 2、配置从服务器指向master change master to master_host=‘10.0.0.81‘,master_user=‘rep‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000001‘,master_log_pos==530; 3、开启从服务器的复制功能 start slave; 4、查看从服务器的复制功能状态 mysql> show slave statusG *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.81 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 530 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 314 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes c83,slave操作 [root@c83 ~]# mysql -p123456 1、关闭从服务器的复制功能 stop slave; 2、配置从服务器指向master change master to master_host=‘10.0.0.81‘,master_user=‘rep‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000001‘,master_log_pos==530; 3、开启从服务器的复制功能 start slave; 4、查看从服务器的复制功能状态 mysql> show slave statusG *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.81 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 530 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 314 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
2.3、安装MHA
2.3.1、安装依赖
四台机器都操作 yum install perl-DBD-MySQL -y yum install -y perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
2.3.2、授权MHA管理用户
前三台操作 grant all privileges on *.* to mha@‘10.0.0.%‘ identified by ‘mha‘; flush privileges;
2.3.3、安装MHA node节点
四台机器都操作 rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
2.3.4、安装MHA 管理节点
注意:MHA管理节点不要装到mysql主库和切换的从库上(备用的主库),否则会在后面的vip无法漂移 [root@ c84 ~]# rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
2.3.5、配置MHA
[root@ c84 ~]# mkdir -p /etc/mha [root@ c84 ~]# mkdir -p /var/log/mha/app1 [root@ c84 ~]# vim /etc/mha/app1.cnf [server default] manager_log=/var/log/mha/app1/manager.log manager_workdir=/var/log/mha/app1 master_binlog_dir=/var/lib/mysql #binlog目录,如果说mysql的环境不一样,binlog位置不同,每台服务器的binlog的位置在server标签里面即可 user=mha password=mha ping_interval=2 repl_password=123456 repl_user=rep ssh_user=root [server1] hostname=10.0.0.81 port=3306 [server2] hostname=10.0.0.82 port=3306 [server3] hostname=10.0.0.83 port=3306 ignore_fail=1 #如果这个节点挂了,mha将不可用,加上这个参数,slave挂了一样可以用 no_master=1 #从将这台主机转换为master #candidate_master=1 #如果候选master有延迟的话,relay日志超过100m,failover切换不能成功,加上此参数后会忽略延迟日志大小。 #check_repl_delay=0 #用防止master故障时,切换slave时有延迟,卡在那切不过来 注意以上配置文件,添加时不能有空格和注释
2.3.6、启动测试
ssh检测
[root@ c84 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf
主从复制检测
[root@ c84 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf
报错:
需要在前三台的mysql配置文件中添加:
skip-name-resolve
#当然这个也可以直接在刚开始就添加进去
添加完需要重启mysql,重新配置主从
1、重启mysql systemctl restart mysql 2、配置主从 2.1、master端 [root@81 ~]# mysql -p123456 mysql> show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql-bin.000004 | 191 | | | ac342a62-9b2e-11ea-9c1b-000c29913c03:1-7 | +------------------+----------+--------------+------------------+------------------------------------------+ 2.2、slave(备用master)端 [root@c82 ~]# mysql -p123456 mysql> stop slave; mysql> change master to master_host=‘10.0.0.81‘,master_user=‘rep‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000004‘,master_log_pos=191; mysql> start slave; mysql> show slave statusG *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.81 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 191 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 314 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes 2.3、slave端 [root@83 ~]# mysql -p123456 mysql> stop slave; mysql> change master to master_host=‘10.0.0.81‘,master_user=‘rep‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000004‘,master_log_pos=191; mysql> start slave; mysql> show slave statusG *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.81 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 191 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 314 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes
再次主从复制检测
[root@ c84 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf
成功
2.3.7、启动MHA
[root@ c84 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
查看MHA状态
[root@ c84 ~]# masterha_check_status --conf=/etc/mha/app1.cnf app1 (pid:18420) is running(0:PING_OK), master:10.0.0.81
2.3.8、测试MHA故障转移
1、停掉主c81主10.0.0.81 [root@ c81 ~]# systemctl stop mysql 2、查看c83的slave状态,发现master变成10.0.0.82 [root@ c83 ~]# mysql -uroot -p123456 -e "show slave statusG"
3、查看c82的master状态 [root@ c82 ~]# mysql -uroot -p123456 -e "show master status;"
4、查看c84的MHA配置文件
发生故障时,MHA做了什么?
①当作为主库的c7m01上的MySQL宕机以后,mha通过检测发现c7m01的mysql宕机了,那么会将binlog日志最全的从库(c702)立刻提升为主库,而其他的从库会指向新的主库进行再次同步。
②MHA会自己结束自己的进程,还会将/etc/mha/app1.cnf配置文件中,坏掉的那台机器剔除。
2.3.9、MHA故障还原
先将宕机mysql修复,然后加到mysql一主两从集群 1、[root@ c84 ~]# grep -i "CHANGE MASTER TO MASTER" /var/log/mha/app1/manager.log | tail -1 Fri May 22 21:07:21 2020 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST=‘10.0.0.82‘, MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER=‘rep‘, MASTER_PASSWORD=‘xxx‘; 2、启动c81的mysql [root@ c81 ~]# systemctl start mysql 3、执行命令 [root@ c81 ~]# mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_HOST=‘10.0.0.82‘, MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER=‘rep‘, MASTER_PASSWORD=‘123456‘;" 4、开启主从复制功能 [root@ c81 ~]# mysql -uroot -p123456 -e ‘start slave;‘ 5、查看主从复制状态 [root@ c81 ~]# mysql -uroot -p123456 -e ‘show slave statusG‘ 重新在将[server1]标签添加到MHA配置文件,并启动MHA
1、添加配置文件 [root@ c84 ~]# vim /etc/mha/app1.cnf [server1] hostname=10.0.0.81 port=3306 2、启动MHA [root@ c84 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
2.4、配置VIP漂移
2.4.1、VIP漂移的两种方式
①通过keepalived的方式,管理虚拟IP的漂移。
②通过MHA自带脚本的方式,管理虚拟IP的漂移#用mha自带的一 -个VIP漂移的脚本,哪个提升为主,就飘到那个上
面,要根据binlog最新的slave提升。
2.4.2、MHA脚本方式
修改配置文件 [root@ c84 ~]# vim /etc/mha/app1.cnf master_ip_failover_script=/usr/bin/master_ip_failover
编写漂移脚本master_ip_failover
注意:修改脚本中的网卡名和ip地址
[root@ c84 ~]# vim /usr/bin/master_ip_failover #!/usr/bin/env perl use strict; use warnings FATAL => ‘all‘; #!/usr/bin/env perl use strict; #!/usr/bin/env perl use strict; use warnings FATAL => ‘all‘; use Getopt::Long; my ( use strict; use warnings FATAL => ‘all‘; use strict; use warnings FATAL => ‘all‘; use Getopt::Long; my ( use Getopt::Long; use Getopt::Long; my ( $command, $ssh_user, $orig_master_host, $orig_master_ip, my ( $command, $ssh_user, $orig_master_host, $orig_master_ip, my ( my ( my ( $command, $ssh_user, $orig_master_host, $ori $command, $ssh_user, $orig_master_host, $ $command, $ssh_user, $orig_master_host, $orig_master_ip, $orig_master_port, $new_master_host, $new_master_ip, $new_master_port $orig_master_port, $new_master_host, $new_master_ip, $new_master_port ); ); my $vip = ‘10.0.0.89/24‘; #!/usr/bin/env perl use strict; use warnings FATAL => ‘all‘; use Getopt::Long; my ( $command, $ssh_user, $orig_master_host, $orig_master_ip, $orig_master_port, $new_master_host, $new_master_ip, $new_master_port ); my $vip = ‘10.0.0.89/24‘; my $key = ‘1‘; my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; GetOptions( ‘command=s‘ => $command, ‘ssh_user=s‘ => $ssh_user, ‘orig_master_host=s‘ => $orig_master_host, ‘orig_master_ip=s‘ => $orig_master_ip, ‘orig_master_port=i‘ => $orig_master_port, ‘new_master_host=s‘ => $new_master_host, ‘new_master_ip=s‘ => $new_master_ip, ‘new_master_port=i‘ => $new_master_port, exit &main(); sub main { print " IN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip=== "; if ( $command eq "stop" || $command eq "stopssh" ) { my $exit_code = 1; eval { print "Disabling the VIP on old master: $orig_master_host "; &stop_vip(); $exit_code = 0; }; if ($@) { warn "Got Error: $@ "; exit $exit_code; } exit $exit_code; } elsif ( $command eq "start" ) { my $exit_code = 10; eval { print "Enabling the VIP - $vip on the new master - $new_master_host "; &start_vip(); $exit_code = 0; }; if ($@) { warn $@; exit $exit_code; } exit $exit_code; } elsif ( $command eq "status" ) { print "Checking the Status of the script.. OK "; exit 0; } else { &usage(); exit 1; } } sub start_vip() { `ssh $ssh_user@$new_master_host " $ssh_start_vip "`; } sub stop_vip() { return 0 unless ($ssh_user); `ssh $ssh_user@$orig_master_host " $ssh_stop_vip "`; } sub usage { print "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port "; }
给一个执行权限
[root@ c84 ~]# chmod +x /usr/bin/master_ip_failover
2.4.3、手动绑定VIP,在主库主机绑定
[root@ c82 ~]# ifconfig ens33:1 10.0.0.89 [root@ c82 ~]# ip addr show ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:23:1d:38 brd ff:ff:ff:ff:ff:ff inet 10.0.0.82/24 brd 10.0.0.255 scope global ens33 valid_lft forever preferred_lft forever inet 10.0.0.89/8 brd 10.255.255.255 scope global ens33:1 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe23:1d38/64 scope link valid_lft forever preferred_lft forever
2.4.4、重启MHA
[root@ c84 ~]# masterha_stop --conf=/etc/mha/app1.cnf [root@ c84 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
2.4.5、查看MHA日志
[root@ c84 ~]# tail -f /var/log/mha/app1/manager.log
2.6、模拟主库宕机VIP漂移
[root@ c82 ~]# ifconfig ens33:1 10.0.0.89 [root@ c82 ~]# ip addr show ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:23:1d:38 brd ff:ff:ff:ff:ff:ff inet 10.0.0.82/24 brd 10.0.0.255 scope global ens33 valid_lft forever preferred_lft forever inet 10.0.0.89/8 brd 10.255.255.255 scope global ens33:1 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe23:1d38/64 scope link valid_lft forever preferred_lft forever 关闭c82的mysql [root@ c82 ~]# systemctl stop mysql 注意可能会有几秒延迟 [root@ c82 ~]# ip addr show ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:23:1d:38 brd ff:ff:ff:ff:ff:ff inet 10.0.0.82/24 brd 10.0.0.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe23:1d38/64 scope link valid_lft forever preferred_lft forever
查看c81上mysql的master状态和VIP
1、VIP [root@ c81 ~]# ip addr show ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:91:3c:03 brd ff:ff:ff:ff:ff:ff inet 10.0.0.81/24 brd 10.0.0.255 scope global ens33 valid_lft forever preferred_lft forever inet 10.0.0.89/24 brd 10.0.0.255 scope global secondary ens33:1 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe91:3c03/64 scope link valid_lft forever preferred_lft forever 2、master状态 [root@ c81 ~]# mysql -uroot -p123456 -e ‘show master statusG‘ Warning: Using a password on the command line interface can be insecure. *************************** 1. row *************************** File: mysql-bin.000002 Position: 946 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: e9a0b767-9c25-11ea-a267-000c29913c03:1-4, e9dc963a-9c25-11ea-a267-000c29231d38:1-4
3、Mysql之Atlas(读写分离)
数据库中间件Atlas与Mycat比较分库分表压测报告https://blog.csdn.net/izhitao/article/details/71680714
Atlas是由Qihoo 360公司Web平台部基础架构团队开发维护的一-个基于MySQL协议的数据中间层项目。它在MySQL言
方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量bug ,添加了很多功能特性。目前该项目在360公司内部得到了广
泛应用,很多MySQL业务已经接入了Atlas平台,每天承载的读写请求数达几十亿条。
源码Github : https://github.com/Qihoo360/Atlas
3.1、什么是读写分离
读写分离,基本的原理是让主数据库处理事务性增、删、改操作( INSERT. DELETE、 UPDATE ) , 而从数据库处理
SELECT查询操作。数据库复制把主数据库操作的变更同步到集群中的从数据库。
3.2、为什么读写分离.
因为数据库的“写" (写10000条数据到oracle可能要3分钟)操作是比较耗时的。但是数据库的“读" (从oracle读10000
条数据可能只要5秒钟)。所以读写分离,解决的是,数据库的写入,影响了查询的效率。
3.3、什么时候要读写分离
数据库不一定要读写分离,如果程序使用数据库较多时,而更新少,查询多的情况下会考虑使用,利用数据库主从同步
可以减少数据库压力,提高性能。当然,数据库也有其它优化方案。memcache 或是表折分,或是搜索引学。都是解
决方法。
3.4、Atlas的功用与应用场景
Atlas的功能有:
读写分离、从库负载均衡、自动分表、IP过滤、 SQL语句黑白名单、DBA可平滑上下线DB、自动摘除宕机的DB。
Atlas的使用场景:
Atlas是一个位于前端 应用与后端MySQL数据库之间的中间件,它使得应用程序员无需再关心读写分离、分表等与
MySQL相关的细节,可以专注于编写业务逻辑,同时使得DBA的运维工作对前端应用透明,上下线DB前端应用无感知。
3.5、 Atlas的安装过程
注意:
1、Atlas只能安装运行在64位的系统上
2、Centos 5.X安装Atlas-XX.el5.x86_ _64.rpm , Centos 6.X安装Atlas-XX.el6.x86 _64.rpm(经过测试centos7也可以使用6
的版本)
3、后端mysq|版本应大于5.1 ,建议使用Mysql 5.6以上
1、安装altas [root@ c84 ~]# wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm [root@ c84 ~]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm 2、修改配置文件 [root@ c84 ~]# cp /usr/local/mysql-proxy/conf/test.cnf{,.bak} #备份 [root@ c84 ~]# vim /usr/local/mysql-proxy/conf/test.cnf 全删,加上以下内容 [mysql-proxy] admin-username = user admin-password = pwd proxy-backend-addresses = 10.0.0.89:3306 # 设置主库VIP的地址 proxy-read-only-backend-addresses = 10.0.0.82:3306,10.0.0.83:3306 #设置只读的从库地址 pwds = rep:/iZxz+0GRoA=,mha:O2jBXONX098= #设置数据库管理用户,加密方法:/usr/local/mysql-proxy/bin/encrypt 密码 daemon = true keepalive = true event-threads = 8 log-level = message log-path = /usr/local/mysql-proxy/log sql-log =ON proxy-address = 0.0.0.0:1234 admin-address = 0.0.0.0:2345 charset = utf8 3、启动altas #altas可以通过不同配置文件,同时启动多个Atlas代理多套MHA节点; /usr/local/mysql-proxy/bin/mysql-proxyd test start #启动 /usr/local/mysql-proxy/bin/mysql-proxyd test stop #停止 /usr/local/mysql-proxy/bin/mysql-proxyd test restart #重启
启动成功
[root@ c84 ~]# /usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started
4、查看altas ps -ef | grep mysql-proxy
3.6、Atlas读写分离
1、读测试 [root@ c84 ~]# mysql -umha -pmha -P1234 -h10.0.0.84 mysql> select @@server_id; +-------------+ | @@server_id | +-------------+ | 3 | +-------------+ 1 row in set (0.01 sec) mysql> select @@server_id; +-------------+ | @@server_id | +-------------+ | 2 | +-------------+ 1 row in set (0.01 sec) 2、写测试 [root@ c84 ~]# mysql -umha -pmha -P1234 -h10.0.0.84 mysql> begin;select @@server_id;commit; Query OK, 0 rows affected (0.01 sec) +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> create database qq; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | qq | +--------------------+ 4 rows in set (0.01 sec)
3.7、Atlas管理操作
1、登录管理接口 [root@ c84 ~]# mysql -uuser -ppwd -P2345 -h10.0.0.84 2、查看帮助信息 mysql> select * from help; +----------------------------+---------------------------------------------------------+ | command | description | +----------------------------+---------------------------------------------------------+ | SELECT * FROM help | shows this help | | SELECT * FROM backends | lists the backends and their state | | SET OFFLINE $backend_id | offline backend server, $backend_id is backend_ndx‘s id | | SET ONLINE $backend_id | online backend server, ... | | ADD MASTER $backend | example: "add master 127.0.0.1:3306", ... | | ADD SLAVE $backend | example: "add slave 127.0.0.1:3306", ... | | REMOVE BACKEND $backend_id | example: "remove backend 1", ... | | SELECT * FROM clients | lists the clients | | ADD CLIENT $client | example: "add client 192.168.1.2", ... | | REMOVE CLIENT $client | example: "remove client 192.168.1.2", ... | | SELECT * FROM pwds | lists the pwds | | ADD PWD $pwd | example: "add pwd user:raw_password", ... | | ADD ENPWD $pwd | example: "add enpwd user:encrypted_password", ... | | REMOVE PWD $pwd | example: "remove pwd user", ... | | SAVE CONFIG | save the backends to config file | | SELECT VERSION | display the version of Atlas | +----------------------------+---------------------------------------------------------+ 16 rows in set (0.00 sec) 3、查看后端的代理库 mysql> SELECT * FROM backends; +-------------+----------------+-------+------+ | backend_ndx | address | state | type | +-------------+----------------+-------+------+ | 1 | 10.0.0.89:3306 | up | rw | | 2 | 10.0.0.82:3306 | up | ro | | 3 | 10.0.0.83:3306 | up | ro | +-------------+----------------+-------+------+ 3 rows in set (0.00 sec) 4、下线后端节点 mysql> SET OFFLINE 3; +-------------+----------------+---------+------+ | backend_ndx | address | state | type | +-------------+----------------+---------+------+ | 3 | 10.0.0.83:3306 | offline | ro | +-------------+----------------+---------+------+ 1 row in set (0.00 sec) mysql> SELECT * FROM backends; +-------------+----------------+---------+------+ | backend_ndx | address | state | type | +-------------+----------------+---------+------+ | 1 | 10.0.0.89:3306 | up | rw | | 2 | 10.0.0.82:3306 | up | ro | | 3 | 10.0.0.83:3306 | offline | ro | +-------------+----------------+---------+------+ 3 rows in set (0.00 sec) 5、上线后端节点 mysql> SET ONLINE 3; +-------------+----------------+---------+------+ | backend_ndx | address | state | type | +-------------+----------------+---------+------+ | 3 | 10.0.0.83:3306 | unknown | ro | +-------------+----------------+---------+------+ 1 row in set (0.00 sec) mysql> SELECT * FROM backends; +-------------+----------------+-------+------+ | backend_ndx | address | state | type | +-------------+----------------+-------+------+ | 1 | 10.0.0.89:3306 | up | rw | | 2 | 10.0.0.82:3306 | up | ro | | 3 | 10.0.0.83:3306 | up | ro | +-------------+----------------+-------+------+ 3 rows in set (0.00 sec)
以上是关于mysql高可用MHA+Atlas读写分离的主要内容,如果未能解决你的问题,请参考以下文章
专职DBA-基于MHA高可用搭建MySQL读写分离架构-Atlas
mysql+MHA高可用+atlas读写分离(成产环境运用场景)