第1章 利用Atlas软件实现读写分离
参考资料: https://www.cnblogs.com/yyhh/archive/2015/12/29/5084844.html#l03
读写分离优点:
(1) 提高并发处理效率
(2) 提高数据库读写效率
下载Atlas会有两个版本,其中有个分表的版本,但是这个需要其他的依赖,我这边不需要分表这种需求,所以安装普通的版本
Atlas (普通) : Atlas-2.2.1.el6.x86_64.rpm
Atlas (分表) : Atlas-sharding_1.0.1-el6.x86_64.rpm
安装过程
(1) 首先进入Linux的Home目录下,下载非分表的安装包
# cd /home/
# wget http://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm
(2) 下载好了之后,进行安装
[root@localhost home]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
Preparing... ########################################### [100%]
1:Atlas ########################################### [100%]
若是提示:
error: unpacking of archive failed on file /usr/local/mysql-proxy/lib/libevent-2.0.so.5;5d80e20c: cpio: read
error: Atlas-2.2.1-1.x86_64: install failed
则需要将/usr/lib64/libevent-2.0.so.5
这个文件复制到/usr/local/mysql-proxy/lib/
下
cp /usr/lib64/libevent-2.0.so.5 /usr/local/mysql-proxy/lib/
安装目录文件作用
安装好了,它会默认在”/usr/local/mysql-proxy”下给你生成4个文件夹,以及需要配置的文件,如下:
[root@localhost home]# ll /usr/local/mysql-proxy/
total 16
drwxr-xr-x. 2 root root 4096 Dec 28 10:47 bin
drwxr-xr-x. 2 root root 4096 Dec 28 10:47 conf
drwxr-xr-x. 3 root root 4096 Dec 28 10:47 lib
drwxr-xr-x. 2 root root 4096 Dec 17 2014 log
目录介绍:
bin目录下放的都是可执行文件
- “
encrypt
”是用来生成MySQL密码加密的,在配置的时候会用到 - “
mysql-proxy
”是MySQL自己的读写分离代理 - “
mysql-proxyd
”是360弄出来的,后面有个“d”,服务的启动、重启、停止。都是用他来执行的
conf目录下放的是配置文件
- “
test.cnf
”只有一个文件,用来配置代理的,可以使用vim来编辑
lib目录下放的是一些包,以及Atlas的依赖
log目录下放的是日志,如报错等错误信息的记录
配置过程
首先进入bin目录,使用encrypt来对数据库的密码进行加密,
我的MySQL数据的用户名是old,密码是123456,我需要对密码进行加密
shell > /usr/local/mysql-proxy/bin/encrypt 123456
得到结果:
/iZxz+0GRoA=
配置Atlas,使用vim进行编辑
shell> cd /usr/local/mysql-proxy/conf/
shell> vim test.cnf
进入后,可以在Atlas进行配置,360写的中文注释都很详细,根据注释来配置信息,其中比较重要,需要说明的配置如下:
这是用来登录到Atlas的管理员的账号与密码,与之对应的是“#Atlas监听的管理接口IP和端口”,也就是说需要设置管理员登录的端口,才能进入管理员界面,默认端口是2345,也可以指定IP登录,指定IP后,其他的IP无法访问管理员的命令界面。
方便测试,我这里没有指定IP和端口登录。
#管理接口的用户名
admin-username = user
#管理接口的密码
admin-password = pwd
这是用来配置主数据的地址与从数据库的地址,这里配置的主数据库是51,从数据库是7
#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 172.16.1.51:3306
#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
proxy-read-only-backend-addresses = 172.16.1.7:3306@1
这个是用来配置MySQL的账户与密码的,我的MySQL的用户是old,密码是123456,刚刚使用Atlas提供的工具生成了对应的加密密码
#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!
pwds = old:/iZxz+0GRoA=
这里需要注意 主从服务器都要有这个用户 创建用户命令
mysql> GRANT ALL ON *.* TO 'old'@'%' identified by '123456';
这是设置工作接口与管理接口的,如果ip设置的”0.0.0.0”就是说任意IP都可以访问这个接口,当然也可以指定IP和端口,方便测试我这边没有指定,工作接口的用户名密码与MySQL的账户对应的,管理员的用户密码与上面配置的管理员的用户密码对应。
#Atlas监听的工作接口IP和端口
proxy-address = 0.0.0.0:1234
#Atlas监听的管理接口IP和端口
admin-address = 0.0.0.0:2345
启动Atlas
shell> /usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started
输入命令 可以进入虚拟数据库中看到可以干些什么
mysql -uuser -ppwd -P2345 -h127.0.0.1
在虚拟数据库中执行命令可以查看读写状态
mysql> select * from backends;
启动时讨厌的报错
2019-09-18 08:48:37: (critical) chassis-frontend.c:122: Failed to get log directory, please set by --log-path
2019-09-18 08:48:37: (message) Initiating shutdown, requested from mysql-proxy-cli.c:381
2019-09-18 08:48:37: (message) shutting down normally, exit code is: 1
出现一条 critical 日志,大致意思是“无法找到保存日志文件的路径位置,需要通过 --log-path 进行指定”。
而执行 mysql-proxyd 脚本得到输出如下:
[root@Betty ~]# mysql-proxyd
Usage: /usr/local/mysql-proxy/bin/mysql-proxyd
instance {start|stop|restart|status}
代表能够成功执行Atlas服务
测试一下Atlas服务器的MySQL状态,要确认它是关闭状态,并且使用mysql命令,进不去数据库
[root@localhost bin]# /etc/init.d/mysqld status
mysqld is stopped
[root@localhost bin]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
确认系统中自带的MySQL进不去了,使用如下命令,进入Atlas的管理模式“mysql -h127.0.0.1 -P2345 -uuser -ppwd ”,能进去说明Atlas正常运行着呢,因为它会把自己当成一个MySQL数据库,所以在不需要数据库环境的情况下,也可以进入到MySQL数据库模式。
[root@localhost bin]# mysql -h127.0.0.1 -P2345 -uuser -ppwd
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.99-agent-admin
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
新的分割线---------------------------------------------------------------------------
我们可以访问“help”表,来看MySQL管理员模式都能做些什么。可以使用SQL语句来访问
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>
也可以使用工作接口来访问,使用命令“mysql -h127.0.0.1 -P1234 -ubuck -phello
”
[root@localhost bin]# mysql -h127.0.0.1 -P1234 -ubuck -phello
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.81-log
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Atlas中间件实现站点数据库关联
修改站点文件配置文件 将数据库关联到Atlas服务器上 (数据库迁移)
以phpcmsV9为例
修改站点数据库配置文件
配置文件路径:/www/caches/configs/database.php
修改配置文件 vim /www/caches/configs/database.php
登陆网站查看是否成功
第2章 Atlas故障错误
最简单的错误就是数据库配置错误 此错误先进行远程连接Atlas数据库 查看是否连接成功 如果不成功请去Atlas配置文件找问题
能够远程连接上请查看站点目录数据库配置文件是否配置正确
下面是我出错误后在网上看到的atlas错误与优化
- 使用atlas却发现“读库闲置,框架还是去主库读写数据
配置完atlas之后,发现使用jdbc框架的话,读库和写库各司其职,但是使用mybatis框架之后,就发现框架的读写都去了主库,把读库放置一边,那么这种情况是因为“有事务存在的话,atlas就会强制走主库”,遇到这种情况就检查一下是否有事务的存在,比如@Transactional,如果要解决的话,就加上“@Transactional(propagation=Propagation.NOT_SUPPORTED)”即可。
自动读写分离挺好,但有时候我写完马上就想读,万一主从同步延迟怎么办?
SQL语句前增加**/*master*/
就可以将读请求强制发往主库。在mysql命令行测试该功能时,需要加-c
选项,以防mysql客户端过滤掉注释信息。不过这不能从本质上解决问题,使用Atlas需要考虑到这点, 提高主机的IO性能,加大memory可以缓解延迟症状,但依旧不能避免延迟的出现,尤其是读多写少的应用。resource limit问题
atlas有自己的连接池,会吃掉很多CPU, php应用端改用短链接来连接atlas, 这时候atlas对php发送来的sql只负责验证和转发的操作,后端DB的连接由atlas自己管理,未使用的连接线程进行剔除操作(DB的wait_timeout和interactive_timeout设置为300s,超时亦退出)。
2014-04-12 20:56:29: (warning) (libevent) event_del: event has no event_base set.
2014-04-12 20:56:29: (critical) last message repeated 5 times
2014-04-12 20:56:29: (critical) network-conn-pool-lua.c.144: socket() failed: Too many open files (24)
2014-04-12 20:56:29: (warning) (libevent) event_del: event has no event_base set.
2014-04-12 20:56:30: (debug) chassis-unix-daemon.c:168: 12951 returned: 12951
2014-04-12 20:56:30: (critical) chassis-unix-daemon.c:196: [angel] PID=12951 died on signal=11 (it used 16 kBytes max) ... waiting 3min before restart
如果MySQL后端的连接数也满了可能会报以下错误:
2014-11-13 12:21:07: (critical) network_mysqld_proto_password_scramble: assertion `20 == challenge_len‘ failed
2014-11-13 12:21:07: (warning) (libevent) event_del: event has no event_base set.
2014-11-13 12:21:07: (critical)
可以临时增加MySQL connection数量:
echo -n “Max processes=SOFT_LIMIT:HARD_LIMIT” > /proc/`pidof mysqld`/limits
- 关于Too many open files错误,可能由两种情况引起:
一、php长连接连接到atlas后,每个线程占用一个FD,直到超出系统资源限制而出现too many错误;
二、php应用端发送到atlas的sql过多,大量并发的情况下,linevent维护的队列过多,每个event吃一个FD,超出系统资源限制引起too many错误;
避免too many错误,增加用户的ulimit值加大FD的使用量,可增加系统ulimit 资源到 ~/.bash_profile文件或/etc/security/limits.conf文件:
# cat .bash_profile
# .bash_profile
...
...
export PATH
ulimit -n 16384