1 首先拥有三台机器 一台是主服务器,一台是从服务器,还有一台是部署mycat。
wget --no-cookies \
--no-check-certificate \
--header \
"Cookie: oraclelicense=accept-securebackup-cookie" \
http://download.oracle.com/otn-pub/java/jdk/8u181-\
b13/96a7b8442fe848ef90c96a2fad6ed6d1/jdk-8u181-linux-\
x64.tar.g
从http开始 ,需要在 Java官网中搜
image.png
2 解压环境
tar -xf jdk-8u181-linux-x64.tar.gz -C /usr/local
ln -s /usr/local/jdk1.8.0_181/ /usr/local/java
3 配置环境变量
vi /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/java
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
4 使环境变量生效
source /etc/profile
部署 MyCat
image.png
image.png
wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
解压
tar -xf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/
ls /usr/local/mycat/
bin catlet conf lib logs version.txt
配置 MyCat
用户和密码是应用程序连接到 MyCat 使用的,可以自定义配置
其中的schemas 配置项所对应的值是逻辑数据库的名字,也可以自定义,但是这个名字需要和后面 schema.xml 文件中配置的一致。
cd /usr/local/mycat/conf
vim vim server.xml
<user name="mycatdb">
<property name="password">123456</property>
<property name="schemas">schema_shark_db</property>
<!-- 表级 DML 权限设置 -->
<!--
<privileges check="false">
<schema name="TESTDB" dml="0110" >
<table name="tb01" dml="0000"></table>
<table name="tb02" dml="1111"></table>
</schema>
</privileges>
-->
</user>
<!--下面是另一个用户,并且设置的访问 TESTED 逻辑数据库的权限是 只读
<user name="user">
<property name="password">user</property>
<property name="schemas">TESTDB</property>
<property name="readOnly">true</property>
</user>
-->
配置 schema.xml
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="schema_shark_db"
checkSQLschema="false"
sqlMaxLimit="100"
dataNode='dn1'>
<!--这里定义的是分库分表的信息-->
</schema>
<!--下面是配置读写分离的信息-->
<dataNode name="dn1"
dataHost="localhost1" database="shark_db">
</dataNode>
<dataHost name="localhost1"
maxCon="1000" minCon="10"
balance="0" writeType="0"
dbType="mysql" dbDriver="native"
switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="172.16.153.10:3306"
user="root" password="123">
<!-- can have multi read hosts -->
<readHost host="hostS2" url="172.16.153.11:3306"
user="root" password="123" />
</writeHost>
</dataHost>
</mycat:schema>
逻辑库和分表设置
<schema name="schema_shark_db" // 逻辑库名称
checkSQLschema="false" // 不检查
sqlMaxLimit="100" // 最大连接数
dataNode='dn1'> // 数据节点名称
<!--这里定义的是分表的信息-->
</schema>
数据节点
dataHost="localhost1" // 主机组
database="shark_db"> // 真实的数据库名称
</dataNode>
主机组
这里balance=3
<dataHost name="localhost1"
maxCon="1000" minCon="10" // 连接
balance="0" // 负载均衡
writeType="0" // 写模式配置
dbType="mysql" dbDriver="native" // 数据库配置
switchType="1" slaveThreshold="100">
<!--这里可以配置关于这个主机组的成员信息,和针对这些主机的健康检查语句-->
</dataHost>
健康检查
<heartbeat>select user()</heartbeat>
读写配置
这里修改 writeHost......url=“主服务器的IP” user=“用户名” password=“密码”
readHost......url="从服务器的IP" user=“用户名” password=“密码”
<writeHost host="hostM1" url="172.16.153.10:3306"
user="root" password="123">
<!-- can have multi read hosts -->
<readHost host="hostS2" url="172.16.153.11:3306"
user="root" password="123" /></writeHost>
配置 log4j2.xml
<!--设置日志级别为 debug , 默认是 info-->
<asyncRoot level="debug" includeLocation="true">
启动 mycat
/usr/local/mycat/bin/mycat start
在真实的 master 数据库上给用户授权
shark_db 需和
grant all on *.* to root@'%' identified by '123';
flush privileges;
测试
测试是否能正常登录上 主服务器
mysql -uroot -p'123' -h172.16.153.10
继续测试是否能登录上从服务器
mysql -uroot -p'123' -h172.16.153.11
通过客户端进行测试是否能登录到 mycat 上
注意端口号是 8066
shell> mysql -umycatdb -p123456 -P8066 -h172.16.153.162
mysql> show databases;
+-----------------+
| DATABASE |
+-----------------+
| schema_shark_db |
+-----------------+
1 row in set (0.00 sec)
之后查询 mycat 主机上 mycat 安装目录下的 logs/mycat.log 日志。
在日志重搜索查询的语句或者查询 从库的 ip 地址,应该能搜索到