能满足数据库数据大量存储;提高了查询性能
MyCAT通过定义表的分片规则来实现分片,每个表格可以捆绑一个分片规则,每个分片规则指定一个分片字段并绑定一个函数,来实现动态分片算法。
1.Schema:逻辑库,与MySQL中的Database(数据库)对应,一个逻辑库中定义了所包括的Table。
2.Table:逻辑表,即物理数据库中存储的某一张表,与传统数据库不同,这里的表格需要声明其所存储的逻辑数据节点DataNode。在此可以指定表的分片规则。
3.DataNode:MyCAT的逻辑数据节点,是存放table的具体物理节点,也称之为分片节点,通过DataSource来关联到后端某个具体数据库上
4.DataSource:定义某个物理库的访问地址,用于捆绑到Datanode上
5、分片规则:前面讲了数据切分,一个大表被分成若干个分片表,就需要一定的规则,这样按照某种业务规则把数据分到某个分片的规则就是分片规则,数据切分选择合适的分片规则非常重要,将极大的避免后续数据处理的难点
数据库 | mysql-1 | mysql-2 |
---|---|---|
实例 | 3307 3308 3309 3310 | 3307 3308 3309 3310 |
互为主从 | mysql-1:3307←→mysql-2:3307 | mysql-1:3308←→mysql-2:3308 |
单向主从 | mysql-1:3309—→mysql-1:3307 | mysql-2:3309—→mysql-2:3307 |
单向主从 | mysql-1:3310—→mysql-1:3308 | mysql-2:3310—→mysql-2:3308 |
shard1 | shard2 | |
---|---|---|
Master | mysql-1:3307 | mysql-2:3308 |
slave1 | mysql-1:3309 | mysql-2:3310 |
Standby Master | mysql-2:3307 | mysql-1:3308 |
slave2 | mysql-2:3309 | mysql-1:3310 |
[root@mysql-1 ~]# mkdir -p /data/33{07..10}/data
[root@mysql-1 ~]# mysqld --initialize-insecure --user=mysql --datadir=/data/3307/data --basedir=/usr/local/mysql
[root@mysql-1 ~]# mysqld --initialize-insecure --user=mysql --datadir=/data/3308/data --basedir=/usr/local/mysql
[root@mysql-1 ~]# mysqld --initialize-insecure --user=mysql --datadir=/data/3309/data --basedir=/usr/local/mysql
[root@mysql-1 ~]# mysqld --initialize-insecure --user=mysql --datadir=/data/3310/data --basedir=/usr/local/mysql
[root@mysql-1 ~]# cat >/data/3307/my.cnf<
[mysqld]
basedir=/usr/local/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
[root@mysql-1 ~]# cat >/data/3308/my.cnf<
[mysqld]
basedir=/usr/local/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
[root@mysql-1 ~]# cat >/data/3309/my.cnf<
[mysqld]
basedir=/usr/local/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
[root@mysql-1 ~]# cat >/data/3310/my.cnf<
[mysqld]
basedir=/usr/local/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
[root@mysql-1 ~]# cat >/etc/systemd/system/mysqld3307.service<
[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=/usr/local/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
LimitNOFILE = 5000
EOF
[root@mysql-1 ~]# cat >/etc/systemd/system/mysqld3308.service<
[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=/usr/local/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf
LimitNOFILE = 5000
EOF
[root@mysql-1 ~]# cat >/etc/systemd/system/mysqld3309.service<
[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=/usr/local/mysql/bin/mysqld --defaults-file=/data/3309/my.cnf
LimitNOFILE = 5000
EOF
[root@mysql-1 ~]# cat >/etc/systemd/system/mysqld3310.service<
[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=/usr/local/mysql/bin/mysqld --defaults-file=/data/3310/my.cnf
LimitNOFILE = 5000
EOF
[root@mysql-2 ~]# cat >/data/3307/my.cnf<
[mysqld]
basedir=/usr/local/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
[root@mysql-2 ~]# cat >/data/3308/my.cnf<
[mysqld]
basedir=/usr/local/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
[root@mysql-2 ~]# cat >/data/3309/my.cnf<
[mysqld]
basedir=/usr/local/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
[root@mysql-2 ~]# cat >/data/3310/my.cnf<
[mysqld]
basedir=/usr/local/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
[root@mysql-2 ~]# cat >/etc/systemd/system/mysqld3307.service<
[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=/usr/local/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
LimitNOFILE = 5000
EOF
[root@mysql-2 ~]# cat >/etc/systemd/system/mysqld3308.service<
[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=/usr/local/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf
LimitNOFILE = 5000
EOF
[root@mysql-2 ~]# cat >/etc/systemd/system/mysqld3309.service<
[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=/usr/local/mysql/bin/mysqld --defaults-file=/data/3309/my.cnf
LimitNOFILE = 5000
EOF
[root@mysql-2 ~]# cat >/etc/systemd/system/mysqld3310.service<
[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=/usr/local/mysql/bin/mysqld --defaults-file=/data/3310/my.cnf
LimitNOFILE = 5000
EOF
[root@mysql-1 ~]# chown -R mysql.mysql /data/*
[root@mysql-1 ~]# systemctl start mysqld3307
[root@mysql-1 ~]# systemctl start mysqld3308
[root@mysql-1 ~]# systemctl start mysqld3309
[root@mysql-1 ~]# systemctl start mysqld3310
[root@mysql-1 ~]# netstat -anpt | grep mysql
tcp6 0 0 :::3307 :::* LISTEN 17583/mysqld
tcp6 0 0 :::3308 :::* LISTEN 17590/mysqld
tcp6 0 0 :::3309 :::* LISTEN 17597/mysqld
tcp6 0 0 :::3310 :::* LISTEN 17685/mysqld
[root@mysql-1 ~]# mysql -S /data/3307/mysql.sock -e "show variables like 'server_id'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 7 |
+---------------+-------+
[root@mysql-1 ~]# mysql -S /data/3308/mysql.sock -e "show variables like 'server_id'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 8 |
+---------------+-------+
[root@mysql-1 ~]# mysql -S /data/3309/mysql.sock -e "show variables like 'server_id'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 9 |
+---------------+-------+
[root@mysql-1 ~]# mysql -S /data/3310/mysql.sock -e "show variables like 'server_id'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 10 |
+---------------+-------+
[root@mysql-2 ~]# mysql -S /data/3307/mysql.sock -e "show variables like 'server_id'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 17 |
+---------------+-------+
[root@mysql-2 ~]# mysql -S /data/3308/mysql.sock -e "show variables like 'server_id'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 18 |
+---------------+-------+
[root@mysql-2 ~]# mysql -S /data/3309/mysql.sock -e "show variables like 'server_id'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 19 |
+---------------+-------+
[root@mysql-2 ~]# mysql -S /data/3310/mysql.sock -e "show variables like 'server_id'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 20 |
+---------------+-------+
1、mysql-1:3307←→mysql-2:3307
[root@mysql-2 ~]# mysql -S /data/3307/mysql.sock -e "grant replication slave on *.* to repl@'192.168.1.%' identified by '123';"
[root@mysql-2 ~]# mysql -S /data/3307/mysql.sock -e "grant all on *.* to root@'192.168.1.%' identified by '123' with grant option;"
[root@mysql-1 ~]# mysql -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.20', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
[root@mysql-1 ~]# mysql -S /data/3307/mysql.sock -e "start slave;"
[root@mysql-1 ~]# mysql -S /data/3307/mysql.sock -e "show slave status\G"
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.20
Master_User: repl
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1045
Relay_Log_File: mysql-1-relay-bin.000002
Relay_Log_Pos: 1258
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@mysql-2 ~]# mysql -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.10', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
[root@mysql-2 ~]# mysql -S /data/3307/mysql.sock -e "start slave;"
[root@mysql-2 ~]# mysql -S /data/3307/mysql.sock -e "show slave status\G"
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.10
Master_User: repl
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1045
Relay_Log_File: mysql-2-relay-bin.000002
Relay_Log_Pos: 414
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
2、mysql-1:3308←→mysql-2:3308
[root@mysql-1 ~]# mysql -S /data/3308/mysql.sock -e "grant replication slave on *.* to repl@'192.168.1.%' identified by '123';"
[root@mysql-1 ~]# mysql -S /data/3308/mysql.sock -e "grant all on *.* to root@'192.168.1.%' identified by '123' with grant option;"
[root@mysql-2 ~]# mysql -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.10', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
[root@mysql-2 ~]# mysql -S /data/3308/mysql.sock -e "start slave;"
[root@mysql-2 ~]# mysql -S /data/3308/mysql.sock -e "show slave status\G"
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.10
Master_User: repl
Master_Port: 3308
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 755
Relay_Log_File: mysql-2-relay-bin.000002
Relay_Log_Pos: 968
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@mysql-1 ~]# mysql -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.20', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
[root@mysql-1 ~]# mysql -S /data/3308/mysql.sock -e "start slave;"
[root@mysql-1 ~]# mysql -S /data/3308/mysql.sock -e "show slave status\G"
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.20
Master_User: repl
Master_Port: 3308
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 755
Relay_Log_File: mysql-1-relay-bin.000002
Relay_Log_Pos: 414
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
1、mysql-1:3309—→mysql-1:3307
[root@mysql-1 ~]# mysql -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.10', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
[root@mysql-1 ~]# mysql -S /data/3309/mysql.sock -e "start slave;"
[root@mysql-1 ~]# mysql -S /data/3309/mysql.sock -e "show slave status\G"
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.10
Master_User: repl
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1045
Relay_Log_File: mysql-1-relay-bin.000002
Relay_Log_Pos: 1258
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
2、mysql-2:3309—→mysql-2:3307
[root@mysql-2 ~]# mysql -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.20', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
[root@mysql-2 ~]# mysql -S /data/3309/mysql.sock -e "start slave;"
[root@mysql-2 ~]# mysql -S /data/3309/mysql.sock -e "show slave status\G"
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.20
Master_User: repl
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1045
Relay_Log_File: mysql-2-relay-bin.000002
Relay_Log_Pos: 1258
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
3、mysql-2:3310—→mysql-2:3308
[root@mysql-2 ~]# mysql -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.20', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
[root@mysql-2 ~]# mysql -S /data/3310/mysql.sock -e "start slave;"
[root@mysql-2 ~]# mysql -S /data/3310/mysql.sock -e "show slave status\G"
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.20
Master_User: repl
Master_Port: 3308
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 755
Relay_Log_File: mysql-2-relay-bin.000002
Relay_Log_Pos: 968
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
4、mysql-1:3310—→mysql-1:3308
[root@mysql-1 ~]# mysql -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.10', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
[root@mysql-1 ~]# mysql -S /data/3310/mysql.sock -e "start slave;"
[root@mysql-1 ~]# mysql -S /data/3310/mysql.sock -e "show slave status\G"
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.10
Master_User: repl
Master_Port: 3308
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 755
Relay_Log_File: mysql-1-relay-bin.000002
Relay_Log_Pos: 968
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@mysql-1 ~]# mysql -S /data/3307/mysql.sock -e "show slave status\G"|grep Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@mysql-1 ~]# mysql -S /data/3308/mysql.sock -e "show slave status\G"|grep Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@mysql-1 ~]# mysql -S /data/3309/mysql.sock -e "show slave status\G"|grep Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@mysql-1 ~]# mysql -S /data/3310/mysql.sock -e "show slave status\G"|grep Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@mysql-2 ~]# mysql -S /data/3307/mysql.sock -e "show slave status\G"|grep Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@mysql-2 ~]# mysql -S /data/3308/mysql.sock -e "show slave status\G"|grep Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@mysql-2 ~]# mysql -S /data/3309/mysql.sock -e "show slave status\G"|grep Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@mysql-2 ~]# mysql -S /data/3310/mysql.sock -e "show slave status\G"|grep Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@mysql-1 ~]# yum -y install java
[root@mysql-1 ~]# wget http://dl.mycat.org.cn/1.6.7.4/Mycat-server-1.6.7.4-release/Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz
[root@mysql-1 ~]# tar zxf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz -C /usr/local/
[root@mysql-1 ~]# vim /etc/profile
export PATH=/usr/local/mycat/bin:$PATH
[root@mysql-1 ~]# source /etc/profile
[root@mysql-1 ~]# mycat start
Starting Mycat-server...
[root@mysql-1 ~]# mysql -uroot -p123456 -h 127.0.0.1 -P8066
mysql>
Mycat的默认端口号为:8066
Mycat的读写分离是建立在Mysq的主从复制的基础上的,修改配置文件 schema.xml
[root@mysql-1 ~]# cd /usr/local/mycat/conf
[root@mysql-1 conf]# mv schema.xml schema.xml.bak
[root@mysql-1 conf]# vim schema.xml
<!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="mysql-1" database= "world" />
#后端主机定义
<dataHost name="mysql-1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db1" url="192.168.1.10:3307" user="root" password="123">
<readHost host="db2" url="192.168.1.10:3309" user="root" password="123" />
</writeHost>
</dataHost>
</mycat:schema>
[root@mysql-1 ~]# mycat start
Starting Mycat-server...
Mycat-server is already running.
[root@mysql-1 ~]# mysql -uroot -p123456 -h 192.168.1.10 -P 8066
Your MySQL connection id is 2
Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (OpenCloudDB)
mysql> select @@server_id; //测试读
+-------------+
| @@server_id |
+-------------+
| 9 |
+-------------+
1 row in set (0.05 sec)
mysql> begin;select @@server_id;commit; //测试写
Query OK, 0 rows affected (0.01 sec)
+-------------+
| @@server_id |
+-------------+
| 7 |
+-------------+
属性 | 说明 |
---|---|
schema name | 定义mycat实例中的逻辑库,mycat可以有多个逻辑库,每个逻辑库都有自己的相关配置。可以使用schema标签来划分这些不同的逻辑库 |
dataNode | 该标签用于绑定逻辑库到某个具体的database上 |
dataHost | 定义了具体数据库实例,读写分离配置和心跳语句 |
属性 | 说明 |
---|---|
maxCon | 最大的并发连接数 |
minCon | mycat在启动之后,会在后端节点上自动开启的连接线程 |
tempReadHostAvailable | 这个一主一从时(1个writehost,1个readhost时),可以开启这个参数,如果2个writehost,2个readhost时 |
|
监测心跳 |
[root@mysql-1 ~]# cd /usr/local/mycat/conf/
[root@mysql-1 conf]# vim schema.xml
<!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="mysql-1" database= "world" />
<dataHost name="mysql-1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
#真正的写节点,负责写操作
<writeHost host="db1" url="192.168.1.10:3307" user="root" password="123">
<readHost host="db2" url="192.168.1.10:3309" user="root" password="123" />
</writeHost>
#准备写节点,负责读,当192.168.1.10:3307宕掉,会切换成为真正的写节点
<writeHost host="db3" url="192.168.1.20:3307" user="root" password="123">
<readHost host="db4" url="192.168.1.20:3309" user="root" password="123" />
</writeHost>
</dataHost>
</mycat:schema>
取值 | balance(负载均衡类型) | writeType(负载均衡类型) | switchType |
---|---|---|---|
-1 | / | / | 不自动切换 |
0 | 不开启读写分离机制,所有读操作都发送到当前可用的writeHost上 | / | 默认值,自动切换 |
1 | 全部的readHost与standby writeHost参与select语句的负载均衡 | 所有写操作发送到配置的第一个writeHost | 基于MySQL主从同步的状态决定是否切换 ,心跳语句为 show slave status |
2 | 所有读操作都随机的在writeHost、readhost上分发 | 所有写操作都随机的发送到配置的writeHost | / |
作用:是指按照业务将表进行分类, 分布到不同的数据库上面,这样也就将数据或者说压力分担到不同的库上面
如何划分表分库的原则: 有紧密关联关系的表应该在一个库里,相互没有关联关系的表可以分到不同的库里。
[root@mysql-1 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">
<table name="user" dataNode="sh1"/>
<table name="order_t" dataNode="sh2"/>
</schema>
<dataNode name="sh1" dataHost="mysql-1" database= "taobao" />
<dataNode name="sh2" dataHost="mysql-2" database= "taobao" />
<dataHost name="mysql-1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db1" url="192.168.1.10:3307" user="root" password="123">
<readHost host="db2" url="192.168.1.10:3309" user="root" password="123" />
</writeHost>
<writeHost host="db3" url="192.168.1.20:3307" user="root" password="123">
<readHost host="db4" url="192.168.1.20:3309" user="root" password="123" />
</writeHost>
</dataHost>
<dataHost name="mysql-2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db1" url="192.168.1.10:3308" user="root" password="123">
<readHost host="db2" url="192.168.1.10:3310" user="root" password="123" />
</writeHost>
<writeHost host="db3" url="192.168.1.20:3308" user="root" password="123">
<readHost host="db4" url="192.168.1.20:3310" user="root" password="123" />
</writeHost>
</dataHost>
</mycat:schema>
目的:将大数据量进行分布存储,提供均衡的访问路由
是指按照某个字段的某种规则来分散到多个库之中, 每个表中包含一部分数据。简单来说,我们可以将数据的水平切分理解为是按照数据行的切分,就 是将表中的某些行切分到一个数据库,而另外的某些行又切分到其他的数据库中。
目的:根据某个字段的数值范围来确定这些数据到底存放在哪一个分片上
[root@mysql-1 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">
<table name="t3" dataNode="sh1,sh2" rule="auto-sharding-long" />
[root@mysql-1 conf]# vim rule.xml
<tableRule name="auto-sharding-long">
<rule>
<columns>id</columns> //以id作为数值范围
<algorithm>rang-long</algorithm>
</rule>
<function name="rang-long"
class="io.mycat.route.function.AutoPartitionByLong">
<property name="mapFile">autopartition-long.txt</property>
</function>
[root@mysql-1 conf]# vim autopartition-long.txt
# range start-end ,data node index
# K=1000,M=10000. K=千,M=万
0-500M=0
500M-1000M=1
注意:分片规则配置文件autopartition-long.txt中配置的节点要跟dataNode数量对应上,不然可能会报错
目的:切分规则根据配置中输入的数值n。此种分片规则将数据分成n份,从而将数据均匀的分布于各个节点上
取余分片方式:分片键(一个列)与节点数量进行取余,得到余数,将数据写入对应节点
[root@mysql-1 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">
<table name="t4" dataNode="sh1,sh2" rule="mod-long" />
[root@mysql-1 conf]# 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> //2即为节点数
</function>
目的:将符合条件的记录分配到对应节点上,不满足条件的分配到默认节点
[root@mysql-1 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">
<table name="t5" dataNode="sh1,sh2" rule="sharding-by-intfile" />
[root@mysql-1 conf]# vim rule.xml
<tableRule name="sharding-by-intfile">
<rule>
<columns>sharding_id</columns>
<algorithm>hash-int</algorithm>
</rule>
<function name="hash-int"
class="io.mycat.route.function.PartitionByFileMap">
<property name="mapFile">partition-hash-int.txt</property>
<property name="type">1</property> //支持字符串(中文)
<property name="defaultNode">0</property>
</function>
[root@mysql-1 conf]# vim partition-hash-int.txt
10000=0
10010=1
DEFAULT_NODE=1 //情况之外
参数 | 说明 |
---|---|
columns | 标识将要分片的表字段 |
algorithm | 分片函数 |
mapFile | 标识配置文件名称 |
指在所有节点都保存一份一样的数据
[root@mysql-1 conf]# vim schema.xml
<table name="t6" primaryKey="id" type="global" dataNode="sh1,sh2" />