Mycat中介軟體實現Mysql資料分片(上篇)
架構圖:
機器規劃:
IP地址 | 主機名 | 角色 | 備註 |
10.4.132.50 | k8s01 | mycat,master |
|
10.4.132.42 | k8s02 | master |
|
10.4.132.66 | k8s03 | master |
|
Mycat下載地址:
Mysql下載地址:
[root@k8s01 soft]# wget
[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]#
[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的值。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25854343/viewspace-2665474/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Mycat中介軟體實現Mysql資料分片( 下篇)MySql
- Mycat中介軟體實現Mysql主從讀寫分離MySql
- 分散式資料庫中介軟體 MyCat 搞起來!分散式資料庫
- Mycat中介軟體實現Percona Cluster讀寫分離
- 開源資料庫中介軟體-MyCa初探與分片實踐資料庫
- 分散式資料庫中介軟體 MyCat | 分庫分表實踐分散式資料庫
- MyCat分片:分片規則的十四種演算法詳細解讀&程式碼實現(上篇)演算法
- 資料庫路由中介軟體MyCat - 原始碼篇(15)資料庫路由原始碼
- Mycat中的特性----資料分片
- MySQL中介軟體MySql
- 開源 | MySQL資料傳輸中介軟體—DTLEMySql
- PHP 框架中介軟體實現PHP框架
- MyCat分片:水平拆分例項解析和程式碼實現!
- MySQL中介軟體--ProxySQLMySql
- 資料庫系列:業內主流MySQL資料中介軟體梳理資料庫MySql
- 資料庫中介軟體sharding-jdbc實現讀寫分離資料庫JDBC
- Redux 中介軟體的實現原理Redux
- 資料庫中介軟體 MyCAT原始碼分析:【單庫單表】查詢【推薦閱讀】資料庫原始碼
- MySQL中介軟體總結MySql
- MySQL中介軟體之ProxySQLMySql
- MyCat的坑如何在分散式中介軟體DBLE上改善分散式
- 實戰!Spring Boot 整合 阿里開源中介軟體 Canal 實現資料增量同步!Spring Boot阿里
- 資料庫中介軟體詳解資料庫
- 理解Laravel中介軟體核心實現原理Laravel
- mycat分片及主從(二)
- MyCat 讀寫分離 資料庫分庫分表 中介軟體 安裝部署,及簡單使用資料庫
- 關於使用中介軟體實現返回 JSONJSON
- MySQL運維9-Mycat分庫分表之列舉分片MySql運維
- mysql學習筆記之快速搭建PXC叢集(Mycat分片)MySql筆記
- KingShard MySQL中介軟體快速入門MySql
- mycat 水平切分|垂直切分|ER分片
- 分散式資料庫中介軟體的實現原理介紹一:分庫分表分散式資料庫
- Express中介軟體body-parser簡單實現Express
- redux, koa, express 中介軟體實現對比解析ReduxExpress
- .Net Core如何優雅的實現中介軟體
- 從-1開始實現一箇中介軟體
- node JS 中 express 中介軟體實現原理分析JSExpress
- MySQL運維12-Mycat分庫分表之按天分片MySql運維