自动分表
    使用Atlas的分表功能时,首先需要在配置文件test.cnf设置tables参数。tables参数设置格式:数据库名.表名.分表字段.子表数量,比如:你的数据库名叫school,表名叫stu,分表字段叫id,总共分为2张表,那么就写为school.stu.id.2,如果还有其他的分表,以逗号分隔即可。用户需要手动建立2张子表(stu_0,stu_1,注意子表序号是从0开始的)。所有的子表必须在DB的同一个database里。当通过Atlas执行(SELECT、DELETE、UPDATE、INSERT、REPLACE)操作时,Atlas会根据分表结果(id%2=k),定位到相应的子表(stu_k)。例如,执行select * from stu where id=3;,Atlas会自动从stu_1这张子表返回查询结果。但如果执行SQL语句(select * from stu;)时不带上id,则会提示执行stu 表不存在。
Atlas暂不支持自动建表和跨库分表的功能。
Atlas目前支持分表的语句有SELECT、DELETE、UPDATE、INSERT、REPLACE。

 

自动分表案例:
如果一张表将存贮1000w数据:
表:school.stu  
字段:id   name
手动创建5张表:stu_0   stu_1   stu_2   stu_3    stu_4
 

以上分表方式,1000w数据非常均匀存储,取数据不均匀,因为要考虑业务需求,如果业务查询热点数据集中在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

  1. 添加配置:tables = school.stu.id.5
    vim /usr/loca/mysql-proxy/conf/test.cnf 
    
    [mysql-proxy]
        .
        .
      省略部分
        .
        .
    tables = school.stu.id.5

     

  2. 重启Atlas:(主库中)手工创建,分表后的库和表,分别为定义的school  和 stu_0 stu_1 stu_2 stu_3 stu_4

    [root@db03 mysql-proxy]# /usr/local/mysql-proxy/bin/mysql-proxyd test restart   #重启Atlas
    OK: MySQL-Proxy of test is stopped
    OK: MySQL-Proxy of test is started
    
    [root@db03 mysql-proxy]# mysql -umha -pmha -h10.0.0.23 -P33060   #连接
    Warning: Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.0.81-log Source distribution
    
    Copyright (c) 2000, 2017, 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> create database school;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> use school;  #创建数据库和表
    Database changed
    mysql> create table stu_0 (id int,name varchar(20));
    Query OK, 0 rows affected (0.06 sec)
    
    mysql> create table stu_1 (id int,name varchar(20));
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> create table stu_2 (id int,name varchar(20));
    Query OK, 0 rows affected (0.72 sec)
    
    mysql> create table stu_3 (id int,name varchar(20));
    Query OK, 0 rows affected (0.04 sec)
    
    mysql> create table stu_4 (id int,name varchar(20));
    Query OK, 0 rows affected (0.06 sec)

     

  3. 插入数据测试

    mysql> use school;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> insert into stu values (3,'wang5');
    Query OK, 1 row affected (0.09 sec)
    
    mysql> 
    mysql> insert into stu values (2,'li4');
    Query OK, 1 row affected (0.01 sec)
    
    mysql> 
    mysql> insert into stu values (1,'zhang3');
    Query OK, 1 row affected (0.01 sec)
    
    mysql> 
    mysql> insert into stu values (4,'m6');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> 
    mysql> insert into stu values (5,'zou7');
    Query OK, 1 row affected (0.01 sec)
    
    mysql> commit;
    Query OK, 0 rows affected (0.00 sec)

     

  4. 查看数据

    mysql> select * from stu;
    ERROR 1146 (42S02): Table 'school.stu' doesn't exist
    mysql> select * from stu where id =1;
    +------+--------+
    | id   | name   |
    +------+--------+
    |    1 | zhang3 |
    +------+--------+
    1 row in set (0.00 sec)
    
    mysql> select * from stu_0;
    +------+------+
    | id   | name |
    +------+------+
    |    5 | zou7 |
    +------+------+
    1 row in set (0.00 sec)
    
    mysql> select * from stu_1;
    +------+--------+
    | id   | name   |
    +------+--------+
    |    1 | zhang3 |
    +------+--------+
    1 row in set (0.00 sec)
    
    mysql> select * from stu_2;
    +------+------+
    | id   | name |
    +------+------+
    |    2 | li4  |
    +------+------+
    1 row in set (0.00 sec)
    
    mysql> select * from stu_3;
    +------+-------+
    | id   | name  |
    +------+-------+
    |    3 | wang5 |
    +------+-------+
    1 row in set (0.00 sec)
    
    mysql> select * from stu_4;
    +------+------+
    | id   | name |
    +------+------+
    |    4 | m6   |
    +------+------+
    1 row in set (0.00 sec)
    

    ## 当select * from stu; 时报错stu不存在,必须带上where 条件才可以。数据也是均匀分布在每个分表中。

 

IP过滤及SQL黑名单

该参数用来实现IP过滤功能。在传统的开发模式中,应用程序直接连接DB,因此DB会对部署应用的机器(比如web服务器)的IP作访问授权。在引入中间层后,因为连接DB的是Atlas,所以DB改为对部署Atlas的机器的IP作访问授权,如果任意一台客户端都可以连接Atlas,就会带来潜在的风险。client-ips参数用来控制连接Atlas的客户端的IP,可以是精确IP,也可以是IP段,以逗号分隔写在一行上即可。
如:
client-ips=192.168.1.2, 192.168.2,这就代表192.168.1.2这个IP和192.168.2.*这个段的IP可以连接Atlas,其他IP均不能连接。
如果该参数不设置,则任意IP均可连接Atlas。
如果设置了client-ips参数,且Atlas前面挂有LVS,则必须设置lvs-ips参数,否则可以不设置lvs-ips。

 

SQL语句黑白名单
Atlas会屏蔽不带where条件的delete和update操作,以及sleep函数。

最后修改于 2019-09-05 15:29:41
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇