有两个dbgroup(数据库组), 每个dbgroup有一个master, sharding_test使用range的方
式, 以id作为shard key, 属于test数据库, dbgroup0属于范围0 - 999, dbgroup1 属于
范围 1000 - 1999
dbgroup0 有一主, 192.168.237.130:3308
dbgroup1 有一主, 192.168.237.131:3308
[mysql-proxy]
admin-username = user
admin-password = pwd
#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 192.168.237.128:3308
#proxy-read-only-backend-addresses = 192.168.237.130:3308@1,192.168.237.131:3308@1
daemon = true
keepalive = false
event-threads = 4
log-level = debug
log-path = /usr/local/mysql-proxy/log
sql-log = realtime
proxy-address = 0.0.0.0:1234
admin-address = 0.0.0.0:2345
charset = UTF8
wait-timeout = 3600
pwds = root:S4HJu78/H/6I/aYp2Xdb8Q==
[shardrule-0]
table = test3.sharding_test
type = range
shard-key = id
groups = 0:0-999,1:1000-1999
[group-0]
# master
proxy-backend-addresses=192.168.237.130:3308
# slave
#proxy-read-only-backend-addresses=127.0.0.1:3308
[group-1]
proxy-backend-addresses=192.168.237.131:3308
#proxy-read-only-backend-addresses=127.0.0.1:3310
运行Atlas
进入/usr/local/mysql-proxy/bin目录,执行下面的命令启动、重启或停止Atlas。
(1). sudo ./mysql-proxyd test start,启动Atlas。
(2). sudo ./mysql-proxyd test restart,重启Atlas。
(3). sudo ./mysql-proxyd test stop,停止Atlas
执行命令: mysql -h 127.0.0.1 -P 1234 -u 用户名 -p,如果能连上则证明Atlas初步
测试正常,可以再尝试发几条SQL语句看看执行结果是否正确。
Atlas sharding只对sql语句提供有限的支持, 目前支持基本的Select, insert/replace, delete, update语句, 支持全部的Where语法(SQL-92标准), 不支持DDL(create drop alter)以及一些管理语句, DDL请直连MYSQL执行, 请只在Atlas上执行Select, insert, delete, update(CRUD)语句
对于以下语句, 如果语句命中了多台dbgroup, Atlas均未做支持(如果语句只命中了一个dbgroup, 如select count(*) from test where id < 1000, 其中dbgroup0范围是0 - 1000, 那么这些特性都是支持的)
Limit Offset (支持Limit 同一个dbgroup)
Order by
Group by
Join
count, Max, Min等函数不支持
子查询在Sharding中可能会返回不正确的结果, 也请不要使用子查询. 请把语句拆分成多句执行
对于写操作, 如果写操作命中了多个数据库组, 由于部分成功(某个组执行失败)需要回滚的问题, 暂时不支持写操作命中多个数据组的语句.请拆分成多个sql语句执行
Atlas可能会在接下来的版本中对其中的一些特性中做出支持.
例用Atlas插入几条数据,做一下测试:
$ mysql -h127.0.0.1 -P1234 -uroot -pmysqltest -c
mysql> use test3;
Database changed
mysql> insert into sharding_test(id, name, age) values(1, 'test', 0);
Query OK, 1 row affected (0.00 sec)
mysql> insert into sharding_test(id, name, age) values(50, 'test', 0), (999, 'test', 0);
Query OK, 2 rows affected (0.00 sec)
以上几条数据都插入到了dbgroup0, 请注意第二条多值插入的语句, 因为50和999都命中了dbgroup0, 所以其执行成功, 但是如果执行以下的语句:
mysql> insert into sharding_test(id, name, age) values(100, 'test', 0), (1500, 'test',
0);
ERROR 1105 (HY000): Proxy Warning - write operation is only allow to one
dbgroup! 在sharding的表中, 这是不允许的, 因为id为100命中了dbgroup0, 而id为1500 命中了dbgroup1, 由于分布式的多值插入可能导致部分成功, 需要回滚, 这个Atlas暂不支持. update, delete, replace同理.
再插几条数据到dbgroup1:
mysql> insert into sharding_test(id, name, age) values(1000, 'test', 0), (1999,
'test', 0);
Query OK, 2 rows affected (0.00 sec)
mysql> select * from sharding_test;
+------+------+------+----------+----------+
| id | name | age | birthday | nickname |
+------+------+------+----------+----------+
| 1 | test | 0 | NULL | NULL |
| 50 | test | 0 | NULL | NULL |
| 999 | test | 0 | NULL | NULL |
| 1000 | test | 0 | NULL | NULL |
| 1999 | test | 0 | NULL | NULL |
mysql> select * from sharding_test where id>50;
+------+------+------+----------+----------+
| id | name | age | birthday | nickname |
+------+------+------+----------+----------+
| 999 | test | 0 | NULL | NULL |
| 1000 | test | 0 | NULL | NULL |
| 1999 | test | 0 | NULL | NULL |
#JOIN操作,不支持
mysql> select * from sharding_test a,test.temp b on a.>ERROR 1105 (sqlst): Proxy Warning - Sharing Hit Multi Dbgroup Not Support
SQL
#update操作
mysql> update sharding_test set;
ERROR 1105 (HY000): Proxy Warning - Syntax Forbidden!
mysql> update sharding_test set where id<2000;
ERROR 1105 (sqlst): Proxy Warning - write operation is only allow to one
dbgroup!
mysql> update sharding_test set where id<999;
Query OK, 2 rows affected (0.01 sec)
#delete操作
mysql> delete from sharding_test;
ERROR 1105 (HY000): Proxy Warning - Syntax Forbidden!
mysql> delete from sharding_test where id<2000;
ERROR 1105 (sqlst): Proxy Warning - write operation is only allow to one
dbgroup!
mysql> delete from sharding_test where id>1900;
Query OK, 1 row affected (0.01 sec)