数据分片概述、环境准备、部署MyCAT服务、全局表、分片表、ER表

这篇具有很好参考价值的文章主要介绍了数据分片概述、环境准备、部署MyCAT服务、全局表、分片表、ER表。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1 案例1:部署mycat服务

1.1 问题

  1. 把主机mysql60 配置为 MySQL59 的从服务器
  2. 把主机mysql62 配置为 MySQL61 的从服务器
  3. 把主机mycat63 配置为mycat服务器
  4. 客户端192.168.88.50访问mycat服务

1.2 方案

准备6台虚拟机,具体配置如表-1

数据分片概述、环境准备、部署MyCAT服务、全局表、分片表、ER表,服务器,运维,网络,mysql,数据库

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:把MySQL60配置为MySQL59的从服务器

1)配置主服务器MySQL59

    //启用binlog日志
    [root@mysql59 ~]# yum –y  install mysql-server  mysql
    [root@mysql59 ~]# systemctl start mysqld
    [root@mysql59 ~]# vim /etc/my.cnf.d/mysql-server.cnf
    [mysqld]
    server-id=59
    log-bin=mysql59
    :wq
    [root@mysql59 ~]# systemctl  restart mysqld
    //用户授权
    [root@mysql59 ~]# mysql
    mysql> create user repluser@"%" identified by "123qqq...A"; 创建用户
    Query OK, 0 rows affected (0.11 sec)
    mysql> grant replication slave on *.*  to repluser@"%"; 授予权限
    Query OK, 0 rows affected (0.09 sec)
    //查看日志信息
    mysql> show master status;
    +----------------+----------+--------------+------------------+-------------------+
    | File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +----------------+----------+--------------+------------------+-------------------+
    | mysql59.000001 |      667 |              |                  |                   |
    +----------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)

2)配置slave服务器MySQL60

    //指定server-id 并重启数据库服务
    [root@mysql60 ~]# yum –y  install mysql-serv    er  mysql
    [root@mysql60 ~]# systemctl start mysqld
    [root@mysql60 ~]# vim /etc/my.cnf.d/mysql-server.cnf
    [mysqld]
    server-id=60
    :wq
    [root@mysql60 ~]# systemctl  restart mysqld
    //登陆服务指定主服务器信息
    [root@mysql60 ~]# mysql
    mysql> change master to  master_host="192.168.88.59" , master_user="repluser" , master_password="123qqq...A" ,master_log_file="mysql59.000001" , master_log_pos=667;
    Query OK, 0 rows affected, 8 warnings (0.34 sec)
    //启动slave进程
    mysql> start slave ; 
    Query OK, 0 rows affected, 1 warning (0.04 sec)
    //查看状态信息
    mysql> show slave status \G  
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for source to send event
                      Master_Host: 192.168.88.59
                      Master_User: repluser
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql59.000001
              Read_Master_Log_Pos: 667
                   Relay_Log_File: mysql60-relay-bin.000002
                    Relay_Log_Pos: 322
            Relay_Master_Log_File: mysql59.000001
                 Slave_IO_Running: Yes   //IO线程
                Slave_SQL_Running: Yes   //SQL线程
                  Replicate_Do_DB: 
              Replicate_Ignore_DB: 
               Replicate_Do_Table: 
           Replicate_Ignore_Table: 
          Replicate_Wild_Do_Table: 
      Replicate_Wild_Ignore_Table: 
                       Last_Errno: 0
                       Last_Error: 
                     Skip_Counter: 0
              Exec_Master_Log_Pos: 667
                  Relay_Log_Space: 533
                  Until_Condition: None
                   Until_Log_File: 
                    Until_Log_Pos: 0
               Master_SSL_Allowed: No
               Master_SSL_CA_File: 
               Master_SSL_CA_Path: 
                  Master_SSL_Cert: 
                Master_SSL_Cipher: 
                   Master_SSL_Key: 
            Seconds_Behind_Master: 0
    Master_SSL_Verify_Server_Cert: No
                    Last_IO_Errno: 0
                    Last_IO_Error: 
                   Last_SQL_Errno: 0
                   Last_SQL_Error: 
      Replicate_Ignore_Server_Ids: 
                 Master_Server_Id: 59
                      Master_UUID: 38c02165-005e-11ee-bd2d-525400007271
                 Master_Info_File: mysql.slave_master_info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
               Master_Retry_Count: 86400
                      Master_Bind: 
          Last_IO_Error_Timestamp: 
         Last_SQL_Error_Timestamp: 
                   Master_SSL_Crl: 
               Master_SSL_Crlpath: 
               Retrieved_Gtid_Set: 
                Executed_Gtid_Set: 
                    Auto_Position: 0
             Replicate_Rewrite_DB: 
                     Channel_Name: 
               Master_TLS_Version: 
           Master_public_key_path: 
            Get_master_public_key: 0
                Network_Namespace: 
    1 row in set, 1 warning (0.00 sec)
    mysql>

步骤二:把MySQL62配置为MySQL61的从服务器

1)配置主服务器MySQL61

    //启用binlog日志
    [root@mysql61 ~]# yum –y  install mysql-server  mysql
    [root@mysql61 ~]# systemctl start mysqld
    [root@mysql61 ~]# vim /etc/my.cnf.d/mysql-server.cnf
    [mysqld]
    server-id=61
    log-bin=mysql61
    :wq
    [root@mysql61 ~]# systemctl  restart mysqld
    //用户授权
    [root@mysql61 ~]# mysql
    mysql> create user repluser@"%" identified by "123qqq...A"; 创建用户
    Query OK, 0 rows affected (0.11 sec)
    mysql> grant replication slave on *.*  to repluser@"%"; 授予权限
    Query OK, 0 rows affected (0.09 sec)
    //查看日志信息
    mysql> show master status;
    +----------------+----------+--------------+------------------+-------------------+
    | File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +----------------+----------+--------------+------------------+-------------------+
    | mysql61.000001 |      667 |              |                  |                   |
    +----------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)

2)配置slave服务器MySQL62

    //指定server-id 并重启数据库服务
    [root@mysql62 ~]# yum –y  install mysql-server  mysql
    [root@mysql62 ~]# systemctl start mysqld
    [root@mysql62 ~]# vim /etc/my.cnf.d/mysql-server.cnf
    [mysqld]
    server-id=62
    :wq
    [root@mysql62 ~]# systemctl  restart mysqld
    //登陆服务指定主服务器信息
    [root@mysql62 ~]# mysql
    mysql> change master to  master_host="192.168.88.61" , master_user="repluser" , master_password="123qqq...A" ,master_log_file="mysql61.000001" , master_log_pos=667;
    Query OK, 0 rows affected, 8 warnings (0.34 sec)
    //启动slave进程
    mysql> start slave ; 
    Query OK, 0 rows affected, 1 warning (0.04 sec)
    //查看状态信息
    mysql> show slave status \G  
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for source to send event
                      Master_Host: 192.168.88.61
                      Master_User: repluser
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql61.000001
              Read_Master_Log_Pos: 667
                   Relay_Log_File: mysql62-relay-bin.000002
                    Relay_Log_Pos: 322
            Relay_Master_Log_File: mysql61.000001
                 Slave_IO_Running: Yes   //IO线程
                Slave_SQL_Running: Yes   //SQL线程
                  Replicate_Do_DB: 
              Replicate_Ignore_DB: 
               Replicate_Do_Table: 
           Replicate_Ignore_Table: 
          Replicate_Wild_Do_Table: 
      Replicate_Wild_Ignore_Table: 
                       Last_Errno: 0
                       Last_Error: 
                     Skip_Counter: 0
              Exec_Master_Log_Pos: 667
                  Relay_Log_Space: 533
                  Until_Condition: None
                   Until_Log_File: 
                    Until_Log_Pos: 0
               Master_SSL_Allowed: No
               Master_SSL_CA_File: 
               Master_SSL_CA_Path: 
                  Master_SSL_Cert: 
                Master_SSL_Cipher: 
                   Master_SSL_Key: 
            Seconds_Behind_Master: 0
    Master_SSL_Verify_Server_Cert: No
                    Last_IO_Errno: 0
                    Last_IO_Error: 
                   Last_SQL_Errno: 0
                   Last_SQL_Error: 
      Replicate_Ignore_Server_Ids: 
                 Master_Server_Id: 61
                      Master_UUID: 38c02165-005e-11ee-bd2d-525400007271
                 Master_Info_File: mysql.slave_master_info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
               Master_Retry_Count: 86400
                      Master_Bind: 
          Last_IO_Error_Timestamp: 
         Last_SQL_Error_Timestamp: 
                   Master_SSL_Crl: 
               Master_SSL_Crlpath: 
               Retrieved_Gtid_Set: 
                Executed_Gtid_Set: 
                    Auto_Position: 0
             Replicate_Rewrite_DB: 
                     Channel_Name: 
               Master_TLS_Version: 
           Master_public_key_path: 
            Get_master_public_key: 0
                Network_Namespace: 
    1 row in set, 1 warning (0.00 sec)
    mysql>

步骤三:把主机mycat63配置为mycat服务器。

1)拷贝软件到mycat63主机

    [root@server1 ~]# scp /linux-soft/s3/mycat2-1.21-release-jar-with-dependencies.jar  root@192.168.88.63:/root/
    [root@server1 ~]# scp /linux-soft/s3/mycat2-install-template-1.21.zip  root@192.168.88.63:/root/

2)安装mycat软件

    //安装jdk
    [root@mycat63 ~]# yum -y install java-1.8.0-openjdk.x86_64
    //安装mycat
    [root@mycat63 ~]# which unzip || yum -y  install unzip
    [root@mycat63 ~]# unzip mycat2-install-template-1.21.zip
    [root@mycat63 ~]# mv mycat /usr/local/
    //安装依赖
    [root@mycat63 ~]# cp mycat2-1.21-release-jar-with-dependencies.jar  /usr/local/mycat/lib/
    //修改权限
    [root@mycat63 ~]# chmod -R 777 /usr/local/mycat/ 

3)定义客户端连接时使用的用户:

    [root@mycat63 ~]# vim  /usr/local/mycat/conf/users/root.user.json
    {
            "dialect":"mysql",
            "ip":null,
            "password":"654321",
            "transactionType":"proxy",
            "username":"mycat"
    }
    :wq

定义连接的数据库服务 

    [root@mycat63 ~]# vim  /usr/local/mycat/conf/datasources/prototypeDs.data
    {
            "dbType":"mysql",
            "idleTimeout":60000,
            "initSqls":[],
            "initSqlsGetConnection":true,
            "instanceType":"READ_WRITE",
            "maxCon":1000,
            "maxConnectTimeout":3000,
            "maxRetryCount":5,
            "minCon":1,
            "name":"prototypeDs",
            "password":"123456", 密码
            "type":"JDBC",
            "url":"jdbc:mysql://localhost:3306/mysql?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8", 连接本机的数据库服务
            "user":"plj", 用户名
            "weight":0
    }
    :wq

5)在mycat63主机运行数据库服务

    [root@mycat63 ~]# yum -y  install  mysql-server  mysql
    [root@mycat63 ~]# systemctl start mysqld
    //创建plj用户
    [root@mycat63 ~]# mysql
    mysql> create user plj@"%" identified by "123456"; 创建用户
    Query OK, 0 rows affected (0.05 sec)
    mysql> grant all on *.* to plj@"%" ; 授予权限
    Query OK, 0 rows affected (0.39 sec)
    mysql> exit
    Bye
    [root@mycat63 ~]#

6)启动mycat服务

    [root@mycat63 ~]# /usr/local/mycat/bin/mycat help
    Usage: /usr/local/mycat/bin/mycat { console | start | stop | restart | status | dump }
    [root@mycat63 ~]# /usr/local/mycat/bin/mycat start
    Starting mycat2...
    //半分钟左右 能看到端口
    [root@mycat63 ~]# netstat  -utnlp  | grep 8066
    tcp6       0      0 :::8066  :::*       LISTEN      57015/java          
    [root@mycat63 ~]#

步骤四:连接mycat服务器

1)连接本机的mycat服务

    [root@mycat63 ~]# mysql -h127.0.0.1 -P8066 -umycat -p654321
    mysql> show databases;
    +--------------------+
    | `Database`         |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    +--------------------+
    3 rows in set (0.11 sec)
    Mysql>

步骤五:添加数据源

1)连接本机的mycat服务,添加数据源

    [root@mycat63 ~]# mysql -h127.0.0.1 -P8066 -umycat -p654321
    //添加MySQL59
    MySQL>/*+ mycat:createdatasource{
    "name":"dw0", "url":"jdbc:mysql://192.168.88.59:3306","user":"plj","password":"123456"}*/;
    //添加MySQL60
    Mysql>/*+ mycat:createdatasource{
    "name":"dr0", "url":"jdbc:mysql://192.168.88.60:3306","user":"plj","password":"123456"}*/;
    //添加MySQL61
    Mysql>/*+ mycat:createdatasource{
    "name":"dw1", "url":"jdbc:mysql://192.168.88.61:3306","user":"plj","password":"123456"}*/;
    //添加MySQL62
    Mysql>/*+ mycat:createdatasource{
    "name":"dr1", "url":"jdbc:mysql://192.168.88.62:3306","user":"plj","password":"123456"}*/;
    Mysql>

 2)查看存放目录

    [root@mycat63 ~]# ls /usr/local/mycat/conf/datasources/
    dr0.datasource.json  dr1.datasource.json  dw0.datasource.json  dw1.datasource.json  prototypeDs.datasource.json

 3)查看数据信息

    mysql> /*+mycat:showDataSources{}*/ \G
    *************************** 1. row ***************************
                       NAME: dw0
                   USERNAME: plj
                   PASSWORD: 123456
                    MAX_CON: 1000
                    MIN_CON: 1
                  EXIST_CON: 0
                    USE_CON: 0
            MAX_RETRY_COUNT: 5
        MAX_CONNECT_TIMEOUT: 30000
                    DB_TYPE: mysql
                        URL: jdbc:mysql://192.168.88.59:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
                     WEIGHT: 0
                   INIT_SQL: 
    INIT_SQL_GET_CONNECTION: true
              INSTANCE_TYPE: READ_WRITE
               IDLE_TIMEOUT: 60000
                     DRIVER: {
            CreateTime:"2023-05-08 16:10:26",
            ActiveCount:0,
            PoolingCount:0,
            CreateCount:0,
            DestroyCount:0,
            CloseCount:0,
            ConnectCount:0,
            Connections:[
            ]
    }
                       TYPE: JDBC
                   IS_MYSQL: true
    *************************** 2. row ***************************
                       NAME: dw1
                   USERNAME: plj
                   PASSWORD: 123456
                    MAX_CON: 1000
                    MIN_CON: 1
                  EXIST_CON: 0
                    USE_CON: 0
            MAX_RETRY_COUNT: 5
        MAX_CONNECT_TIMEOUT: 30000
                    DB_TYPE: mysql
                        URL: jdbc:mysql://192.168.88.61:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
                     WEIGHT: 0
                   INIT_SQL: 
    INIT_SQL_GET_CONNECTION: true
              INSTANCE_TYPE: READ_WRITE
               IDLE_TIMEOUT: 60000
                     DRIVER: {
            CreateTime:"2023-05-08 16:10:26",
            ActiveCount:0,
            PoolingCount:0,
            CreateCount:0,
            DestroyCount:0,
            CloseCount:0,
            ConnectCount:0,
            Connections:[
            ]
    }
                       TYPE: JDBC
                   IS_MYSQL: true
    *************************** 3. row ***************************
                       NAME: dr0
                   USERNAME: plj
                   PASSWORD: 123456
                    MAX_CON: 1000
                    MIN_CON: 1
                  EXIST_CON: 0
                    USE_CON: 0
            MAX_RETRY_COUNT: 5
        MAX_CONNECT_TIMEOUT: 30000
                    DB_TYPE: mysql
                        URL: jdbc:mysql://192.168.88.61:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
                     WEIGHT: 0
                   INIT_SQL: 
    INIT_SQL_GET_CONNECTION: true
              INSTANCE_TYPE: READ_WRITE
               IDLE_TIMEOUT: 60000
                     DRIVER: {
            CreateTime:"2023-05-08 16:10:26",
            ActiveCount:0,
            PoolingCount:0,
            CreateCount:0,
            DestroyCount:0,
            CloseCount:0,
            ConnectCount:0,
            Connections:[
            ]
    }
                       TYPE: JDBC
                   IS_MYSQL: true
    *************************** 4. row ***************************
                       NAME: dr1
                   USERNAME: plj
                   PASSWORD: 123456
                    MAX_CON: 1000
                    MIN_CON: 1
                  EXIST_CON: 0
                    USE_CON: 0
            MAX_RETRY_COUNT: 5
        MAX_CONNECT_TIMEOUT: 30000
                    DB_TYPE: mysql
                        URL: jdbc:mysql://192.168.88.62:3306?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&autoReconnect=true
                     WEIGHT: 0
                   INIT_SQL: 
    INIT_SQL_GET_CONNECTION: true
              INSTANCE_TYPE: READ_WRITE
               IDLE_TIMEOUT: 60000
                     DRIVER: {
            CreateTime:"2023-05-08 16:10:26",
            ActiveCount:0,
            PoolingCount:0,
            CreateCount:0,
            DestroyCount:0,
            CloseCount:0,
            ConnectCount:0,
            Connections:[
            ]
    }
                       TYPE: JDBC
                   IS_MYSQL: true
    *************************** 5. row ***************************
                       NAME: prototypeDs
                   USERNAME: plj
                   PASSWORD: 123456
                    MAX_CON: 1000
                    MIN_CON: 1
                  EXIST_CON: 0
                    USE_CON: 0
            MAX_RETRY_COUNT: 5
        MAX_CONNECT_TIMEOUT: 3000
                    DB_TYPE: mysql
                        URL: jdbc:mysql://localhost:3306/mysql?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
                     WEIGHT: 0
                   INIT_SQL: 
    INIT_SQL_GET_CONNECTION: true
              INSTANCE_TYPE: READ_WRITE
               IDLE_TIMEOUT: 60000
                     DRIVER: {
            CreateTime:"2023-05-08 16:10:26",
            ActiveCount:0,
            PoolingCount:0,
            CreateCount:0,
            DestroyCount:0,
            CloseCount:0,
            ConnectCount:0,
            Connections:[
            ]
    }
                       TYPE: JDBC
                   IS_MYSQL: true
    5 rows in set (0.07 sec)
    mysql>

步骤六:配置数据库服务器

1)在主服务器添加plj用户

    [root@mysql59 ~]# mysql 
    mysql> create user plj@"%" identified by "123456";
    Mysql> grant all on *.*  to plj@"%";
    [root@mysql61 ~]# mysql 
    mysql> create user plj@"%" identified by "123456";
    Mysql> grant all on *.*  to plj@"%";

 2)在从服务器查看用户是否同步

    [root@mysql60 ~]# mysql -e 'select user from mysql.user where user="plj"'
    +------+
    | user |
    +------+
    | plj  |
    +------+
    [root@mysql60 ~]# 
    [root@mysql62 ~]# mysql -e 'select user from mysql.user where user="plj"'
    +------+
    | user |
    +------+
    | plj  |
    +------+
    [root@host62 ~]#

步骤七:创建集群

1)连接本机的mycat服务,创建集群

    [root@mycat63 ~]# mysql -h127.0.0.1 -P8066 -umycat -p654321
    mysql> /*!mycat:createcluster{"name":"c0","masters":["dw0"],"replicas":["dr0"]}*/;
    mysql> /*!mycat:createcluster{"name":"c1","masters":["dw1"],"replicas":["dr1"]}*/;
    Mysql>

2)创建的集群保存在mycat安装目录下

    [root@MySQL63 ~]# ls /usr/local/mycat/conf/clusters/
    c0.cluster.json  c1.cluster.json  prototype.cluster.json
    [root@mycat63 ~]#

3)查看集群信息

    mysql> /*+ mycat:showClusters{}*/ \G
    *************************** 1. row ***************************
                 NAME: prototype
          SWITCH_TYPE: SWITCH
    MAX_REQUEST_COUNT: 200
                 TYPE: BALANCE_ALL
             WRITE_DS: prototypeDs
              READ_DS: prototypeDs
              WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
               READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
            AVAILABLE: true
    *************************** 2. row ***************************
                 NAME: c0
          SWITCH_TYPE: SWITCH
    MAX_REQUEST_COUNT: 2000
                 TYPE: BALANCE_ALL
             WRITE_DS: dw0
              READ_DS: dw0,dr0
              WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
               READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
            AVAILABLE: true
    *************************** 3. row ***************************
                 NAME: c1
          SWITCH_TYPE: SWITCH
    MAX_REQUEST_COUNT: 2000
                 TYPE: BALANCE_ALL
             WRITE_DS: dw1
              READ_DS: dw1,dr1
              WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
               READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
            AVAILABLE: true
    3 rows in set (0.03 sec)
    mysql>

2 案例2:测试配置

2.1 问题

  • 练习全局表
  • 练习分片表
  • 练习ER表

2.2 方案

在客户端client50 连接mycat63 存储数据 ,验证mycat63的配置

2.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:练习全局表

全局表 数据会插入到两个库中,并且两个库中都有全部的数据。

    //在mycat63 连接本机的mycat服务建库
    [root@mycat63 ~]# mysql -h127.0.0.1 -umycat -p654321 -P8066
    mysql> create database tarena;
    Query OK, 0 rows affected (0.31 sec)
    mysql> exit
    Bye
    [root@mycat63 ~]# 

 配置文件存放位置

    [root@mycat63 ~]# ls /usr/local/mycat/conf/schemas/tarena.schema.json 
    /usr/local/mycat/conf/schemas/tarena.schema.json
    [root@mycat63 ~]# 

创建全局表

    //客户端client50 连接mycat63主机的 建表存储数据
    [root@client50 ~]# mysql -h192.168.88.63 -umycat -p654321 -P8066
    mysql> create table tarena.dept(dept_id int  , dept_name char(10),primary key(dept_id)) default charset utf8  broadcast;
    Query OK, 0 rows affected (4.46 sec)
    //插入记录
    mysql> insert into tarena.dept values(1,"开发部"),(2,"运维部"),(3,"测试部");
    Query OK, 1 row affected (0.23 sec)
    //查看记录
    mysql> select  * from tarena.dept;
    +---------+-----------+
    | dept_id | dept_name |
    +---------+-----------+
    |       1 | 开发部    |
    |       2 | 运维部    |
    |       3 | 测试部    |
    +---------+-----------+
    3 rows in set (0.33 sec)
    mysql> 

 在4台数据库服务器查看

    [root@mysql59 ~]# mysql -e 'select  * from tarena.dept'
    +---------+-----------+
    | dept_id | dept_name |
    +---------+-----------+
    |       1 | 开发部    |
    |       2 | 运维部    |
    |       3 | 测试部    |
    +---------+-----------+
    [root@host61 ~]# 
    [root@mysql60 ~]# mysql -e 'select  * from tarena.dept'
    +---------+-----------+
    | dept_id | dept_name |
    +---------+-----------+
    |       1 | 开发部    |
    |       2 | 运维部    |
    |       3 | 测试部    |
    +---------+-----------+
    [root@host62 ~]# 
    [root@mysql61 ~]# mysql -e 'select  * from tarena.dept'
    +---------+-----------+
    | dept_id | dept_name |
    +---------+-----------+
    |       1 | 开发部    |
    |       2 | 运维部    |
    |       3 | 测试部    |
    +---------+-----------+
    [root@host63 ~]# 
    [root@mysql62 ~]# mysql -e 'select  * from tarena.dept'
    +---------+-----------+
    | dept_id | dept_name |
    +---------+-----------+
    |       1 | 开发部    |
    |       2 | 运维部    |
    |       3 | 测试部    |
    +---------+-----------+

步骤二:练习分片表

dbpartition 定义分库使用的分片规则,

tbpartition 定义分表使用的分片规则。

mod_hash 分片规则,用employee_id表头的值做取模计算

tbpartitions 1 表的分片数量

dbpartitions 2 库的分片数量

    //连接mycat服务建表
    [root@client50 ~]# mysql -h192.168.88.63 -P8066 -umycat –p654321
    create table tarena.employees(
    employee_id  int  primary key,
    name char(10),dept_id int , 
    mail varchar(30)
    ) default charset utf8
    dbpartition BY mod_hash(employee_id) tbpartition BY mod_hash(employee_id) 
    tbpartitions 1 dbpartitions 2;

 在4台数据库服务器查看表

    [root@mysql59 ~]# mysql -e 'show databases'
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    | tarena             |
    | tarena_0           |
    +--------------------+
    [root@mysql59 ~]# mysql -e 'use tarena_0 ; show tables'
    +--------------------+
    | Tables_in_tarena_0 |
    +--------------------+
    | employees_0        |
    +--------------------+
    [root@host61 ~]# 
    [root@mysql60 ~]# mysql -e 'show databases'
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    | tarena             |
    | tarena_0           |
    +--------------------+
    [root@mysql60 ~]# mysql -e 'use tarena_0 ; show tables'
    +--------------------+
    | Tables_in_tarena_0 |
    +--------------------+
    | employees_0        |
    +--------------------+
    [root@host62 ~]# 
    [root@mysql61 ~]# mysql -e 'show databases'
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    | tarena             |
    | tarena_1           |
    +--------------------+
    [root@mysql61 ~]# mysql -e 'use tarena_1;show tables'
    +--------------------+
    | Tables_in_tarena_1 |
    +--------------------+
    | employees_1        |
    +--------------------+
    [root@host63 ~]# 
    [root@mysql62 ~]# mysql -e 'show databases'
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    | tarena             |
    | tarena_1           |
    +--------------------+
    [root@mysql62 ~]# mysql -e 'use tarena_1;show tables'
    +--------------------+
    | Tables_in_tarena_1 |
    +--------------------+
    | employees_1        |
    +--------------------+
    [root@host64 ~]#

存储数据

    [root@client50 ~]# mysql -h192.168.88.63 -P8066 -umycat –p654321
    mysql> insert into tarena.employees values (9,"a","1","a@163.com");
    Query OK, 1 row affected (0.08 sec)
    mysql> insert into tarena.employees values (8,"B","3","B@QQ.com");
    Query OK, 1 row affected (0.13 sec)
    mysql> insert into tarena.employees values (7,"C","2","c@QQ.com");
    Query OK, 1 row affected (0.02 sec)
    mysql> insert into tarena.employees values (6,"C","2","c@QQ.com");
    Query OK, 1 row affected (0.06 sec)
    mysql> select  * from tarena.employees;
    +-------------+------+---------+-----------+
    | employee_id | name | dept_id | mail      |
    +-------------+------+---------+-----------+
    |           6 | C    |       2 | c@QQ.com  |
    |           8 | B    |       3 | B@QQ.com  |
    |           7 | C    |       2 | c@QQ.com  |
    |           9 | a    |       1 | a@163.com |
    +-------------+------+---------+-----------+
    4 rows in set (2.07 sec)

 在数据库服务器本机查看数据

    [root@mysql59 ~]# mysql -e 'select  * from tarena_0.employees_0'
    +-------------+------+---------+----------+
    | employee_id | name | dept_id | mail     |
    +-------------+------+---------+----------+
    |           6 | C    |       2 | c@QQ.com |
    |           8 | B    |       3 | B@QQ.com |
    +-------------+------+---------+----------+
    [root@mysql59 ~]# 
    [root@mysql60 ~]# mysql -e 'select  * from tarena_0.employees_0'
    +-------------+------+---------+----------+
    | employee_id | name | dept_id | mail     |
    +-------------+------+---------+----------+
    |           6 | C    |       2 | c@QQ.com |
    |           8 | B    |       3 | B@QQ.com |
    +-------------+------+---------+----------+
    [root@mysql60 ~]# 
    [root@mysql61 ~]# mysql -e 'select * from tarena_1.employees_1'
    +-------------+------+---------+-----------+
    | employee_id | name | dept_id | mail      |
    +-------------+------+---------+-----------+
    |           7 | C    |       2 | c@QQ.com  |
    |           9 | a    |       1 | a@163.com |
    +-------------+------+---------+-----------+
    [root@mysql61 ~]# 
    [root@mysql62 ~]# mysql -e 'select * from tarena_1.employees_1'
    +-------------+------+---------+-----------+
    | employee_id | name | dept_id | mail      |
    +-------------+------+---------+-----------+
    |           7 | C    |       2 | c@QQ.com  |
    |           9 | a    |       1 | a@163.com |
    +-------------+------+---------+-----------+
    [root@mysql62 ~]#

步骤三:练习ER表

ER表,称为关联表,表示数据逻辑上有关联性的两个或多个表,例如工资表和员工表。对于关联表,通常希望他们能够有相同的分片规则,这样在进行关联查询时,能够快速定位到同一个数据分片中。MyCat2中对于关联表,不需要有过多的声明,他可以根据分片规则自行判断。

1)连接mycat服务建表

    [root@client50 ~]# mysql -h192.168.88.63 -P8066 -umycat –p654321
    mysql> create table tarena.salary(
    employee_id int primary key, 
    p_date date , basic int , bonus int 
    ) DEFAULT CHARSET=utf8 
    dbpartition BY mod_hash(employee_id) 
    tbpartition BY mod_hash(employee_id) tbpartitions 1;
    Query OK, 1 row affected (1.93 sec)

2)在MyCat2终端查看关联表关系。

    [root@mycat63 ~]# mysql -h127.0.0.1 -P8066 -umycat –p654321
    mysql> /*+ mycat:showErGroup{}*/ ;
    +---------+------------+-----------+
    | groupId | schemaName | tableName |
    +---------+------------+-----------+
    | 0       | tarena     | employees |
    | 0       | tarena     | salary    |
    +---------+------------+-----------+
    2 rows in set (0.00 sec)
    mysql> 

3)在2台主服务器查看表

    [root@mysql59 ~]# mysql -e 'use tarena_0 ; show tables'
    +--------------------+
    | Tables_in_tarena_0 |
    +--------------------+
    | employees_0        |
    | salary_0           |
    +--------------------+
    [root@mysql59 ~]# 
    [root@mysql61 ~]# mysql -e 'use tarena_1;show tables'
    +--------------------+
    | Tables_in_tarena_1 |
    +--------------------+
    | employees_1        |
    | salary_1           |
    +--------------------+
    [root@mysql61~]# 

 4)插入数据

    [root@client50 ~]# mysql -h192.168.88.63 -P8066 -umycat –p654321 
    mysql> desc tarena.salary;
    +-------------+------+------+-----+---------+-------+
    | Field       | Type | Null | Key | Default | Extra |
    +-------------+------+------+-----+---------+-------+
    | employee_id | int  | NO   | PRI | NULL    |       |
    | p_date      | date | YES  |     | NULL    |       |
    | basic       | int  | YES  |     | NULL    |       |
    | bonus       | int  | YES  |     | NULL    |       |
    +-------------+------+------+-----+---------+-------+
    4 rows in set (0.07 sec)
    mysql> insert into tarena.salary values(6,20230110,20000,2000);
    Query OK, 1 row affected (0.28 sec)
    mysql> insert into tarena.salary values(7,20230210,25000,2500);
    Query OK, 1 row affected (0.21 sec)
    mysql> insert into tarena.salary values(8,20230310,30000,3000);
    Query OK, 1 row affected (0.26 sec)
    mysql> insert into tarena.salary values(9,20230410,35000,3500);
    Query OK, 1 row affected (0.05 sec)
    mysql> select  * from tarena.salary;
    +-------------+------------+-------+-------+
    | employee_id | p_date     | basic | bonus |
    +-------------+------------+-------+-------+
    |           6 | 2023-01-10 | 20000 |  2000 |
    |           8 | 2023-03-10 | 30000 |  3000 |
    |           7 | 2023-02-10 | 25000 |  2500 |
    |           9 | 2023-04-10 | 35000 |  3500 |
    +-------------+------------+-------+-------+
    4 rows in set (0.16 sec)
    mysql> 

 5)在4台数据库服务器本机查看文章来源地址https://www.toymoban.com/news/detail-804477.html

    [root@mysql59 ~]# mysql -e 'select  * from tarena_0.employees_0'
    +-------------+------+---------+----------+
    | employee_id | name | dept_id | mail     |
    +-------------+------+---------+----------+
    |           6 | C    |       2 | c@QQ.com |
    |           8 | B    |       3 | B@QQ.com |
    +-------------+------+---------+----------+
    [root@mysql59 ~]#
    [root@mysql60 ~]# mysql -e 'select  * from tarena_0.salary_0'
    +-------------+------------+-------+-------+
    | employee_id | p_date     | basic | bonus |
    +-------------+------------+-------+-------+
    |           6 | 2023-01-10 | 20000 |  2000 |
    |           8 | 2023-03-10 | 30000 |  3000 |
    +-------------+------------+-------+-------+
    [root@mysql60 ~]# 
    [root@mysql61 ~]# mysql -e 'select * from tarena_1.employees_1'
    +-------------+------+---------+-----------+
    | employee_id | name | dept_id | mail      |
    +-------------+------+---------+-----------+
    |           7 | C    |       2 | c@QQ.com  |
    |           9 | a    |       1 | a@163.com |
    +-------------+------+---------+-----------+
    [root@mysql62 ~]# mysql -e 'select * from tarena_1.salary_1'
    +-------------+------------+-------+-------+
    | employee_id | p_date     | basic | bonus |
    +-------------+------------+-------+-------+
    |           7 | 2023-02-10 | 25000 |  2500 |
    |           9 | 2023-04-10 | 35000 |  3500 |
    +-------------+------------+-------+-------+
    [root@mysql62~]# 

到了这里,关于数据分片概述、环境准备、部署MyCAT服务、全局表、分片表、ER表的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 数据库概述、部署MySQL服务、必备命令 、密码管理、安装图形软件、SELECT语法 、筛选条件

    目录 1 案例1:构建MySQL服务器 1.1 问题 1.2 方案 1.3 步骤 2 案例2:密码管理 2.1 问题 2.2 步骤 3 案例3:安装图形软件 3.1 问题 3.2 方案 3.3 步骤 4 案例4:筛选条件 4.1 问题 4.2 方案 4.3 步骤 1.1 问题 在IP地址192.168.88.50主机和192.168.88.51主机上部署mysql服务 练习必备命令的使用 1.2 方案

    2024年02月19日
    浏览(51)
  • 数据库概述、部署MySQL服务、必备命令、密码管理、安装图形软件、SELECT语法 、筛选条件

    Top 案例1:构建MySQL服务器 案例2:密码管理 案例3:安装图形软件 案例4:筛选条件 1.1 问题 在IP地址192.168.88.50主机和192.168.88.51主机上部署mysql服务 练习必备命令的使用 1.2 方案 准备2台虚拟机,要求如下: 表-1   配置yum源、关闭selinux和防火墙,如果忘记了请自行补习前边课

    2024年02月12日
    浏览(61)
  • 微服务项目环境准备(个人留用)

    下载nacos 在config中找到mysql-schema.sql,并创建数据库 在config中找到application.properties文件,并修改对应的nacos中的数据库配置信息 使用单机模式启动nacos 登录nacos平台,单机模式无需使用密码 将本机需要开发的服务,注册到nacos上,配置内容需要提供,如果少了配置项,本地的

    2024年01月20日
    浏览(42)
  • 【手写数据库toadb】01 开发数据库内核准备阶段-开发环境准备

    ​ 专栏内容 : 手写数据库toadb 本专栏主要介绍如何从零开发,开发的步骤,以及开发过程中的涉及的原理,遇到的问题等,让大家能跟上并且可以一起开发,让每个需要的人成为参与者。 本专栏会定期更新,对应的代码也会定期更新,每个阶段的代码会打上tag,方便阶段学

    2024年01月22日
    浏览(48)
  • 【Linux系统基础】(5)在Linux上集群化环境前置准备及部署Zookeeper、Kafka软件详细教程

    在前面,我们所学习安装的软件,都是以单机模式运行的。 后续,我们将要学习大数据相关的软件部署,所以后续我们所安装的软件服务,大多数都是以集群化(多台服务器共同工作)模式运行的。 所以,在当前小节,我们需要完成集群化环境的前置准备,包括创建多台虚

    2024年02月03日
    浏览(44)
  • 大数据环境准备与配置说明文档

    大家好,我是邵奈一,一个不务正业的程序猿、正儿八经的斜杠青年。 1、世人称我为:被代码耽误的诗人、没天赋的书法家、五音不全的歌手、专业跑龙套演员、不合格的运动员… 2、这几年,我整理了很多IT技术相关的教程给大家,爱生活、爱分享。 3、如果您觉得文章有

    2024年02月22日
    浏览(42)
  • 分布式 - 服务器Nginx:一小时入门系列之Nginx环境准备

    1. Nginx 环境准备 虚拟机镜像,基于Centos7,网盘链接: https://pan.baidu.com/s/1NmCR-vdAcZLouRRn9V1yTA 密码: 1b60,虚拟机的用户名/密码:root/123456,使用的环境和应用放置在/home目录下: 目录 简介 说明 AdminLTE-3.2.0 静态web后台管理系统 静态web的配置演示 apps/ruoyi-admin.jar 若依后台管理系统

    2024年02月14日
    浏览(45)
  • redis集群和分片-Redis Cluster:分布式环境中的数据分片、主从复制和 Sentinel 哨兵

    当涉及到 Redis 中的集群、分片、主从复制和 Sentinel 哨兵时,这些是构建分布式 Redis 环境中非常重要的概念和组件。下面详细介绍这些概念以及它们在分布式环境中的作用。 Redis Cluster 是 Redis 官方提供的分布式解决方案,用于管理和维护多个 Redis 节点的分布式数据存储。R

    2024年02月13日
    浏览(61)
  • 【大数据】Doris 的集群规划和环境准备

    Doris 作为一款开源的 MPP 架构 OLAP 数据库,能够运行在绝大多数主流的商用服务器上。为了能够充分运用 MPP 架构的并发优势,以及 Doris 的高可用特性,我们建议 Doris 的部署遵循以下需求。 Linux 系统 版本 CentOS 7.1 7.1 7.1 及以上 Ubuntu 16.04 16.04 16.04 及以上 操作系统安装要求 ✅

    2024年02月03日
    浏览(49)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包