Linux操作文档——MySQL搭建MyCAT(5.7.26)

Posted 互联网技术资源社区

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux操作文档——MySQL搭建MyCAT(5.7.26)相关的知识,希望对你有一定的参考价值。

一、架构设计

1、主从规划

Linux操作文档——MySQL搭建MyCAT(5.7.26)

2、分片规划

Linux操作文档——MySQL搭建MyCAT(5.7.26)

二、环境搭建

1、准备实例

1、安装mysql

[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

2、准备mysql-1配置文件

[root@mysql-1 ~]# cat >/data/3307/my.cnf<<EOF[mysqld]basedir=/usr/local/mysqldatadir=/data/3307/datasocket=/data/3307/mysql.sockport=3307log-error=/data/3307/mysql.loglog_bin=/data/3307/mysql-binbinlog_format=rowskip-name-resolveserver-id=7gtid-mode=onenforce-gtid-consistency=truelog-slave-updates=1EOF[root@mysql-1 ~]# cat >/data/3308/my.cnf<<EOF[mysqld]basedir=/usr/local/mysqldatadir=/data/3308/dataport=3308socket=/data/3308/mysql.socklog-error=/data/3308/mysql.loglog_bin=/data/3308/mysql-binbinlog_format=rowskip-name-resolveserver-id=8gtid-mode=onenforce-gtid-consistency=truelog-slave-updates=1EOF[root@mysql-1 ~]# cat >/data/3309/my.cnf<<EOF[mysqld]basedir=/usr/local/mysqldatadir=/data/3309/datasocket=/data/3309/mysql.sockport=3309log-error=/data/3309/mysql.loglog_bin=/data/3309/mysql-binbinlog_format=rowskip-name-resolveserver-id=9gtid-mode=onenforce-gtid-consistency=truelog-slave-updates=1EOF[root@mysql-1 ~]# cat >/data/3310/my.cnf<<EOF[mysqld]basedir=/usr/local/mysqldatadir=/data/3310/datasocket=/data/3310/mysql.sockport=3310log-error=/data/3310/mysql.loglog_bin=/data/3310/mysql-binbinlog_format=rowskip-name-resolveserver-id=10gtid-mode=onenforce-gtid-consistency=truelog-slave-updates=1EOF[root@mysql-1 ~]# cat >/etc/systemd/system/mysqld3307.service<<EOF[Unit]Description=MySQL ServerDocumentation=man:mysqld(8)Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.htmlAfter=network.targetAfter=syslog.target[Install]WantedBy=multi-user.target[Service]User=mysqlGroup=mysqlExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3307/my.cnfLimitNOFILE = 5000EOF[root@mysql-1 ~]# cat >/etc/systemd/system/mysqld3308.service<<EOF[Unit]Description=MySQL ServerDocumentation=man:mysqld(8)Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.htmlAfter=network.targetAfter=syslog.target[Install]WantedBy=multi-user.target[Service]User=mysqlGroup=mysqlExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3308/my.cnfLimitNOFILE = 5000EOF[root@mysql-1 ~]# cat >/etc/systemd/system/mysqld3309.service<<EOF[Unit]Description=MySQL ServerDocumentation=man:mysqld(8)Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.htmlAfter=network.targetAfter=syslog.target[Install]WantedBy=multi-user.target[Service]User=mysqlGroup=mysqlExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3309/my.cnfLimitNOFILE = 5000EOF[root@mysql-1 ~]# cat >/etc/systemd/system/mysqld3310.service<<EOF[Unit]Description=MySQL ServerDocumentation=man:mysqld(8)Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.htmlAfter=network.targetAfter=syslog.target[Install]WantedBy=multi-user.target[Service]User=mysqlGroup=mysqlExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3310/my.cnfLimitNOFILE = 5000EOF


3、准备mysql-2配置文件

[root@mysql-2 ~]# cat >/data/3307/my.cnf<<EOF[mysqld]basedir=/usr/local/mysqldatadir=/data/3307/datasocket=/data/3307/mysql.sockport=3307log-error=/data/3307/mysql.loglog_bin=/data/3307/mysql-binbinlog_format=rowskip-name-resolveserver-id=17gtid-mode=onenforce-gtid-consistency=truelog-slave-updates=1EOF[root@mysql-2 ~]# cat >/data/3308/my.cnf<<EOF[mysqld]basedir=/usr/local/mysqldatadir=/data/3308/dataport=3308socket=/data/3308/mysql.socklog-error=/data/3308/mysql.loglog_bin=/data/3308/mysql-binbinlog_format=rowskip-name-resolveserver-id=18gtid-mode=onenforce-gtid-consistency=truelog-slave-updates=1EOF[root@mysql-2 ~]# cat >/data/3309/my.cnf<<EOF[mysqld]basedir=/usr/local/mysqldatadir=/data/3309/datasocket=/data/3309/mysql.sockport=3309log-error=/data/3309/mysql.loglog_bin=/data/3309/mysql-binbinlog_format=rowskip-name-resolveserver-id=19gtid-mode=onenforce-gtid-consistency=truelog-slave-updates=1EOF[root@mysql-2 ~]# cat >/data/3310/my.cnf<<EOF[mysqld]basedir=/usr/local/mysqldatadir=/data/3310/datasocket=/data/3310/mysql.sockport=3310log-error=/data/3310/mysql.loglog_bin=/data/3310/mysql-binbinlog_format=rowskip-name-resolveserver-id=20gtid-mode=onenforce-gtid-consistency=truelog-slave-updates=1EOF[root@mysql-2 ~]# cat >/etc/systemd/system/mysqld3307.service<<EOF[Unit]Description=MySQL ServerDocumentation=man:mysqld(8)Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.htmlAfter=network.targetAfter=syslog.target[Install]WantedBy=multi-user.target[Service]User=mysqlGroup=mysqlExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3307/my.cnfLimitNOFILE = 5000EOF[root@mysql-2 ~]# cat >/etc/systemd/system/mysqld3308.service<<EOF[Unit]Description=MySQL ServerDocumentation=man:mysqld(8)Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.htmlAfter=network.targetAfter=syslog.target[Install]WantedBy=multi-user.target[Service]User=mysqlGroup=mysqlExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3308/my.cnfLimitNOFILE = 5000EOF[root@mysql-2 ~]# cat >/etc/systemd/system/mysqld3309.service<<EOF[Unit]Description=MySQL ServerDocumentation=man:mysqld(8)Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.htmlAfter=network.targetAfter=syslog.target[Install]WantedBy=multi-user.target[Service]User=mysqlGroup=mysqlExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3309/my.cnfLimitNOFILE = 5000EOF[root@mysql-2 ~]# cat >/etc/systemd/system/mysqld3310.service<<EOF[Unit]Description=MySQL ServerDocumentation=man:mysqld(8)Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.htmlAfter=network.targetAfter=syslog.target[Install]WantedBy=multi-user.target[Service]User=mysqlGroup=mysqlExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3310/my.cnfLimitNOFILE = 5000EOF

4、修改权限,启动多实例

[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 mysqltcp6 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 |+---------------+-------+

2、配置主从

1、配置互为主从

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 statusG"*************************** 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 statusG"*************************** 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 statusG"*************************** 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 statusG"*************************** 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

2、配置单向主从

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 statusG"*************************** 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 statusG"*************************** 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 statusG"*************************** 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 statusG"*************************** 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

3、检测主从状态

[root@mysql-1 ~]# mysql -S /data/3307/mysql.sock -e "show slave statusG"|grep Yes Slave_IO_Running: Yes Slave_SQL_Running: Yes[root@mysql-1 ~]# mysql -S /data/3308/mysql.sock -e "show slave statusG"|grep Yes Slave_IO_Running: Yes Slave_SQL_Running: Yes[root@mysql-1 ~]# mysql -S /data/3309/mysql.sock -e "show slave statusG"|grep Yes Slave_IO_Running: Yes Slave_SQL_Running: Yes[root@mysql-1 ~]# mysql -S /data/3310/mysql.sock -e "show slave statusG"|grep Yes Slave_IO_Running: Yes Slave_SQL_Running: Yes[root@mysql-2 ~]# mysql -S /data/3307/mysql.sock -e "show slave statusG"|grep Yes Slave_IO_Running: Yes Slave_SQL_Running: Yes[root@mysql-2 ~]# mysql -S /data/3308/mysql.sock -e "show slave statusG"|grep Yes Slave_IO_Running: Yes Slave_SQL_Running: Yes[root@mysql-2 ~]# mysql -S /data/3309/mysql.sock -e "show slave statusG"|grep Yes Slave_IO_Running: Yes Slave_SQL_Running: Yes[root@mysql-2 ~]# mysql -S /data/3310/mysql.sock -e "show slave statusG"|grep Yes Slave_IO_Running: Yes Slave_SQL_Running: Yes

三、MyCAT安装

[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/profileexport PATH=/usr/local/mycat/bin:$PATH[root@mysql-1 ~]# source /etc/profile[root@mysql-1 ~]# mycat startStarting Mycat-server...[root@mysql-1 ~]# mysql -uroot -p123456 -h 127.0.0.1 -P8066mysql> 


四、Mycat基础应用

1、实现1主1从读写分离

[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 startStarting Mycat-server...Mycat-server is already running.[root@mysql-1 ~]# mysql -uroot -p123456 -h 192.168.1.10 -P 8066Your MySQL connection id is 2Server 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 |+-------------+

Linux操作文档——MySQL搭建MyCAT(5.7.26)

2、实现高可用+读写分离

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

五、Mycat高级应用-分布式解决方案

1、垂直切分

作用:根据业务的不同,将不同业务的表放到不同的数据库中,但可以在映射的表中一起查看到

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

2、水平拆分

目的:将大数据量进行分布存储,提供均衡的访问路由

1、范围分片

目的:根据某个字段的数值范围来确定这些数据到底存放在哪一个分片上

注意:分片规则配置文件autopartition-long.txt中配置的节点要跟dataNode数量对应上,不然可能会报错

[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=0500M-1000M=1

2、取模分片

目的:切分规则根据配置中输入的数值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>

3、枚举分片

目的:将符合条件的记录分配到对应节点上,不满足条件的分配到默认节点

[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.txt10000=010010=1DEFAULT_NODE=1 //情况之外

3、全局表

指在所有节点都保存一份一样的数据

[root@mysql-1 conf]# vim schema.xml <table name="t6" primaryKey="id" type="global" dataNode="sh1,sh2" />

觉得本文对你有帮助?请分享给更多人

关注「互联网技术资源社区」加星标,提升全栈技能


以上是关于Linux操作文档——MySQL搭建MyCAT(5.7.26)的主要内容,如果未能解决你的问题,请参考以下文章

MyCAT+MySQL搭建

mysql与mycat搭建实现集群与读写分离

Linux下MyCat和MyCat_web的安装和配置

Centos 7使用MyCat搭建 MySQL-读写分离

mysql+mycat搭建稳定高可用集群,负载均衡,主备复制,读写分离

mysql+myca搭建稳定高可用集群,负载均衡,主备复制,读写分离