#MySQL-分布式架構(gòu)-MyCAT

MySQL-分布式架構(gòu)-MyCAT

1. MyCAT基礎(chǔ)架構(gòu)圖

image.png

2. MyCAT基礎(chǔ)架構(gòu)準(zhǔn)備

環(huán)境準(zhǔn)備

  • 兩臺(tái)虛擬機(jī) db01 db02
  • 每臺(tái)創(chuàng)建四個(gè)mysql實(shí)例:3307 3308 3309 3310

刪除歷史環(huán)境

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

創(chuàng)建相關(guān)目錄初始化數(shù)據(jù)

  • 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

準(zhǔn)備配置文件和啟動(dòng)腳本

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

修改權(quán)限歌溉,啟動(dòng)多實(shí)例

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'"

節(jié)點(diǎn)主從規(guī)劃

箭頭指向誰是主庫

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

分片規(guī)劃

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

開始配置

  • 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 status\G"

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 status\G"
  • 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 status\G"
  • 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 status\G"
  • 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 status\G"

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 status\G"
  • 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 status\G"
  • 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 status\G"

檢測(cè)主從狀態(tài)

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

如果中間出現(xiàn)錯(cuò)誤,在每個(gè)節(jié)點(diǎn)進(jìn)行執(zhí)行以下命令(2.9 狀態(tài)正確,忽略此步驟)

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;"

MySQL分布式架構(gòu)介紹

image.png
  1. schema拆分及業(yè)務(wù)分庫
  2. 垂直拆分-分庫分表
  3. 水平拆分-分片

企業(yè)代表產(chǎn)品

360 Atlas-Sharding
Alibaba cobar
Mycat
TDDL
Heisenberg
Oceanus
Vitess
OneProxy
DRDS

3. MyCAT安裝

安裝java環(huán)境

  • yum install -y java

上傳MySQL包到/application 并解壓

(略)

  • Mycat-server-xxxxx.linux.tar.gz
    http://dl.mycat.io/
  • tar xf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz

配置環(huán)境變量

  • vim /etc/profile
  • export PATH=/application/mycat/bin:$PATH
  • source /etc/profile

啟動(dòng)和連接

  • mycat start
  • mysql -uroot -p123456 -h 10.0.0.52 -P8066
  • mysql -uroot -p123456 -h 127.0.0.1 -P8066

4. 配置文件介紹

logs目錄:
wrapper.log       ---->mycat啟動(dòng)日志
mycat.log         ---->mycat詳細(xì)工作日志
conf目錄:
schema.xml      
主配置文件(讀寫分離、高可用乾胶、分布式策略定制识窿、節(jié)點(diǎn)控制)
server.xml
mycat軟件本身相關(guān)的配置
rule.xml 
分片規(guī)則配置文件,記錄分片規(guī)則列表、使用方法等

5.應(yīng)用前環(huán)境準(zhǔn)備

用戶創(chuàng)建及數(shù)據(jù)庫導(dǎo)入

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

配置文件處理

  • cd /application/mycat/conf
  • mv schema.xml schema.xml.bak
  • 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="dn1"> 
</schema>  
    <dataNode name="dn1" dataHost="localhost1" database= "wordpress" />  
    <dataHost name="localhost1" 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>

6. 配置文件簡(jiǎn)單介紹

邏輯庫:schema

<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"> 
</schema>  

數(shù)據(jù)節(jié)點(diǎn):datanode

<dataNode name="dn1" dataHost="localhost1" database= "world" />  

數(shù)據(jù)主機(jī):datahost(w和r)

<dataHost name="localhost1" 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.52:3309" user="root" password="123" /> 
    </writeHost> 
    </dataHost>

7.Mycat實(shí)現(xiàn)讀寫分離功能

  • 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>

重啟生效

/application/mycat/conf/mycat restart

讀寫分離測(cè)試

  • mysql -uroot -p -h 127.0.0.1 -P8066
  • mysql -uroot -p123456 -h 10.0.0.52 -P8066
  • mysql> select @@server_id;
  • mysql> begin; select @@server_id;commit;
    以上案例實(shí)現(xiàn)了1主1從的讀寫分離功能,寫操作落到主庫锻煌,讀操作落到從庫.如果主庫宕機(jī)姻蚓,從庫不能在繼續(xù)提供服務(wù)了狰挡。

8. 配置讀寫分離及高可用

mv schema.xml schema.xml.rw
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:負(fù)責(zé)寫操作的writehost (第一個(gè)redhost)
standby writeHost :和readhost一樣,只提供讀服務(wù)(之后的redhost)

當(dāng)寫節(jié)點(diǎn)宕機(jī)后倦沧,后面跟的readhost也不提供服務(wù)殉农,這時(shí)候standby的writehost就提供寫服務(wù)超凳,
后面跟的readhost提供讀服務(wù)

測(cè)試

  • /application/mycat/conf/mycat restart
  • mysql -uroot -p123456 -h 127.0.0.1 -P 8066
  • show variables like 'server_id';

讀寫分離測(cè)試

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';commit;

對(duì)db01 3307節(jié)點(diǎn)進(jìn)行關(guān)閉和啟動(dòng),測(cè)試讀寫操作

9. 配置中的屬性介紹

balance屬性

負(fù)載均衡類型轮傍,目前的取值有3種: 
1. balance="0", 不開啟讀寫分離機(jī)制,所有讀操作都發(fā)送到當(dāng)前可用的writeHost上杭跪。 
2. balance="1",全部的readHost與standby writeHost參與select語句的負(fù)載均衡涧尿,簡(jiǎn)單的說,
  當(dāng)雙主雙從模式(M1->S1缺亮,M2->S2桥言,并且M1與 M2互為主備)号阿,正常情況下,M2,S1,S2都參與select語句的負(fù)載均衡园担。 
3. balance="2"枯夜,所有讀操作都隨機(jī)的在writeHost、readhost上分發(fā)。

writeType屬性

負(fù)載均衡類型劝枣,目前的取值有2種: 
1. writeType="0", 所有寫操作發(fā)送到配置的第一個(gè)writeHost织鲸,
第一個(gè)掛了切到還生存的第二個(gè)writeHost,重新啟動(dòng)后已切換后的為主稳诚,切換記錄在配置文件中:dnindex.properties . 
2. writeType=“1”瀑踢,所有寫操作都隨機(jī)的發(fā)送到配置的writeHost橱夭,但不推薦使用

switchType屬性

-1 表示不自動(dòng)切換 
1 默認(rèn)值,自動(dòng)切換 
2 基于MySQL主從同步的狀態(tài)決定是否切換 棘劣,心跳語句為 show slave status 

datahost其他配置

<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1"> 

maxCon="1000":最大的并發(fā)連接數(shù)
minCon="10" :mycat在啟動(dòng)之后,會(huì)在后端節(jié)點(diǎn)上自動(dòng)開啟的連接線程

tempReadHostAvailable="1"
這個(gè)一主一從時(shí)(1個(gè)writehost首昔,1個(gè)readhost時(shí)),可以開啟這個(gè)參數(shù)勒奇,如果2個(gè)writehost撬陵,2個(gè)readhost時(shí)
<heartbeat>select user()</heartbeat>  監(jiān)測(cè)心跳

10. 垂直分表

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>

創(chuàng)建測(cè)試庫和表:
[root@db01 conf]# mysql -S /data/3307/mysql.sock -e "create database taobao charset utf8;"
[root@db01 conf]# mysql -S /data/3308/mysql.sock -e "create database taobao charset utf8;"
[root@db01 conf]# mysql -S /data/3307/mysql.sock -e "use taobao;create table user(id int,name varchar(20))";
[root@db01 conf]# mysql -S /data/3308/mysql.sock -e "use taobao;create table order_t(id int,name varchar(20))"

11. MyCAT核心特性——分片(水平拆分)

分片:對(duì)一個(gè)"bigtable"巨税,比如說t3表

(1)行數(shù)非常多,800w
(2)訪問非常頻繁

分片的目的:
(1)將大數(shù)據(jù)量進(jìn)行分布存儲(chǔ)
(2)提供均衡的訪問路由

分片策略:
范圍 range  800w  1-400w 400w01-800w
取模 mod    取余數(shù)
枚舉 
哈希 hash 
時(shí)間 流水

優(yōu)化關(guān)聯(lián)查詢
全局表
ER分片

12 .范圍分片

比如說t3表
(1)行數(shù)非常多驶兜,2000w(1-1000w:sh1   1000w01-2000w:sh2)
(2)訪問非常頻繁抄淑,用戶訪問較離散
mv 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

創(chuàng)建測(cè)試表:
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);"

測(cè)試:
重啟mycat
mycat restart
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');

13. 取模分片(mod-long)

取余分片方式:分片鍵(一個(gè)列)與節(jié)點(diǎn)數(shù)量進(jìn)行取余驰后,得到余數(shù)灶芝,將數(shù)據(jù)寫入對(duì)應(yīng)節(jié)點(diǎn)
vim schema.xml
<table name="t4" dataNode="sh1,sh2" rule="mod-long" />
vim rule.xml
<property name="count">2</property>

準(zhǔn)備測(cè)試環(huán)境
     
創(chuàng)建測(cè)試表:
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);"

重啟mycat 
mycat restart 

測(cè)試: 
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');

分別登錄后端節(jié)點(diǎn)查詢數(shù)據(jù)
mysql -S /data/3307/mysql.sock 
use taobao
select * from t4;

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

14. 枚舉分片

t5 表
id name telnum
1   bj   1212
2   sh   22222
3   bj   3333
4   sh   44444
5   bj   5555

sharding-by-intfile
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>
                <property name="defaultNode">0</property>
</function> 

partition-hash-int.txt 配置: 
bj=0 
sh=1
DEFAULT_NODE=1 
columns 標(biāo)識(shí)將要分片的表字段夜涕,algorithm 分片函數(shù), 其中分片函數(shù)配置中女器,mapFile標(biāo)識(shí)配置文件名稱

準(zhǔn)備測(cè)試環(huán)境
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);"
重啟mycat 
mycat restart 
mysql -uroot -p123456 -h10.0.0.51 -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');

15 . 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 

使用場(chǎng)景:
如果你的業(yè)務(wù)中有些數(shù)據(jù)類似于數(shù)據(jù)字典涣澡,比如配置文件的配置俏拱,
常用業(yè)務(wù)的配置或者數(shù)據(jù)量不大很少變動(dòng)的表,這些表往往不是特別大事格,
而且大部分的業(yè)務(wù)場(chǎng)景都會(huì)用到驹愚,那么這種表適合于Mycat全局表,無須對(duì)數(shù)據(jù)進(jìn)行切分谁鳍,
要在所有的分片上保存一份數(shù)據(jù)即可倘潜,Mycat 在Join操作中志于,業(yè)務(wù)表與全局表進(jìn)行Join聚合會(huì)優(yōu)先選擇相同分片內(nèi)的全局表join伺绽,
避免跨庫Join奈应,在進(jìn)行數(shù)據(jù)插入操作時(shí),mycat將把數(shù)據(jù)分發(fā)到全局表對(duì)應(yīng)的所有分片執(zhí)行,在進(jìn)行數(shù)據(jù)讀取時(shí)候?qū)?huì)隨機(jī)獲取一個(gè)節(jié)點(diǎn)讀取數(shù)據(jù)点把。 

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

后端數(shù)據(jù)準(zhǔn)備
mysql -S /data/3307/mysql.sock 
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 
use taobao
create table t_area  (id int not null primary key auto_increment,name varchar(20) not null);

重啟mycat 
mycat restart 

測(cè)試: 
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');

16. E-R分片

A 
join 
B  
為了防止跨分片join挺份,可以使用E-R模式
A   join   B
on  a.xx=b.yy
join C
on A.id=C.id
<table name="A" dataNode="sh1,sh2" rule="mod-long"> 
       <childTable name="B" joinKey="yy" parentKey="xx" /> 
</table>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子躲因,更是在濱河造成了極大的恐慌搞监,老刑警劉巖琐驴,帶你破解...
    沈念sama閱讀 206,214評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異牢酵,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)潘拨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,307評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門茫船,熙熙樓的掌柜王于貴愁眉苦臉地迎上來涩禀,“玉大人艾船,你說我怎么就攤上這事∫常” “怎么了烤惊?”我有些...
    開封第一講書人閱讀 152,543評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵喇完,是天一觀的道長(zhǎng)不脯。 經(jīng)常有香客問我防楷,道長(zhǎng),這世上最難降的妖魔是什么亿昏? 我笑而不...
    開封第一講書人閱讀 55,221評(píng)論 1 279
  • 正文 為了忘掉前任递礼,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好蔚出,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,224評(píng)論 5 371
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般师骗。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,007評(píng)論 1 284
  • 那天鸳君,我揣著相機(jī)與錄音砸紊,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的廊酣。 我是一名探鬼主播,決...
    沈念sama閱讀 38,313評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼戒职,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了蚓曼?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,956評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤其弊,失蹤者是張志新(化名)和其女友劉穎仰担,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,441評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,925評(píng)論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片魁袜。...
    茶點(diǎn)故事閱讀 38,018評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡融师,死狀恐怖旱爆,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情窘茁,我是刑警寧澤怀伦,帶...
    沈念sama閱讀 33,685評(píng)論 4 322
  • 正文 年R本政府宣布,位于F島的核電站山林,受9級(jí)特大地震影響房待,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜驼抹,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,234評(píng)論 3 307
  • 文/蒙蒙 一桑孩、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧砂蔽,春花似錦洼怔、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,240評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至诡右,卻和暖如春安岂,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背帆吻。 一陣腳步聲響...
    開封第一講書人閱讀 31,464評(píng)論 1 261
  • 我被黑心中介騙來泰國打工域那, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,467評(píng)論 2 352
  • 正文 我出身青樓次员,卻偏偏與公主長(zhǎng)得像败许,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子淑蔚,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,762評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容