十四、讀寫分離 Atlas架構 1、安裝軟體 2、修改配置 3、啟動atlas 4、測試 5、管理atlas 6、擴展——atlas 自動分表 ...
十四、讀寫分離 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