数据分片概述、环境准备、部署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

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=59log-bin=mysql59:wq[root@mysql59 ~]# systemctl  restart mysqld//用户授权[root@mysql59 ~]# mysqlmysql> 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 ~]# mysqlmysql> 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 eventMaster_Host: 192.168.88.59Master_User: repluserMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql59.000001Read_Master_Log_Pos: 667Relay_Log_File: mysql60-relay-bin.000002Relay_Log_Pos: 322Relay_Master_Log_File: mysql59.000001Slave_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: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 667Relay_Log_Space: 533Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 59Master_UUID: 38c02165-005e-11ee-bd2d-525400007271Master_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Replica has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0Network_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=61log-bin=mysql61:wq[root@mysql61 ~]# systemctl  restart mysqld//用户授权[root@mysql61 ~]# mysqlmysql> 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 ~]# mysqlmysql> 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 eventMaster_Host: 192.168.88.61Master_User: repluserMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql61.000001Read_Master_Log_Pos: 667Relay_Log_File: mysql62-relay-bin.000002Relay_Log_Pos: 322Relay_Master_Log_File: mysql61.000001Slave_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: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 667Relay_Log_Space: 533Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 61Master_UUID: 38c02165-005e-11ee-bd2d-525400007271Master_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Replica has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0Network_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 ~]# mysqlmysql> 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> exitBye[root@mycat63 ~]#

6)启动mycat服务

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

步骤四:连接mycat服务器

1)连接本机的mycat服务

    [root@mycat63 ~]# mysql -h127.0.0.1 -P8066 -umycat -p654321mysql> 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//添加MySQL59MySQL>/*+ mycat:createdatasource{"name":"dw0", "url":"jdbc:mysql://192.168.88.59:3306","user":"plj","password":"123456"}*/;//添加MySQL60Mysql>/*+ mycat:createdatasource{"name":"dr0", "url":"jdbc:mysql://192.168.88.60:3306","user":"plj","password":"123456"}*/;//添加MySQL61Mysql>/*+ mycat:createdatasource{"name":"dw1", "url":"jdbc:mysql://192.168.88.61:3306","user":"plj","password":"123456"}*/;//添加MySQL62Mysql>/*+ 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: dw0USERNAME: pljPASSWORD: 123456MAX_CON: 1000MIN_CON: 1EXIST_CON: 0USE_CON: 0MAX_RETRY_COUNT: 5MAX_CONNECT_TIMEOUT: 30000DB_TYPE: mysqlURL: jdbc:mysql://192.168.88.59:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=trueWEIGHT: 0INIT_SQL: INIT_SQL_GET_CONNECTION: trueINSTANCE_TYPE: READ_WRITEIDLE_TIMEOUT: 60000DRIVER: {CreateTime:"2023-05-08 16:10:26",ActiveCount:0,PoolingCount:0,CreateCount:0,DestroyCount:0,CloseCount:0,ConnectCount:0,Connections:[]}TYPE: JDBCIS_MYSQL: true*************************** 2. row ***************************NAME: dw1USERNAME: pljPASSWORD: 123456MAX_CON: 1000MIN_CON: 1EXIST_CON: 0USE_CON: 0MAX_RETRY_COUNT: 5MAX_CONNECT_TIMEOUT: 30000DB_TYPE: mysqlURL: jdbc:mysql://192.168.88.61:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=trueWEIGHT: 0INIT_SQL: INIT_SQL_GET_CONNECTION: trueINSTANCE_TYPE: READ_WRITEIDLE_TIMEOUT: 60000DRIVER: {CreateTime:"2023-05-08 16:10:26",ActiveCount:0,PoolingCount:0,CreateCount:0,DestroyCount:0,CloseCount:0,ConnectCount:0,Connections:[]}TYPE: JDBCIS_MYSQL: true*************************** 3. row ***************************NAME: dr0USERNAME: pljPASSWORD: 123456MAX_CON: 1000MIN_CON: 1EXIST_CON: 0USE_CON: 0MAX_RETRY_COUNT: 5MAX_CONNECT_TIMEOUT: 30000DB_TYPE: mysqlURL: jdbc:mysql://192.168.88.61:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=trueWEIGHT: 0INIT_SQL: INIT_SQL_GET_CONNECTION: trueINSTANCE_TYPE: READ_WRITEIDLE_TIMEOUT: 60000DRIVER: {CreateTime:"2023-05-08 16:10:26",ActiveCount:0,PoolingCount:0,CreateCount:0,DestroyCount:0,CloseCount:0,ConnectCount:0,Connections:[]}TYPE: JDBCIS_MYSQL: true*************************** 4. row ***************************NAME: dr1USERNAME: pljPASSWORD: 123456MAX_CON: 1000MIN_CON: 1EXIST_CON: 0USE_CON: 0MAX_RETRY_COUNT: 5MAX_CONNECT_TIMEOUT: 30000DB_TYPE: mysqlURL: jdbc:mysql://192.168.88.62:3306?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&autoReconnect=trueWEIGHT: 0INIT_SQL: INIT_SQL_GET_CONNECTION: trueINSTANCE_TYPE: READ_WRITEIDLE_TIMEOUT: 60000DRIVER: {CreateTime:"2023-05-08 16:10:26",ActiveCount:0,PoolingCount:0,CreateCount:0,DestroyCount:0,CloseCount:0,ConnectCount:0,Connections:[]}TYPE: JDBCIS_MYSQL: true*************************** 5. row ***************************NAME: prototypeDsUSERNAME: pljPASSWORD: 123456MAX_CON: 1000MIN_CON: 1EXIST_CON: 0USE_CON: 0MAX_RETRY_COUNT: 5MAX_CONNECT_TIMEOUT: 3000DB_TYPE: mysqlURL: jdbc:mysql://localhost:3306/mysql?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=trueWEIGHT: 0INIT_SQL: INIT_SQL_GET_CONNECTION: trueINSTANCE_TYPE: READ_WRITEIDLE_TIMEOUT: 60000DRIVER: {CreateTime:"2023-05-08 16:10:26",ActiveCount:0,PoolingCount:0,CreateCount:0,DestroyCount:0,CloseCount:0,ConnectCount:0,Connections:[]}TYPE: JDBCIS_MYSQL: true5 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 -p654321mysql> /*!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: prototypeSWITCH_TYPE: SWITCHMAX_REQUEST_COUNT: 200TYPE: BALANCE_ALLWRITE_DS: prototypeDsREAD_DS: prototypeDsWRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1READ_L: io.mycat.plug.loadBalance.BalanceRandom$1AVAILABLE: true*************************** 2. row ***************************NAME: c0SWITCH_TYPE: SWITCHMAX_REQUEST_COUNT: 2000TYPE: BALANCE_ALLWRITE_DS: dw0READ_DS: dw0,dr0WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1READ_L: io.mycat.plug.loadBalance.BalanceRandom$1AVAILABLE: true*************************** 3. row ***************************NAME: c1SWITCH_TYPE: SWITCHMAX_REQUEST_COUNT: 2000TYPE: BALANCE_ALLWRITE_DS: dw1READ_DS: dw1,dr1WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1READ_L: io.mycat.plug.loadBalance.BalanceRandom$1AVAILABLE: true3 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 -P8066mysql> create database tarena;Query OK, 0 rows affected (0.31 sec)mysql> exitBye[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 -P8066mysql> 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 –p654321create table tarena.employees(employee_id  int  primary key,name char(10),dept_id int , mail varchar(30)) default charset utf8dbpartition 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 –p654321mysql> 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 –p654321mysql> 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 –p654321mysql> /*+ 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台数据库服务器本机查看

    [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~]# 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/240444.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Docker中创建并配置MySQL、nginx、redis等容器

Docker中安装并配置MySQL、nginx、redis等 文章目录 Docker中安装并配置MySQL、nginx、redis等一、创建nginx容器①:拉取镜像②:运行nginx镜像③:从nginx容器中映射nginx配置文件到本地④:重启nginx并重新配置nginx的挂载 二、创建…

React全局状态管理

redux是一个状态管理框架,它可以帮助我们清晰定义state和处理函数,提高可读性,并且redux中的状态是全局共享,规避组件间通过props传递状态等操作。 快速使用 在React应用的根节点,需要借助React的Context机制存放整个…

新定义51单片机(RD8G37)实现测距测速仪

本文描述用新定义51单片机(RD8G37)超声波一体测距传感器实现简单的测距测速仪。 测距仪演示效果 新定义RD8G37Q48RJ开发板 超声波测距模块: 8位并口屏 1、main.c unsigned short timeConsuming0; unsigned int oldDistance;void rectClearS…

jeecgboot 前端bug or 后端 看图

无法显示文本 只能显示value 很恶心 如果用 varchar 就可以 不知道有没有别的方式 用int 解决 ,可能是我没有发现好的方法

Peter算法小课堂—并查集

我们先来看太戈编程467题 攀亲戚 题目描述: 最近你发现自己和古代一个皇帝长得很像:都有两个鼻子一个眼睛,你想知道这皇帝是不是你的远方亲戚,你是不是皇亲国戚。目前你能掌握的信息有m条,关于n个人:第i条…

手机崩溃日志的查找与分析

手机崩溃日志的查找与分析 摘要 本文介绍了一款名为克魔助手的iOS应用日志查看工具,该工具可以方便地查看iPhone设备上应用和系统运行时的实时日志和崩溃日志。同时还提供了崩溃日志的分析查看模块,可以对苹果崩溃日志进行符号化、格式化和分析&#x…

yolov5训练自己的数据

目录 1. 环境搭建2. 数据准备3. 数据标注4. 数据整理4.1 数据集切分4.2 修改数据文件4.3 修改模型文件 5. 训练模型5.1 训练5.2 验证5.3 测试 6. 训练结果分析 1. 环境搭建 安装anaconda、python、 cuda、 cudnn、 pytoch、 torchvision、 torchaudio等等。这里不详述 2. 数据…

软件测试大作业||测试计划+测试用例+性能用例+自动化用例+测试报告

xxx学院 2023—2024 学年度第二学期期末考试 《软件测试》(A)试题(开卷) 题目:以某一 web 系统为测试对象,完成以下文档的编写: (满分 100 分) (1&am…

量化研究员!你应该如何写一手好代码

即使是Quant Researcher, 写一手高质量的代码也是非常重要的。再好的思路,如果不能正确地实现,都是没有意义的。 写一手高质量的代码的意义,对Quant developer来讲就更是自不待言了。这篇笔记就介绍一些python best practice。 始…

QT第二周周三

题目&#xff1a;使用图片绘制出仪表盘 代码&#xff1a; widget.h #ifndef WIDGET_H #define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACEclass Widget : public QWidget {Q_OBJECTpublic:Widget(QWidget *paren…

申请开启|成为亚马逊云科技 Community Builder,共建云端社区!

在探索由技术打造的云端世界时&#xff0c;和同行者一起学习&#xff0c;与技术专家共同探讨是开发者成长的最佳助力&#xff01; 亚马逊云科技开发者社区 Community Builders 为技术爱好者和新兴思想领袖提供技术资源、学习和交流机会&#xff0c;帮助开发者探索、分享技术相关…

【车载HMI开发工具--EB GUIDE 与 Unity 合作提供一体化的沉浸式 HMI 设计开发工具链】【转载】

随着车载高性能计算平台的日益普及以及显示器尺寸和数量的不断增加&#xff0c;沉浸式车载人机交互界面&#xff08;HMI&#xff09;的需求也在持续增长。为了将实时 3D 技术带入车载 HMI 领域&#xff0c;Unity 与 Elektrobit (EB)展开了合作&#xff0c;EB 是推进 HMI 功能安…

CC工具箱使用指南:【添加字段(批量)】

一、简介 Arcgis中添加字段是常用的一个操作&#xff0c;软件中也自带有添加字段工具。 如果要给一个要素或表批量添加字段&#xff0c;可以用迭代器或批处理。 但如果理复杂一点&#xff0c;有多个GDB要素、表格&#xff0c;或者是SHP文件&#xff0c;需要给这个要素或表添…

Git将某个文件合并到指定分支

企业开发中&#xff0c;经常会单独拉分支去做自己的需求开发&#xff0c;但是某些时候一些公共的配置我们需要从主线pull&#xff0c;这时候整个分支merge显然不合适 1.切换至待合并文件的分支 git checkout <branch>2.将目标分支的单个文件合并到当前分支 git checkou…

.NET国产化改造探索(三)、银河麒麟安装.NET 8环境

随着时代的发展以及近年来信创工作和…废话就不多说了&#xff0c;这个系列就是为.NET遇到国产化需求的一个闭坑系列。接下来&#xff0c;看操作。 上一篇介绍了如何在银河麒麟操作系统上安装人大金仓数据库&#xff0c;这篇文章详细介绍下在银河麒麟操作系统上安装.NET8环境。…

最新 生成pdf文字和表格

生成pdf文字和表格 先看效果 介绍 java项目&#xff0c;使用apache的pdfbox工具&#xff0c;可分页&#xff0c;自定义列 依赖 <dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.22<…

第一讲_HarmonyOS应用开发环境准备

HarmonyOS应用开发环境准备 1. 知识储备2. 环境搭建2.1 安装node.js2.2 配置node.js2.3 安装命令行工具2.4 安装DevEco Studio2.5 配置DevEco Studio 1. 知识储备 HarmonyOS提供了一套UI开发框架&#xff0c;即方舟开发框架&#xff08;ArkUI框架&#xff09;。方舟开发框架可…

Flutter开发进阶之动画

Flutter开发进阶之动画 在Flutter中&#xff0c;动画是至关重要的一个部分&#xff0c;它能够为应用程序提供更加丰富和生动的用户体验&#xff0c;Flutter中的动画系统是UI框架的核心功能之一&#xff0c;也是开发者学习Flutter框架的重要部分&#xff0c;由于动画原理在所有…

架设一台NFS服务器,并按照以下要求配置

1、开放/nfs/shared目录&#xff0c;供所有用户查询资料 2、开放/nfs/upload目录&#xff0c;为192.168.xxx.0/24网段主机可以上传目录&#xff0c; 并将所有用户及所属的组映射为nfs-upload,其UID和GID均为210 3、将/home/tom目录仅共享给192.168.xxx.xxx这台主机&#xff0c;…

Linux系统:yum仓库

目录 一、yum 1、yum概述 2、yum仓库 3、yum实现过程原理 二、yum配置文件详解 1、主配置文件 2、yum仓库设置文件 3、yum日志文件 三、yum命令详解 1、查询 1.1 yum list [软件名] 1.2 yum info [软件名] 1.3 yum search <关键词> 1.4 yum provides <关…