Mycat是一个开源的分布式数据库中间件,主要用于提供数据库的分库分表、读写分离、负载均衡等功能。以下是Mycat的几个主要作用:
- 分库分表:Mycat可以将单个数据库拆分成多个小数据库,并将数据分布在不同的节点上,以提高数据库的扩展性和性能。同时,Mycat还支持数据的水平分表,将大表拆分成多个小表,分散数据存储和查询压力。
- 读写分离:Mycat可以根据配置将读和写操作分发到不同的数据库节点上,以提升读取性能和并发处理能力。通过将读操作分布到多个节点,可以平衡负载,减轻写操作对主库的压力,从而提高整体系统的性能和可靠性。
- 负载均衡:Mycat可以在多个数据库节点之间实现负载均衡,通过智能地分发数据库请求,确保每个节点得到合理的负载。这有助于提高系统的吞吐量和响应速度,并提供较高的可伸缩性和可用性。
- 数据分片:Mycat允许根据指定的字段将数据分片存储在不同的节点上,从而实现数据的分布式存储和查询。这种方式可以显著提高大规模数据的处理能力和查询效率。
- 数据路由和透明化:Mycat可以根据SQL语句中的条件自动判断数据应该路由到哪个节点,并将查询结果透明地返回给客户端,使得客户端无需关心具体的数据库分布和细节。
1. MyCAT架构部署
1.1. 192.168.50.88:
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service
1.1.1. 3306实例安装:
创建MySQL虚拟用户
useradd-s/sbin/nologin-M mysql
创建目录
mkdir -p /server/tools
mkdir -p /opt/mysql
mkdir -p /data/mysql/mysql3306/{data,logs}
cd /server/tools
解压二进制包
tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
将软件部分移动到指定目录
mv mysql-5.7.26-linux-glibc2.12-x86_64 /opt/mysql/mysql-5.7.26
创建软连接
ln -s /opt/mysql/mysql-5.7.26/ /usr/local/mysql
删除mariadb
rpm -e --nodeps mariadb-libs
配置文件整理
vim /data/mysql/mysql3306/my3306.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/mysql3306/data
socket = /data/mysql/mysql3306/mysql.sock
server_id = 1
port = 3306
log_error=/data/mysql/mysql3306/logs/error.log
log_bin=/data/mysql/mysql3306/logs/mysql-bin
binlog_format=row
gtid_mode=on
enforce_gtid_consistency=true
log_slave_updates=1
max_connections=1024
wait_timeout=60
sort_buffer_size=2M
max_allowed_packet=32M
join_buffer_size=2M
innodb_buffer_pool_size=128M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=32M
innodb_log_file_size=128M
innodb_log_files_in_group=2
binlog_cache_size=2M
max_binlog_cache_size=8M
max_binlog_size=512M
expire_logs_days=7
slow_query_log=on
slow_query_log_file=/data/mysql/mysql3306/logs/slow.log
long_query_time=0.5
log_queries_not_using_indexes=1
安装MySQL依赖包
yum install libaio-devel -y
yum install numactl -y
。更改MySQL相关目录的用户组
chown -R mysql:mysql /usr/local/mysql/*
chown -R mysql:mysql /data/*
初始化数据库
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3306/data
加入环境变量
vim /etc/profile
export PATH="/usr/local/mysql/bin:$PATH"
source /etc/profile
启动MySQL
mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf &
查看是否启动成功
netstat -lntup |grep mysql

连接mysql
mysql -S /data/mysql/mysql3306/mysql.sock

1.1.2. 3307实例安装:
创建数据目录
mkdir -p /data/mysql/mysql3307/{data,logs}
创建配置文件
vim /data/mysql/mysql3307/my3307.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/mysql3307/data
socket = /data/mysql/mysql3307/mysql.sock
server_id = 2
port = 3307
log_error=/data/mysql/mysql3307/logs/error.log
log_bin=/data/mysql/mysql3307/logs/mysql-bin
binlog_format=row
gtid_mode=on
enforce_gtid_consistency=true
log_slave_updates=1
max_connections=1024
wait_timeout=60
sort_buffer_size=2M
max_allowed_packet=32M
join_buffer_size=2M
innodb_buffer_pool_size=128M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=32M
innodb_log_file_size=128M
innodb_log_files_in_group=2
binlog_cache_size=2M
max_binlog_cache_size=8M
max_binlog_size=512M
expire_logs_days=7
slow_query_log=on
slow_query_log_file=/data/mysql/mysql3307/logs/slow.log
long_query_time=0.5
log_queries_not_using_indexes=1
更改MySQL相关目录的用户组
chown -R mysql:mysql /data/*
初始化数据库
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3307/my3307.cnf --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3307/data
多实例的启动关闭
mysqld --defaults-file=/data/mysql/mysql3307/my3307.cnf &
1.1.3. 3308实例安装:
创建数据目录
mkdir -p /data/mysql/mysql3308/{data,logs}
创建配置文件
vim /data/mysql/mysql3308/my3308.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/mysql3308/data
socket = /data/mysql/mysql3308/mysql.sock
server_id = 3
port = 3308
log_error=/data/mysql/mysql3308/logs/error.log
log_bin=/data/mysql/mysql3308/logs/mysql-bin
binlog_format=row
gtid_mode=on
enforce_gtid_consistency=true
log_slave_updates=1
max_connections=1024
wait_timeout=60
sort_buffer_size=2M
max_allowed_packet=32M
join_buffer_size=2M
innodb_buffer_pool_size=128M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=32M
innodb_log_file_size=128M
innodb_log_files_in_group=2
binlog_cache_size=2M
max_binlog_cache_size=8M
max_binlog_size=512M
expire_logs_days=7
slow_query_log=on
slow_query_log_file=/data/mysql/mysql3308/logs/slow.log
long_query_time=0.5
log_queries_not_using_indexes=1
更改MySQL相关目录的用户组
chown -R mysql:mysql /data/*
初始化数据库
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3308/my3308.cnf --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3308/data
多实例的启动关闭
mysqld --defaults-file=/data/mysql/mysql3308/my3308.cnf &
1.1.4. 3309实例安装:
创建数据目录
mkdir -p /data/mysql/mysql3309/{data,logs}
创建配置文件
vim /data/mysql/mysql3309/my3309.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/mysql3309/data
socket = /data/mysql/mysql3309/mysql.sock
server_id = 4
port = 3309
log_error=/data/mysql/mysql3309/logs/error.log
log_bin=/data/mysql/mysql3309/logs/mysql-bin
binlog_format=row
gtid_mode=on
enforce_gtid_consistency=true
log_slave_updates=1
max_connections=1024
wait_timeout=60
sort_buffer_size=2M
max_allowed_packet=32M
join_buffer_size=2M
innodb_buffer_pool_size=128M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=32M
innodb_log_file_size=128M
innodb_log_files_in_group=2
binlog_cache_size=2M
max_binlog_cache_size=8M
max_binlog_size=512M
expire_logs_days=7
slow_query_log=on
slow_query_log_file=/data/mysql/mysql3309/logs/slow.log
long_query_time=0.5
log_queries_not_using_indexes=1
更改MySQL相关目录的用户组
chown -R mysql:mysql /data/*
初始化数据库
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3309/my3309.cnf --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3309/data
多实例的启动关闭
mysqld --defaults-file=/data/mysql/mysql3309/my3309.cnf &

1.2. 192.168.50.89:
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service
1.2.1. 3306实例安装:
创建MySQL虚拟用户
useradd-s/sbin/nologin-M mysql
创建目录
mkdir -p /server/tools
mkdir -p /opt/mysql
mkdir -p /data/mysql/mysql3306/{data,logs}
cd /server/tools
解压二进制包
tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
将软件部分移动到指定目录
mv mysql-5.7.26-linux-glibc2.12-x86_64 /opt/mysql/mysql-5.7.26
创建软连接
ln -s /opt/mysql/mysql-5.7.26/ /usr/local/mysql
删除mariadb
rpm -e --nodeps mariadb-libs
配置文件整理
vim /data/mysql/mysql3306/my3306.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/mysql3306/data
socket = /data/mysql/mysql3306/mysql.sock
server_id = 11
port = 3306
log_error=/data/mysql/mysql3306/logs/error.log
log_bin=/data/mysql/mysql3306/logs/mysql-bin
binlog_format=row
gtid_mode=on
enforce_gtid_consistency=true
log_slave_updates=1
max_connections=1024
wait_timeout=60
sort_buffer_size=2M
max_allowed_packet=32M
join_buffer_size=2M
innodb_buffer_pool_size=128M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=32M
innodb_log_file_size=128M
innodb_log_files_in_group=2
binlog_cache_size=2M
max_binlog_cache_size=8M
max_binlog_size=512M
expire_logs_days=7
slow_query_log=on
slow_query_log_file=/data/mysql/mysql3306/logs/slow.log
long_query_time=0.5
log_queries_not_using_indexes=1
安装MySQL依赖包
yum install libaio-devel -y
yum install numactl -y
更改MySQL相关目录的用户组
chown -R mysql:mysql /usr/local/mysql/*
chown -R mysql:mysql /data/*
初始化数据库
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3306/data
加入环境变量
vim /etc/profile
export PATH="/usr/local/mysql/bin:$PATH"
source /etc/profile
启动MySQL
mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf &
查看是否启动成功
netstat -lntup |grep mysql

连接mysql
mysql -S /data/mysql/mysql3306/mysql.sock

1.2.2. 3307实例安装:
创建数据目录
mkdir -p /data/mysql/mysql3307/{data,logs}
创建配置文件
vim /data/mysql/mysql3307/my3307.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/mysql3307/data
socket = /data/mysql/mysql3307/mysql.sock
server_id = 12
port = 3307
log_error=/data/mysql/mysql3307/logs/error.log
log_bin=/data/mysql/mysql3307/logs/mysql-bin
binlog_format=row
gtid_mode=on
enforce_gtid_consistency=true
log_slave_updates=1
max_connections=1024
wait_timeout=60
sort_buffer_size=2M
max_allowed_packet=32M
join_buffer_size=2M
innodb_buffer_pool_size=128M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=32M
innodb_log_file_size=128M
innodb_log_files_in_group=2
binlog_cache_size=2M
max_binlog_cache_size=8M
max_binlog_size=512M
expire_logs_days=7
slow_query_log=on
slow_query_log_file=/data/mysql/mysql3307/logs/slow.log
long_query_time=0.5
log_queries_not_using_indexes=1
更改MySQL相关目录的用户组
chown -R mysql:mysql /data/*
初始化数据库
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3307/my3307.cnf --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3307/data
多实例的启动关闭
mysqld --defaults-file=/data/mysql/mysql3307/my3307.cnf &
1.2.3. 3308实例安装:
创建数据目录
mkdir -p /data/mysql/mysql3308/{data,logs}
创建配置文件
vim /data/mysql/mysql3308/my3308.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/mysql3308/data
socket = /data/mysql/mysql3308/mysql.sock
server_id = 13
port = 3308
log_error=/data/mysql/mysql3308/logs/error.log
log_bin=/data/mysql/mysql3308/logs/mysql-bin
binlog_format=row
gtid_mode=on
enforce_gtid_consistency=true
log_slave_updates=1
max_connections=1024
wait_timeout=60
sort_buffer_size=2M
max_allowed_packet=32M
join_buffer_size=2M
innodb_buffer_pool_size=128M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=32M
innodb_log_file_size=128M
innodb_log_files_in_group=2
binlog_cache_size=2M
max_binlog_cache_size=8M
max_binlog_size=512M
expire_logs_days=7
slow_query_log=on
slow_query_log_file=/data/mysql/mysql3308/logs/slow.log
long_query_time=0.5
log_queries_not_using_indexes=1
更改MySQL相关目录的用户组
chown -R mysql:mysql /data/*
初始化数据库
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3308/my3308.cnf --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3308/data
多实例的启动关闭
mysqld --defaults-file=/data/mysql/mysql3308/my3308.cnf &
1.2.4. 3309实例安装:
创建数据目录
mkdir -p /data/mysql/mysql3309/{data,logs}
创建配置文件
vim /data/mysql/mysql3309/my3309.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/mysql3309/data
socket = /data/mysql/mysql3309/mysql.sock
server_id = 14
port = 3309
log_error=/data/mysql/mysql3309/logs/error.log
log_bin=/data/mysql/mysql3309/logs/mysql-bin
binlog_format=row
gtid_mode=on
enforce_gtid_consistency=true
log_slave_updates=1
max_connections=1024
wait_timeout=60
sort_buffer_size=2M
max_allowed_packet=32M
join_buffer_size=2M
innodb_buffer_pool_size=128M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=32M
innodb_log_file_size=128M
innodb_log_files_in_group=2
binlog_cache_size=2M
max_binlog_cache_size=8M
max_binlog_size=512M
expire_logs_days=7
slow_query_log=on
slow_query_log_file=/data/mysql/mysql3309/logs/slow.log
long_query_time=0.5
log_queries_not_using_indexes=1
更改MySQL相关目录的用户组
chown -R mysql:mysql /data/*
初始化数据库
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3309/my3309.cnf --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3309/data
。多实例的启动关闭
mysqld --defaults-file=/data/mysql/mysql3309/my3309.cnf &

1.3. 创建复制用户
创建复制用户 88:3306执行:
mysql -S /data/mysql/mysql3306/mysql.sock
create user repl@'%' identified by '123456';
grant replication slave on *.* to repl@'%';

创建复制用户 88:3307执行:
mysql -S /data/mysql/mysql3307/mysql.sock
create user repl@'%' identified by '123456';
grant replication slave on *.* to repl@'%';
1.3.1. m1<--->sm1
192.168.50.88:3306 <--->192.168.50.89:3306
192.168.50.88:3306 --->192.168.50.88:3308
192.168.50.89:3306 --->192.168.50.89:3308
192.168.50.89:3306
mysql -S /data/mysql/mysql3306/mysql.sock
CHANGE MASTER TO
MASTER_HOST='192.168.50.88',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
master_auto_position=1,
MASTER_CONNECT_RETRY=10;
start slave;
192.168.50.88:3306
CHANGE MASTER TO
MASTER_HOST='192.168.50.89',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
master_auto_position=1,
MASTER_CONNECT_RETRY=10;
start slave;
192.168.50.88:3308
CHANGE MASTER TO
MASTER_HOST='192.168.50.88',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
master_auto_position=1,
MASTER_CONNECT_RETRY=10;
start slave;
192.168.50.89:3308
CHANGE MASTER TO
MASTER_HOST='192.168.50.89',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
master_auto_position=1,
MASTER_CONNECT_RETRY=10;
start slave;
1.3.2. m2<--->sm2
192.168.50.89:3307 <--->192.168.50.88:3307
192.168.50.89:3307 --->192.168.50.89:3309
192.168.50.88:3307 --->192.168.50.88:3309
192.168.50.89:3307
CHANGE MASTER TO
MASTER_HOST='192.168.50.88',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_PORT=3307,
master_auto_position=1,
MASTER_CONNECT_RETRY=10;
start slave;
192.168.50.88:3307
CHANGE MASTER TO
MASTER_HOST='192.168.50.89',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_PORT=3307,
master_auto_position=1,
MASTER_CONNECT_RETRY=10;
start slave;
192.168.50.89:3309
CHANGE MASTER TO
MASTER_HOST='192.168.50.89',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_PORT=3307,
master_auto_position=1,
MASTER_CONNECT_RETRY=10;
start slave;
192.168.50.88:3309
CHANGE MASTER TO
MASTER_HOST='192.168.50.88',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_PORT=3307,
master_auto_position=1,
MASTER_CONNECT_RETRY=10;
start slave;
1.4. MyCAT安装
预先安装Java运行环境
yum install -y java
java -version


下载Mycat-server-xxxxx.linux.tar.gz
wget http://dl.mycat.org.cn/1.6.5/Mycat-server-1.6.5-release-20180122220033-linux.tar.gz
解压文件
tar zxf Mycat-server-1.6.5.tar.gz
mv mycat/ /usr/local/
配置环境变量
vim /etc/profile
PATH="/usr/local/mysql/bin:/usr/local/mycat/bin:$PATH"
. /etc/profile

启动和连接
mycat start
连接mycat:
mysql -uroot -p123456 -h 127.0.0.1 -P8066 对外连接(root 123456是server.xml配置文件中的)
9066 对内端口


查看mycat自带的表
use TESTDB; 默认库
show tables;

配置文件介绍
schema.xml ----->主配置文件(读写分离、高可用、分表、节点控制)
server.xml ----->mycat软件本身相关的配置
rule.xml ----->分片规则配置文件(分片规则列表、使用方法)

2. MyCAT读写分离架构
88:3306:
create user root@'%' identified by '123456';
grant all on *.* to root@'%';
89:3307:
create user root@'%' identified by '123456';
grant all on *.* to root@'%';
1主1从:读写分离架构
192.168.50.88
3306 写 id1号
3308 读 id3号
cd /usr/local/mycat/conf/
cp 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= "testhf" />
<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="192.168.50.88:3306" user="root" password="123456">
<readHost host="db2" url="192.168.50.88:3308" user="root" password="123456" />
</writeHost>
</dataHost>
</mycat:schema>
重启:mycat restart
连接:mysql -uroot -p123456 -h192.168.50.88 -P8066

验证:
再连接:mysql -uroot -p123456 -h192.168.50.88 -P8066 验证
读在3308 上面,写在3306上面 --【如果写节点3306宕机,读3308也无法提供服务】--【mqsql -S /data/mysql/mysql306/mysql.sock shutdown】
mysql>select @@server_id; --测试读,在 id=3 的库
mysql> begin;select @@server_id;commit; --测试写,在 id=1 的库

ERROR 3009 (HY000): java.lang.IllegalArgumentException: Invalid DataSource:0
create database testhf;
3. MyCAT高可用读写分离架构

88:3306、3308
89:3306、3308
写:1
读:11/3/13(在 3 个实例上轮切)
cd /usr/local/mycat/conf/
cp schema.xml schema.xml.bak2
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= "testhf" />
<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="192.168.50.88:3306" user="root" password="123456">
<readHost host="db2" url="192.168.58.88:3308" user="root" password="123456" />
</writeHost>
<writeHost host="db3" url="192.168.50.89:3306" user="root" password="123456">
<readHost host="db4" url="192.168.50.89:3308" user="root" password="123456" />
</writeHost>
</dataHost>
</mycat:schema>
重启:mycat restart
连接:mysql -uroot -p123456 -h192.168.50.88 -P8066
验证:
写:1
读:11/3/13(在 3 个实例上轮切)

--【模拟 88:3306宕机 >mysql -S /data/mysql/mysql3306/mysql.sock shutdown】
再连接:mysql -uroot -p123456 -h192.168.50.88 -P8066 验证


4. schema.xml配置文件详解--分片
配置:
cd /usr/local/mysql/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="dn1">
<table name="country" dataNode="dn2"/>
</schema>
<dataNode name="dn1" dataHost="localhost1" database= "testhf" />
<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="192.168.50.88:3306" user="root" password="123456">
<readHost host="db2" url="192.168.50.88:3308" user="root" password="123456" />
</writeHost>
</dataHost>
</mycat:schema>
(1)schema标签:用于定义逻辑库
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"> </schema>
dataNode="dn1" 指逻辑库默认的分片
checkSQLschema="false"
当应用执行【select *from TESTDB.tf_user;】时(表名前指定了mycat逻辑库名),两种取值:
true:mycat会把语句转换为【select * from tf_user;】
false:会报错
sqlMaxLimit="100"
相当于sql的结果集中,加上【limit N】。如果sql本身已经指定limit,则以sql指定的为准。
(2)table标签:是shcema标签的子标签,用于定义逻辑表及逻辑表的分片规则
<table name="country" dataNode="sh2"/>
name属性:逻辑表的表名,同一个schema表名必须唯一。
dataNode属性:定义这个逻辑表所属的分片,用英文逗号间隔,如:dataNode="dn1,dn2"
rule属性:该属性用于指定逻辑表要使用的规则名字,规则名字在 rule.xml 中定义,必须与 tableRule 标签中 name 属性属性值一一对应。
ruleRequired属性:该属性用于指定表是否绑定分片规则,如果配置为 true,但没有配置具体 rule 的话 ,程序会报错
primaryKey属性:指定该逻辑表对应真实表的主键。MyCat会缓存主键(通过primaryKey属性配置)与具体 dataNode的信息。当分片规则使用非主键进行分片时,那么在使用主键进行查询时,MyCat就会通过缓存先确定记录在哪个dataNode上,然后再在该dataNode上执行查询。如果缓存并没有命中的话,还是会发送语句给所有的dataNode。
关于Mycat的主键缓存,其机制是:当根据主键查询的SQL语句第一次执行时,Mycat会对其结果进行分析,确定该主键在哪个分片上,并进行该主键到分片ID的缓存。通过连接MyCAT的9066管理端口,执行show@@cache,可以显示当前缓存的使用情况。可在sql执行前后的2个时间点执行show @@cache,通过结果信息中的LAST_PUT和LAST_ACCESS列,判断相应表的缓存是否有被更新过。
type属性:该属性定义了逻辑表的类型,目前逻辑表只有"全局表"和"普通表"两种类型。对应的配置:全局表:global。普通表:不指定该值为 global 的所有表。
(3)dataNode标签:
<dataNode name="dn1" dataHost="localhost1" database= "testhf" />
name 属性:指定分片的名字
dataHost 属性:定义该分片属于哪个数据库实例
database 属性:定义该分片属于哪个具体数据库实例上的具体库(即对应mysql中实际的DB)
(4)dataHost标签:用于定义后端真实数据库实例
<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="192.168.50.88:3307" user="root" password="123">
<readHost host="db2" url="192.168.50.88:3309" user="root" password="123" />
</writeHost>
</dataHost>
minCon最小并发连接数
maxCon最大并发连接数
balance 属性
负载均衡类型:
balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的writeHost 上。
balance="1",全部的 readHost 与 stand by writeHost 参与 select 语句的负载均衡,简单的说,当双主双从模式(M1->S1,M2->S2,并且 M1 与 M2 互为主备),正常情况下,M2,S1,S2 都参与 select 语句的负载均衡。
balance="2",所有读操作都随机的在 writeHost、 readhost 上分发。
writeType 属性
writeType="0", 所有写操作发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties.
writeType="1",所有写操作都随机的发送到配置的 writeHost,1.5 以后废弃不推荐。与MHA配合使用时,必须设置为1
dbType 属性:指定后端连接的数据库类型,目前支持二进制的 mysql 协议,还有其他使用 JDBC 连接的数据库。例如:mongodb、 oracle、 spark 等。
dbDriver 属性
指定连接后端数据库使用的 Driver,目前可选的值有 native 和 JDBC。使用native 的话,因为这个值执行的是二进制的 mysql 协议,所以可以使用 mysql 和 maridb。其他类型的数据库则需要使用 JDBC 驱动来支持。
switchType属性:主节点宕机后切换模式
-1 不自动切换
1 自动切换
2 基于mysql主从同步状态(同步,延时)决定是否切换,心跳语句是 show slave status
* slaveThreshold 主从延时时间
5. MyCAT垂直分表

思路:88:3306和 89:3307的库里面都有t1 和t2 两张表,要实现t1 表和t2 表数据读写在不同的库上面
登录 88:3306 mysql -S /data/mysql/mysql3306/mysql.sock
use testhf
create table t1(id int);
create table t2(id int,name varchar(10));
登录 89:3307 mysql -S /data/mysql/mysql3307/mysql.sock
use testhf
create table t1(id int);
create table t2(id int,name varchar(10));
配置 mycat:
cd /usr/local/mysql/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="dn1">
<table name="t1" dataNode="dn1"/>
<table name="t2" dataNode="dn2"/>
</schema>
<dataNode name="dn1" dataHost="localhost1" database= "testhf" />
<dataNode name="dn2" dataHost="localhost2" database= "testhf" />
<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="192.168.50.88:3306" user="root" password="123456">
<readHost host="db2" url="192.168.50.88:3308" user="root" password="123456" />
</writeHost>
<writeHost host="db3" url="192.168.50.89:3306" user="root" password="123456">
<readHost host="db4" url="192.168.50.89:3308" user="root" password="123456" />
</writeHost>
</dataHost>
<dataHost name="localhost2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db1" url="192.168.50.89:3307" user="root" password="123456">
<readHost host="db2" url="192.168.50.89:3309" user="root" password="123456" />
</writeHost>
<writeHost host="db3" url="192.168.50.88:3307" user="root" password="123456">
<readHost host="db4" url="192.168.50.88:3309" user="root" password="123456" />
</writeHost>
</dataHost>
</mycat:schema>
重启:mycat restart
连接:mysql -uroot -p123456 -h192.168.50.88 -P8066
验证:对t1 表的操作会落到 88:3306和 88:3308分片上面去,对t2 表的操作会落到 89:3307和 89:3309分片上面去
insert into t1 values (1);
insert into t2 values (1,'hefei');

验证:88:3306>mqsql -S /data/mysql/mysql3306/mysql.sock --[88:3306上面有t1 表的数据,没有 t2 表的数据]

89:3307>mysql -S /data/mysql/mysql3307/mysql.sock --[89:3307上面有t2 表的数据,没有t1 表的数据]

6. MyCAT水平分表

登录 88:3306 mysql -S /data/mysql/mysql3306/mysql.sock
use testhf
create table t3(id int auto_increment primary key);
登录 89:3307 mysql -S /data/mysql/mysql3307/mysql.sock
use testhf
create table t3(id int auto_increment primary key);
配置:
cd /usr/local/mysql/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="dn1">
<table name="t3" dataNode="dn1,dn2" rule="auto-sharding-long" />
</schema>
<dataNode name="dn1" dataHost="localhost1" database= "testhf" />
<dataNode name="dn2" dataHost="localhost2" database= "testhf" />
<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="192.168.50.88:3306" user="root" password="123456">
<readHost host="db2" url="192.168.50.88:3308" user="root" password="123456" />
</writeHost>
<writeHost host="db3" url="192.168.50.89:3306" user="root" password="123456">
<readHost host="db4" url="192.168.50.89:3308" user="root" password="123456" />
</writeHost>
</dataHost>
<dataHost name="localhost2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db1" url="192.168.50.89:3307" user="root" password="123456">
<readHost host="db2" url="192.168.50.89:3309" user="root" password="123456" />
</writeHost>
<writeHost host="db3" url="192.168.50.88:3307" user="root" password="123456">
<readHost host="db4" url="192.168.50.88:3309" user="root" password="123456" />
</writeHost>
</dataHost>
</mycat:schema>
分配规则配置
cd /usr/local/mysql/conf/
vim rule.xml



Caused by: io.mycat.config.util.ConfigException: Illegal table conf : table [ T3 ] rule function [ rang-long ] partition size : 3 > table datanode size : 2, please make sure table datanode size = function partition size
(这个报错是在规则文件中配置的文件问题,注释掉:
#0-500M=0
#500M-1000M=1
#1000M-1500M=2
)
--[0-10的id放在第一个分片,11-20的id放到第二个分片]
0-10=0
11-20=1
重启:mycat restart
连接:mysql -uroot -p123456 -h192.168.50.88 -P8066
验证:user testhf
insert into t3**(id)** values(1); --[id这个要带上插入]
insert into t3**(id)** values(2);
insert into t3**(id)** values(11);
insert into t3**(id)** values(12);

登录 88:3306的mysql查询只能查询到id=1的数据

登录 89:3307的mysql查询只能查询到id=11的数据

登录mycat查询数据是全的。

如果插入的数据不是在这个范围,就会报错:
ERROR 1064 (HY000): can't find any valid datanode :T3 -> ID -> 22