十四、读写分离 Atlas架构
1、安装软件
rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
2、修改配置
cd /usr/local/mysql-proxy/
vim /usr/local/mysql-proxy/conf/test.cnf
[mysql-proxy]
admin-username = user
admin-password = pwd
proxy-backend-addresses = 10.0.0.55:3306
proxy-read-only-backend-addresses = 10.0.0.51:3306,10.0.0.52:3306
pwds = repl:3yb5jEku5h4=,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:33060
admin-address = 0.0.0.0:2345
charset=utf8
/usr/local/mysql-proxy/bin/encrypt 123 ---->制作加密密码
3、启动atlas
/usr/local/mysql-proxy/bin/mysql-proxyd test start
ps -ef |grep proxy
4、测试
测试读写分离:
读的测试
mysql -uroot -p123 -h10.0.0.53 -P33060
show variables like ‘server_id‘;
写操作测试:
设置两个从节点只读
set global read_only=1;
连接测试
mysql -umha -pmha -h10.0.0.53 -P33060
create database db1;
5、管理atlas
连接管理接口:
mysql -uuser -ppwd -h127.0.0.1 -P2345
打印帮助:
mysql> select * from help;查看帮助命令
select * from backends;查看读写节点
动态添加删除节点:
REMOVE BACKEND 3;
ADD SLAVE 10.0.0.53:3306;
SAVE CONFIG;(配置文件中自动去除已经被删除的或者添加该节点)
6、扩展——atlas 自动分表
school.stu 1000w
id name
stu_0 stu_1 stu_2 stu_3 stu_4
以上分表方式,存数据非常均匀,取数据不均与,因为要考虑业务需求
如果业务查询热点数据集中在id是1-200w这些数据,那么读取就不均匀
取模分表
n/5 取余数 (0,1,2,3,4)
(1)如果是 0 则分到 stu_0
(2)如果是 1 则分到 stu_1
(3)如果是 2 则分到 stu_2
(4)如果是 3 则分到 stu_3
(5)如果是 4 则分到 stu_4
取余数
配置文件
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;
select * from stu_01
或者 select * from stu where id=01