MHA(Master High Availability),由日本DeNA公司youshimaton(现就任于Facebook公司)开发。MHA能作到在10~30秒以内自动完成数据库的Failover,Failover的过程当中,能最大程度上保证数据的一致性。
该软件由两部分组成:MHA Manager(管理节点)和MHA Node(数据节点)。
半同步复制,能够大大下降数据丢失的风险。MHA能够与半同步复制结合起来。
MHA高可用集群,要求一个复制集群中须要有三台数据库服务器,一主二从,不支持多实例。出于机器成本的考虑,淘宝也在该基础上进行了改造,淘宝TMHA支持一主一从
注意:必须使用独立的数据库节点,不支持多实例。前端
Manager工具包主要包括如下几个工具:node
Node工具包(这些工具一般由MHA Manager的脚本触发,无需人为操做)主要包括如下几个工具:mysql
一、Manager程序负责监控全部已知Node(1主2从全部节点)
二、当主库发生意外宕机
2.1 mysql实例故障(SSH可以链接到主机)
一、监控到主库宕机,选择一个新主(取消从库角色,reset slave),选择标准:数据较新的从库会被选择为新主(show slave status\G)
二、从库经过MHA自带脚本程序,当即保存缺失部分的binlog
三、二号从库会从新与新主构建主从关系,继续提供服务
四、若是VIP机制,将vip从原主库漂移到新主,让应用程序无感知
2.2 主节点服务器宕机(SSH已经链接不上了)
一、监控到主库宕机,尝试SSH链接,尝试失败
二、选择一个数据较新的从库成为新主库(取消从库角色 reset slave),判断细节:show slave status\G
三、计算从库之间的relay-log的差别,补偿到2号从库
四、二号从库会从新与新主构建主从关系,继续提供服务
五、若是VIP机制,将vip从原主库漂移到新主,让应用程序无感知
六、若是有binlog server机制,会继续讲binlog server中的记录的缺失部分的事务,补偿到新的主库git
relay_log_purge=0 #保留mysql中relay_log 每一个节点都须要作好解析 172.16.1.51 db01 172.16.1.52 db02 172.16.1.53 db03
mha下载地址:https://github.com/yoshinorim/mha4mysql-manager/releases 每一个点都须要进行安装mha-node yum -y install mha4mysql-node-0.58-0.el7.centos.noarch.rpm
grant all privileges on *.* to mha@'172.16.1.%' identified by 'mha';
ln -s /application/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog ln -s /application/mysql/bin/mysql /usr/bin/mysql
yum -y install mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
mkdir -p /etc/mha mkdir -p /var/log/mha/app1 ----》能够管理多套主从复制 vim /etc/mha/app1.cnf [server default] manager_log=/var/log/mha/app1/manager manager_workdir=/var/log/mha/app1 master_binlog_dir=/data/binlog user=mha password=mha ping_interval=2 repl_password=123 repl_user=repl ssh_user=root [server1] hostname=172.16.1.51 port=3306 [server2] hostname=172.16.1.52 port=3306 [server3] hostname=172.16.1.53 port=3306
ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa >/dev/null 2>&1 ssh-copy-id -i /root/.ssh/id_dsa.pub root@172.16.1.51 ssh-copy-id -i /root/.ssh/id_dsa.pub root@172.16.1.52 ssh-copy-id -i /root/.ssh/id_dsa.pub root@172.16.1.53
[root@db03 tools]# masterha_check_ssh --conf=/etc/mha/app1.cnf Tue Apr 30 20:04:52 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping .Tue Apr 30 20:04:52 2019 - [info] Reading application default configuration from /etc/mha/app1.cnf.. Tue Apr 30 20:04:52 2019 - [info] Reading server configuration from /etc/mha/app1.cnf.. Tue Apr 30 20:04:52 2019 - [info] Starting SSH connection tests.. Tue Apr 30 20:04:53 2019 - [debug] Tue Apr 30 20:04:52 2019 - [debug] Connecting via SSH from root@172.16.1.51(172.16.1.51:22) to root@172.16. 1.52(172.16.1.52:22)..Tue Apr 30 20:04:52 2019 - [debug] ok. Tue Apr 30 20:04:52 2019 - [debug] Connecting via SSH from root@172.16.1.51(172.16.1.51:22) to root@172.16. 1.53(172.16.1.53:22)..Tue Apr 30 20:04:53 2019 - [debug] ok. Tue Apr 30 20:04:53 2019 - [debug] Tue Apr 30 20:04:52 2019 - [debug] Connecting via SSH from root@172.16.1.52(172.16.1.52:22) to root@172.16. 1.51(172.16.1.51:22)..Tue Apr 30 20:04:53 2019 - [debug] ok. Tue Apr 30 20:04:53 2019 - [debug] Connecting via SSH from root@172.16.1.52(172.16.1.52:22) to root@172.16. 1.53(172.16.1.53:22)..Tue Apr 30 20:04:53 2019 - [debug] ok. Tue Apr 30 20:04:53 2019 - [error][/usr/share/perl5/vendor_perl/MHA/SSHCheck.pm, ln63] Tue Apr 30 20:04:53 2019 - [debug] Connecting via SSH from root@172.16.1.53(172.16.1.53:22) to root@172.16. 1.51(172.16.1.51:22)..Warning: Permanently added '172.16.1.53' (ECDSA) to the list of known hosts. Permission denied (publickey,password). Tue Apr 30 20:04:53 2019 - [error][/usr/share/perl5/vendor_perl/MHA/SSHCheck.pm, ln111] SSH connection from root@172.16.1.53(172.16.1.53:22) to root@172.16.1.51(172.16.1.51:22) failed!SSH Configuration Check Failed! at /usr/bin/masterha_check_ssh line 44. [root@db03 tools]#
[root@db03 tools]# masterha_check_ssh --conf=/etc/mha/app1.cnf Tue Apr 30 20:04:52 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping .Tue Apr 30 20:04:52 2019 - [info] Reading application default configuration from /etc/mha/app1.cnf.. Tue Apr 30 20:04:52 2019 - [info] Reading server configuration from /etc/mha/app1.cnf.. Tue Apr 30 20:04:52 2019 - [info] Starting SSH connection tests.. Tue Apr 30 20:04:53 2019 - [debug] Tue Apr 30 20:04:52 2019 - [debug] Connecting via SSH from root@172.16.1.51(172.16.1.51:22) to root@172.16. 1.52(172.16.1.52:22)..Tue Apr 30 20:04:52 2019 - [debug] ok. Tue Apr 30 20:04:52 2019 - [debug] Connecting via SSH from root@172.16.1.51(172.16.1.51:22) to root@172.16. 1.53(172.16.1.53:22)..Tue Apr 30 20:04:53 2019 - [debug] ok. Tue Apr 30 20:04:53 2019 - [debug] Tue Apr 30 20:04:52 2019 - [debug] Connecting via SSH from root@172.16.1.52(172.16.1.52:22) to root@172.16. 1.51(172.16.1.51:22)..Tue Apr 30 20:04:53 2019 - [debug] ok. Tue Apr 30 20:04:53 2019 - [debug] Connecting via SSH from root@172.16.1.52(172.16.1.52:22) to root@172.16. 1.53(172.16.1.53:22)..Tue Apr 30 20:04:53 2019 - [debug] ok. Tue Apr 30 20:04:53 2019 - [error][/usr/share/perl5/vendor_perl/MHA/SSHCheck.pm, ln63] Tue Apr 30 20:04:53 2019 - [debug] Connecting via SSH from root@172.16.1.53(172.16.1.53:22) to root@172.16. 1.51(172.16.1.51:22)..Warning: Permanently added '172.16.1.53' (ECDSA) to the list of known hosts. Permission denied (publickey,password). Tue Apr 30 20:04:53 2019 - [error][/usr/share/perl5/vendor_perl/MHA/SSHCheck.pm, ln111] SSH connection from root@172.16.1.53(172.16.1.53:22) to root@172.16.1.51(172.16.1.51:22) failed!SSH Configuration Check Failed! at /usr/bin/masterha_check_ssh line 44. [root@db03 tools]# masterha_check_repl --conf=/etc/mha/app1.cnf Tue Apr 30 20:05:38 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping .Tue Apr 30 20:05:38 2019 - [info] Reading application default configuration from /etc/mha/app1.cnf.. Tue Apr 30 20:05:38 2019 - [info] Reading server configuration from /etc/mha/app1.cnf.. Tue Apr 30 20:05:38 2019 - [info] MHA::MasterMonitor version 0.58. Tue Apr 30 20:05:40 2019 - [info] GTID failover mode = 1 Tue Apr 30 20:05:40 2019 - [info] Dead Servers: Tue Apr 30 20:05:40 2019 - [info] Alive Servers: Tue Apr 30 20:05:40 2019 - [info] 172.16.1.51(172.16.1.51:3306) Tue Apr 30 20:05:40 2019 - [info] 172.16.1.52(172.16.1.52:3306) Tue Apr 30 20:05:40 2019 - [info] 172.16.1.53(172.16.1.53:3306) Tue Apr 30 20:05:40 2019 - [info] Alive Slaves: Tue Apr 30 20:05:40 2019 - [info] 172.16.1.52(172.16.1.52:3306) Version=5.6.43-log (oldest major version between slaves) log-bin:enabledTue Apr 30 20:05:40 2019 - [info] GTID ON Tue Apr 30 20:05:40 2019 - [info] Replicating from 172.16.1.51(172.16.1.51:3306) Tue Apr 30 20:05:40 2019 - [info] 172.16.1.53(172.16.1.53:3306) Version=5.6.43-log (oldest major version between slaves) log-bin:enabledTue Apr 30 20:05:40 2019 - [info] GTID ON Tue Apr 30 20:05:40 2019 - [info] Replicating from 172.16.1.51(172.16.1.51:3306) Tue Apr 30 20:05:40 2019 - [info] Current Alive Master: 172.16.1.51(172.16.1.51:3306) Tue Apr 30 20:05:40 2019 - [info] Checking slave configurations.. Tue Apr 30 20:05:40 2019 - [info] read_only=1 is not set on slave 172.16.1.52(172.16.1.52:3306). Tue Apr 30 20:05:40 2019 - [info] read_only=1 is not set on slave 172.16.1.53(172.16.1.53:3306). Tue Apr 30 20:05:40 2019 - [info] Checking replication filtering settings.. Tue Apr 30 20:05:40 2019 - [info] binlog_do_db= , binlog_ignore_db= Tue Apr 30 20:05:40 2019 - [info] Replication filtering check ok. Tue Apr 30 20:05:40 2019 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package check ing.Tue Apr 30 20:05:40 2019 - [info] Checking SSH publickey authentication settings on the current master.. Tue Apr 30 20:05:40 2019 - [info] HealthCheck: SSH to 172.16.1.51 is reachable. Tue Apr 30 20:05:40 2019 - [info] 172.16.1.51(172.16.1.51:3306) (current master) +--172.16.1.52(172.16.1.52:3306) +--172.16.1.53(172.16.1.53:3306) Tue Apr 30 20:05:40 2019 - [info] Checking replication health on 172.16.1.52.. Tue Apr 30 20:05:40 2019 - [info] ok. Tue Apr 30 20:05:40 2019 - [info] Checking replication health on 172.16.1.53.. Tue Apr 30 20:05:40 2019 - [info] ok. Tue Apr 30 20:05:40 2019 - [warning] master_ip_failover_script is not defined. Tue Apr 30 20:05:40 2019 - [warning] shutdown_script is not defined. Tue Apr 30 20:05:40 2019 - [info] Got exit code 0 (Not master dead). MySQL Replication Health is OK. [root@db03 tools]#
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 &
[root@db01 tools]# /etc/init.d/mysqld stop Shutting down MySQL...... SUCCESS!
[root@db03 ~]# cat /var/log/mha/app1/manager ...... CHANGE MASTER TO MASTER_HOST='172.16.1.52', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='xxx'; #此处是恢复故障机的命令 ...... ----- Failover Report ----- app1: MySQL Master failover 172.16.1.51(172.16.1.51:3306) to 172.16.1.52(172.16.1.52:3306) succeeded Master 172.16.1.51(172.16.1.51:3306) is down! Check MHA Manager logs at db03:/var/log/mha/app1/manager for details. Started automated(non-interactive) failover. Selected 172.16.1.52(172.16.1.52:3306) as a new master. 172.16.1.52(172.16.1.52:3306): OK: Applying all logs succeeded. 172.16.1.53(172.16.1.53:3306): OK: Slave started, replicating from 172.16.1.52(172.16.1.52:3306) 172.16.1.52(172.16.1.52:3306): Resetting slave info succeeded. Master failover to 172.16.1.52(172.16.1.52:3306) completed successfully. #此处表示已经成功切换到52这一台机器了
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.1.52 #已成功切换到db02 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 805 Relay_Log_File: db03-relay-bin.000002 Relay_Log_Pos: 408 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 805 Relay_Log_Space: 611 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 52 Master_UUID: 901b2d37-6af9-11e9-9c6c-000c29481d4a Master_Info_File: /application/mysql/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: fac6353b-6a35-11e9-9770-000c29c0e349:1-3 Auto_Position: 1 1 row in set (0.00 sec)
1.在db01上,将db01从新加入到主从复制 [root@db01 tools]# /etc/init.d/mysqld start CHANGE MASTER TO MASTER_HOST='172.16.1.52', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl',MASTER_PASSWORD='123'; start slave 2.在mha配置文件中从新加入db01 [root@db03 tools]# vim /etc/mha/app1.cnf [server default] ...... [server1] hostname=172.16.1.51 port=3306 ...... 3.启动MHA了manager程序 masterha_check_ssh --conf=/etc/mha/app1.cnf masterha_check_ssh --conf=/etc/mha/app1.cnf 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 &
[root@db03 bin]# cat 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.55/24'; my $key = '1'; my $ssh_start_vip = "/sbin/ifconfig eth1:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig eth1:$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"; [root@db03 bin]# dos2unix /usr/local/bin/master_ip_failover [root@db03 bin]# chmod +x master_ip_failover
[root@db03 bin]# vim /etc/mha/app1.cnf [server default] ...... master_ip_failover_script=/usr/local/bin/master_ip_failover ......
my $vip = '172.16.1.100/24'; my $key = '1'; my $ssh_start_vip = "/sbin/ifconfig eth1:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig eth1:$key down";
masterha_stop --conf=/etc/mha/app1.cnf 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 &
ifconfig eth1:1 172.16.1.100/24 #如最小化安装没有ifconfig 命令则须要使用yum -y install net-tools安装 [root@db02 tools]# ip a 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:48:1d:54 brd ff:ff:ff:ff:ff:ff inet 172.16.1.52/24 brd 172.16.1.255 scope global eth1 valid_lft forever preferred_lft forever inet 172.16.1.100/24 brd 172.16.1.255 scope global secondary eth1:1 valid_lft forever preferred_lft forever
中止主库 [root@db02 tools]# /etc/init.d/mysqld stop Shutting down MySQL...... SUCCESS! 此时发现vip已经在db01上了 [root@db01 tools]# ip a 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:c0:e3:53 brd ff:ff:ff:ff:ff:ff inet 172.16.1.51/24 brd 172.16.1.255 scope global eth1 valid_lft forever preferred_lft forever inet 172.16.1.100/24 brd 172.16.1.255 scope global secondary eth1:1 valid_lft forever preferred_lft forever
mysql> CHANGE MASTER TO MASTER_HOST='172.16.1.51', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='re pl',MASTER_PASSWORD='123'; 在mha配置文件中加入 [server2] hostname=172.16.1.52 port=3306 启动mha
生产环境中通常是找一台额外的机器,必需要有5.6以上的版本,支持gtid并开启,咱们直接用的是db03程序员
[root@db03 bin]# vim /etc/mha/app1.cnf [server default] manager_log=/var/log/mha/app1/manager manager_workdir=/var/log/mha/app1 master_binlog_dir=/data/binlog master_ip_failover_script=/usr/local/bin/master_ip_failover password=mha ping_interval=2 repl_password=123 repl_user=repl ssh_user=root user=mha [server1] hostname=172.16.1.51 port=3306 [server2] hostname=172.16.1.52 port=3306 [binlog1] no_master=1 hostname=172.16.1.53 master_binlog_dir=/data/mysql/binlog #提早建立好,这个目录不能和原有的binlog一致 mkdir -p /data/mysql/binlog chown -R mysql.mysql /data/mysql/*
cd /data/mysql/binlog -----》必须进入到本身建立好的目录 mysqlbinlog -R --host=172.16.1.51 --user=mha --password=mha --raw --stop-never mysql-bin.000001 &
masterha_stop --conf=/etc/mha/app1.cnf 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 &
刷新binlog日志在主库上 mysql> flush logs; 查看binserver目录 [root@db03 binlog]# ls mysql-bin.000001 mysql-bin.000002 mysql-bin.000003 mysql-bin.000004
ping_interval=2(在server标签下设置) manager检测节点存活的间隔时间,总共会探测4次。 candidate_master=1(在节点标签下设置) #设置为候选master,若是设置该参数之后,发生主从切换之后将会将此从库提高为主库,即便这个主库不是集群中事件最新的slave,默认状况下若是一个slave落后master 100M的relay logs的话,MHA将不会选择该slave做为一个新的master,由于对于这个slave的恢复须要花费很长时间,经过设置check_repl_delay=0,MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机很是有用,由于这个候选主在切换的过程当中必定是新的master check_repl_delay=0(节点标签下设置) #用防止master故障时,切换时slave有延迟,卡在那里切不过来
Atlas 是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目。它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量bug,添加了不少功能特性。目前该项目在360公司内部获得了普遍应用,不少MySQL业务已经接入了Atlas平台,天天承载的读写请求数达几十亿条。
源码Github: https://github.com/Qihoo360/Atlasgithub
Atlas的功能有:读写分离、从库负载均衡、自动分表、IP过滤、SQL语句黑白名单、DBA可平滑上下线DB、自动摘除宕机的DB
Atlas的使用场景:Atlas是一个位于前端应用与后端MySQL数据库之间的中间件,它使得应用程序员无需再关心读写分离、分表等与MySQL相关的细节,能够专一于编写业务逻辑,同时使得DBA的运维工做对前端应用透明,上下线DB前端应用无感知web
下载地址:https://github.com/Qihoo360/Atlas/releases
注意:
一、Atlas只能安装运行在64位的系统上
二、Centos 5.X安装 Atlas-XX.el5.x86_64.rpm,Centos 6.X安装Atlas-XX.el6.x86_64.rpm(通过测试centos7也可使用6的版本)
三、后端mysql版本应大于5.1,建议使用Mysql 5.6以上sql
1.安装altas rpm -ivh Atlas-2.2.1.el6.x86_64.rpm 2.修改配置文件 cd /usr/local/mysql-proxy/ cp test.cnf test.cnf.bak vim /usr/local/mysql-proxy/conf/test.cnf [mysql-proxy] admin-username = user admin-password = pwd proxy-backend-addresses = 172.16.1.100:3306 # 设置写入主库vip的地址 proxy-read-only-backend-addresses = 172.16.1.52:3306,10.0.0.53:3306 # 设置只读的从库地址 pwds = repl:3yb5jEku5h4=,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:33060 admin-address = 0.0.0.0:2345 charset=utf8 3.启动atlas /usr/local/mysql-proxy/bin/mysql-proxyd test start
读测试 mysql -umha -pmha -h172.16.1.53 -P33060 show variables like 'server_id'; mysql> show variables like 'server_id'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 52 | +---------------+-------+ 1 row in set (0.01 sec) mysql> show variables like 'server_id'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 53 | +---------------+-------+ 1 row in set (0.00 sec) 写测试 set global read_only=1; # 设置两个从节点为只读模式 mysql -umha -pmha -h172.16.1.53 -P33060 create database db1;
链接管理接口: mysql -uuser -ppwd -h127.0.0.1 -P2345 打印帮助: 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) 查看全部节点 mysql> SELECT * FROM backends; +-------------+-------------------+-------+------+ | backend_ndx | address | state | type | +-------------+-------------------+-------+------+ | 1 | 172.16.1.100:3306 | up | rw | | 2 | 172.16.1.52:3306 | up | ro | | 3 | 172.16.1.53:3306 | up | ro | +-------------+-------------------+-------+------+ 3 rows in set (0.00 sec) 动态删除节点: mysql> REMOVE BACKEND 3; Empty set (0.00 sec) mysql> SELECT * FROM backends; +-------------+-------------------+-------+------+ | backend_ndx | address | state | type | +-------------+-------------------+-------+------+ | 1 | 172.16.1.100:3306 | up | rw | | 2 | 172.16.1.52:3306 | up | ro | +-------------+-------------------+-------+------+ 2 rows in set (0.00 sec) 动态添加节点: mysql> ADD SLAVE 172.16.1.53:3306; Empty set (0.00 sec) mysql> SELECT * FROM backends; +-------------+-------------------+-------+------+ | backend_ndx | address | state | type | +-------------+-------------------+-------+------+ | 1 | 172.16.1.100:3306 | up | rw | | 2 | 172.16.1.52:3306 | up | ro | | 3 | 172.16.1.53:3306 | up | ro | +-------------+-------------------+-------+------+ 3 rows in set (0.00 sec) 将修改保存至配置文件中 SAVE CONFIG;
使用Atlas的分表功能时,首先须要在配置文件test.cnf设置tables参数。
tables参数设置格式:数据库名.表名.分表字段.子表数量,
好比:
你的数据库名叫school,表名叫stu,分表字段叫id,总共分为2张表,那么就写school.stu.id.2,若是还有其余的分表,以逗号分隔便可。
用户须要手动创建2张子表(stu_0,stu_1,注意子表序号是从0开始的)。全部的子表必须在DB的同一个database里。
当经过Atlas执行(SELECT、DELETE、UPDATE、INSERT、REPLACE)操做时,Atlas会根据分表结果(id%2=k),定位到相应的子表(stu_k)。例如,执行select * from stu where id=3;,Atlas会自动从stu_1这张子表返回查询结果。但若是执行SQL语句(select * from stu;)时不带上id,则会提示执行stu 表不存在。
Atlas暂不支持自动建表和跨库分表的功能。
Atlas目前支持分表的语句有SELECT、DELETE、UPDATE、INSERT、REPLACE。数据库
配置文件 vim /usr/local/mysql-proxy/conf/test.cnf tables = school.stu.id.5 重启atlas (主库)手工建立,分表后的库和表,分别为定义的school 和 stu_0 stu_1 stu_2 stu_3 stu_4 create database school; use school create table stu_0 (id int,name varchar(20)); create table stu_1 (id int,name varchar(20)); create table stu_2 (id int,name varchar(20)); create table stu_3 (id int,name varchar(20)); create table stu_4 (id int,name varchar(20)); 测试: insert into stu values (3,'wang5'); insert into stu values (2,'li4'); insert into stu values (1,'zhang3'); insert into stu values (4,'m6'); insert into stu values (5,'zou7'); commit;
IP过滤:client-ips 该参数用来实现IP过滤功能。 在传统的开发模式中,应用程序直接链接DB,所以DB会对部署应用的机器(好比web服务器)的IP做访问受权。 在引入中间层后,由于链接DB的是Atlas,因此DB改成对部署Atlas的机器的IP做访问受权,若是任意一台客户端均可以链接Atlas,就会带来潜在的风险。 client-ips参数用来控制链接Atlas的客户端的IP,能够是精确IP,也能够是IP段,以逗号分隔写在一行上便可。 如: client-ips=192.168.1.2, 192.168.2,这就表明192.168.1.2这个IP和192.168.2.*这个段的IP能够链接Atlas,其余IP均不能链接。 若是该参数不设置,则任意IP都可链接Atlas。 若是设置了client-ips参数,且Atlas前面挂有LVS,则必须设置lvs-ips参数,不然能够不设置lvs-ips。 SQL语句黑白名单 Atlas会屏蔽不带where条件的delete和update操做,以及sleep函数。
<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">vim