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

news2024/11/25 22:56:30

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

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

实验记录——TT

8月份的小小实验 安装虚拟环境下载相关软件功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants 创建一个自定义列表如何创建一个注脚注释也…

Flask Web开发实战(狼书)| 笔记第1、2章

前言 2023-8-11 以前对网站开发萌生了想法,又有些急于求成,在B站照着视频敲了一个基于flask的博客系统。但对于程序的代码难免有些囫囵吞枣,存在许多模糊或不太理解的地方,只会照葫芦画瓢。 而当自己想开发一个什么网站的时&…

关于 Windows 11 的云端备份

一、Windows 云端备份不是Windows 11专享 Windows 11即将正式推出一项云端备份功能,它的作用是为您备份几乎所有内容 - 不仅仅是您的文件和文件夹,还包括应用程序、系统设置、登录详细信息等,以便您可以立即启动并运行新电脑。这些详细信息与…

【Vue2】动态组件的使用-切换组件和keep-alive,以及异步组件

目录 想实现切换不同列表展示不同数据方法一方法二,动态组件动态组件的父组件如何传的值动态组件的子组件如何接受传的值 认识keep-alive缓存组件的生命周期 异步组件,单独打包,实现webpack分包,如何对组件进行单独打包&#xff0…

实习笔记(一)

自定义注解: 自定义注解中有三个元注解Target,Retention,Document /*** 系统日志注解** author Mark sunlightcsgmail.com*/ Target(ElementType.METHOD) Retention(RetentionPolicy.RUNTIME) Documented public interface SysLog {String value() default "…

react-router-dom6 路由不合法

1、昨天用react-router-dom6搭建了个项目,简直了,一个bug直接给我应用干废了! 2、我试了试不合法的url是如何处理的,随意输入了个反斜杠 http://localhost:8089/#/\ 3、结果应用搞岔气了, 意思是我的url不合法。 这没问…

Qt creator之对齐参考线——新增可视化缩进功能

Qt creator随着官方越来越重视,更新频率也在不断加快,今天无意中发现qt creator新版有了对齐参考线,也称可视化缩进Visualize Indent,默认为启用状态。 下图为旧版Qt Creator显示设置栏: 下图为新版本Qt Creator显示设…

ctfshow—萌新—web1

0x00 前言 CTF 加解密合集CTF Web合集 0x01 题目 0x02 Write Up 解法1 标准的数字型注入 查列名 http://cc3ecc3f-8c42-4624-979e-277a51ea85d2.challenge.ctf.show/?id1%20union%20select%20table_name,column_name,3%20from%20information_schema.columns%20where%20t…

Linux—LVM基础

Linux—LVM 一、什么是LVM二、LVM名词解释三、LVM的写入模式四、LVM的工作原理五、LVM的优缺点六、创建PV/VG/LV的方法补充说明 一、什么是LVM LVM(Logical Volume Manager),即逻辑卷管理,是Linux环境下对磁盘分区进行管理的一种…

校园宿舍管理系统java学生楼层宿管 jsp源代码

本项目为前几天收费帮学妹做的一个项目,Java EE JSP项目,在工作环境中基本使用不到,但是很多学校把这个当作编程入门的项目来做,故分享出本项目供初学者参考。 一、项目描述 校园宿舍管理系统 系统有1权限:管理员 二…

代码随想录算法训练营第三十六天|背包理论基础,LeetCode 416

目录 背包理论基础 动态规划五步曲: 1.确定dp[i][j]的含义 2.找出递推公式 3.初始化dp数组 4.确定遍历顺序 5.举例dp数组 背包理论基础II 动态规划五步曲: 1.确定dp[j]含义 2.找出递推公式 3.初始化dp数组 4.确定遍历顺序 5.举例dp数组 L…

[MAUI]在.NET MAUI中实现可拖拽排序列表

.NET MAUI 中提供了拖放(drag-drop)手势识别器,允许用户通过拖动手势来移动控件。在这篇文章中,我们将学习如何使用拖放手势识别器来实现可拖拽排序列表。在本例中,列表中显示不同大小的磁贴(Tile)并且可以拖拽排序。 …

【JavaEE基础学习打卡02】是时候了解Java EE了!

目录 前言一、为什么要学习Java EE二、Java EE规范介绍1.什么是规范?2.什么是Java EE规范?3.Java EE版本 三、Java EE应用程序模型1.模型前置说明2.模型具体说明 总结 前言 📜 本系列教程适用于 Java Web 初学者、爱好者,小白白。…

断点续传的未来发展趋势与前景展望

断点续传是一种在网络传输中断后,能够从中断的位置继续传输的技术。它可以有效地避免因为网络不稳定、服务器故障、用户操作等原因导致的传输失败,节省了用户的时间和流量,提高了传输的效率和可靠性。断点续传在很多场景中都有广泛的应用&…

DEWDROP65 DM蓝牙5.2双模热插拔PCB

键盘使用说明索引(均为出厂默认值) 软件支持(驱动的详细使用帮助)一些常见问题解答(FAQ)首次使用步骤蓝牙配对规则(重要)蓝牙和USB切换键盘默认层默认触发层0的FN键配置的功能默认功…

【Linux】Shell脚本之流程控制语句 if判断、for循环、while循环、case循环判断 + 实战详解[⭐建议收藏!!⭐]

👨‍🎓博主简介 🏅云计算领域优质创作者   🏅华为云开发者社区专家博主   🏅阿里云开发者社区专家博主 💊交流社区:运维交流社区 欢迎大家的加入! 🐋 希望大家多多支…

什么是BP反向传播算法

什么是BP反向传播算法 神经网络是个好工具,但就像有的刀削铁如泥,有的却只能拿来切豆腐。 真正决定神经网络好不好用的是神经元之间连接的权重和神经元的阈值。 如何确定这些数字,大部分时间我们都在使用反向传播,也就是常说的B…

nginx 基础

巩固基础,砥砺前行 。 只有不断重复,才能做到超越自己。 能坚持把简单的事情做到极致,也是不容易的。 nginx简易 #配置负载均衡 upstream myaaa {server localhost:8089;server localhost:8099;}server {listen 8085;server_name lo…

WebRTC | SDP详解

目录 一、SDP标准规范 1. SDP结构 2. SDP内容及type类型 二、WebRTC中的SDP结构 1. 媒体信息描述 (1)SDP中媒体信息格式 i. “artpmap”属性 ii. “afmtp”属性 (2)SSRC与CNAME (3)举个例子 &…

JVM---理解jvm之对象已死怎么判断?

目录 引用计数算法 什么是引用 可达性分析算法(用的最多的) 引用计数算法 定义:在对象中添加一个引用计数器,每当有一个地方引用它时,计数器值就加一;当引用失效时,计数器值就减一&#xff1…