前言
MyCat是一个彻底开源的,面向企业应用开发的大数据库集群,支持事务、ACID、可以替代MySQL的加强版数据库.
其功能有可以视为MySQL集群的企业级数据库,用来替代昂贵的Oracle集群.融合了内存缓存技术、NoSQL技术、HDFS大数据的新型SQL Server,结合传统数据库和新型分布式数据仓库的新一代企业级数据库产品.
◆案例1◆ 配置MyCat实现读写分离
安装JDK
tar -xzvf jdk-8u171-linux-x64.tar.gz -C /usr/local/
mv /usr/local/jdk1.8.0_171/ /usr/local/jdk
配置JAVA环境变量
vim /etc/profile #============================================================================= #Java-JDK-Path export JAVA_HOME=/usr/local/jdk export JAVA_BIN=/usr/local/jdk/bin export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME JAVA_BIN PATH CLASSPATH #============================================================================= source /etc/profile
进入所有数据库依次创建远程登陆权限
grant all on *.* to "lyshark"@"%" identified by "123123"; create database lysharkdb; #创建测试数据库
解压MyCat
tar -xzvf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz -C /usr/local/
修改MyCat配置文件
a. 修改用户授权文件,写入MyCat登陆账号
vim /usr/local/mycat/conf/server.xml <user name="root" defaultAccount="true"> #指定MyCat登陆用户名 <property name="password">123456</property> #指定密码 <property name="schemas">lysharkDB</property> #指定同步数据库 </user> <user name="user"> #指定MyCat登陆用户名 <property name="password">123456</property> #指定密码 <property name="schemas">lysharkDB</property> #指定同步数据库 <property name="readOnly">true</property> #指定只读 </user>
b. 修改MyCat读写分离策略
vim /usr/local/mycat/conf/schema.xml <?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="lysharkDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn_test"> </schema> <dataNode name="dn_test" dataHost="dh_43" database="lysharkDB" /> <dataHost name="dh_43" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="43_M" url="192.168.1.13:3306" user="lyshark" password="123"> # 配置写主机 <readHost host="45_S1" url="192.168.1.14:3306" user="lyshark" password="123" /> # 配置读主机 <readHost host="45_S2" url="192.168.1.15:3306" user="lyshark" password="123" /> # 配置读主机 </writeHost> </dataHost> </mycat:schema> schema name="lysharkDB" # 指定你要同步的数据库 database="lysharkDB" # 同上 dataNode="dn_test" dataNode name="dn_test" dataHost="dh_43"
启动MyCat
/usr/local/mycat/bin/mycat start
查看端口
netstat -an |grep "9066" #虚拟schema管理端口 netstat -an |grep "8066" #虚拟schema登陆端口
登录MyCat管理端
mysql -uroot -p123456 -h127.0.0.1 -P 9066 MySQL [(none)]> show @@heartbeat; #RS_CODE为1表示心跳正常 +-------+-------+--------------+------+---------+-------+--------+---------+--------------+---------------------+-------+ | NAME | TYPE | HOST | PORT | RS_CODE | RETRY | STATUS | TIMEOUT | EXECUTE_TIME | LAST_ACTIVE_TIME | STOP | +-------+-------+--------------+------+---------+-------+--------+---------+--------------+---------------------+-------+ | 43_M | mysql | 192.168.1.12 | 3306 | 1 | 0 | idle | 0 | 173,173,173 | 2018-05-11 09:10:15 | false | | 45_S1 | mysql | 192.168.1.13 | 3306 | 1 | 0 | idle | 0 | 226,226,226 | 2018-05-11 09:10:15 | false | | 45_S2 | mysql | 192.168.1.14 | 3306 | 1 | 0 | idle | 0 | 206,206,206 | 2018-05-11 09:10:15 | false | +-------+-------+--------------+------+---------+-------+--------+---------+--------------+---------------------+-------+ MySQL [(none)]> show @@datasource; #查看读写分离的机器配置情况 +----------+-------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+ | DATANODE | NAME | TYPE | HOST | PORT | W/R | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD | +----------+-------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+ | dn_test | 43_M | mysql | 192.168.1.12 | 3306 | W | 0 | 10 | 1000 | 19 | 0 | 0 | | dn_test | 45_S1 | mysql | 192.168.1.13 | 3306 | R | 0 | 4 | 1000 | 12 | 0 | 0 | | dn_test | 45_S2 | mysql | 192.168.1.14 | 3306 | R | 0 | 4 | 1000 | 12 | 0 | 0 | +----------+-------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
登录MyCat读写分离服务
mysql -uroot -p123456 -h127.0.0.1 -P 8066 [root@localhost ~]# mysql -uroot -p123456 -h127.0.0.1 -P 8066 Welcome to the MariaDB monitor. Commands end with ; or g. Your MySQL connection id is 2 Server version: 5.6.29-mycat-1.6.5-release-20180122220033 MyCat Server (OpenCloundDB) Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. MySQL [(none)]>
◆案例2◆ 配置MyCat-Web监控页面
前戏:下载MyCat以及Zookeeper
wget http://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz wget http://dl.mycat.io/1.6.5/Mycat-server-1.6.5-release-20180122220033-linux.tar.gz wget http://dl.mycat.io/mycat-web-1.0/Mycat-web-1.0-SNAPSHOT-20170102153329-linux.tar.gz
安装JDK
tar -xzvf jdk-8u171-linux-x64.tar.gz -C /usr/local/
mv /usr/local/jdk1.8.0_171/ /usr/local/jdk
配置JAVA环境变量
vim /etc/profile #============================================================================= #Java-JDK-Path export JAVA_HOME=/usr/local/jdk export JAVA_BIN=/usr/local/jdk/bin export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME JAVA_BIN PATH CLASSPATH #============================================================================= source /etc/profile
安装MySQL并配置my.cnf
yum install -y mariadb mariadb-server vim /etc/my.cnf [mysqld] lower_case_table_names = 1
安装zookeeper
tar -xzvf zookeeper-3.4.12.tar.gz -C /usr/local/ cd /usr/local/zookeeper-3.4.12/conf/ cp zoo_sample.cfg zoo.cfg 修改其中的↓ dataDir=/usr/local/... dataLogDir=/usr/local/...
运行zookeeper
cd /usr/local/zookeeper-3.4.12/bin/ ./zkServer.sh start 查看状态:netstat -ant | grep 2181 tcp 0 0 :::2181 :::* LISTEN 如果出现错误:nohup: failed to run command `java’: No such file or directory 可以在zkServer.sh中的首行添加如下代码 export JAVA_HOME=/usr/lib/jdk export PATH=$JAVA_HOME/bin:$PATH
安装并运行Mycat-Web
tar -xzvf Mycat-web-1.0-SNAPSHOT-20170102153329-linux.tar.gz -C /usr/local cd /usr/local/mycat-web ./start.sh & netstat -an | grep 8082 客户访问 http://localhost:8082/mycat
博客搬运地址