NSD DBA DAY08
1 案例1:部署mycat服务
1.1 问题
- 把主机mysql60 配置为 MySQL59 的从服务器
- 把主机mysql62 配置为 MySQL61 的从服务器
- 把主机mycat63 配置为mycat服务器
- 客户端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 \~\]# vim /etc/my.cnf.d/mysql-server.cnf
- server-id=59
- log-bin=mysql59
- :wq
-
root@mysql59 \~\]# systemctl restart mysqld
-
root@mysql59 \~\]# mysql
- 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 \~\]# vim /etc/my.cnf.d/mysql-server.cnf
- server-id=60
- :wq
-
root@mysql60 \~\]# systemctl restart mysqld
-
root@mysql60 \~\]# mysql
- 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 \~\]# vim /etc/my.cnf.d/mysql-server.cnf
- server-id=61
- log-bin=mysql61
- :wq
-
root@mysql61 \~\]# systemctl restart mysqld
-
root@mysql61 \~\]# mysql
- 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 \~\]# vim /etc/my.cnf.d/mysql-server.cnf
- server-id=62
- :wq
-
root@mysql62 \~\]# systemctl restart mysqld
-
root@mysql62 \~\]# mysql
- 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 [email protected]:/root/
2)安装mycat软件
- //安装jdk
-
root@mycat63 \~\]# yum -y install java-1.8.0-openjdk.x86_64
-
root@mycat63 \~\]# which unzip \|\| yum -y install unzip
-
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/
-
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
- //创建plj用户
-
root@mycat63 \~\]# mysql
- 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 \~\]#
-
root@mycat63 \~\]# /usr/local/mycat/bin/mycat help
-
root@mycat63 \~\]# /usr/local/mycat/bin/mycat start
- //半分钟左右 能看到端口
-
root@mycat63 \~\]# netstat -utnlp \| grep 8066
-
root@mycat63 \~\]#
1)连接本机的mycat服务
-
root@mycat63 \~\]# mysql -h127.0.0.1 -P8066 -umycat -p654321
- +--------------------+
- | `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
- 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/
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> grant all on *.* to plj@"%";
-
root@mysql61 \~\]# mysql
- 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":"c1","masters":["dw1"],"replicas":["dr1"]}*/;
- Mysql>
2)创建的集群保存在mycat安装目录下
-
root@MySQL63 \~\]# ls /usr/local/mycat/conf/clusters/
-
root@mycat63 \~\]#
- 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
- Query OK, 0 rows affected (0.31 sec)
- mysql> exit
- Bye
-
root@mycat63 \~\]#
-
root@mycat63 \~\]# ls /usr/local/mycat/conf/schemas/tarena.schema.json
-
root@mycat63 \~\]#
- //客户端client50 连接mycat63主机的 建表存储数据
-
root@client50 \~\]# mysql -h192.168.88.63 -umycat -p654321 -P8066
- 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 \~\]#
- +---------+-----------+
- | dept_id | dept_name |
- +---------+-----------+
- | 1 | 开发部 |
- | 2 | 运维部 |
- | 3 | 测试部 |
- +---------+-----------+
-
root@host62 \~\]#
- +---------+-----------+
- | dept_id | dept_name |
- +---------+-----------+
- | 1 | 开发部 |
- | 2 | 运维部 |
- | 3 | 测试部 |
- +---------+-----------+
-
root@host63 \~\]#
- +---------+-----------+
- | 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
- 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
- Query OK, 1 row affected (0.08 sec)
- mysql> insert into tarena.employees values (8,"B","3","[email protected]");
- Query OK, 1 row affected (0.13 sec)
- mysql> insert into tarena.employees values (7,"C","2","[email protected]");
- Query OK, 1 row affected (0.02 sec)
- mysql> insert into tarena.employees values (6,"C","2","[email protected]");
- Query OK, 1 row affected (0.06 sec)
- mysql> select * from tarena.employees;
- +-------------+------+---------+-----------+
- | employee_id | name | dept_id | mail |
- +-------------+------+---------+-----------+
- | 6 | C | 2 | [email protected] |
- | 8 | B | 3 | [email protected] |
- | 7 | C | 2 | [email protected] |
- | 9 | a | 1 | [email protected] |
- +-------------+------+---------+-----------+
- 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 | [email protected] |
- | 8 | B | 3 | [email protected] |
- +-------------+------+---------+----------+
-
root@mysql59 \~\]#
-
root@mysql60 \~\]# mysql -e 'select \* from tarena_0.employees_0'
- | employee_id | name | dept_id | mail |
- +-------------+------+---------+----------+
- | 6 | C | 2 | [email protected] |
- | 8 | B | 3 | [email protected] |
- +-------------+------+---------+----------+
-
root@mysql60 \~\]#
-
root@mysql61 \~\]# mysql -e 'select \* from tarena_1.employees_1'
- | employee_id | name | dept_id | mail |
- +-------------+------+---------+-----------+
- | 7 | C | 2 | [email protected] |
- | 9 | a | 1 | [email protected] |
- +-------------+------+---------+-----------+
-
root@mysql61 \~\]#
-
root@mysql62 \~\]# mysql -e 'select \* from tarena_1.employees_1'
- | employee_id | name | dept_id | mail |
- +-------------+------+---------+-----------+
- | 7 | C | 2 | [email protected] |
- | 9 | a | 1 | [email protected] |
- +-------------+------+---------+-----------+
-
root@mysql62 \~\]#
ER表,称为关联表,表示数据逻辑上有关联性的两个或多个表,例如工资表和员工表。对于关联表,通常希望他们能够有相同的分片规则,这样在进行关联查询时,能够快速定位到同一个数据分片中。MyCat2中对于关联表,不需要有过多的声明,他可以根据分片规则自行判断。
1)连接mycat服务建表
-
root@client50 \~\]# mysql -h192.168.88.63 -P8066 -umycat --p654321
- 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\~\]#
-
root@client50 \~\]# mysql -h192.168.88.63 -P8066 -umycat --p654321
- +-------------+------+------+-----+---------+-------+
- | 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 | [email protected] |
- | 8 | B | 3 | [email protected] |
- +-------------+------+---------+----------+
-
root@mysql59 \~\]#
- +-------------+------------+-------+-------+
- | 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 | [email protected] |
- | 9 | a | 1 | [email protected] |
- +-------------+------+---------+-----------+
-
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\~\]#