第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目录下,下载非分表的安装包
(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目录下放的都是可执行文件
1. “encrypt”是用来生成MySQL密码加密的,在配置的时候会用到
2. “mysql-proxy”是MySQL自己的读写分离代理
3. “mysql-proxyd”是360弄出来的,后面有个“d”,服务的启动、重启、停止。都是用他来执行的
conf目录下放的是配置文件
1. “test.cnf”只有一个文件,用来配置代理的,可以使用vim来编辑
lib目录下放的是一些包,以及Atlas的依赖
log目录下放的是日志,如报错等错误信息的记录
我们可以把atlas加入到环境变量中
[root@db03 /usr/local/mysql-proxy]# vim /etc/profile
export PATH=/usr/local/mysql-proxy/bin:$PATH
source /etc/profile
配置过程
首先进入 bin目录,使用encrypt来对数据库的密码进行加密,
我的MySQL数据的用户名是old,密码是123456,我需要对密码进行加密
[root@localhost bin]# /usr/local/mysql-proxy/bin/encrypt 123456
/iZxz+0GRoA=
配置 Atlas配置文件,使用vim进行编辑
[root@localhost conf]#cd /usr/local/mysql-proxy/conf/
[root@localhost conf]# vim test01.cnf # 需要注意的是文件必须以.cnf结尾
在`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
[root@localhost bin]# /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>
测试读写分离
读:
[root@db03 ~]# mysql -umha -pmha -h10.0.0.53 -P33060
db03 [(none)]>select @@server_id;
写:
db03 [(none)]>begin;
db03 [(none)]>select @@server_id;
db03 [(none)]>commit;
Atlas中间件实现站点数据库关联
修改站点文件配置文件 将数据库关联到Atlas服务器上 (数据库迁移)
以phpcmsV9 为例
修改站点数据库配置文件
配置文件路径: /www/caches/configs/database.php
修改配置文件 vim /www/caches/configs/database.php

登陆网站查看是否成功
第2章 Atlas 的管理(在线)
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)
2.1 帮助查看
SELECT * FROM help
2.2 查看后端节点
db03 [(none)]>SELECT * FROM backends ;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.0.0.55:3306 | up | rw |
| 2 | 10.0.0.51:3306 | up | ro |
| 3 | 10.0.0.53:3306 | up | ro |
+-------------+----------------+-------+------+
3 rows in set (0.00 sec)
2.3 在线offline/online一个节点
db03 [(none)]>SET OFFLINE 3;
+-------------+----------------+---------+------+
| backend_ndx | address | state | type |
+-------------+----------------+---------+------+
| 3 | 10.0.0.53:3306 | offline | ro |
+-------------+----------------+---------+------+
1 row in set (0.00 sec)
db03 [(none)]>SELECT * FROM backends ;
+-------------+----------------+---------+------+
| backend_ndx | address | state | type |
+-------------+----------------+---------+------+
| 1 | 10.0.0.55:3306 | up | rw |
| 2 | 10.0.0.51:3306 | up | ro |
| 3 | 10.0.0.53:3306 | offline | ro |
+-------------+----------------+---------+------+
3 rows in set (0.00 sec)
db03 [(none)]>SET ONLINE 3;
+-------------+----------------+---------+------+
| backend_ndx | address | state | type |
+-------------+----------------+---------+------+
| 3 | 10.0.0.53:3306 | unknown | ro |
+-------------+----------------+---------+------+
1 row in set (0.00 sec)
2.4 添加/删除节点
REMOVE BACKEND 2; # 删除
REMOVE BACKEND 1;
ADD SLAVE 10.0.0.51:3306# 添加
ADD master 10.0.0.55:3306
2.5 开用户
分为两步
第一
MySQL主库建用户,授权
db02 [(none)]>grant all on *.* to root@'localhost' identified by '123';
第二
Atlas 添加用户
ADD PWD root:123
2.6 永久保存配置到文件
db03 [(none)]>save config;