mycat-->mysql数据库分库分片
1.环境部署
1.1)服务器选择
system | hostname | IP Address | packget | 数据库 | 表 |
---|---|---|---|---|---|
centos7 | mysql-node02 | 10.1.1.27 | mycat+mysql+java | db1 | user |
centos7 | mysql-node03 | 10.1.1.28 | mysql | db2 | order |
centos7 | mysql-node04 | 10.1.1.29 | mysql | db3 | order |
mycat逻辑数据库
system | hostname | IP Address | packget | 逻辑数据库 | 逻辑表 |
---|---|---|---|---|---|
centos7 | mysql-node02 | 10.1.1.27 | mycat+mysql+java | test-db | order |
数据库用户及密码
hostname | 数据库 | 用户名 | 密码 |
---|---|---|---|
mysql-node02 | db1 | jacky | qaz123 |
mysql-node03 | db2 | jacky | qaz123 |
mysql-node04 | db3 | jacky | qaz123 |
mycat逻辑数据库 | test-db | mycat | qaz123 |
注意:逻辑数据库在主机mysql-node02上
2.mycat分库分表原理图
2.1)垂直分库分表
user表和order表,本来是同一个数据库和表business,由于业务扩大,数据表超大,现在根据业务,垂直分库分表(也可以同一个库中,拆分数据表,性能没有分库分表优,根据实际情况决定)
2.2)水平分库分表
现在order数据表过大,影响查询速度,我们通过水平分库分表,把order拆分到db2和db3。
这个过程是:客户端访问数据库,mycat拦截请求,如果是对order数据表操作,mycat通过算法把数据下发给db2和db3,如果是对user数据表操作,就直接给db1,我们还可以做到通过mycat实现mysql读写分离,及如果是写操作,就转发给db2,如果是读操作,就转发给db3
3.安装mysql数据库(三台服务器)
3.1)mysql数据库安装
wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm
rpm -Uvh mysql57-community-release-el7-10.noarch.rpm
yum install -y mysql-community-server
3.2)修改root(管理员)密码
##查看安装日志,“temporary password”为默认密码
grep "temporary password" /var/log/mysqld.log
2020-01-07T09:59:24.029427Z 1 [Note] A temporary password is generated for root@localhost: 5o_l)yrrj>uE
##登录mysql
mysql -uroot -p'5o_l)yrrj>uE'
3.3)常见问题1:数据库root密码设置简单报错
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements ## 您的密码不满足当前策略要求
# 解决:
set global validate_password_policy=LOW; ##设置验证密码策略为低级 -->validate:验证
set global validate_password_length=6; ##设置验证密码长度
alter user 'root'@'localhost' identified by 'qaz123'; ##修改密码 -->alter:修改 identified:确认,口令
3.4)创建db1、db2、db3数据库
10.1.1.27创建db1
##登录mysql
mysql -uroot -pqaz123
##创建数据库db01
create database db1;
##进入db1数据库
use db1
##创建users表,及定义id,name,indate字段属性,PRIMARY KEY (id)定义主键为id
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL default '',
indate DATETIME NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (id)
)AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;
10.1.1.28创建db2
##登录mysql
mysql -uroot -pqaz123
##创建数据库db01
create database db2;
##进入db1数据库
use db2
##创建users表,及定义id,name,indate字段属性,PRIMARY KEY (id)定义主键为id
CREATE TABLE order (
id INT NOT NULL AUTO_INCREMENT,
value INT NOT NULL default 0,
indate DATETIME NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (id)
)AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;
10.1.1.29创建db3
##登录mysql
mysql -uroot -pqaz123
##创建数据库db01
create database db3;
##进入db1数据库
use db3
##创建users表,及定义id,name,indate字段属性,PRIMARY KEY (id)定义主键为id
CREATE TABLE order (
id INT NOT NULL AUTO_INCREMENT,
value INT NOT NULL default 0,
indate DATETIME NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (id)
)AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;
4.安装mycat中间件(10.1.1.27)
4.1)安装
4.2)配置schema.xml文件()
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<!-- 设置表的存储方式.schema name="TESTDB" 与 server.xml中的 TESTDB 设置一致 -->
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="users" primaryKey="id" dataNode="dn1" />
<table name="order" primaryKey="id" dataNode="dn2,dn3" rule="role1" />
</schema>
<!-- 设置dataNode 对应的数据库,及 mycat 连接的地址dataHost -->
<dataNode name="dn1" dataHost="ds1" database="db1" />
<dataNode name="dn2" dataHost="ds2" database="db2" />
<dataNode name="dn3" dataHost="ds3" database="db3" />
<!-- mycat 逻辑主机dataHost对应的物理主机.其中也设置对应的mysql登陆信息 -->
<dataHost name="ds1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native">
<heartbeat>select user()</heartbeat>
<writeHost host="mysql-node02" url="10.1.1.27:3306" user="root" password="qaz123"/>
</dataHost>
<dataHost name="ds2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native">
<heartbeat>select user()</heartbeat>
<writeHost host="mysql-node03" url="10.1.1.28:3306" user="root" password="qaz123"/>
</dataHost>
<dataHost name="ds3" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native">
<heartbeat>select user()</heartbeat>
<writeHost host="mysql-node04" url="10.1.1.29:3306" user="root" password="qaz123"/>
</dataHost>
</mycat:schema>
4.3)配置server.xml文件()
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
<system>
<!-- ##默认参数就行
<property name="processors">32</property>
<property name="processorExecutor">32</property>
<property name="bindIp">0.0.0.0</property>
<property name="frontWriteQueueSize">4096</property>
<property name="idleTimeout">300000</property>
<property name="mutiNodePatchSize">100</property>
-->
<property name="defaultSqlParser">druidparser</property>
<property name="mutiNodeLimitType">1</property>
<property name="serverPort">8066</property>
<property name="managerPort">9066</property>
<property name="sequnceHandlerType">1</property>
<!-- sequnceHandlerType参数主要配合主键自增情况,后续进行说明-->
</system>
<!-- 任意设置登陆 mycat 的用户名,密码,数据库 多个逻辑库,<property name="schemas">TESTDB01,TESTDB2……</property> -->
<user name="mycat">
<property name="password">qaz123</property>
<property name="schemas">TESTDB</property>
</user>
<!--<user name="user">
<property name="password">user</property>
<property name="schemas">TESTDB</property>
<property name="readOnly">true</property>
</user> -->
<!--
<quarantine>
<whitehost>
<host host="127.0.0.1" user="mycat"/>
<host host="127.0.0.2" user="mycat"/>
</whitehost>
<blacklist check="false"></blacklist>
</quarantine>
-->
</mycat:server>
4.4)配置role.xml文件()
<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License. - You
may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0
- - Unless required by applicable law or agreed to in writing, software -
distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
License for the specific language governing permissions and - limitations
under the License. -->
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
<tableRule name="role1">
<rule>
<columns>id</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">2</property>
</function>
</mycat:rule>
4.1)登录mycat逻辑库
##登录逻辑数据库数据库
mysql -uroot -pqaz123 -h 10.1.1.27 -P8066