1.分表的配置
功能概述:
目前DBProxy线上使用的版本支持分表功能,分表策略目前为:hash。分表的配置可以在配置文件中进行配置,但是在0.2版本及其后的版本支持在admin端口对分表进行动态的配置。
使用方法:
配置文件中配置格式如下:
| tables=dbname.tablename.shardcolname.shardnum[,dbname.tablename.shardcolname.shardnum] ##dbname:数据库名称 tablename:分表的表名 shardcolname:分表键 shardnum:分表个数 tables = person.mt.id.3 |
注意:
1. 如果分表的表名为:tbl ,则子表的命名应为:tbl_idx,例如 tbl_0, tbl_1 ...
2. 如果分表个数为shardnum,则子表序号从0开始到shardnum-1终止
3. 目前只支持同一个库上的分表,且每个表上只支持一个分表键
4. 目前对分表上数据的操作有一些限制,需要特别注意
例如,在数据库名:DBProxy 中创建了两个表名:shard 的子表:shard_0, shard_1,分表个数为2,规定分表键为:a,如下所示。
| guest@127.0.0.1 :person03:40:18>show tables; +------------------+ | Tables_in_person | +------------------+ | mt_0 | | mt_1 | | mt_2 | +------------------+ 3 rows in set (0.00 sec) |
则需要再配置文件中进行配置如下
| tables = person.mt.id.3 |
登陆到admin 3309端进程查看
show tables; #查看系统配置的 分表信息 person.mt.id.3 是否生效
| [root@mycat_eye dbproxy_log]# mysql -uguest -pguest -P3309 -h127.0.0.1 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.0.99-agent-admin Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. guest@127.0.0.1 :(none)03:41:45>show tables; +--------+----------------+ | db | tables | +--------+----------------+ | person | person.mt.id.3 | +--------+----------------+ 1 row in set (0.00 sec) |
除了在配置文件中配置分表信息之外,也可以从Admin上进行分表信息的添加和删除,其语法如下:
add tables 'dbname.tbl.shardcolname.sharnum'; #增加一列分表信息:dbname.tbl.shardcolname.sharnum
remove tables like 'dbname.tbl'; #删除一列分表信息:dbname.tbl
配置完成之后,就可以连接Proxy端口进行数据库的访问了。
注意:删除分区表时,参数是 db.name.tbl 而非完整的分表信息。
通过save config; 命令,可以将Admin端口修改的分表信息保存到配置文件中。
2.手动建表
| [root@mycat_eye conf]# mysql -uguest -p -h127.0.0.1 -P3308 Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.35-33.0-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. guest@127.0.0.1 :(none)03:05:54>show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 14 rows in set (0.01 sec) guest@127.0.0.1 :(none)03:05:59>create database person; Query OK, 1 row affected (0.00 sec) guest@127.0.0.1 :(none)03:06:05>use person Database changed guest@127.0.0.1 :person03:23:10>create table mt_0( -> id int(8), -> addr varchar(20) -> ); Query OK, 0 rows affected (0.09 sec) guest@127.0.0.1 :person03:23:21>create table mt_1( -> id int(8), -> addr varchar(20) -> ); Query OK, 0 rows affected (0.04 sec) guest@127.0.0.1 :person03:23:28>create table mt_2( -> id int(8), -> addr varchar(20) -> ); Query OK, 0 rows affected (0.06 sec) guest@127.0.0.1 :person03:23:35>show tables; +------------------+ | Tables_in_person | +------------------+ | mt_0 | | mt_1 | | mt_2 | +------------------+ 4 rows in set (0.00 sec) guest@127.0.0.1 :person03:24:15>insert into mt(id,addr) values(1,'a'); Query OK, 1 row affected (0.00 sec) guest@127.0.0.1 :person03:24:29>insert into mt(id,addr) values(2,'b'); Query OK, 1 row affected (0.01 sec) guest@127.0.0.1 :person03:24:35>insert into mt(id,addr) values(3,'c'); Query OK, 1 row affected (0.01 sec) guest@127.0.0.1 :person03:24:39>insert into mt(id,addr) values(4,'c'); Query OK, 1 row affected (0.00 sec) guest@127.0.0.1 :person03:24:42>insert into mt(id,addr) values(5,'c'); Query OK, 1 row affected (0.01 sec) guest@127.0.0.1 :person03:24:45>insert into mt(id,addr) values(6,'3'); Query OK, 1 row affected (0.00 sec) guest@127.0.0.1 :person03:24:49>insert into mt(id,addr) values(7,'9'); Query OK, 1 row affected (0.00 sec) guest@127.0.0.1 :person03:24:54>insert into mt(id,addr) values(8,'u'); Query OK, 1 row affected (0.00 sec) guest@127.0.0.1 :person03:25:13>select * from mt_0; +------+------+ | id | addr | +------+------+ | 3 | c | | 6 | 3 | +------+------+ 2 rows in set (0.00 sec) guest@127.0.0.1 :person03:25:18>select * from mt_1; +------+------+ | id | addr | +------+------+ | 1 | a | | 4 | c | | 7 | 9 | +------+------+ 3 rows in set (0.00 sec) guest@127.0.0.1 :person03:25:20>select * from mt_2; +------+------+ | id | addr | +------+------+ | 2 | b | | 5 | c | | 8 | u | +------+------+ 3 rows in set (0.00 sec) |
这篇博客介绍了DBProxy的分表配置方法,包括在配置文件中的静态配置和通过admin端口的动态配置。详细阐述了分表的命名规则、分表键限制以及如何通过特定命令添加和删除分表信息。同时提到了手动建表的相关注意事项。
590

被折叠的 条评论
为什么被折叠?



