Atlas 是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MysqL协议的数据中间层项目。它在MysqL官方推出的MysqL-Proxy 0.8.2版本的基础上,修改了大量BUG,添加了很多功能特性。目前该项目在360公司内部得到了广泛应用,很多MysqL业务已经接入了Atlas平台,每天承载的读写请求数达几十亿条。
源码Github:https://github.com/Qihoo360/Atlas
主要功能:
读写分离
从库负载均衡
自动分表
IP过滤
SQL语句黑白名单
DBA可平滑上下线DB
自动摘除宕机的DB
使用场景:
Atlas是一个位于前端应用与后端MysqL数据库之间的中间件,它使得应用程序员无需再关心读写分离、分表等与MysqL相关的细节,可以专注于编写业务逻辑,同时使得DBA的运维工作对前端应用透明,上下线DB前端应用无感知。
读写分离方案
安装Atlas之前注意的事项:
Atlas只能安装运行在64位的系统上
Centos 5.X安装 Atlas-XX.el5.x86_64.rpm,Centos 6.X安装Atlas-XX.el6.x86_64.rpm。
后端MysqL版本应大于5.1,建议使用MysqL 5.6
实现MysqL读写分离
Atlas的安装:
下载地址:
https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm 或者
https://download.csdn.net/download/fd2025/10375544
rpm -i Atlas-2.2.1.el6.x86_64.rpm
默认安装目录为:/usr/local/MysqL-proxy
编辑Atlas配置文件:
vim /usr/local/MysqL-proxy/test.cnf
[MysqL-proxy]
#带#号的为非必需的配置项目
#管理接口的用户名
admin-username = root
#管理接口的密码
admin-password = root
#设置主库和从库的地址,其中主库为写库,从库为读库
#主库地址:Atlas后端连接的MysqL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 192.168.50.110:3306
#设置从库的地址和端口
proxy-read-only-backend-addresses= 192.168.50.111
#若配置为主库也分担读请求,并且设置权重为(主:从)=(1:3)
#proxy-read-only-backend-addresses = 192.168.50.110:[email protected],[email protected]
#用户名与其对应的加密过的MysqL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MysqL的
#用户名和加密密码!
pwds = root:/iZxz+0GRoA=
#设置Atlas的运行方式,设为true时为守护进程方式,设为false时为前台方式,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。
daemon = true
#设置Atlas的运行方式,设为true时Atlas会启动两个进程,一个为monitor,一个为worker,monitor在worker意外退出后会自动将其重启,
#设为false时只有worker,没有monitor,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。
keepalive = true
#工作线程数,对Atlas的性能有很大影响,可根据情况适当设置
event-threads = 8
#日志级别,分为message、warning、critical、error、deBUG五个级别
log-level = message
#日志存放的路径
log-path = /usr/local/MysqL-proxy/log
#sql日志的开关,可设置为OFF、ON、REALTIME,OFF代表不记录sql日志,ON代表记录sql日志,REALTIME代表记录sql日志且实时写入磁盘,默认为OFF
sql-log = ON
#慢日志输出设置。当设置了该参数时,则日志只输出执行时间超过sql-log-slow(单位:ms)的日志记录。不设置该参数则输出全部日志。
#sql-log-slow = 10
#实例名称,用于同一台机器上多个Atlas实例间的区分
#instance = test
#Atlas监听的工作接口IP和端口,应用程序将连接这个地址,把Atlasd当做MysqL-server,此处携程3306端口
proxy-address = 192.168.50.113:1234
#Atlas监听的管理接口IP和端口
admin-address = 192.168.50.113:2345
#分表设置,此例中person为库名,mt为表名,ID为分表字段,3为子表数量,可设置多项,以逗号分隔,若不分表则不需要设置该项
#tables = person.mt.ID.3
#默认字符集,设置该项后客户端不再需要执行SET nameS语句
#charset = utf8
#允许连接Atlas的客户端的IP,可以是精确IP,也可以是IP段,以逗号分隔,若不设置该项则允许所有IP连接,否则只允许列表中的IP连接
#clIEnt-ips = 127.0.0.1,192.168.1
#Atlas前面挂接的LVS的物理网卡的IP(注意不是虚IP),若有LVS且设置了clIEnt-ips则此项必须设置,否则可以不设置
#lvs-ips = 192.168.1.1
~
安装Atlas
[[email protected] ~]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
Preparing... ########################################### [100%]
1:Atlas ########################################### [100%]
会默认在”/usr/local/MysqL-proxy”下给你生成4个文件夹
[[email protected] ~]# ls -l /usr/local/MysqL-proxy/
总用量 0
drwxr-xr-x 2 root root 75 2月 14 16:07 bin
drwxr-xr-x 2 root root 22 2月 14 16:07 conf
drwxr-xr-x 3 root root 331 2月 14 16:07 lib
drwxr-xr-x 2 root root 6 12月 17 2014 log
bin目录下放的都是可执行文件
l encrypt是用来生成MysqL密码加密的,在配置的时候会用到
l MysqL-proxy是MysqL自己的读写分离代理
l MysqL-proxyd是360弄出来的,后面有个“d”,服务的启动、重启、停止。都是用他来执行的
conf目录下放的是配置文件
l test.cnf只有一个文件,用来配置代理的,可以使用vim来编辑
lib目录下放的是一些包,以及Atlas的依赖
log目录下放的是日志,如报错等错误信息的记录
进入bin目录,使用encrypt来对数据库的密码进行加密,我的MysqL数据的用户名是MysqL,密码是123456,我需要对密码进行加密
[[email protected] ~]# cd /usr/local/MysqL-proxy/bin/
[[email protected] bin]# ./encrypt 123456
/iZxz+0GRoA=
配置Atlas,使用vim进行编辑
[[email protected] bin]# cd ../conf/
[[email protected] conf]# vim test.cnf
#管理接口的用户名
admin-username = user
#管理接口的密码
admin-password = pwd
#Atlas后端连接的MysqL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 192.168.200.111:3306
#Atlas后端连接的MysqL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
proxy-read-only-backend-addresses =192.168.200.112:[email protected],192.168.200.113:[email protected]
#用户名与其对应的加密过的MysqL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密
pwds = myslave:/iZxz+0GRoA=
#Atlas监听的工作接口IP和端口
proxy-address = 0.0.0.0:1234
#Atlas监听的管理接口IP和端口
admin-address = 0.0.0.0:2345
启动Atlas
[[email protected] conf]# /usr/local/MysqL-proxy/bin/MysqL-proxyd test start
OK: MysqL-Proxy of test is started
测试一下Atlas服务器的MysqL状态,要确认它是关闭状态,并且使用MySQL命令,进不去数据库
[@L_502_11@ ~]# systemctl status mariadb
Unit mariadb.service Could not be found.
[[email protected] ~]# MysqL
bash: MysqL: 未找到命令...
确认系统中自带的MysqL进不去了,使用如下命令,进入Atlas的管理模式“MysqL -h192.168.200.114 -P2345 -uuser -ppwd ”,能进去说明Atlas正常运行着呢,因为它会把自己当成一个MysqL数据库,所以在不需要数据库环境的情况下,也可以进入到MysqL数据库模式。
[[email protected] ~]# yum -y install mariadb mariadb-devel
[[email protected] ~]# MysqL -h192.168.200.114 -P2345 -uuser -ppwd
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MysqL connection ID is 1
Server version: 5.0.99-agent-admin
copyright (c) 2000,2017,Oracle,MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input sta
tement.
MysqL [(none)]>
可以访问“help”表,来看MysqL管理员模式都能做些什么。可以使用SQL语句来访问
MysqL [(none)]> select * FROM backends;
+-------------+----------------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------------+-------+------+
| 1 | 192.168.200.111:3306 | up | rw |
| 2 | 192.168.200.112:3306 | up | ro |
| 3 | 192.168.200.113:3306 | up | ro |
+-------------+----------------------+-------+------+
3 rows in set (0.00 sec)
测试atlas读写功能
注意:如果atlas和MysqL处于同一台机器,连接atlas时必须指定-h 127.0.0.1或者机器的IP 因为不指定-h的时候MysqL连接走的unix socket连接,此时-P参数无效,-P后面指定任意端口都是连到的本机的MysqL实例
准备测试环境
在Master上创建一个表,同步到slave1 slave2上
MariaDB [(none)]> create database cloud;
query OK,1 row affected (0.01 sec)
MariaDB [(none)]> use cloud;
Database changed
MariaDB [cloud]> create table data(ID int(10),name varchar(10),address varchar(20));
query OK,0 rows affected (0.24 sec)
MariaDB [cloud]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cloud |
| MysqL |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
分别在从服务器上
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cloud |
| MysqL |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> stop slave;
query OK,0 rows affected (0.01 sec)
然后在主服务器上:
MysqL> insert into data values(‘1‘,‘crushlinx‘,‘this_is_master‘);
query OK,1 row affected (0.04 sec)
应为在服务器上同步了表,所以在从服务器上可以直接手动插入其他内容
在slave1上:
MysqL> use cloud;
Database changed
MysqL> show tables;
+-------------------+
| tables_in_db_test |
+-------------------+
| data |
+-------------------+
1 row in set (0.00 sec)
MysqL> insert into data values(‘2‘,‘crushlinux‘,‘this_is_slave1‘);
query OK,1 row affected (0.01 sec)
在slave2上:
MysqL> use cloud;
Database changed
MysqL> show tables;
+-------------------+
| tables_in_db_test |
+-------------------+
| data |
+-------------------+
1 row in set (0.00 sec)
MysqL> insert into data values(‘3‘,‘this_is_slave2‘);
query OK,1 row affected (0.01 sec)
在atlas上测试读 *** 作:
MysqL> use cloud;
Database changed
MysqL> show tables;
+-------------------+
| tables_in_db_test |
+-------------------+
| data |
+-------------------+
1 row in set (0.01 sec)
MysqL> select * from data;
+------+-------+----------------+
| ID | name | address |
+------+-------+----------------+
| 2 | zhang | this_is_slave1 |
+------+-------+----------------+
1 row in set (0.05 sec)
在atlas上测试写 *** 作
MysqL> insert into data values(‘4‘,‘zhang‘,‘write_test‘);
query OK,1 row affected (0.12 sec)
然后在atlas上进行查询,发现查询不到
MysqL> select * from data;
+------+-------+----------------+
| ID | name | address |
+------+-------+----------------+
| 2 | zhang | this_is_slave1 |
+------+-------+----------------+
1 row in set (0.05 sec)
然后在Master上发现可以查询到
MysqL> select * from data;
+------+-------+----------------+
| ID | name | address |
+------+-------+----------------+
| 1 | zhang | this_is_master |
| 4 | zhang | write_test |
+------+-------+----------------+
2 rows in set (0.00 sec)
若开启slave 数据将会同步到atlas 和从
由此可以验证,已经通过amoeba实现了MysqL的读写分离,目前所有的写 *** 作都全部在MASTER上,所有的读 *** 作都分摊给了slave从服务器,
总结以上是内存溢出为你收集整理的基于 altas 实现读写分离全部内容,希望文章能够帮你解决基于 altas 实现读写分离所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)