千家信息网

Mycat中间件如何实现Mysql数据分片

发表于:2025-02-23 作者:千家信息网编辑
千家信息网最后更新 2025年02月23日,这篇文章主要介绍了Mycat中间件如何实现Mysql数据分片,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。架构图:机器规划:IP地址主
千家信息网最后更新 2025年02月23日Mycat中间件如何实现Mysql数据分片

这篇文章主要介绍了Mycat中间件如何实现Mysql数据分片,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

架构图:

机器规划:

IP地址主机名角色备注
10.4.132.50k8s01mycat,master
10.4.132.42k8s02master
10.4.132.66k8s03master

Mycat下载地址:http://dl.mycat.io/1.6.7.3/20190828135747/Mycat-server-1.6.7.3-release-20190828135747-linux.tar.gz
Mysql下载地址: http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz

1.下载安装Mysql(三台Mysql都需要安装)
[root@k8s01 soft]# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
[root@k8s01 soft]# tar xvf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@k8s01 soft]# cd /usr/local/
[root@k8s01 local]# mv mysql-5.7.27-linux-glibc2.12-x86_64/ mysql-5.7.27
[root@k8s01 local]# chown -R root:root mysql-5.7.27/
[root@k8s01 local]# cd mysql-5.7.27/
[root@k8s01 mysql-5.7.27]# mkdir data
[root@k8s01 mysql-5.7.27]# useradd -r -M -s /bin/nologin mysql
[root@k8s01 mysql-5.7.27]# chown -R mysql:mysql data/
[root@k8s01 mysql-5.7.27]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql-5.7.27 --datadir=/usr/local/mysql-5.7.27/data
2019-11-02T04:24:41.908404Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-11-02T04:24:46.687678Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-11-02T04:24:47.428823Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-11-02T04:24:47.487404Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b42cef88-fd28-11e9-a5cc-000c29ee86d5.
2019-11-02T04:24:47.488204Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-11-02T04:24:47.612739Z 1 [Note] A temporary password is generated for root@localhost: 3m;5yQ_7T#jc --登陆密码
[root@k8s01 mysql-5.7.27]# cp -a support-files/mysql.server /etc/init.d/mysqld
[root@k8s01 mysql-5.7.27]# chkconfig --add mysqld
[root@k8s01 mysql-5.7.27]# chkconfig mysqld on
[root@k8s01 mysql-5.7.27]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql-5.7.27
datadir=/usr/local/mysql-5.7.27/data
[root@k8s01 mysql-5.7.27]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql-5.7.27
datadir=/usr/local/mysql-5.7.27/data
socket=/tmp/mysql.sock
symbolic-links=0
server_id=10
binlog_format=ROW
max_binlog_size=2G
sync_binlog=1
binlog_cache_size=64M
log_bin=bin-log
log_bin_index=bin-index
[mysqld_safe]
log-error=/usr/local/mysql-5.7.27/data/mariadb.log
pid-file=/usr/local/mysql-5.7.27/data/mariadb.pid

[root@k8s01 mysql-5.7.27]# /etc/init.d/mysqld restart
ERROR! MySQL server PID file could not be found!
Starting MySQL.Logging to '/usr/local/mysql-5.7.27/data/mariadb.log'.
... SUCCESS!
[root@k8s01 mysql-5.7.27]# vim /etc/profile
export PATH=$PATH:/usr/local/mysql-5.7.27/bin
[root@k8s01 mysql-5.7.27]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.27
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respectiveowners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set password=password('System135');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@k8s01 mysql-5.7.27]# mysql -u root -pSystem135
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.27 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant all privileges on *.* to repl@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>

2.下载安装Mycat
[root@k8s01 soft]# rpm -ivh jdk-8u221-linux-x64.rpm
warning: jdk-8u221-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:jdk1.8-2000:1.8.0_221-fcs ################################# [100%]
Unpacking JAR files...
tools.jar...
plugin.jar...
javaws.jar...
deploy.jar...
rt.jar...
jsse.jar...
charsets.jar...
localedata.jar...
[root@k8s01 soft]# tar xvf Mycat-server-1.6.7.3-release-20190828135747-linux.tar.gz -C /usr/local/
[root@k8s01 soft]# cd /usr/local/mycat/conf/
[root@k8s01 conf]#

3.数据按范围分片
[root@k8s01 conf]# vim schema.xml

[root@k8s01 conf]# vim rule.xml --其它不要动,只修改以下内容

[root@k8s01 conf]# vim autopartition-long.txt
# range start-end ,data node index
# K=1000,M=10000. --K表示1千,M表示1万
0-5=0 --从0到5放到第一个节点
5-10=1 --从6到10放到第二个节点
10-15=2 --从11到15放到第三个节点
[root@k8s01 conf]# vim server.xml --Mycat登陆用户名和密码

[root@k8s01 conf]# ../bin/mycat restart
Stopping Mycat-server...
Stopped Mycat-server.
Starting Mycat-server...
[root@k8s01 conf]# netstat -antulp | grep 8066
tcp6 0 0 :::8066 :::* LISTEN 46762/java
[root@k8s01 conf]# netstat -antulp | grep 9066
tcp6 0 0 :::9066 :::* LISTEN 46762/java
[root@k8s01 conf]#

4.验证数据分片后存放
[root@k8s01 conf]# /usr/local/mysql-5.7.27/bin/mysql -u root -pSystem135 -P8066 -h 127.0.0.1 --登陆Mycat数据数据

查询写入后的数据:

k8s01节点验证数据:

k8s02节点验证数据:

k8s03节点验证数据:


5.数据按日期(月份)分片
[root@k8s01 conf]# vim schema.xml

[root@k8s01 conf]# vim rule.xml

[root@k8s01 conf]# ../bin/mycat restart
Stopping Mycat-server...
Stopped Mycat-server.
Starting Mycat-server...
[root@k8s01 conf]# !net
netstat -antulp | grep 9066
tcp6 0 0 :::9066 :::* LISTEN 69040/java
[root@k8s01 conf]# netstat -antulp | grep 8066
tcp6 0 0 :::8066 :::* LISTEN 69040/java
[root@k8s01 conf]#
6.验证数据分片后存放

验证数据:

k8s01节点:

k8s02节点:

k8s03节点:

错误处理:
mysql> insert into t_wuhan(create_time,name,age) values("2015-04-01","huanggang",16);
ERROR 1064 (HY000): Can't find a valid data node for specified node index :T_WUHAN -> CREATE_TIME -> 2015-04-01 -> Index : 3
解决方法:
写入4月份数据时会提示找不到节点,是因为有几个节点就会写入几月份数据,比如我只有3个node节点,只能写入1-3月份数据。
7.数据按枚举分片
[root@k8s01 conf]# vim schema.xml[root@k8s01 conf]# vim rule.xml

[root@k8s01 conf]# cat partition-hash-int.txt --可以写多个枚举
学生=0
老师=1
DEFAULT_NODE=2
[root@k8s01 conf]#
8.验证数据分片后存放

验证数据:
k8s01节点:

k8s02节点:

k8s03节点:

错误处理:
mysql> insert into t_wuhan(id,name,age) values(1,"tong","学生");
ERROR 1064 (HY000): columnValue:学生 Please check if the format satisfied.
mysql> insert into t_wuhan(id,name,age) values(1,"tong",'学生');
ERROR 1064 (HY000): columnValue:学生 Please check if the format satisfied.
mysql>

解决方法:


type:type值默认为0,表示数值是整型。值为1,表示是字符串。
defaultNode:值对应partition-hash-int.txt文件中的DEFAULT_NODE的值。

感谢你能够认真阅读完这篇文章,希望小编分享的"Mycat中间件如何实现Mysql数据分片"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

数据 节点 验证 学生 月份 篇文章 地址 登陆 中间件 密码 方法 错误 处理 三个 主机 价值 兴趣 内容 只有 同时 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 莫让网事回首网络安全不失守 软件开发过程案例及其模型 学通信工程能干网络安全吗 服务器通过管理口能装软件吗 上网不安全连接到服务器 宁海软件开发文档 软件开发好迷茫怎么办 as代码建数据库 绝地求生未来之役服务器中文 中国百家互联网科技有限公司 怎么样才能打上服务器第一名 广东云空间工具服务器 软件开发模型的特点和区别 上海net软件开发大概多少钱 数据库grant 南宁市网络安全支队 2021三级网络技术题库 软件开发需要资质证书 旁观者维护网络安全 小程序的数据库可以导出吗 无锡个人软件开发业务流程 软件开发一定要有配置管理吗 网络安全法中与中央政府相 独立软件开发商上市公司 联想服务器ts140性能 如何用软件开发的思想开发软件 电商平台软件开发怎么收费 客户端访问ftp服务器时端口 视频在线直播间软件开发 怎么检查服务器会不会自动重启
0