日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

1、MyCAT基礎架構準備

MyCAT基礎架構圖

 

MySQL-分布式架構-MyCAT

 

 

摘自oldguo

1.1 MyCAT基礎架構準備

1.1.1 環境準備:

兩臺虛擬機 db01(10.0.0.51) db02(10.0.0.52) 每臺創建四個MySQL實例:3307 3308 3309 3310 1.1.2 刪除歷史環境:

pkill mysqld
rm -rf /data/330* 
mv /etc/my.cnf /etc/my.cnf.bak

1.1.3 創建相關目錄初始化數據

mkdir /data/33{07..10}/data -p
mysqld --initialize-insecure --user=mysql --datadir=/data/3307/data --basedir=/Application/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3308/data --basedir=/application/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3309/data --basedir=/application/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3310/data --basedir=/application/mysql

1.1.4 準備配置文件和啟動腳本

========db01==============
cat >/data/3307/my.cnf<<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3307/data
socket=/data/3307/mysql.sock
port=3307
log-error=/data/3307/mysql.log
log_bin=/data/3307/mysql-bin
binlog_format=row
skip-name-resolve
server-id=7
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
EOF
cat >/data/3308/my.cnf<<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3308/data
port=3308
socket=/data/3308/mysql.sock
log-error=/data/3308/mysql.log
log_bin=/data/3308/mysql-bin
binlog_format=row
skip-name-resolve
server-id=8
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
EOF
cat >/data/3309/my.cnf<<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3309/data
socket=/data/3309/mysql.sock
port=3309
log-error=/data/3309/mysql.log
log_bin=/data/3309/mysql-bin
binlog_format=row
skip-name-resolve
server-id=9
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
EOF
cat >/data/3310/my.cnf<<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3310/data
socket=/data/3310/mysql.sock
port=3310
log-error=/data/3310/mysql.log
log_bin=/data/3310/mysql-bin
binlog_format=row
skip-name-resolve
server-id=10
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
EOF
cat >/etc/systemd/system/mysqld3307.service<<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
LimitNOFILE = 5000
EOF
cat >/etc/systemd/system/mysqld3308.service<<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf
LimitNOFILE = 5000
EOF
cat >/etc/systemd/system/mysqld3309.service<<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3309/my.cnf
LimitNOFILE = 5000
EOF
cat >/etc/systemd/system/mysqld3310.service<<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3310/my.cnf
LimitNOFILE = 5000
EOF
========db02===============
cat >/data/3307/my.cnf<<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3307/data
socket=/data/3307/mysql.sock
port=3307
log-error=/data/3307/mysql.log
log_bin=/data/3307/mysql-bin
binlog_format=row
skip-name-resolve
server-id=17
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
EOF
cat >/data/3308/my.cnf<<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3308/data
port=3308
socket=/data/3308/mysql.sock
log-error=/data/3308/mysql.log
log_bin=/data/3308/mysql-bin
binlog_format=row
skip-name-resolve
server-id=18
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
EOF
cat >/data/3309/my.cnf<<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3309/data
socket=/data/3309/mysql.sock
port=3309
log-error=/data/3309/mysql.log
log_bin=/data/3309/mysql-bin
binlog_format=row
skip-name-resolve
server-id=19
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
EOF
cat >/data/3310/my.cnf<<EOF
[mysqld]
basedir=/application/mysql
datadir=/data/3310/data
socket=/data/3310/mysql.sock
port=3310
log-error=/data/3310/mysql.log
log_bin=/data/3310/mysql-bin
binlog_format=row
skip-name-resolve
server-id=20
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
EOF
cat >/etc/systemd/system/mysqld3307.service<<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
LimitNOFILE = 5000
EOF
cat >/etc/systemd/system/mysqld3308.service<<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf
LimitNOFILE = 5000
EOF
cat >/etc/systemd/system/mysqld3309.service<<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3309/my.cnf
LimitNOFILE = 5000
EOF
cat >/etc/systemd/system/mysqld3310.service<<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/data/3310/my.cnf
LimitNOFILE = 5000
EOF

1.1.5 修改權限,啟動多實例

chown -R mysql.mysql /data/*
systemctl start mysqld3307
systemctl start mysqld3308
systemctl start mysqld3309
systemctl start mysqld3310
mysql -S /data/3307/mysql.sock -e "show variables like 'server_id'"
mysql -S /data/3308/mysql.sock -e "show variables like 'server_id'"
mysql -S /data/3309/mysql.sock -e "show variables like 'server_id'"
mysql -S /data/3310/mysql.sock -e "show variables like 'server_id'"
 

1.1.6 節點主從規劃 箭頭指向誰是主庫

 10.0.0.51:3307 <-----> 10.0.0.52:3307
 10.0.0.51:3309 ------> 10.0.0.51:3307
 10.0.0.52:3309 ------> 10.0.0.52:3307
 10.0.0.52:3308 <-----> 10.0.0.51:3308
 10.0.0.52:3310 -----> 10.0.0.52:3308
 10.0.0.51:3310 -----> 10.0.0.51:3308

1.1.7 分片規劃

shard1:
 Master:10.0.0.51:3307
 slave1:10.0.0.51:3309
 Standby Master:10.0.0.52:3307
 slave2:10.0.0.52:3309
shard2:
 Master:10.0.0.52:3308
 slave1:10.0.0.52:3310
 Standby Master:10.0.0.51:3308
 slave2:10.0.0.51:3310

1.1.8 開始配置

shard1
10.0.0.51:3307 <-----> 10.0.0.52:3307
db02
mysql -S /data/3307/mysql.sock -e "grant replication slave on *.* to repl@'10.0.0.%' identified by '123';"
mysql -S /data/3307/mysql.sock -e "grant all on *.* to root@'10.0.0.%' identified by '123' with grant option;"
db01
mysql -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSword='123';"
mysql -S /data/3307/mysql.sock -e "start slave;"
mysql -S /data/3307/mysql.sock -e "show slave statusG"
db02
mysql -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3307/mysql.sock -e "start slave;"
mysql -S /data/3307/mysql.sock -e "show slave statusG"
10.0.0.51:3309 ------> 10.0.0.51:3307
db01
mysql -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3309/mysql.sock -e "start slave;"
mysql -S /data/3309/mysql.sock -e "show slave statusG"
10.0.0.52:3309 ------> 10.0.0.52:3307
db02
mysql -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3309/mysql.sock -e "start slave;"
mysql -S /data/3309/mysql.sock -e "show slave statusG"
shard2
10.0.0.52:3308 <-----> 10.0.0.51:3308
db01
mysql -S /data/3308/mysql.sock -e "grant replication slave on *.* to repl@'10.0.0.%' identified by '123';"
mysql -S /data/3308/mysql.sock -e "grant all on *.* to root@'10.0.0.%' identified by '123' with grant option;"
db02
mysql -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3308/mysql.sock -e "start slave;"
mysql -S /data/3308/mysql.sock -e "show slave statusG"
db01
mysql -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3308/mysql.sock -e "start slave;"
mysql -S /data/3308/mysql.sock -e "show slave statusG"
10.0.0.52:3310 -----> 10.0.0.52:3308
db02
mysql -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3310/mysql.sock -e "start slave;"
mysql -S /data/3310/mysql.sock -e "show slave statusG"
10.0.0.51:3310 -----> 10.0.0.51:3308
db01
mysql -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3310/mysql.sock -e "start slave;"
mysql -S /data/3310/mysql.sock -e "show slave statusG"

1.1.9 檢測主從狀態

mysql -S /data/3307/mysql.sock -e "show slave statusG"|grep Running:
mysql -S /data/3308/mysql.sock -e "show slave statusG"|grep Running:
mysql -S /data/3309/mysql.sock -e "show slave statusG"|grep Running:
mysql -S /data/3310/mysql.sock -e "show slave statusG"|grep Running:

1.1.10 如果中間出現錯誤,在每個節點進行執行以下命令(2.9 狀態正確,忽略此步驟)

mysql -S /data/3307/mysql.sock -e "stop slave; reset slave all;"
mysql -S /data/3308/mysql.sock -e "stop slave; reset slave all;"
mysql -S /data/3309/mysql.sock -e "stop slave; reset slave all;"
mysql -S /data/3310/mysql.sock -e "stop slave; reset slave all;"

1.2 Mycat的安裝

MySQL分布式架構介紹:

 

MySQL-分布式架構-MyCAT

 

 

1.2.1 安裝JAVA環境

yum install -y java

1.2.2 上傳到/application 并解壓

略官網:http://www.mycat.io/

1.2.3 配置文件介紹

conf: 
 schema.xml 主配置文件(讀寫分離、高可用、分布式策略定制、節點控制)
 server.xml mycat軟件本身相關的配置
 rule.xml 分片規則配置文件,記錄分片規則列表、使用方法等
xxxx.txt 分片參數功能有關
logs目錄: 
 wrapper.log ---->mycat啟動日志
 mycat.log ---->mycat詳細工作日志

1.2.4 啟動mycat

[root@db02 ~]# vim /etc/profile export PATH=/application/mycat/bin:$PATH [root@db02 ~]# source /etc/profile [root@db02 ~]# mycat start [root@db02 ~]# mysql -uroot -p123456 -h 10.0.0.52 -P8066 1.3 測試數據準備

## db01:
mysql -S /data/3307/mysql.sock 
grant all on *.* to root@'10.0.0.%' identified by '123';
source /root/world.sql
mysql -S /data/3308/mysql.sock 
grant all on *.* to root@'10.0.0.%' identified by '123';
source /root/world.sql

1.4 核心配置文件使用介紹(schema.xml)

(1)邏輯庫定義
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1"> 
</schema> 
(2)數據節點定義
 <dataNode name="sh1" dataHost="oldguo1" database= "world" /> 
(3)數據主機定義
 <dataHost name="oldguo1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"> 
 <heartbeat>select user()</heartbeat> 
 <writeHost host="db1" url="10.0.0.51:3307" user="root" password="123"> 
 <readHost host="db2" url="10.0.0.51:3309" user="root" password="123" /> 
 </writeHost> 
 </dataHost> 

1.5 Mycat實現讀寫分離功能

(1)配置conf文件--schema.xml

[root@db02 /application/mycat/conf]# cat schema.xml <?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1"> </schema> <dataNode name="sh1" dataHost="oldguo1" database= "world" /> <dataHost name="oldguo1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"> <heartbeat>select user()</heartbeat> <writeHost host="db1" url="10.0.0.51:3307" user="root" password="123"> <readHost host="db2" url="10.0.0.51:3309" user="root" password="123" /> </writeHost> </dataHost> </mycat:schema> 

(2)Mycat重啟生效:

mycat restart

(3)模擬測試

[root@db02 /application/mycat/conf]# mysql -uroot -p123456 -h 10.0.0.52 -P8066
mysql> select @@server_id; ----模擬查詢
mysql> begin; select @@server_id;commit; ---模擬寫入

1.6 讀寫分離+高可用功能

[root@db01 conf]# mv schema.xml schema.xml.rw [root@db01 conf]# vim schema.xml <?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1"> </schema> <dataNode name="sh1" dataHost="oldguo1" database= "world" /> <dataHost name="oldguo1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"> <heartbeat>select user()</heartbeat> <writeHost host="db1" url="10.0.0.51:3307" user="root" password="123"> <readHost host="db2" url="10.0.0.51:3309" user="root" password="123" /> </writeHost> <writeHost host="db3" url="10.0.0.52:3307" user="root" password="123"> <readHost host="db4" url="10.0.0.52:3309" user="root" password="123" /> </writeHost> </dataHost> </mycat:schema> 真正的 writehost:負責寫操作的writehost standby writeHost :和readhost一樣,只提供讀服務 當寫節點宕機后,后面跟的readhost也不提供服務,這時候standby的writehost就提供寫服務, 后面跟的readhost提供讀服務 。 測試: mysql -uroot -p123456 -h 127.0.0.1 -P 8066 show variables like 'server_id'; 讀寫分離測試 mysql -uroot -p -h 127.0.0.1 -P8066 show variables like 'server_id'; show variables like 'server_id'; show variables like 'server_id'; begin; show variables like 'server_id'; 對db01 3307節點進行關閉和啟動,測試讀寫操作

1.7 配置中的屬性介紹

1.7.1 balance屬性

負載均衡類型,目前的取值有3種: (1) balance="0", 不開啟讀寫分離機制,所有讀操作都發送到當前可用的writeHost上。 (2) balance="1",全部的readHost與standby writeHost參與select語句的負載均衡,簡單的說, 當雙主雙從模式(M1->S1,M2->S2,并且M1與 M2互為主備),正常情況下,M2,S1,S2都參與select語句的負載均衡。 (3) balance="2",所有讀操作都隨機的在writeHost、readhost上分發。 1.7.2 writeType屬性

負載均衡類型,目前的取值有2種: 
(1)writeType="0", 所有寫操作發送到配置的第一個writeHost,
第一個掛了切到還生存的第二個writeHost,重新啟動后已切換后的為主,切換記錄在配置文件中:dnindex.properties . 
(2)writeType=“1”,所有寫操作都隨機的發送到配置的writeHost,但不推薦使用

1.7.3 switchType屬性

-1 表示不自動切換 1 默認值,自動切換 2 基于MySQL主從同步的狀態決定是否切換 ,心跳語句為 show slave status 1.7.4 datahost其他配置

<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"> maxCon="1000":最大的并發連接數 minCon="10" :mycat在啟動之后,會在后端節點上自動開啟的連接線程 tempReadHostAvailable="1" 這個一主一從時(1個writehost,1個readhost時),可以開啟這個參數,如果2個writehost,2個readhost時 <heartbeat>select user()</heartbeat> 監測心跳 1.8 垂直分表

 

MySQL-分布式架構-MyCAT

 

 

1.8.1 修改schema.xml配置文件

mv schema.xml schema.xml.ha vim schema.xml <?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1"> <table name="user" dataNode="sh1"/> <table name="order_t" dataNode="sh2"/> </schema> <dataNode name="sh1" dataHost="oldguo1" database= "taobao" /> <dataNode name="sh2" dataHost="oldguo2" database= "taobao" /> <dataHost name="oldguo1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"> <heartbeat>select user()</heartbeat> <writeHost host="db1" url="10.0.0.51:3307" user="root" password="123"> <readHost host="db2" url="10.0.0.51:3309" user="root" password="123" /> </writeHost> <writeHost host="db3" url="10.0.0.52:3307" user="root" password="123"> <readHost host="db4" url="10.0.0.52:3309" user="root" password="123" /> </writeHost> </dataHost> <dataHost name="oldguo2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"> <heartbeat>select user()</heartbeat> <writeHost host="db1" url="10.0.0.51:3308" user="root" password="123"> <readHost host="db2" url="10.0.0.51:3310" user="root" password="123" /> </writeHost> <writeHost host="db3" url="10.0.0.52:3308" user="root" password="123"> <readHost host="db4" url="10.0.0.52:3310" user="root" password="123" /> </writeHost> </dataHost> </mycat:schema>

1.8.2 創建測試庫和表:

mysql -S /data/3307/mysql.sock -e "create database taobao charset utf8;" mysql -S /data/3308/mysql.sock -e "create database taobao charset utf8;" mysql -S /data/3307/mysql.sock -e "use taobao;create table user(id int,name varchar(20))"; mysql -S /data/3308/mysql.sock -e "use taobao;create table order_t(id int,name varchar(20))"

1.8.3 重啟MyCat

mycat restart

1.8.4 進入MyCat測試,添加數據測試

mysql -uroot -p123456 -h 10.0.0.51 -P 8066 --這里的ip地址是MyCat安裝的那個虛擬機,就是哪個虛擬機的ip地址 mysql -S /data/3307/mysql.sock -e "use taobao;show tables;" mysql -S /data/3308/mysql.sock -e "use taobao;show tables;" 1.9 MyCAT核心特性——分片(水平拆分)

分片:對一個"bigtable",比如說t3表 (1)行數非常多,800w (2)訪問非常頻繁 分片的目的: (1)將大數據量進行分布存儲 (2)提供均衡的訪問路由 分片策略: 范圍 range 800w 1-400w 400w01-800w 取模 mod 取余數 枚舉 哈希 hash 時間 流水 優化關聯查詢 全局表 ER分片 1.9.1 范圍分片

(1)范圍分片參數配置

比如說t3表
(1)行數非常多,2000w(1-1000w:sh1 1000w01-2000w:sh2)
(2)訪問非常頻繁,用戶訪問較離散
cp schema.xml schema.xml.1 
==========vim schema.xml
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1"> 
 <table name="t3" dataNode="sh1,sh2" rule="auto-sharding-long" />
</schema> 
 <dataNode name="sh1" dataHost="oldguo1" database= "taobao" /> 
 <dataNode name="sh2" dataHost="oldguo2" database= "taobao" /> 
 
 
===========vim rule.xml
<tableRule name="auto-sharding-long">
 <rule>
 <columns>id</columns>
 <algorithm>rang-long</algorithm>
 </rule> 
<function name="rang-long"
 class="io.mycat.route.function.AutoPartitionByLong">
 <property name="mapFile">autopartition-long.txt</property>
</function>
 
===========vim autopartition-long.txt
0-10=0
11-20=1

(2)創建測試表:

mysql -S /data/3307/mysql.sock -e "use taobao;create table t3 (id int not null primary key auto_increment,name varchar(20) not null);" mysql -S /data/3308/mysql.sock -e "use taobao;create table t3 (id int not null primary key auto_increment,name varchar(20) not null);" 

(3)重啟mycat------>mycat restart

(4)登錄mycat插入數據

mysql -uroot -p123456 -h 127.0.0.1 -P 8066 insert into t3(id,name) values(1,'a'); insert into t3(id,name) values(2,'b'); insert into t3(id,name) values(3,'c'); insert into t3(id,name) values(4,'d'); insert into t3(id,name) values(11,'aa'); insert into t3(id,name) values(12,'bb'); insert into t3(id,name) values(13,'cc'); insert into t3(id,name) values(14,'dd'); 

(5)分別登入后端節點測試

mysql -S /data/3307/mysql.sock -e "use taobao;select * from t5;"
mysql -S /data/3308/mysql.sock -e "use taobao;select * from t5;"

1.9.2 取模分片(mod-long)

取余分片方式:分片鍵(一個列)與節點數量進行取余,得到余數,將數據寫入對應節點

(1)取模分片參數配置

=======vim schema.xml <table name="t4" dataNode="sh1,sh2" rule="mod-long" /> ========vim rule.xml <tableRule name="mod-long"> <rule> <columns>id</columns> <algorithm>mod-long</algorithm> </rule> </tableRule> <function name="mod-long" class="io.mycat.route.function.PartitionByMod"> <!-- how many data nodes --> <property name="count">2</property> </function> 

(2)創建測試表:

mysql -S /data/3307/mysql.sock -e "use taobao;create table t4 (id int not null primary key auto_increment,name varchar(20) not null);" mysql -S /data/3308/mysql.sock -e "use taobao;create table t4 (id int not null primary key auto_increment,name varchar(20) not null);"

(3)重啟mycat

mycat restart 

(4)模擬數據測試:

mysql -uroot -p123456 -h10.0.0.52 -P8066 use TESTDB insert into t4(id,name) values(1,'a'); insert into t4(id,name) values(2,'b'); insert into t4(id,name) values(3,'c'); insert into t4(id,name) values(4,'d'); 

(5)分別登錄后端節點查詢數據

mysql -S /data/3307/mysql.sock -e "use taobao;select * from t4;"
mysql -S /data/3308/mysql.sock -e "use taobao;select * from t4;"

1.9.3 枚舉分片

t5 表 id name telnum 1 bj 1212 2 sh 22222 3 bj 3333 4 sh 44444 5 bj 5555 (1)枚舉分片參數配置

=====vim schema.xml
<table name="t5" dataNode="sh1,sh2" rule="sharding-by-intfile" />
=====vim rule.xml
<tableRule name="sharding-by-intfile"> 
 <rule> <columns>name</columns> 
 <algorithm>hash-int</algorithm> 
 </rule> 
</tableRule> 
<function name="hash-int" class="org.opencloudb.route.function.PartitionByFileMap"> 
<property name="mapFile">partition-hash-int.txt</property> 
 <property name="type">1</property>
</function> 
======partition-hash-int.txt 配置: 
bj=0 
sh=1
DEFAULT_NODE=1 
columns 標識將要分片的表字段,algorithm 分片函數, 其中分片函數配置中,mapFile標識配置文件名稱

(2)準備測試環境

mysql -S /data/3307/mysql.sock -e "use taobao;create table t5 (id int not null primary key auto_increment,name varchar(20) not null);"
mysql -S /data/3308/mysql.sock -e "use taobao;create table t5 (id int not null primary key auto_increment,name varchar(20) not null);"

(3)重啟mycat

mycat restart (4)模擬數據測試

mysql -uroot -p123456 -h10.0.0.52 -P8066 use TESTDB insert into t5(id,name) values(1,'bj'); insert into t5(id,name) values(2,'sh'); insert into t5(id,name) values(3,'bj'); insert into t5(id,name) values(4,'sh'); insert into t5(id,name) values(5,'tj'); (5)分別登錄后端節點查詢數據

mysql -S /data/3307/mysql.sock -e "use taobao;select * from t5;"
mysql -S /data/3308/mysql.sock -e "use taobao;select * from t5;"

1.9.4 Mycat全局表

a b c d join t select t1.name ,t.x from t1 join t select t2.name ,t.x from t2 join t select t3.name ,t.x from t3 join t 使用場景: 如果你的業務中有些數據類似于數據字典,比如配置文件的配置, 常用業務的配置或者數據量不大很少變動的表,這些表往往不是特別大, 而且大部分的業務場景都會用到,那么這種表適合于Mycat全局表,無須對數據進行切分, 要在所有的分片上保存一份數據即可,Mycat 在Join操作中,業務表與全局表進行Join聚合會優先選擇相同分片內的全局表join, 避免跨庫Join,在進行數據插入操作時,mycat將把數據分發到全局表對應的所有分片執行,在進行數據讀取時候將會隨機獲取一個節點讀取數據。 (1)Mycat全局表參數配置

=====vim schema.xml 
<table name="t_area" primaryKey="id" type="global" dataNode="sh1,sh2" /> 

(2)后端數據準備

mysql -S /data/3307/mysql.sock -e "use taobao;create table t_area (id int not null primary key auto_increment,name varchar(20) not null);" mysql -S /data/3308/mysql.sock -e "use taobao;create table t_area (id int not null primary key auto_increment,name varchar(20) not null);" 

(3)重啟mycat

mycat restart 

(4)模擬數據測試:

mysql -uroot -p123456 -h10.0.0.52 -P8066
use TESTDB
insert into t_area(id,name) values(1,'a');
insert into t_area(id,name) values(2,'b');
insert into t_area(id,name) values(3,'c');
insert into t_area(id,name) values(4,'d');

(5)分別登錄后端節點查詢數據

mysql -S /data/3307/mysql.sock -e "use taobao;select * from t_area;" mysql -S /data/3308/mysql.sock -e "use taobao;select * from t_area;" 2.9.5 ER分片

有一類業務,例如商品(goods)跟商品明細(goods_detail),明細表會依賴于商品,也就是說會存在表的主 從關系,這類似業務的切分可以抽象出合適的切分規則,比如根據商品 ID 切分,總之部分業務總會可以抽象出父子關系的表。這類表適用于 ER 分片表,子表的記錄與所關聯的父表記錄存放在同一個數據分片上,避免數據 Join 跨庫操作。 goods join goods_detail on goods.id=goods_detail.good_id (1)ER分片參數配置

=====vim schema.xml
<table name="goods" dataNode="sh1,sh2" rule="mod-long"> 
 <childTable name="goods_detail" joinKey="goods_id" parentKey="id" /> 
</table>

(2)后端數據表準備

mysql -S /data/3307/mysql.sock -e "use taobao;create table goods (id int not null primary key auto_increment,name varchar(20) not null);" mysql -S /data/3308/mysql.sock -e "use taobao;create table goods (id int not null primary key auto_increment,name varchar(20) not null);" mysql -S /data/3307/mysql.sock -e "use taobao;create table goods_detail (detail_id int not null primary key auto_increment,name varchar(20) not null,goods_id int not null);" mysql -S /data/3308/mysql.sock -e "use taobao;create table goods_detail (detail_id int not null primary key auto_increment,name varchar(20) not null,goods_id int not null);" 

(3)重啟mycat

mycat restart

(4)模擬數據測試:

mysql -uroot -p123456 -h10.0.0.51 -P8066 ===========goods use TESTDB insert into goods(id,name) values(1,'dn'); insert into goods(id,name) values(2,'sj'); insert into goods(id,name) values(3,'yf'); insert into goods(id,name) values(4,'bk'); ===========goods_detail use TESTDB insert into goods_detail(detail_id,name,goods_id) values(1,'電腦',1); insert into goods_detail(detail_id,name,goods_id) values(2,'500元',1); insert into goods_detail(detail_id,name,goods_id) values(3,'手機',2); insert into goods_detail(detail_id,name,goods_id) values(4,'紅色',2); insert into goods_detail(detail_id,name,goods_id) values(5,'衣服',3); insert into goods_detail(detail_id,name,goods_id) values(6,'s碼',3); insert into goods_detail(detail_id,name,goods_id) values(7,'書籍',4); insert into goods_detail(detail_id,name,goods_id) values(8,'linux方向',4); 

(5)分別登錄后端節點查詢數據

mysql -S /data/3307/mysql.sock -e "use taobao;select * from goods;select * from goods_detail;"
mysql -S /data/3308/mysql.sock -e "use taobao;select * from goods;select * from goods_detail;"
[root@db01 ~]# mysql -S /data/3307/mysql.sock -e "use taobao;select * from goods;select * from goods_detail;"
+----+------+
| id | name |
+----+------+
| 2 | sj |
| 4 | bk |
+----+------+
+-----------+-------------+----------+
| detail_id | name | goods_id |
+-----------+-------------+----------+
| 3 | 手機 | 2 |
| 4 | 紅色 | 2 |
| 7 | 書籍 | 4 |
| 8 | Linux方向 | 4 |
+-----------+-------------+----------+
[root@db01 ~]# mysql -S /data/3308/mysql.sock -e "use taobao;select * from goods;select * from goods_detail;"
+----+------+
| id | name |
+----+------+
| 1 | dn |
| 3 | yf |
+----+------+
+-----------+--------+----------+
| detail_id | name | goods_id |
+-----------+--------+----------+
| 1 | 電腦 | 1 |
| 2 | 500元 | 1 |
| 5 | 衣服 | 3 |
| 6 | s碼 | 3 |
+-----------+--------+----------+
[root@db01 ~]# 

原文:https://www.jianshu.com/p/7ec2847583d4

作者:文娟_狼剩

來源:簡書

分享到:
標簽:MySQL
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定