Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

這篇文章主要介紹了Mycat中間件如何實現(xiàn)MySQL數(shù)據(jù)分片,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

成都創(chuàng)新互聯(lián)專注于網(wǎng)站建設(shè)|成都網(wǎng)站改版|優(yōu)化|托管以及網(wǎng)絡(luò)推廣,積累了大量的網(wǎng)站設(shè)計與制作經(jīng)驗,為許多企業(yè)提供了網(wǎng)站定制設(shè)計服務(wù),案例作品覆蓋成都濕噴機等行業(yè)。能根據(jù)企業(yè)所處的行業(yè)與銷售的產(chǎn)品,結(jié)合品牌形象的塑造,量身開發(fā)品質(zhì)網(wǎng)站。

架構(gòu)圖:

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

機器規(guī)劃:

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.數(shù)據(jù)按范圍分片
[root@k8s01 conf]# vim schema.xml

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

[root@k8s01 conf]# vim rule.xml       --其它不要動,只修改以下內(nèi)容

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

[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放到第一個節(jié)點
5-10=1            --從6到10放到第二個節(jié)點
10-15=2         --從11到15放到第三個節(jié)點        
[root@k8s01 conf]# vim server.xml      --Mycat登陸用戶名和密碼

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

[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.驗證數(shù)據(jù)分片后存放
[root@k8s01 conf]# /usr/local/mysql-5.7.27/bin/mysql -u root -pSystem135 -P8066 -h 127.0.0.1    --登陸Mycat數(shù)據(jù)數(shù)據(jù)

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

查詢寫入后的數(shù)據(jù):

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

k8s01節(jié)點驗證數(shù)據(jù):

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

k8s02節(jié)點驗證數(shù)據(jù):

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

k8s03節(jié)點驗證數(shù)據(jù):

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

5.數(shù)據(jù)按日期(月份)分片
[root@k8s01 conf]# vim schema.xml

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片[root@k8s01 conf]# vim rule.xmlMycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

[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.驗證數(shù)據(jù)分片后存放

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

驗證數(shù)據(jù):

k8s01節(jié)點:

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

k8s02節(jié)點:

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

k8s03節(jié)點:

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

錯誤處理:
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月份數(shù)據(jù)時會提示找不到節(jié)點,是因為有幾個節(jié)點就會寫入幾月份數(shù)據(jù),比如我只有3個node節(jié)點,只能寫入1-3月份數(shù)據(jù)。
7.數(shù)據(jù)按枚舉分片
[root@k8s01 conf]# vim schema.xmlMycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片[root@k8s01 conf]# vim rule.xml Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

[root@k8s01 conf]# cat partition-hash-int.txt      --可以寫多個枚舉
學(xué)生=0
老師=1
DEFAULT_NODE=2
[root@k8s01 conf]#
8.驗證數(shù)據(jù)分片后存放

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

驗證數(shù)據(jù):
k8s01節(jié)點:

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

k8s02節(jié)點:

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

k8s03節(jié)點:

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

錯誤處理:
mysql> insert into t_wuhan(id,name,age) values(1,"tong","學(xué)生");
ERROR 1064 (HY000): columnValue:學(xué)生 Please check if the format satisfied.
mysql> insert into t_wuhan(id,name,age) values(1,"tong",'學(xué)生');
ERROR 1064 (HY000): columnValue:學(xué)生 Please check if the format satisfied.
mysql>

解決方法:

Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片

type:type值默認(rèn)為0,表示數(shù)值是整型。值為1,表示是字符串。
defaultNode:值對應(yīng)partition-hash-int.txt文件中的DEFAULT_NODE的值。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

網(wǎng)站欄目:Mycat中間件如何實現(xiàn)Mysql數(shù)據(jù)分片
文章路徑:http://muchs.cn/article16/ghjddg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司、ChatGPT、響應(yīng)式網(wǎng)站、自適應(yīng)網(wǎng)站、企業(yè)網(wǎng)站制作靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作