MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司的youshimaton(现就职于Facebook公司)开发,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中,MHA能做到在0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用。
该软件由两部分组成: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(第二版)》)从代码层面看,MHA就是一套Perl脚本,那么相信以阿里系的技术实力,将MHA改成支持一主一从也并非难事。
从宕机崩溃的master保存二进制日志事件(binlog events);
识别含有最新更新的slave;
应用差异的中继日志(relay log)到其他slave;
应用从master保存的二进制日志事件(binlog events);
提升一个slave为新master;
使用其他的slave连接新的master进行复制。
官方介绍:https://code.google.com/archive/p/mysql-master-ha/
1.1基本环境
1.2,同步系统时间
echo "*/5* * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" >>/var/spool/cron/root
1.3,关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
1.4,,做hosts解析
(1)修改主机名
[root@ c41 ~] hostnamectl set-hostname c41 #master
[root@ c42 ~] hostnamectl set-hostname c42 #slave1
[root@ c43 ~] hostnamectl set-hostname c43 #salve2
[root@ c44 ~] hostnamectl set-hostname c44 #mha
(2)添加hosts解析
cat>> /etc/hosts<<EOF
c41 10.0.0.41
c42 10.0.0.42
c43 10.0.0.43
c44 10.0.0.44
EOF
1.5,分别在四台服务器上做免密连接,四台可以互相免密登录
cat >ssh.sh<<\EOF
#!/bin/bash
yum -y install sshpass &> /dev/null
UserName=root
IPlist=(10.0.0.44 10.0.0.41 10.0.0.42 10.0.0.43 )
#创建密钥
ssh-keygen -t dsa -f ~/.ssh/id_dsa -P "" &>/dev/null
#分发公钥
for i in ${IPlist[*]}
do
sshpass -p "123456" ssh-copy-id -i ~/.ssh/id_dsa.pub -p 22 -o StrictHostKeyChecking=no $UserName@$i &>/dev/null
done
EOF
四台服务器都要执行
# sh ssh.sh
2.1,在41-43主机上安装mysql,并启动mysql
#安装mysql的安装yum repo
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
#在centos7上安装mysql,而不是centos7默认的mariadb
yum install mysql-server -y
启动mysql
systemctl start mysql
2.2,登录mysql并修改密码
mysqladmin -uroot password "123456"
2.3,在c41上配置master主库
(1)修改mysql主配置文件
[root@ c41 ~] vim /etc/my.cnf
[mysqld]
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
(2)重启mysql
[root@ c41 ~] systemctl restart mysql
(3)登录mysql,创建授权用户
[root@ c41 ~] mysql -uroot -p123456
mysql> grant replication slave on *.* to 'repl'@'10.0.0.%' identified by '123456';
mysql> flush privileges;
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000001 | 531 | | | 1de8a4a7-9bc8-11ea-a004-000c29dc4316:1-3 |
+------------------+----------+--------------+------------------+------------------------------------------+
(4)查看gtid状态
mysql> show global variables like '%gtid%';
+---------------------------------+------------------------------------------+
| Variable_name | Value |
+---------------------------------+------------------------------------------+
| binlog_gtid_simple_recovery | OFF |
| enforce_gtid_consistency | ON #执行gtid |
| gtid_executed | f740f0a8-9a7f-11ea-97a8-000c29dc4316:1-2 |
| gtid_mode | ON #开启gtid模块 |
| gtid_owned | |
| gtid_purged | |
| simplified_binlog_gtid_recovery | OFF |
+---------------------------------+------------------------------------------+
2.4,在c42上配置Slave1从库(备主库)
(1)修改mysql从配置文件
[root@ c42 ~] vim /etc/my.cnf
[mysqld]
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
(2)重启mysql
[root@ c42 ~] systemctl restart mysql
(3)创建同步用户
[root@ c42 ~] mysql -uroot -p123456
mysql> grant replication slave on *.* to 'repl'@'10.0.0.%' identified by '123456';
mysql> flush privileges;
(4)配置从服务器指向master
mysql> stop slave;
mysql> change master to master_host='10.0.0.41',master_user='repl',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=531;
mysql> start slave;
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.41
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 531
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.5,在c42上配置Slave2从库
(1)修改mysql从配置文件
[root@ c43 ~] vim /etc/my.cnf
[mysqld]
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
(2)重启mysql
[root@ c43 ~] systemctl restart mysql
(3)创建同步用户
[root@ c43 ~] mysql -uroot -p123456
mysql> grant replication slave on *.* to 'repl'@'10.0.0.%' identified by '123456';
mysql> flush privileges;
(4)配置从服务器指向master
mysql> stop slave;
mysql> change master to master_host='10.0.0.41',master_user='repl',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=531;
mysql> start slave;
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.41
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 531
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
3.1,安装依赖
在所有主机上操作
yum -y install perl-DBD-mysql perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
3.2,创建mha管理用户
在c41-c4主从
mysql -uroot -p123456
mysql> grant all privileges on *.* to 'mha'@'10.0.0.%' identified by 'mha';
mysql> flush privileges;
3.3,安装MHA_node节点
在四台主机上安装
#我这里上提前准备好的,上传即可
rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
3.4,安装Manager管理节点
MHA节点安装在c44上,不要安装到master和slave服务器上,否则会出现vip无法漂移发生
[root@ c44 ~] rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
3.5,在manager节点上配置MHA
(1)创建mha目录
[root@ c44 ~] mkdir -p /etc/mha
[root@ c44 ~] mkdir -p /var/log/mha/app1
(2)添加配置文件
[root@ c44 ~] 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
user=mha
password=mha
ping_interval=2
repl_password=123456
repl_user=repl
ssh_user=root
[server1]
hostname=10.0.0.41
port=3306
[server2]
hostname=10.0.0.42
port=3306
[server3]
hostname=10.0.0.43
port=3306
ignore_fail=1
no_master=1
#candidate_master=1
#check_repl_delay=0
server default段是manager的一些基本配置参数,server1、server2、server3分别对应复制中的master、第一个slave、第二个slave。该文件的语法要求严格,变量值后不要有多余的空格。主要配置项说明如下。
manager_log:设置manager的日志文件。
manager_workdir:设置manager的工作目录。
master_binlog_dir:设置master保存binlog的位置,以便MHA可以找到master的日志,这里的也就是mysql的数据目录。
password:设置mysql中root用户的密码。
ping_interval:设置监控主库,发送ping包的时间间隔,默认是3秒,尝试三次没有回应的时候自动进行railover。
repl_password:设置复制用户的密码。
repl_user:设置复制环境中的复制用户名
ssh_user:设置ssh的登录用户名。
user:设置监控用户为root。
candidate_master:设置为候选master。设置该参数以后,发生主从切换以后将会将此从库提升为主库,即使这个主库不是集群中事件最新的slave。
check_repl_delay:默认情况下如果一个slave落后master 100M的relay logs的话,MHA将不会选择该slave作为一个新的master,因为对于这个slave的恢复需要花费很长时间。
通过设置check_repl_delay=0,MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机非常有用,因为这个候选主在切换的过程中一定是新的master。
3.6,MHA启动测试
(1)检查ssh
[root@ c44 ~] masterha_check_ssh --conf=/etc/mha/app1.cnf
[root@ c44 ~] masterha_check_repl --conf=/etc/mha/app1.cnf
解决方法:
(1)在一主二从的数据库中重新同步授权repl用户
mysql> grant replication slave on *.* to 'repl'@'10.0.0.%' identified by '123456';
mysql> flush privileges;
(2)再进行主从复制检测
[root@ c44 ~] masterha_check_repl --conf=/etc/mha/app1.cnf
4.1,后台启动
[root@ c44 ~] 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 &
[1] 20237
4.2,查看MHA状态
[root@ c44 ~] masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:20237) is running(0:PING_OK), master:10.0.0.41
#running--启动状态
4.3,关闭MHA
[root@ c44 ~] masterha_stop --conf=/etc/mha/app1.cnf
Stopped app1 successfully.
[1]+ Exit 1
4.4,从库重新加入新主
[root@ c44 ~] grep -i "CHANGE MASTER TO MASTER" /var/log/mha/app1/manager.log | tail -1
常见报错
会导致MHA故障转移后,主从不一致,一般是后台启动MHA没有正常退出造成的。
注:有终止信号,异常退出。
解决办法
反复多试几次启动和关闭命令
#启动命令
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 &
#停止命令
masterha_stop --conf=/etc/mha/app1.cnf
5.1,宕掉c41上master主库
[root@ c41 ~] systemctl stop mysql
5.2,查看c43的slave状态
会发现Master_Host变成了10.0.0.42
[root@ c7s43 ~] mysql -p123456 -e "show slave status \G"
[root@ c742 ~] mysql -p123456 -e "show master status"
[root@ c44 ~] cat /etc/mha/app1.cnf
发生故障时,MHA做了什么?
1,当作为master的c41主机上的mysql宕机以后,MHA通过检测发现c41的mysql宕机了,就会将binlog日志最全的从库c42主机(备用master)立刻提升为主库,而其他的从库会指向新的主库进行再次同步。
2,MHA自己会结束自己的进程,还会将/etc/mha/app1.cnf配置文件中发生故障的那台主机机去除。
6.1,修复宕机的c41上的mysql,重新加入一主二从集群
(1)在c44mha主机上操作
[root@ c744 ~] grep "CHANGE MASTER TO MASTER" /var/log/mha/app1/manager.log | tail -1
Fri May 22 16:34:55 2020 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='10.0.0.42', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='xxx';
(2)在c41主机上操作
#重启mysql
[root@ c41 ~] systemctl restart mysql
#指定c42为master主库
[root@ c41 ~] mysql -p123456 -e "CHANGE MASTER TO MASTER_HOST='10.0.0.42', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123456';"
Warning: Using a password on the command line interface can be insecure.
#开启主从复制
[root@ c41 ~] mysql -p123456 -e "start slave"
#查看主从状态
[root@ c41 ~] mysql -p123456 -e "show slave status \G"
6.2,重新将server1 标签添加到c44主机MHA的配置文件中
[root@ c44 ~] 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
password=mha
ping_interval=2
repl_password=123456
repl_user=repl
ssh_user=root
user=mha
#手动添加server1模块
[server1]
hostname=10.0.0.41
port=3306
[server2]
hostname=10.0.0.42
port=3306
[server3]
hostname=10.0.0.43
ignore_fail=1
no_master=1
port=3306
6.3,重启MHA
[root@ c44 ~] 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 &
[1] 21508
查看mha状态
[root@ c44 ~] masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:21508) is running(0:PING_OK), master:10.0.0.42
#running--启动状态
#master为10.0.0.42
ip漂移的方式:
(1),通过keepalived的方式,管理虚拟IP的漂移。
(2),通过MHA自带脚本的方式,管理虚拟IP的漂移#用mha自带的一个VIP漂移的脚本,哪台服务器变为(master),就漂到那个上面,根据binlog最新的slave提升。
7.1,以脚本方式配置MHA
(1)修改MHA配置文件
[root@ c44 ~] vim /etc/mha/app1.cnf
[server default]
master_ip_failover_script=/usr/bin/master_ip_failover #添加vip漂移脚本功能
(2)编写漂移脚本
[root@ c744 ~] vim /usr/bin/master_ip_failover
#!/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.100/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 "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
if ( $command eq "stop" || $command eq "stopssh" ) {
my $exit_code = 1;
eval {
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
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 \n";
&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 \n";
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\n";
}
(3)添加执行权限
[root@ c44 ~] chmod +x /usr/bin/master_ip_failover
7.2,在c42上手动添加vip
[root@ c42 ~] ifconfig ens33:1 10.0.0.100/24
[root@ c42 ~] ip a show ens33
(1)先关闭MHA
[root@ c44 ~] masterha_stop --conf=/etc/mha/app1.cnf
Stopped app1 successfully.
[1]+ Exit 1
(2)启动MHA
[root@ c44 ~] 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 &
[1] 22022
(3)查看mha状态
[root@ c44 ~] masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:22022) is running(0:PING_OK), master:10.0.0.42
8.1,关闭c42上的备份主库mysql
[root@ c42 ~] systemctl stop mysql
[root@ c42 ~] ip a 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:60:cb:77 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.42/24 brd 10.0.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe60:cb77/64 scope link
valid_lft forever preferred_lft forever
8.2,查看c41上vip和mysql状态
(1)查看vip
[root@ c41 ~] ip a show ens33
[root@ c41 ~] mysql -p123456 -e "show master status"
[root@ c42 ~] systemctl restart mysql
数据库中间件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
读写分离定义:
读写分离,基本的原理是让主数据库处理事务性增、删、改操作( INSERT. DELETE、 UPDATE ) , 而从数据库处理SELECT查询操作。数据库复制把主数据库操作的变更同步到集群中的从数据库。
读写分离作用:
因为数据库的“写" (写10000条数据到oracle可能要3分钟)操作是比较耗时的。但是数据库的“读" (从oracle读10000条数据可能只要5秒钟)。所以读写分离,解决的是,数据库的写入,影响了查询的效率。
读写分离意义:
数据库不一定要读写分离,如果程序使用数据库较多时,而更新少,查询多的情况下会考虑使用,利用数据库主从同步
可以减少数据库压力,提高性能。当然,数据库也有其它优化方案。memcache 或是表折分,或是搜索引学。都是解
决方法。
Atlas的功能有:
读写分离、从库负载均衡、自动分表、IP过滤、 SQL语句黑白名单、DBA可平滑上下线DB、自动摘除宕机的DB。
Atlas的使用场景:
Atlas是一个位于前端 应用与后端MySQL数据库之间的中间件,它使得应用程序员无需再关心读写分离、分表等与MySQL相关的细节,可以专注于编写业务逻辑,同时使得DBA的运维工作对前端应用透明,上下线DB前端应用无感知。
注意事项:
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以上
9.1,在c44上安装atlas
#下载Atlas
[root@ c44 ~] wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm
#安装Atlas
[root@ c44 ~] rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
9.2,修改配置文件
[root@ c44 ~] vim /usr/local/mysql-proxy/conf/test.cnf
[root@ c44 ~] egrep -v '^#|^$' /usr/local/mysql-proxy/conf/test.cnf
[mysql-proxy]
admin-username = user
admin-password = pwd
proxy-backend-addresses = 10.0.0.100:3306
proxy-read-only-backend-addresses = 10.0.0.42:3306,10.0.0.43:3306
pwds = repl:/iZxz+0GRoA=,mha:O2jBXONX098=
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
9.3,启动Atlas
[root@ c44 ~] /usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started
# Atlas可以通过不同的配置文件,同时启动多个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@ c44 ~] ps -ef|grep mysql-proxy
root 22218 1 0 18:27 ? 00:00:00 /usr/local/mysql-proxy/bin/mysql-prox --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
root 22219 22218 0 18:27 ? 00:00:00 /usr/local/mysql-proxy/bin/mysql-prox --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
root 22232 19112 0 18:28 pts/1 00:00:00 grep --color=auto mysql-proxy
9.4,Atlas读写分离测试
(1)读测试:
#在Atlas主机上安装mysql,并启动
[root@ c44 ~] yum -y install mysql
[root@ c44 ~] systemctl start mysql
#登录mysql
[root@ c44 ~] mysql -umha -pmha -P1234 -h10.0.0.44
MySQL [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 3 |
+-------------+
1 row in set (0.00 sec)
MySQL [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 2 |
+-------------+
注意: 发现上面’server_id每次的结果都不-样,分别是2台从库的serve_id ,并且每执行一次命令 , server_id就会变换一次,这是因为默认读操作的权重都是1,两台从DB默认就是负载均衡。
(2)写测试:
[root@ c44 ~] mysql -umha -pmha -P1234 -h10.0.0.44
MySQL [(none)]> begin;select @@server_id;commit;
Query OK, 0 rows affected (0.00 sec)
+-------------+
| @@server_id |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)
MySQL [(none)]> create database wg;
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| wg |
+--------------------+
4 rows in set (0.00 sec)
(1)在c41上查看
[root@ c41 ~]# mysql -uroot -p123456 -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| wg |
+--------------------+
(2)在c43上查看
[root@ c43 ~] mysql -uroot -p123456 -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| wg |
+--------------------+
9.5,Altas管理操作
(1)登录管理接口
[root@ c44 ~] mysql -uuser -ppwd -P2345 -h 10.0.0.44
(2)查看帮助信息
MySQL [(none)]> 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 [(none)]> select * from backends;
+-------------+-----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+-----------------+-------+------+
| 1 | 10.0.0.100:3306 | up | rw |
| 2 | 10.0.0.42:3306 | up | ro |
| 3 | 10.0.0.43:3306 | up | ro |
+-------------+-----------------+-------+------+
3 rows in set (0.00 sec)
(4)下线后端节点
MySQL [(none)]> set offline 3;
+-------------+----------------+---------+------+
| backend_ndx | address | state | type |
+-------------+----------------+---------+------+
| 3 | 10.0.0.43:3306 | offline | ro |
+-------------+----------------+---------+------+
1 row in set (0.00 sec
MySQL [(none)]> select * from backends;
+-------------+-----------------+---------+------+
| backend_ndx | address | state | type |
+-------------+-----------------+---------+------+
| 1 | 10.0.0.100:3306 | up | rw |
| 2 | 10.0.0.42:3306 | up | ro |
| 3 | 10.0.0.43:3306 | offline | ro |
+-------------+-----------------+---------+------+
3 rows in set (0.00 sec)
(5)上线后端节点
MySQL [(none)]> set online 3;
+-------------+----------------+---------+------+
| backend_ndx | address | state | type |
+-------------+----------------+---------+------+
| 3 | 10.0.0.43:3306 | unknown | ro |
+-------------+----------------+---------+------+
1 row in set (0.00 sec)
MySQL [(none)]> select * from backends;
+-------------+-----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+-----------------+-------+------+
| 1 | 10.0.0.100:3306 | up | rw |
| 2 | 10.0.0.42:3306 | up | ro |
| 3 | 10.0.0.43:3306 | up | ro |
+-------------+-----------------+-------+------+
3 rows in set (0.00 sec)