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

Top

NSD DBA DAY08

  1. 案例1:部署mycat服务
  2. 案例2:测试配置

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

  1. //启用binlog日志
  2. root@mysql59 \~# yum --y install mysql-server mysql
  3. root@mysql59 \~# systemctl start mysqld
  4. root@mysql59 \~# vim /etc/my.cnf.d/mysql-server.cnf
  5. mysqld

  6. server-id=59
  7. log-bin=mysql59
  8. :wq
  9. root@mysql59 \~# systemctl restart mysqld
  10. //用户授权
  11. root@mysql59 \~# mysql
  12. mysql> create user repluser@"%" identified by "123qqq...A"; 创建用户
  13. Query OK, 0 rows affected (0.11 sec)
  14. mysql> grant replication slave on *.* to repluser@"%"; 授予权限
  15. Query OK, 0 rows affected (0.09 sec)
  16. //查看日志信息
  17. mysql> show master status;
  18. +----------------+----------+--------------+------------------+-------------------+
  19. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  20. +----------------+----------+--------------+------------------+-------------------+
  21. | mysql59.000001 | 667 | | | |
  22. +----------------+----------+--------------+------------------+-------------------+
  23. 1 row in set (0.00 sec)

2)配置slave服务器MySQL60

复制代码
  1. //指定server-id 并重启数据库服务
  2. root@mysql60 \~# yum --y install mysql-serv er mysql
  3. root@mysql60 \~# systemctl start mysqld
  4. root@mysql60 \~# vim /etc/my.cnf.d/mysql-server.cnf
  5. mysqld

  6. server-id=60
  7. :wq
  8. root@mysql60 \~# systemctl restart mysqld
  9. //登陆服务指定主服务器信息
  10. root@mysql60 \~# mysql
  11. 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;
  12. Query OK, 0 rows affected, 8 warnings (0.34 sec)
  13. //启动slave进程
  14. mysql> start slave ;
  15. Query OK, 0 rows affected, 1 warning (0.04 sec)
  16. //查看状态信息
  17. mysql> show slave status \G
  18. *************************** 1. row ***************************
  19. Slave_IO_State: Waiting for source to send event
  20. Master_Host: 192.168.88.59
  21. Master_User: repluser
  22. Master_Port: 3306
  23. Connect_Retry: 60
  24. Master_Log_File: mysql59.000001
  25. Read_Master_Log_Pos: 667
  26. Relay_Log_File: mysql60-relay-bin.000002
  27. Relay_Log_Pos: 322
  28. Relay_Master_Log_File: mysql59.000001
  29. Slave_IO_Running: Yes //IO线程
  30. Slave_SQL_Running: Yes //SQL线程
  31. Replicate_Do_DB:
  32. Replicate_Ignore_DB:
  33. Replicate_Do_Table:
  34. Replicate_Ignore_Table:
  35. Replicate_Wild_Do_Table:
  36. Replicate_Wild_Ignore_Table:
  37. Last_Errno: 0
  38. Last_Error:
  39. Skip_Counter: 0
  40. Exec_Master_Log_Pos: 667
  41. Relay_Log_Space: 533
  42. Until_Condition: None
  43. Until_Log_File:
  44. Until_Log_Pos: 0
  45. Master_SSL_Allowed: No
  46. Master_SSL_CA_File:
  47. Master_SSL_CA_Path:
  48. Master_SSL_Cert:
  49. Master_SSL_Cipher:
  50. Master_SSL_Key:
  51. Seconds_Behind_Master: 0
  52. Master_SSL_Verify_Server_Cert: No
  53. Last_IO_Errno: 0
  54. Last_IO_Error:
  55. Last_SQL_Errno: 0
  56. Last_SQL_Error:
  57. Replicate_Ignore_Server_Ids:
  58. Master_Server_Id: 59
  59. Master_UUID: 38c02165-005e-11ee-bd2d-525400007271
  60. Master_Info_File: mysql.slave_master_info
  61. SQL_Delay: 0
  62. SQL_Remaining_Delay: NULL
  63. Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
  64. Master_Retry_Count: 86400
  65. Master_Bind:
  66. Last_IO_Error_Timestamp:
  67. Last_SQL_Error_Timestamp:
  68. Master_SSL_Crl:
  69. Master_SSL_Crlpath:
  70. Retrieved_Gtid_Set:
  71. Executed_Gtid_Set:
  72. Auto_Position: 0
  73. Replicate_Rewrite_DB:
  74. Channel_Name:
  75. Master_TLS_Version:
  76. Master_public_key_path:
  77. Get_master_public_key: 0
  78. Network_Namespace:
  79. 1 row in set, 1 warning (0.00 sec)
  80. mysql>

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

1)配置主服务器MySQL61

复制代码
  1. //启用binlog日志
  2. root@mysql61 \~# yum --y install mysql-server mysql
  3. root@mysql61 \~# systemctl start mysqld
  4. root@mysql61 \~# vim /etc/my.cnf.d/mysql-server.cnf
  5. mysqld

  6. server-id=61
  7. log-bin=mysql61
  8. :wq
  9. root@mysql61 \~# systemctl restart mysqld
  10. //用户授权
  11. root@mysql61 \~# mysql
  12. mysql> create user repluser@"%" identified by "123qqq...A"; 创建用户
  13. Query OK, 0 rows affected (0.11 sec)
  14. mysql> grant replication slave on *.* to repluser@"%"; 授予权限
  15. Query OK, 0 rows affected (0.09 sec)
  16. //查看日志信息
  17. mysql> show master status;
  18. +----------------+----------+--------------+------------------+-------------------+
  19. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  20. +----------------+----------+--------------+------------------+-------------------+
  21. | mysql61.000001 | 667 | | | |
  22. +----------------+----------+--------------+------------------+-------------------+
  23. 1 row in set (0.00 sec)

2)配置slave服务器MySQL62

复制代码
  1. //指定server-id 并重启数据库服务
  2. root@mysql62 \~# yum --y install mysql-server mysql
  3. root@mysql62 \~# systemctl start mysqld
  4. root@mysql62 \~# vim /etc/my.cnf.d/mysql-server.cnf
  5. mysqld

  6. server-id=62
  7. :wq
  8. root@mysql62 \~# systemctl restart mysqld
  9. //登陆服务指定主服务器信息
  10. root@mysql62 \~# mysql
  11. 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;
  12. Query OK, 0 rows affected, 8 warnings (0.34 sec)
  13. //启动slave进程
  14. mysql> start slave ;
  15. Query OK, 0 rows affected, 1 warning (0.04 sec)
  16. //查看状态信息
  17. mysql> show slave status \G
  18. *************************** 1. row ***************************
  19. Slave_IO_State: Waiting for source to send event
  20. Master_Host: 192.168.88.61
  21. Master_User: repluser
  22. Master_Port: 3306
  23. Connect_Retry: 60
  24. Master_Log_File: mysql61.000001
  25. Read_Master_Log_Pos: 667
  26. Relay_Log_File: mysql62-relay-bin.000002
  27. Relay_Log_Pos: 322
  28. Relay_Master_Log_File: mysql61.000001
  29. Slave_IO_Running: Yes //IO线程
  30. Slave_SQL_Running: Yes //SQL线程
  31. Replicate_Do_DB:
  32. Replicate_Ignore_DB:
  33. Replicate_Do_Table:
  34. Replicate_Ignore_Table:
  35. Replicate_Wild_Do_Table:
  36. Replicate_Wild_Ignore_Table:
  37. Last_Errno: 0
  38. Last_Error:
  39. Skip_Counter: 0
  40. Exec_Master_Log_Pos: 667
  41. Relay_Log_Space: 533
  42. Until_Condition: None
  43. Until_Log_File:
  44. Until_Log_Pos: 0
  45. Master_SSL_Allowed: No
  46. Master_SSL_CA_File:
  47. Master_SSL_CA_Path:
  48. Master_SSL_Cert:
  49. Master_SSL_Cipher:
  50. Master_SSL_Key:
  51. Seconds_Behind_Master: 0
  52. Master_SSL_Verify_Server_Cert: No
  53. Last_IO_Errno: 0
  54. Last_IO_Error:
  55. Last_SQL_Errno: 0
  56. Last_SQL_Error:
  57. Replicate_Ignore_Server_Ids:
  58. Master_Server_Id: 61
  59. Master_UUID: 38c02165-005e-11ee-bd2d-525400007271
  60. Master_Info_File: mysql.slave_master_info
  61. SQL_Delay: 0
  62. SQL_Remaining_Delay: NULL
  63. Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
  64. Master_Retry_Count: 86400
  65. Master_Bind:
  66. Last_IO_Error_Timestamp:
  67. Last_SQL_Error_Timestamp:
  68. Master_SSL_Crl:
  69. Master_SSL_Crlpath:
  70. Retrieved_Gtid_Set:
  71. Executed_Gtid_Set:
  72. Auto_Position: 0
  73. Replicate_Rewrite_DB:
  74. Channel_Name:
  75. Master_TLS_Version:
  76. Master_public_key_path:
  77. Get_master_public_key: 0
  78. Network_Namespace:
  79. 1 row in set, 1 warning (0.00 sec)
  80. mysql>

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

1)拷贝软件到mycat63主机

复制代码
  1. root@server1 \~# scp /linux-soft/s3/mycat2-1.21-release-jar-with-dependencies.jar root@192.168.88.63:/root/
  2. root@server1 \~# scp /linux-soft/s3/mycat2-install-template-1.21.zip root@192.168.88.63:/root/

2)安装mycat软件

复制代码
  1. //安装jdk
  2. root@mycat63 \~# yum -y install java-1.8.0-openjdk.x86_64
  3. //安装mycat
  4. root@mycat63 \~# which unzip || yum -y install unzip
  5. root@mycat63 \~# unzip mycat2-install-template-1.21.zip
  6. root@mycat63 \~# mv mycat /usr/local/
  7. //安装依赖
  8. root@mycat63 \~# cp mycat2-1.21-release-jar-with-dependencies.jar /usr/local/mycat/lib/
  9. //修改权限
  10. root@mycat63 \~# chmod -R 777 /usr/local/mycat/

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

复制代码
  1. root@mycat63 \~# vim /usr/local/mycat/conf/users/root.user.json
  2. {
  3. "dialect":"mysql",
  4. "ip":null,
  5. "password":"654321",
  6. "transactionType":"proxy",
  7. "username":"mycat"
  8. }
  9. :wq
  1. 定义连接的数据库服务
复制代码
  1. root@mycat63 \~# vim /usr/local/mycat/conf/datasources/prototypeDs.data
  2. {
  3. "dbType":"mysql",
  4. "idleTimeout":60000,
  5. "initSqls":\[\],
  6. "initSqlsGetConnection":true,
  7. "instanceType":"READ_WRITE",
  8. "maxCon":1000,
  9. "maxConnectTimeout":3000,
  10. "maxRetryCount":5,
  11. "minCon":1,
  12. "name":"prototypeDs",
  13. "password":"123456", 密码
  14. "type":"JDBC",
  15. "url":"jdbc:mysql://localhost:3306/mysql?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8", 连接本机的数据库服务
  16. "user":"plj", 用户名
  17. "weight":0
  18. }
  19. :wq

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

复制代码
  1. root@mycat63 \~# yum -y install mysql-server mysql
  2. root@mycat63 \~# systemctl start mysqld
  3. //创建plj用户
  4. root@mycat63 \~# mysql
  5. mysql> create user plj@"%" identified by "123456"; 创建用户
  6. Query OK, 0 rows affected (0.05 sec)
  7. mysql> grant all on *.* to plj@"%" ; 授予权限
  8. Query OK, 0 rows affected (0.39 sec)
  9. mysql> exit
  10. Bye
  11. root@mycat63 \~#

6)启动mycat服务

复制代码
  1. root@mycat63 \~# /usr/local/mycat/bin/mycat help
  2. Usage: /usr/local/mycat/bin/mycat { console | start | stop | restart | status | dump }
  3. root@mycat63 \~# /usr/local/mycat/bin/mycat start
  4. Starting mycat2...
  5. //半分钟左右 能看到端口
  6. root@mycat63 \~# netstat -utnlp | grep 8066
  7. tcp6 0 0 :::8066 :::* LISTEN 57015/java
  8. root@mycat63 \~#

步骤四:连接mycat服务器

1)连接本机的mycat服务

复制代码
  1. root@mycat63 \~# mysql -h127.0.0.1 -P8066 -umycat -p654321
  2. mysql> show databases;
  3. +--------------------+
  4. | `Database` |
  5. +--------------------+
  6. | information_schema |
  7. | mysql |
  8. | performance_schema |
  9. +--------------------+
  10. 3 rows in set (0.11 sec)
  11. Mysql>

步骤五:添加数据源

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

复制代码
  1. root@mycat63 \~# mysql -h127.0.0.1 -P8066 -umycat -p654321
  2. //添加MySQL59
  3. MySQL>/*+ mycat:createdatasource{
  4. "name":"dw0", "url":"jdbc:mysql://192.168.88.59:3306","user":"plj","password":"123456"}*/;
  5. //添加MySQL60
  6. Mysql>/*+ mycat:createdatasource{
  7. "name":"dr0", "url":"jdbc:mysql://192.168.88.60:3306","user":"plj","password":"123456"}*/;
  8. //添加MySQL61
  9. Mysql>/*+ mycat:createdatasource{
  10. "name":"dw1", "url":"jdbc:mysql://192.168.88.61:3306","user":"plj","password":"123456"}*/;
  11. //添加MySQL62
  12. Mysql>/*+ mycat:createdatasource{
  13. "name":"dr1", "url":"jdbc:mysql://192.168.88.62:3306","user":"plj","password":"123456"}*/;
  14. Mysql>

2)查看存放目录

复制代码
  1. root@mycat63 \~# ls /usr/local/mycat/conf/datasources/
  2. dr0.datasource.json dr1.datasource.json dw0.datasource.json dw1.datasource.json prototypeDs.datasource.json

3)查看数据信息

复制代码
  1. mysql> /*+mycat:showDataSources{}*/ \G
  2. *************************** 1. row ***************************
  3. NAME: dw0
  4. USERNAME: plj
  5. PASSWORD: 123456
  6. MAX_CON: 1000
  7. MIN_CON: 1
  8. EXIST_CON: 0
  9. USE_CON: 0
  10. MAX_RETRY_COUNT: 5
  11. MAX_CONNECT_TIMEOUT: 30000
  12. DB_TYPE: mysql
  13. URL: jdbc:mysql://192.168.88.59:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
  14. WEIGHT: 0
  15. INIT_SQL:
  16. INIT_SQL_GET_CONNECTION: true
  17. INSTANCE_TYPE: READ_WRITE
  18. IDLE_TIMEOUT: 60000
  19. DRIVER: {
  20. CreateTime:"2023-05-08 16:10:26",
  21. ActiveCount:0,
  22. PoolingCount:0,
  23. CreateCount:0,
  24. DestroyCount:0,
  25. CloseCount:0,
  26. ConnectCount:0,
  27. Connections:[
  28. ]
  29. }
  30. TYPE: JDBC
  31. IS_MYSQL: true
  32. *************************** 2. row ***************************
  33. NAME: dw1
  34. USERNAME: plj
  35. PASSWORD: 123456
  36. MAX_CON: 1000
  37. MIN_CON: 1
  38. EXIST_CON: 0
  39. USE_CON: 0
  40. MAX_RETRY_COUNT: 5
  41. MAX_CONNECT_TIMEOUT: 30000
  42. DB_TYPE: mysql
  43. URL: jdbc:mysql://192.168.88.61:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
  44. WEIGHT: 0
  45. INIT_SQL:
  46. INIT_SQL_GET_CONNECTION: true
  47. INSTANCE_TYPE: READ_WRITE
  48. IDLE_TIMEOUT: 60000
  49. DRIVER: {
  50. CreateTime:"2023-05-08 16:10:26",
  51. ActiveCount:0,
  52. PoolingCount:0,
  53. CreateCount:0,
  54. DestroyCount:0,
  55. CloseCount:0,
  56. ConnectCount:0,
  57. Connections:[
  58. ]
  59. }
  60. TYPE: JDBC
  61. IS_MYSQL: true
  62. *************************** 3. row ***************************
  63. NAME: dr0
  64. USERNAME: plj
  65. PASSWORD: 123456
  66. MAX_CON: 1000
  67. MIN_CON: 1
  68. EXIST_CON: 0
  69. USE_CON: 0
  70. MAX_RETRY_COUNT: 5
  71. MAX_CONNECT_TIMEOUT: 30000
  72. DB_TYPE: mysql
  73. URL: jdbc:mysql://192.168.88.61:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
  74. WEIGHT: 0
  75. INIT_SQL:
  76. INIT_SQL_GET_CONNECTION: true
  77. INSTANCE_TYPE: READ_WRITE
  78. IDLE_TIMEOUT: 60000
  79. DRIVER: {
  80. CreateTime:"2023-05-08 16:10:26",
  81. ActiveCount:0,
  82. PoolingCount:0,
  83. CreateCount:0,
  84. DestroyCount:0,
  85. CloseCount:0,
  86. ConnectCount:0,
  87. Connections:[
  88. ]
  89. }
  90. TYPE: JDBC
  91. IS_MYSQL: true
  92. *************************** 4. row ***************************
  93. NAME: dr1
  94. USERNAME: plj
  95. PASSWORD: 123456
  96. MAX_CON: 1000
  97. MIN_CON: 1
  98. EXIST_CON: 0
  99. USE_CON: 0
  100. MAX_RETRY_COUNT: 5
  101. MAX_CONNECT_TIMEOUT: 30000
  102. DB_TYPE: mysql
  103. URL: jdbc:mysql://192.168.88.62:3306?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&autoReconnect=true
  104. WEIGHT: 0
  105. INIT_SQL:
  106. INIT_SQL_GET_CONNECTION: true
  107. INSTANCE_TYPE: READ_WRITE
  108. IDLE_TIMEOUT: 60000
  109. DRIVER: {
  110. CreateTime:"2023-05-08 16:10:26",
  111. ActiveCount:0,
  112. PoolingCount:0,
  113. CreateCount:0,
  114. DestroyCount:0,
  115. CloseCount:0,
  116. ConnectCount:0,
  117. Connections:[
  118. ]
  119. }
  120. TYPE: JDBC
  121. IS_MYSQL: true
  122. *************************** 5. row ***************************
  123. NAME: prototypeDs
  124. USERNAME: plj
  125. PASSWORD: 123456
  126. MAX_CON: 1000
  127. MIN_CON: 1
  128. EXIST_CON: 0
  129. USE_CON: 0
  130. MAX_RETRY_COUNT: 5
  131. MAX_CONNECT_TIMEOUT: 3000
  132. DB_TYPE: mysql
  133. URL: jdbc:mysql://localhost:3306/mysql?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
  134. WEIGHT: 0
  135. INIT_SQL:
  136. INIT_SQL_GET_CONNECTION: true
  137. INSTANCE_TYPE: READ_WRITE
  138. IDLE_TIMEOUT: 60000
  139. DRIVER: {
  140. CreateTime:"2023-05-08 16:10:26",
  141. ActiveCount:0,
  142. PoolingCount:0,
  143. CreateCount:0,
  144. DestroyCount:0,
  145. CloseCount:0,
  146. ConnectCount:0,
  147. Connections:[
  148. ]
  149. }
  150. TYPE: JDBC
  151. IS_MYSQL: true
  152. 5 rows in set (0.07 sec)
  153. mysql>

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

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

复制代码
  1. root@mysql59 \~# mysql
  2. mysql> create user plj@"%" identified by "123456";
  3. Mysql> grant all on *.* to plj@"%";
  4. root@mysql61 \~# mysql
  5. mysql> create user plj@"%" identified by "123456";
  6. Mysql> grant all on *.* to plj@"%";

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

复制代码
  1. root@mysql60 \~# mysql -e 'select user from mysql.user where user="plj"'
  2. +------+
  3. | user |
  4. +------+
  5. | plj |
  6. +------+
  7. root@mysql60 \~#
  8. root@mysql62 \~# mysql -e 'select user from mysql.user where user="plj"'
  9. +------+
  10. | user |
  11. +------+
  12. | plj |
  13. +------+
  14. root@host62 \~#

步骤七:创建集群

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

复制代码
  1. root@mycat63 \~# mysql -h127.0.0.1 -P8066 -umycat -p654321
  2. mysql> /*!mycat:createcluster{"name":"c0","masters":"dw0","replicas":"dr0"}*/;
  3. mysql> /*!mycat:createcluster{"name":"c1","masters":"dw1","replicas":"dr1"}*/;
  4. Mysql>

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

复制代码
  1. root@MySQL63 \~# ls /usr/local/mycat/conf/clusters/
  2. c0.cluster.json c1.cluster.json prototype.cluster.json
  3. root@mycat63 \~#

3)查看集群信息

复制代码
  1. mysql> /*+ mycat:showClusters{}*/ \G
  2. *************************** 1. row ***************************
  3. NAME: prototype
  4. SWITCH_TYPE: SWITCH
  5. MAX_REQUEST_COUNT: 200
  6. TYPE: BALANCE_ALL
  7. WRITE_DS: prototypeDs
  8. READ_DS: prototypeDs
  9. WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
  10. READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
  11. AVAILABLE: true
  12. *************************** 2. row ***************************
  13. NAME: c0
  14. SWITCH_TYPE: SWITCH
  15. MAX_REQUEST_COUNT: 2000
  16. TYPE: BALANCE_ALL
  17. WRITE_DS: dw0
  18. READ_DS: dw0,dr0
  19. WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
  20. READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
  21. AVAILABLE: true
  22. *************************** 3. row ***************************
  23. NAME: c1
  24. SWITCH_TYPE: SWITCH
  25. MAX_REQUEST_COUNT: 2000
  26. TYPE: BALANCE_ALL
  27. WRITE_DS: dw1
  28. READ_DS: dw1,dr1
  29. WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
  30. READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
  31. AVAILABLE: true
  32. 3 rows in set (0.03 sec)
  33. mysql>

2 案例2:测试配置

2.1 问题

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

2.2 方案

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

2.3 步骤

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

步骤一:练习全局表

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

复制代码
  1. //在mycat63 连接本机的mycat服务建库
  2. root@mycat63 \~# mysql -h127.0.0.1 -umycat -p654321 -P8066
  3. mysql> create database tarena;
  4. Query OK, 0 rows affected (0.31 sec)
  5. mysql> exit
  6. Bye
  7. root@mycat63 \~#

配置文件存放位置

复制代码
  1. root@mycat63 \~# ls /usr/local/mycat/conf/schemas/tarena.schema.json
  2. /usr/local/mycat/conf/schemas/tarena.schema.json
  3. root@mycat63 \~#

创建全局表

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

在4台数据库服务器查看

复制代码
  1. root@mysql59 \~# mysql -e 'select * from tarena.dept'
  2. +---------+-----------+
  3. | dept_id | dept_name |
  4. +---------+-----------+
  5. | 1 | 开发部 |
  6. | 2 | 运维部 |
  7. | 3 | 测试部 |
  8. +---------+-----------+
  9. root@host61 \~#
  10. root@mysql60 \~# mysql -e 'select * from tarena.dept'
  11. +---------+-----------+
  12. | dept_id | dept_name |
  13. +---------+-----------+
  14. | 1 | 开发部 |
  15. | 2 | 运维部 |
  16. | 3 | 测试部 |
  17. +---------+-----------+
  18. root@host62 \~#
  19. root@mysql61 \~# mysql -e 'select * from tarena.dept'
  20. +---------+-----------+
  21. | dept_id | dept_name |
  22. +---------+-----------+
  23. | 1 | 开发部 |
  24. | 2 | 运维部 |
  25. | 3 | 测试部 |
  26. +---------+-----------+
  27. root@host63 \~#
  28. root@mysql62 \~# mysql -e 'select * from tarena.dept'
  29. +---------+-----------+
  30. | dept_id | dept_name |
  31. +---------+-----------+
  32. | 1 | 开发部 |
  33. | 2 | 运维部 |
  34. | 3 | 测试部 |
  35. +---------+-----------+

步骤二:练习分片表

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

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

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

tbpartitions 1 表的分片数量

dbpartitions 2 库的分片数量

复制代码
  1. //连接mycat服务建表
  2. root@client50 \~# mysql -h192.168.88.63 -P8066 -umycat --p654321
  3. create table tarena.employees(
  4. employee_id int primary key,
  5. name char(10),dept_id int ,
  6. mail varchar(30)
  7. ) default charset utf8
  8. dbpartition BY mod_hash(employee_id) tbpartition BY mod_hash(employee_id)
  9. tbpartitions 1 dbpartitions 2;

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

复制代码
  1. root@mysql59 \~# mysql -e 'show databases'
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | mysql |
  7. | performance_schema |
  8. | sys |
  9. | tarena |
  10. | tarena_0 |
  11. +--------------------+
  12. root@mysql59 \~# mysql -e 'use tarena_0 ; show tables'
  13. +--------------------+
  14. | Tables_in_tarena_0 |
  15. +--------------------+
  16. | employees_0 |
  17. +--------------------+
  18. root@host61 \~#
  19. root@mysql60 \~# mysql -e 'show databases'
  20. +--------------------+
  21. | Database |
  22. +--------------------+
  23. | information_schema |
  24. | mysql |
  25. | performance_schema |
  26. | sys |
  27. | tarena |
  28. | tarena_0 |
  29. +--------------------+
  30. root@mysql60 \~# mysql -e 'use tarena_0 ; show tables'
  31. +--------------------+
  32. | Tables_in_tarena_0 |
  33. +--------------------+
  34. | employees_0 |
  35. +--------------------+
  36. root@host62 \~#
  37. root@mysql61 \~# mysql -e 'show databases'
  38. +--------------------+
  39. | Database |
  40. +--------------------+
  41. | information_schema |
  42. | mysql |
  43. | performance_schema |
  44. | sys |
  45. | tarena |
  46. | tarena_1 |
  47. +--------------------+
  48. root@mysql61 \~# mysql -e 'use tarena_1;show tables'
  49. +--------------------+
  50. | Tables_in_tarena_1 |
  51. +--------------------+
  52. | employees_1 |
  53. +--------------------+
  54. root@host63 \~#
  55. root@mysql62 \~# mysql -e 'show databases'
  56. +--------------------+
  57. | Database |
  58. +--------------------+
  59. | information_schema |
  60. | mysql |
  61. | performance_schema |
  62. | sys |
  63. | tarena |
  64. | tarena_1 |
  65. +--------------------+
  66. root@mysql62 \~# mysql -e 'use tarena_1;show tables'
  67. +--------------------+
  68. | Tables_in_tarena_1 |
  69. +--------------------+
  70. | employees_1 |
  71. +--------------------+
  72. root@host64 \~#

存储数据

复制代码
  1. root@client50 \~# mysql -h192.168.88.63 -P8066 -umycat --p654321
  2. mysql> insert into tarena.employees values (9,"a","1","a@163.com");
  3. Query OK, 1 row affected (0.08 sec)
  4. mysql> insert into tarena.employees values (8,"B","3","B@QQ.com");
  5. Query OK, 1 row affected (0.13 sec)
  6. mysql> insert into tarena.employees values (7,"C","2","c@QQ.com");
  7. Query OK, 1 row affected (0.02 sec)
  8. mysql> insert into tarena.employees values (6,"C","2","c@QQ.com");
  9. Query OK, 1 row affected (0.06 sec)
  10. mysql> select * from tarena.employees;
  11. +-------------+------+---------+-----------+
  12. | employee_id | name | dept_id | mail |
  13. +-------------+------+---------+-----------+
  14. | 6 | C | 2 | c@QQ.com |
  15. | 8 | B | 3 | B@QQ.com |
  16. | 7 | C | 2 | c@QQ.com |
  17. | 9 | a | 1 | a@163.com |
  18. +-------------+------+---------+-----------+
  19. 4 rows in set (2.07 sec)

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

复制代码
  1. root@mysql59 \~# mysql -e 'select * from tarena_0.employees_0'
  2. +-------------+------+---------+----------+
  3. | employee_id | name | dept_id | mail |
  4. +-------------+------+---------+----------+
  5. | 6 | C | 2 | c@QQ.com |
  6. | 8 | B | 3 | B@QQ.com |
  7. +-------------+------+---------+----------+
  8. root@mysql59 \~#
  9. root@mysql60 \~# mysql -e 'select * from tarena_0.employees_0'
  10. +-------------+------+---------+----------+
  11. | employee_id | name | dept_id | mail |
  12. +-------------+------+---------+----------+
  13. | 6 | C | 2 | c@QQ.com |
  14. | 8 | B | 3 | B@QQ.com |
  15. +-------------+------+---------+----------+
  16. root@mysql60 \~#
  17. root@mysql61 \~# mysql -e 'select * from tarena_1.employees_1'
  18. +-------------+------+---------+-----------+
  19. | employee_id | name | dept_id | mail |
  20. +-------------+------+---------+-----------+
  21. | 7 | C | 2 | c@QQ.com |
  22. | 9 | a | 1 | a@163.com |
  23. +-------------+------+---------+-----------+
  24. root@mysql61 \~#
  25. root@mysql62 \~# mysql -e 'select * from tarena_1.employees_1'
  26. +-------------+------+---------+-----------+
  27. | employee_id | name | dept_id | mail |
  28. +-------------+------+---------+-----------+
  29. | 7 | C | 2 | c@QQ.com |
  30. | 9 | a | 1 | a@163.com |
  31. +-------------+------+---------+-----------+
  32. root@mysql62 \~#

步骤三:练习ER表

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

1)连接mycat服务建表

复制代码
  1. root@client50 \~# mysql -h192.168.88.63 -P8066 -umycat --p654321
  2. mysql> create table tarena.salary(
  3. employee_id int primary key,
  4. p_date date , basic int , bonus int
  5. ) DEFAULT CHARSET=utf8
  6. dbpartition BY mod_hash(employee_id)
  7. tbpartition BY mod_hash(employee_id) tbpartitions 1;
  8. Query OK, 1 row affected (1.93 sec)

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

复制代码
  1. root@mycat63 \~# mysql -h127.0.0.1 -P8066 -umycat --p654321
  2. mysql> /*+ mycat:showErGroup{}*/ ;
  3. +---------+------------+-----------+
  4. | groupId | schemaName | tableName |
  5. +---------+------------+-----------+
  6. | 0 | tarena | employees |
  7. | 0 | tarena | salary |
  8. +---------+------------+-----------+
  9. 2 rows in set (0.00 sec)
  10. mysql>

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

复制代码
  1. root@mysql59 \~# mysql -e 'use tarena_0 ; show tables'
  2. +--------------------+
  3. | Tables_in_tarena_0 |
  4. +--------------------+
  5. | employees_0 |
  6. | salary_0 |
  7. +--------------------+
  8. root@mysql59 \~#
  9. root@mysql61 \~# mysql -e 'use tarena_1;show tables'
  10. +--------------------+
  11. | Tables_in_tarena_1 |
  12. +--------------------+
  13. | employees_1 |
  14. | salary_1 |
  15. +--------------------+
  16. root@mysql61\~#

4)插入数据

复制代码
  1. root@client50 \~# mysql -h192.168.88.63 -P8066 -umycat --p654321
  2. mysql> desc tarena.salary;
  3. +-------------+------+------+-----+---------+-------+
  4. | Field | Type | Null | Key | Default | Extra |
  5. +-------------+------+------+-----+---------+-------+
  6. | employee_id | int | NO | PRI | NULL | |
  7. | p_date | date | YES | | NULL | |
  8. | basic | int | YES | | NULL | |
  9. | bonus | int | YES | | NULL | |
  10. +-------------+------+------+-----+---------+-------+
  11. 4 rows in set (0.07 sec)
  12. mysql> insert into tarena.salary values(6,20230110,20000,2000);
  13. Query OK, 1 row affected (0.28 sec)
  14. mysql> insert into tarena.salary values(7,20230210,25000,2500);
  15. Query OK, 1 row affected (0.21 sec)
  16. mysql> insert into tarena.salary values(8,20230310,30000,3000);
  17. Query OK, 1 row affected (0.26 sec)
  18. mysql> insert into tarena.salary values(9,20230410,35000,3500);
  19. Query OK, 1 row affected (0.05 sec)
  20. mysql> select * from tarena.salary;
  21. +-------------+------------+-------+-------+
  22. | employee_id | p_date | basic | bonus |
  23. +-------------+------------+-------+-------+
  24. | 6 | 2023-01-10 | 20000 | 2000 |
  25. | 8 | 2023-03-10 | 30000 | 3000 |
  26. | 7 | 2023-02-10 | 25000 | 2500 |
  27. | 9 | 2023-04-10 | 35000 | 3500 |
  28. +-------------+------------+-------+-------+
  29. 4 rows in set (0.16 sec)
  30. mysql>

5)在4台数据库服务器本机查看

复制代码
  1. root@mysql59 \~# mysql -e 'select * from tarena_0.employees_0'
  2. +-------------+------+---------+----------+
  3. | employee_id | name | dept_id | mail |
  4. +-------------+------+---------+----------+
  5. | 6 | C | 2 | c@QQ.com |
  6. | 8 | B | 3 | B@QQ.com |
  7. +-------------+------+---------+----------+
  8. root@mysql59 \~#
  9. root@mysql60 \~# mysql -e 'select * from tarena_0.salary_0'
  10. +-------------+------------+-------+-------+
  11. | employee_id | p_date | basic | bonus |
  12. +-------------+------------+-------+-------+
  13. | 6 | 2023-01-10 | 20000 | 2000 |
  14. | 8 | 2023-03-10 | 30000 | 3000 |
  15. +-------------+------------+-------+-------+
  16. root@mysql60 \~#
  17. root@mysql61 \~# mysql -e 'select * from tarena_1.employees_1'
  18. +-------------+------+---------+-----------+
  19. | employee_id | name | dept_id | mail |
  20. +-------------+------+---------+-----------+
  21. | 7 | C | 2 | c@QQ.com |
  22. | 9 | a | 1 | a@163.com |
  23. +-------------+------+---------+-----------+
  24. root@mysql62 \~# mysql -e 'select * from tarena_1.salary_1'
  25. +-------------+------------+-------+-------+
  26. | employee_id | p_date | basic | bonus |
  27. +-------------+------------+-------+-------+
  28. | 7 | 2023-02-10 | 25000 | 2500 |
  29. | 9 | 2023-04-10 | 35000 | 3500 |
  30. +-------------+------------+-------+-------+
  31. root@mysql62\~#
相关推荐
千里马学框架3 小时前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台3 小时前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone4 小时前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc4 小时前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo6 小时前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077006 小时前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼7 小时前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone8 小时前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen8 小时前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone8 小时前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui