522 lines
24 KiB
Markdown
522 lines
24 KiB
Markdown
|
<h1><center>读写分离</center></h1>
|
|||
|
|
|||
|
**作者:行癫(盗版必究)**
|
|||
|
|
|||
|
------
|
|||
|
|
|||
|
## 一:读写分离部署
|
|||
|
|
|||
|
#### 1.环境介绍
|
|||
|
|
|||
|
![image-20221006131313211](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20221006131313211.png)
|
|||
|
|
|||
|
#### 2.读写分离集群部署
|
|||
|
|
|||
|
##### A:数据库集群部署(略)
|
|||
|
|
|||
|
单主单从;多主多从等均可
|
|||
|
|
|||
|
##### B:Mycat部署
|
|||
|
|
|||
|
新机器,不需要安装mysql
|
|||
|
|
|||
|
安装jdk环境:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@mycat ~]# tar xf jdk-8u211-linux-x64.tar.gz -C /usr/local/
|
|||
|
[root@mycat ~]# mv /usr/local/jdk1.8.0_211/ /usr/local/java
|
|||
|
```
|
|||
|
|
|||
|
设置环境变量:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@mycat ~]# vi /etc/profile
|
|||
|
JAVA_HOME=/usr/local/java
|
|||
|
PATH=$JAVA_HOME/bin:$PATH
|
|||
|
export JAVA_HOME PATH
|
|||
|
[root@mycat ~]# source /etc/profile
|
|||
|
[root@mycat ~]# java -version
|
|||
|
java version "1.8.0_211"
|
|||
|
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
|
|||
|
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
|
|||
|
```
|
|||
|
|
|||
|
安装mycat:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@mycat ~]# tar xf Mycat-server-1.6.7.1-release-20190627191042-linux.tar.gz -C /usr/local/
|
|||
|
```
|
|||
|
|
|||
|
设置环境变量:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@mycat ~]# vi ~/.bash_profile
|
|||
|
PATH=$PATH:$HOME/bin:/usr/local/mycat/bin
|
|||
|
[root@mycat ~]# source ~/.bash_profile
|
|||
|
```
|
|||
|
|
|||
|
##### C:数据库部署
|
|||
|
|
|||
|
Mysql中添加数据库和账户:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@master-1 ~]# mysql -u root -pQianFeng@123
|
|||
|
mysql: [Warning] Using a password on the command line interface can be insecure.
|
|||
|
Welcome to the MySQL monitor. Commands end with ; or \g.
|
|||
|
Your MySQL connection id is 10
|
|||
|
Server version: 5.7.39-log MySQL Community Server (GPL)
|
|||
|
|
|||
|
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
|
|||
|
|
|||
|
Oracle is a registered trademark of Oracle Corporation and/or its
|
|||
|
affiliates. Other names may be trademarks of their respective
|
|||
|
owners.
|
|||
|
|
|||
|
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
|
|||
|
|
|||
|
mysql> create database shop;
|
|||
|
Query OK, 1 row affected (0.00 sec)
|
|||
|
|
|||
|
mysql> create database bbs;
|
|||
|
Query OK, 1 row affected (0.00 sec)
|
|||
|
|
|||
|
mysql> create database blog;
|
|||
|
Query OK, 1 row affected (0.00 sec)
|
|||
|
|
|||
|
mysql> grant all on shop.* to shop@'%' identified by 'QianFeng@123';
|
|||
|
Query OK, 0 rows affected, 1 warning (0.00 sec)
|
|||
|
|
|||
|
mysql> grant all on bbs.* to bbs@'%' identified by 'QianFeng@123';
|
|||
|
Query OK, 0 rows affected, 1 warning (0.00 sec)
|
|||
|
|
|||
|
mysql> grant all on blog.* to blog@'%' identified by 'QianFeng@123';
|
|||
|
Query OK, 0 rows affected, 1 warning (0.00 sec)
|
|||
|
```
|
|||
|
|
|||
|
注意:
|
|||
|
|
|||
|
创建的库跟mycat关联,原则上一个库对应一个项目,根据实际情况创建
|
|||
|
|
|||
|
创建的库需要单独授权用户进行管理,用户名和库名根据实际情况决定
|
|||
|
|
|||
|
##### D:Mycat配置
|
|||
|
|
|||
|
server.xml:Mycat的配置文件,设置账号、参数等
|
|||
|
|
|||
|
```shell
|
|||
|
[root@mycat conf]# vim server.xml
|
|||
|
<?xml version="1.0" encoding="UTF-8"?>
|
|||
|
<!DOCTYPE mycat:server SYSTEM "server.dtd">
|
|||
|
<mycat:server xmlns:mycat="http://io.mycat/">
|
|||
|
<system>
|
|||
|
<property name="nonePasswordLogin">0</property>
|
|||
|
<property name="useHandshakeV10">1</property>
|
|||
|
<property name="useSqlStat">0</property>
|
|||
|
<property name="useGlobleTableCheck">0</property>
|
|||
|
<property name="sequnceHandlerType">2</property>
|
|||
|
<property name="sequnceHandlerPattern">(?:(\s*next\s+value\s+for\s*MYCATSEQ_(\w+))(,|\)|\s)*)+</property>
|
|||
|
<property name="subqueryRelationshipCheck">false</property>
|
|||
|
<property name="processorBufferPoolType">0</property>
|
|||
|
<property name="handleDistributedTransactions">0</property>
|
|||
|
<property name="useOffHeapForMerge">0</property>
|
|||
|
<property name="memoryPageSize">64k</property>
|
|||
|
<property name="spillsFileBufferSize">1k</property>
|
|||
|
<property name="useStreamOutput">0</property>
|
|||
|
<property name="systemReserveMemorySize">384m</property>
|
|||
|
<property name="useZKSwitch">false</property>
|
|||
|
<property name="strictTxIsolation">false</property>
|
|||
|
<property name="useZKSwitch">true</property>
|
|||
|
</system>
|
|||
|
<user name="root" defaultAccount="true">
|
|||
|
<property name="password">123456</property>
|
|||
|
<property name="schemas">TESTDB</property>
|
|||
|
</user>
|
|||
|
<user name="shop">
|
|||
|
<property name="password">123456</property>
|
|||
|
<property name="schemas">shop</property>
|
|||
|
</user>
|
|||
|
<user name="bbs">
|
|||
|
<property name="password">123456</property>
|
|||
|
<property name="schemas">bbs</property>
|
|||
|
</user>
|
|||
|
<user name="blog">
|
|||
|
<property name="password">123456</property>
|
|||
|
<property name="schemas">blog</property>
|
|||
|
</user>
|
|||
|
</mycat:server>
|
|||
|
```
|
|||
|
|
|||
|
注意:
|
|||
|
|
|||
|
```shell
|
|||
|
user 用户配置节点
|
|||
|
–name 登录的用户名,也就是连接Mycat的用户名
|
|||
|
–password 登录的密码,也就是连接Mycat的密码
|
|||
|
–schemas 数据库名,这里会和schema.xml中的配置关联,多个用逗号分开,例如需要这个用户需要管理两个数据库db1,db2,则配置db1,db2
|
|||
|
```
|
|||
|
|
|||
|
schema.xml:Mycat对应的物理数据库和数据库表的配置
|
|||
|
|
|||
|
```shell
|
|||
|
[root@mycat conf]# cat schema.xml
|
|||
|
<?xml version="1.0"?>
|
|||
|
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
|
|||
|
<mycat:schema xmlns:mycat="http://io.mycat/">
|
|||
|
|
|||
|
<schema name="shop" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"> </schema>
|
|||
|
<schema name="bbs" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn2"> </schema>
|
|||
|
<schema name="blog" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn3"> </schema>
|
|||
|
<dataNode name="dn1" dataHost="localhost1" database="shop" />
|
|||
|
<dataNode name="dn2" dataHost="localhost2" database="bbs" />
|
|||
|
<dataNode name="dn3" dataHost="localhost3" database="blog" />
|
|||
|
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
|
|||
|
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
|
|||
|
<heartbeat>show status like 'wsrep%'</heartbeat>
|
|||
|
<writeHost host="mysql-1" url="mysql-1:3306" user="shop" password="QianFeng@123">
|
|||
|
<readHost host="slave-1" url="slave-1:3306" user="shop" password="QianFeng@123" />
|
|||
|
<readHost host="slave-2" url="slave-2:3306" user="shop" password="QianFeng@123" />
|
|||
|
</writeHost>
|
|||
|
<writeHost host="mysql-2" url="mysql-2:3306" user="shop" password="QianFeng@123">
|
|||
|
<readHost host="slave-1" url="slave-1:3306" user="shop" password="QianFeng@123" />
|
|||
|
<readHost host="slave-2" url="slave-2:3306" user="shop" password="QianFeng@123" />
|
|||
|
</writeHost>
|
|||
|
</dataHost>
|
|||
|
<dataHost name="localhost2" maxCon="1000" minCon="10" balance="1"
|
|||
|
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
|
|||
|
<heartbeat>show status like 'wsrep%'</heartbeat>
|
|||
|
<writeHost host="mysql-1" url="mysql-1:3306" user="bbs" password="QianFeng@123">
|
|||
|
<readHost host="slave-1" url="slave-1:3306" user="bbs" password="QianFeng@123" />
|
|||
|
<readHost host="slave-2" url="slave-1:3306" user="bbs" password="QianFeng@123" />
|
|||
|
</writeHost>
|
|||
|
<writeHost host="mysql-2" url="mysql-2:3306" user="bbs" password="QianFeng@123">
|
|||
|
<readHost host="slave-1" url="slave-1:3306" user="bbs" password="QianFeng@123" />
|
|||
|
<readHost host="slave-2" url="slave-2:3306" user="bbs" password="QianFeng@123" />
|
|||
|
</writeHost>
|
|||
|
</dataHost>
|
|||
|
<dataHost name="localhost3" maxCon="1000" minCon="10" balance="1"
|
|||
|
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
|
|||
|
<heartbeat>show status like 'wsrep%'</heartbeat>
|
|||
|
<writeHost host="mysql-1" url="mysql-1:3306" user="blog" password="QianFeng@123">
|
|||
|
<readHost host="slave-1" url="slave-1:3306" user="blog" password="QianFeng@123" />
|
|||
|
<readHost host="slave-2" url="slave-1:3306" user="blog" password="QianFeng@123" />
|
|||
|
</writeHost>
|
|||
|
<writeHost host="mysql-2" url="mysql-2:3306" user="blog" password="QianFeng@123">
|
|||
|
<readHost host="slave-1" url="slave-1:3306" user="blog" password="QianFeng@123" />
|
|||
|
<readHost host="slave-2" url="slave-2:3306" user="blog" password="QianFeng@123" />
|
|||
|
</writeHost>
|
|||
|
</dataHost>
|
|||
|
</mycat:schema>
|
|||
|
```
|
|||
|
|
|||
|
注意:
|
|||
|
|
|||
|
```shell
|
|||
|
balance=1 开启读写分离机制,所有读操作都发送到当前备用的 writeHost 上。
|
|||
|
wirteType=0 所有写操作发送到第一个writeHost,第一个挂了切换到第二个
|
|||
|
switchType=3 基于MySQL Galera cluster的切换机制,心跳语句为show status like 'wsrep%'
|
|||
|
```
|
|||
|
|
|||
|
##### E:启动服务
|
|||
|
|
|||
|
```shell
|
|||
|
[root@mycat conf]# mycat start
|
|||
|
Starting Mycat-server...
|
|||
|
[root@mycat conf]# jps
|
|||
|
1494 WrapperSimpleApp
|
|||
|
1528 Jps
|
|||
|
```
|
|||
|
|
|||
|
查看端口:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@mycat conf]# ss -antpl
|
|||
|
State Recv-Q Send-Q Local Address:Port Peer Address:Port
|
|||
|
LISTEN 0 128 *:22 *:* users:(("sshd",pid=837,fd=3))
|
|||
|
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=933,fd=13))
|
|||
|
LISTEN 0 1 127.0.0.1:32000 *:* users:(("java",pid=1494,fd=4))
|
|||
|
LISTEN 0 50 :::37680 :::* users:(("java",pid=1494,fd=57))
|
|||
|
LISTEN 0 128 :::22 :::* users:(("sshd",pid=837,fd=4))
|
|||
|
LISTEN 0 100 ::1:25 :::* users:(("master",pid=933,fd=14))
|
|||
|
LISTEN 0 50 :::1984 :::* users:(("java",pid=1494,fd=58))
|
|||
|
LISTEN 0 100 :::8066 :::* users:(("java",pid=1494,fd=79))
|
|||
|
LISTEN 0 50 :::32834 :::* users:(("java",pid=1494,fd=59))
|
|||
|
LISTEN 0 100 :::9066 :::* users:(("java",pid=1494,fd=75))
|
|||
|
```
|
|||
|
|
|||
|
##### F:客户端测试
|
|||
|
|
|||
|
mycat连接测试:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@master-1 ~]# mysql -u shop -p123456 -P 8066 -h 10.0.0.47
|
|||
|
mysql: [Warning] Using a password on the command line interface can be insecure.
|
|||
|
Welcome to the MySQL monitor. Commands end with ; or \g.
|
|||
|
Your MySQL connection id is 1
|
|||
|
Server version: 5.6.29-mycat-1.6.7.1-release-20190627191042 MyCat Server (OpenCloudDB)
|
|||
|
|
|||
|
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
|
|||
|
|
|||
|
Oracle is a registered trademark of Oracle Corporation and/or its
|
|||
|
affiliates. Other names may be trademarks of their respective
|
|||
|
owners.
|
|||
|
|
|||
|
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
|
|||
|
|
|||
|
mysql> show databases;
|
|||
|
+----------+
|
|||
|
| DATABASE |
|
|||
|
+----------+
|
|||
|
| shop |
|
|||
|
+----------+
|
|||
|
1 row in set (0.00 sec)
|
|||
|
|
|||
|
mysql> use shop
|
|||
|
Database changed
|
|||
|
mysql> create table t1(id int);
|
|||
|
Query OK, 0 rows affected (0.01 sec)
|
|||
|
|
|||
|
mysql> show tables;
|
|||
|
+----------------+
|
|||
|
| Tables_in_shop |
|
|||
|
+----------------+
|
|||
|
| t1 |
|
|||
|
+----------------+
|
|||
|
1 row in set (0.01 sec)
|
|||
|
|
|||
|
mysql> insert into t1 values(1);
|
|||
|
Query OK, 1 row affected (0.03 sec)
|
|||
|
|
|||
|
mysql> insert into t1 values(2);
|
|||
|
Query OK, 1 row affected (0.01 sec)
|
|||
|
```
|
|||
|
|
|||
|
从服务数据验证:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@slave-2 ~]# mysql -u root -pQianFeng@123
|
|||
|
mysql: [Warning] Using a password on the command line interface can be insecure.
|
|||
|
Welcome to the MySQL monitor. Commands end with ; or \g.
|
|||
|
Your MySQL connection id is 7
|
|||
|
Server version: 5.7.39-log MySQL Community Server (GPL)
|
|||
|
|
|||
|
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
|
|||
|
|
|||
|
Oracle is a registered trademark of Oracle Corporation and/or its
|
|||
|
affiliates. Other names may be trademarks of their respective
|
|||
|
owners.
|
|||
|
|
|||
|
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
|
|||
|
|
|||
|
mysql> show databases;
|
|||
|
+--------------------+
|
|||
|
| Database |
|
|||
|
+--------------------+
|
|||
|
| information_schema |
|
|||
|
| mysql |
|
|||
|
| performance_schema |
|
|||
|
| qfcloud |
|
|||
|
| sys |
|
|||
|
+--------------------+
|
|||
|
5 rows in set (0.00 sec)
|
|||
|
|
|||
|
mysql> show databases;
|
|||
|
+--------------------+
|
|||
|
| Database |
|
|||
|
+--------------------+
|
|||
|
| information_schema |
|
|||
|
| bbs |
|
|||
|
| blog |
|
|||
|
| mysql |
|
|||
|
| performance_schema |
|
|||
|
| qfcloud |
|
|||
|
| shop |
|
|||
|
| sys |
|
|||
|
+--------------------+
|
|||
|
8 rows in set (0.00 sec)
|
|||
|
|
|||
|
mysql> use shop
|
|||
|
Reading table information for completion of table and column names
|
|||
|
You can turn off this feature to get a quicker startup with -A
|
|||
|
|
|||
|
Database changed
|
|||
|
mysql> show tables;
|
|||
|
+----------------+
|
|||
|
| Tables_in_shop |
|
|||
|
+----------------+
|
|||
|
| t1 |
|
|||
|
+----------------+
|
|||
|
1 row in set (0.00 sec)
|
|||
|
|
|||
|
mysql> select * from t1;
|
|||
|
+------+
|
|||
|
| id |
|
|||
|
+------+
|
|||
|
| 1 |
|
|||
|
| 2 |
|
|||
|
+------+
|
|||
|
2 rows in set (0.00 sec)
|
|||
|
```
|
|||
|
|
|||
|
#### 3.Mycat配置文件
|
|||
|
|
|||
|
##### server.xml
|
|||
|
|
|||
|
```shell
|
|||
|
一:server.xml 配置文件
|
|||
|
1.privileges标签
|
|||
|
对用户的 schema以及表进行精细化的DML(数据操纵语言)权限控制
|
|||
|
<privileges check="false"> </privileges> --check 表示是否开启DML权限检查。默认是关闭。
|
|||
|
--dml 顺序说明:insert,update,select,delete
|
|||
|
<schema name="db1" dml="0110" >
|
|||
|
<table name="tb01" dml="0000"></table>
|
|||
|
<table name="tb02" dml="1111"></table> </schema>
|
|||
|
db1的权限是update,select。
|
|||
|
tb01的权限是啥都不能干。
|
|||
|
tb02的权限是insert,update,select,delete。
|
|||
|
其他表默认是udpate,select。
|
|||
|
|
|||
|
2.system标签
|
|||
|
这个标签内嵌套的所有 property 标签都与系统配置有关。
|
|||
|
<property name="charset">utf8</property>
|
|||
|
字符集
|
|||
|
<property name="processors">1</property>
|
|||
|
处理线程数量,默认是cpu数量。
|
|||
|
<property name="processorBufferChunk">4096</property>
|
|||
|
每次读取留的数量,默认4096。
|
|||
|
<property name="processorBufferPool">409600</property>
|
|||
|
创建共享buffer需要占用的总空间大小。processorBufferChunk*processors*100。
|
|||
|
<property name="processorBufferPoolType">0</property>
|
|||
|
默认为0。0表示DirectByteBufferPool,1表示ByteBufferArena。
|
|||
|
<property name="processorBufferLocalPercent">100</property>
|
|||
|
二级共享buffer是processorBufferPool的百分比,这里设置的是百分比。
|
|||
|
<property name="sequnceHandlerType">100</property>
|
|||
|
全局ID生成方式。(0:为本地文件方式,1:为数据库方式;2:为时间戳序列方式;3:为ZK生成ID;4:为ZK递增ID生成。
|
|||
|
<property name="useCompression">1</property>
|
|||
|
是否开启mysql压缩协议。1为开启,0为关闭,默认关闭。
|
|||
|
<property name="packetHeaderSize">4</property>
|
|||
|
指定 Mysql 协议中的报文头长度。默认 4。
|
|||
|
<property name="maxPacketSize">16M</property>
|
|||
|
指定 Mysql 协议可以携带的数据最大长度。默认 16M。
|
|||
|
<property name="idleTimeout">1800000</property>
|
|||
|
指定连接的空闲超时时间。某连接在发起空闲检查下,发现距离上次使用超过了空闲时间,那么这个连接会被回收,就是被直接的关闭掉。默认 30 分钟,单位毫秒。
|
|||
|
<property name="txIsolation">3</property>
|
|||
|
前端连接的初始化事务隔离级别,只在初始化的时候使用,后续会根据客户端传递过来的属性对后端数据库连接进行同步。默认为 REPEATED_READ,设置值为数字默认 3。
|
|||
|
READ_UNCOMMITTED = 1;
|
|||
|
READ_COMMITTED = 2;
|
|||
|
REPEATED_READ = 3;
|
|||
|
SERIALIZABLE = 4;
|
|||
|
<property name="sqlExecuteTimeout">300</property>
|
|||
|
SQL 执行超时的时间,Mycat 会检查连接上最后一次执行 SQL 的时间,若超过这个时间则会直接关闭这连接。默认时间为 300 秒,单位秒。
|
|||
|
<property name="processorCheckPeriod">1000</property>
|
|||
|
清理 NIOProcessor 上前后端空闲、超时和关闭连接的间隔时间。默认是 1 秒,单
|
|||
|
位毫秒。
|
|||
|
<property name="dataNodeIdleCheckPeriod">300000</property>
|
|||
|
对后端连接进行空闲、超时检查的时间间隔,默认是 300 秒,单位毫秒。
|
|||
|
<property name="dataNodeHeartbeatPeriod">10000</property>
|
|||
|
对后端所有读、写库发起心跳的间隔时间,默认是 10 秒,单位毫秒。
|
|||
|
<property name="bindIp">0.0.0.0</property>
|
|||
|
mycat 服务监听的 IP 地址,默认值为 0.0.0.0。
|
|||
|
<property name="serverPort">8066</property>
|
|||
|
定义 mycat 的使用端口,默认值为 8066。
|
|||
|
<property name="managerPort">9066</property>
|
|||
|
定义 mycat 的管理端口,默认值为 9066。
|
|||
|
<property name="fakeMySQLVersion">5.6</property>
|
|||
|
mycat 模拟的 mysql 版本号,默认值为 5.6 版本,如非特需,不要修改这个值,目前支持设置 5.5,5.6,5.7 版本,其他版本可能会有问题。
|
|||
|
<property name="useSqlStat">0</property>
|
|||
|
是否开启实时统计。1为开启;0为关闭 。
|
|||
|
<property name="useGlobleTableCheck">0</property>
|
|||
|
是否开启全局表一致性检测。1为开启;0为关闭 。
|
|||
|
<property name="handleDistributedTransactions">0</property>
|
|||
|
分布式事务开关。0为不过滤分布式事务;1为过滤分布式事务;2 为不过滤分布式事务,但是记录分布式事务日志。
|
|||
|
<property name="maxStringLiteralLength">65535</property>
|
|||
|
默认是65535。 64K 用于sql解析时最大文本长度
|
|||
|
以上举例的属性仅仅是一部分,可以配置的变量很多。
|
|||
|
System标签下的属性,一般是上线后,需要根据实际运行的情况,分析后调优的时候进行修改。
|
|||
|
|
|||
|
3. Firewall标签
|
|||
|
防火墙的设置,也就是在网络层对请求的地址进行限制,主要是从安全角度来保证Mycat不被匿名IP进行访问
|
|||
|
<firewall>
|
|||
|
<whitehost>
|
|||
|
<host host="127.0.0.1" user="mycat"/>
|
|||
|
<host host="127.0.0.2" user="mycat"/>
|
|||
|
</whitehost>
|
|||
|
<blacklist check="false">
|
|||
|
</blacklist>
|
|||
|
</firewall>
|
|||
|
```
|
|||
|
|
|||
|
##### schema.xml
|
|||
|
|
|||
|
```shell
|
|||
|
一:schema.xml
|
|||
|
–schema 数据库设置,此数据库为逻辑数据库,name与server.xml中schema对应
|
|||
|
–dataNode 分片信息,也就是分库相关配置
|
|||
|
–dataHost 物理数据库,真正存储数据的数据库
|
|||
|
|
|||
|
1、schema 标签
|
|||
|
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="10"> </schema>
|
|||
|
schema标签用来定义mycat实例中的逻辑库,mycat可以有多个逻辑库,每个逻辑库都有自己的相关配置。可以使用schema标签来划分这些不同的逻辑库,如果不配置schema标签,所有表的配置会属于同一个默认的逻辑库。逻辑库的概念和MySql的database的概念一样,我们在查询两个不同逻辑库中的表的时候,需要切换到该逻辑库下进行查询。
|
|||
|
–name 逻辑数据库名,与server.xml中的schema对应
|
|||
|
–checkSQLschema 数据库前缀相关设置,当该值为true时,例如我们执行语句select * from TESTDB.company 。mycat会把语句修改为 select * from company 去掉TESTDB。
|
|||
|
–sqlMaxLimit 当该值设置为某个数值时,每条执行的sql语句,如果没有加上limit语句,Mycat会自动加上对应的值。不写的话,默认返回所有的值。需要自己sql语句加limit。
|
|||
|
|
|||
|
2、dataNode标签
|
|||
|
<dataNode name="dn1" dataHost="localhost1" database="db1" />
|
|||
|
datanode标签定义了mycat中的数据节点,也就是数据分片。一个datanode标签就是一个独立的数据分片。
|
|||
|
localhost1数据库实例上的db1物理数据库,这就组成一个数据分片,最后我们用dn1来标示这个分片。
|
|||
|
–name 定义数据节点的名字,这个名字需要唯一。我们在table标签上用这个名字来建立表与分片对应的关系
|
|||
|
–dataHost 用于定义该分片属于哪个数据库实例,属性与datahost标签上定义的name对应
|
|||
|
–database 用于定义该分片属于数据库实例上 的具体库。
|
|||
|
|
|||
|
3、dataHost标签
|
|||
|
这个标签直接定义了具体数据库实例,读写分离配置和心跳语句。
|
|||
|
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
|
|||
|
<heartbeat>select user()</heartbeat>
|
|||
|
<writeHost host="hostM1" url="192.168.1.100:3306" user="root" password="123456">
|
|||
|
<readHost host="hostS1" url="192.168.1.101:3306" user="root" password="123456" />
|
|||
|
</writeHost>
|
|||
|
</dataHost>
|
|||
|
|
|||
|
–name 唯一标示dataHost标签,供上层使用
|
|||
|
–maxCon 指定每个读写实例连接池的最大连接。
|
|||
|
–minCon 指定每个读写实例连接池的最小连接,初始化连接池的大小
|
|||
|
–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上分发
|
|||
|
balance=“3”:所有读请求随机的分发到writeHst对应的readHost执行,writeHost不负担读写压力。
|
|||
|
–writeType 负载均衡类型。
|
|||
|
writeType=“0”, 所有写操作发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties .
|
|||
|
writeType=“1”,所有写操作都随机的发送到配置的 writeHost。1.5以后版本废弃不推荐。
|
|||
|
–switchType -1不自动切换
|
|||
|
1 默认值 自动切换
|
|||
|
2 基于MySql主从同步的状态决定是否切换心跳语句为 show slave status
|
|||
|
3 基于mysql galary cluster 的切换机制(适合集群) 心跳语句为 show status like ‘wsrep%’
|
|||
|
–dbType 指定后端链接的数据库类型目前支持二进制的mysql协议,还有其他使用jdbc链接的数据库,例如:mongodb,oracle,spark等
|
|||
|
–dbDriver 指定连接后段数据库使用的driver,目前可选的值有native和JDBC。使用native的话,因为这个值执行的是二进制的mysql协议,所以可以使用mysql和maridb,其他类型的则需要使用JDBC驱动来支持。
|
|||
|
如果使用JDBC的话需要符合JDBC4标准的驱动jar 放到mycat\lib目录下,并检查驱动jar包中包括如下目录结构文件 META-INF\services\java.sql.Driver。 在这个文件写上具体的driver类名,例如com.mysql.jdbc.Driver
|
|||
|
writeHost readHost指定后端数据库的相关配置给mycat,用于实例化后端连接池。
|
|||
|
–tempReadHostAvailable
|
|||
|
如果配置了这个属性 writeHost 下面的 readHost 仍旧可用,默认 0 可配置(0、1)。
|
|||
|
1)heartbeat标签
|
|||
|
这个标签内指明用于和后端数据库进行心跳检查的语句。
|
|||
|
例如:MYSQL 可以使用 select user(),Oracle 可以使用 select 1 from dual 等。
|
|||
|
2) writeHost /readHost 标签
|
|||
|
这两个标签都指定后端数据库的相关配置,用于实例化后端连接池。唯一不同的是,writeHost 指定写实例、readHost 指定读实例。
|
|||
|
在一个 dataHost 内可以定义多个 writeHost 和 readHost。但是,如果 writeHost 指定的后端数据库宕机,那么这个 writeHost 绑定的所有 readHost 都将不可用。
|
|||
|
另一方面,由于这个 writeHost 宕机,系统会自动的检测到,并切换到备用的 writeHost 上去。这两个标签的属性相同,这里就一起介绍。
|
|||
|
–host 用于标识不同实例,一般 writeHost 我们使用M1,readHost 我们用S1。
|
|||
|
–url 后端实例连接地址。Native:地址:端口 JDBC:jdbc的url
|
|||
|
–password 后端存储实例需要的密码
|
|||
|
–user 后端存储实例需要的用户名字
|
|||
|
–weight 权重 配置在 readhost 中作为读节点的权重
|
|||
|
–usingDecrypt 是否对密码加密,默认0。
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|