MyCat 读写分离

2022-12-28 14:00:48 浏览数 (1)

MyCat是一个彻底开源的,面向企业应用开发的大数据库集群,支持事务、ACID、可以替代MySQL的加强版数据库,其功能有可以视为MySQL集群的企业级数据库,用来替代昂贵的Oracle集群.融合了内存缓存技术、NoSQL技术、HDFS大数据的新型SQLServer,结合传统数据库和新型分布式数据仓库的新一代企业级数据库产品.

MyCat 读写分离

代码语言:javascript复制
192.168.1.5  MyCat   Server

192.168.1.11 Master  主MariaDB
192.168.1.12 Slave1  从1
192.168.1.13 Slave2  从2

1.在配置读写分离前请确保你的主机有四台,分别在每台主机安装MariaDB数据库,并初始化,需要注意的是MyCat服务端的MariaDB不需要初始化.

代码语言:javascript复制
[root@localhost ~]# yum install -y mariadb mariadb-server
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# systemctl enable mariadb

2.在MyCat服务端安装JDK环境,由于MyCat是使用Java开发的,在这里我们要先配置一下JDK环境.

代码语言:javascript复制
[root@localhost ~]# tar -xzvf jdk-8u171-linux-x64.tar.gz -C /usr/local/
[root@localhost ~]# mv /usr/local/jdk1.8.0_171/ /usr/local/jdk
[root@localhost ~]# 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
#------------------------------------------------------------------

[root@localhost ~]# source /etc/profile
[root@localhost ~]# java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)

3.此步骤需要进入每一个数据库创建授权用户,也就是给MyCAT准备的使用数据库的授权用户.

代码语言:javascript复制
[root@localhost ~]# mysql -uroot -p
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]> create database MyCatDB;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all on MyCatDB.* to "MyCat"@"%" identified by "123123";
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> grant all on MyCatDB.* to "MyCat"@"localhost" identified by "123123";
Query OK, 0 rows affected (0.00 sec)

4.解压MyCat工具,并修改MyCat用户授权文件,写入MyCat登陆账户,这里配置文件有很多选项,我们不需要动,只需要修改以下几个地方就行.

代码语言:javascript复制
[root@localhost ~]# wget http://dl.mycat.io/1.6.5/Mycat-server-1.6.5-release-20180122220033-linux.tar.gz
[root@localhost ~]# tar -xzvf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz -C /usr/local/
[root@localhost ~]# vim /usr/local/mycat/conf/server.xml

        <user name="root" defaultAccount="true">			#指定MyCat登陆用户名
                <property name="password">123456</property>		#指定密码
                <property name="schemas">MyCatDB</property>		#指定同步数据库
        </user>

        <user name="user">						#指定MyCat登陆用户名
                <property name="password">123456</property>		#指定密码
                <property name="schemas">MyCatDB</property>		#指定同步数据库
                <property name="readOnly">true</property>		#指定只读
        </user>

5.修改MyCat读写分离策略,这里我们删除源文件,并自己创建一个,写入以下内容.

代码语言:javascript复制
[root@localhost ~]# 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="MyCatDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn_test"> </schema>
<dataNode name="dn_test" dataHost="localhost" database="MyCatDB" />

<dataHost name="localhost" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
                
                <heartbeat>select user()</heartbeat>
                <writeHost host="Master1" url="192.168.1.11:3306" user="MyCat" password="123123">			#配置写主机
                <readHost host="Slave1" url="192.168.1.12:3306" user="MyCat" password="123123" />			#配置读主机
		<readHost host="Slave2" url="192.168.1.13:3306" user="MyCat" password="123123" />			#配置读主机
                </writeHost>
        </dataHost>
</mycat:schema>

6.启动MyCat若无报错则说明启动成功,查看端口看虚拟端口是否开启成功.

代码语言:javascript复制
[root@localhost ~]# /usr/local/mycat/bin/mycat start
[root@localhost ~]# netstat -an |grep "9066"                 #虚拟schema管理端口
[root@localhost ~]# netstat -an |grep "8066"                 #虚拟schema登陆端口

7.在MyCat服务主机登陆数据库,测试同步效果.

代码语言:javascript复制
[root@localhost ~]# mysql -uroot -p123456 -h 127.0.0.1 -P 9066

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 (monitor)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MySQL [(none)]> show @@heartbeat;                                                       #RS_CODE为1表示心跳正常
 --------- ------- -------------- ------ --------- ------- -------- --------- -------------- --------------------- ------- 
| NAME    | TYPE  | HOST         | PORT | RS_CODE | RETRY | STATUS | TIMEOUT | EXECUTE_TIME | LAST_ACTIVE_TIME    | STOP  |
 --------- ------- -------------- ------ --------- ------- -------- --------- -------------- --------------------- ------- 
| Master1 | mysql | 192.168.1.11 | 3306 |      -1 |     0 | idle   |       0 | 0,16,16      | 2018-12-21 01:29:43 | false |
| Slave1  | mysql | 192.168.1.12 | 3306 |      -1 |     0 | idle   |       0 | 34,31,31     | 2018-12-21 01:29:43 | false |
| Slave2  | mysql | 192.168.1.13 | 3306 |      -1 |     0 | idle   |       0 | 1,16,16      | 2018-12-21 01:29:43 | false |
 --------- ------- -------------- ------ --------- ------- -------- --------- -------------- --------------------- ------- 
3 rows in set (0.00 sec)

MySQL [(none)]> show @@datasource;                                                      #查看读写分离的机器配置情况
 ---------- --------- ------- -------------- ------ ------ -------- ------ ------ --------- ----------- ------------ 
| DATANODE | NAME    | TYPE  | HOST         | PORT | W/R  | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD |
 ---------- --------- ------- -------------- ------ ------ -------- ------ ------ --------- ----------- ------------ 
| dn_test  | Master1 | mysql | 192.168.1.11 | 3306 | W    |      0 |    0 | 1000 |       0 |         0 |          0 |
| dn_test  | Slave1  | mysql | 192.168.1.12 | 3306 | R    |      0 |    0 | 1000 |       0 |         0 |          0 |
| dn_test  | Slave2  | mysql | 192.168.1.13 | 3306 | R    |      0 |    0 | 1000 |       0 |         0 |          0 |
 ---------- --------- ------- -------------- ------ ------ -------- ------ ------ --------- ----------- ------------ 
3 rows in set (0.01 sec)

MySQL [(none)]>

8.登录MyCat代理端,测试读写分离服务.

代码语言:javascript复制
[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 3
Server version: 5.6.29-mycat-1.6.5-release-20180122220033 MyCat Server (OpenCloundDB)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MySQL [(none)]>
MySQL [(none)]>
MySQL [(none)]>
MySQL [(none)]> show databases;
 ---------- 
| DATABASE |
 ---------- 
| MyCatDB  |
 ---------- 
1 row in set (0.00 sec)

MySQL [(none)]>

Web监控页配置

1.首先我们先来下载MyCat以及Zookeeper.

代码语言:javascript复制
[root@localhost ~]# wget http://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz
[root@localhost ~]# wget http://dl.mycat.io/mycat-web-1.0/Mycat-web-1.0-SNAPSHOT-20170102153329-linux.tar.gz

2.在MyCat服务端安装JDK环境,由于MyCat是使用Java开发的,在这里我们要先配置一下JDK环境.

代码语言:javascript复制
[root@localhost ~]# tar -xzvf jdk-8u171-linux-x64.tar.gz -C /usr/local/
[root@localhost ~]# mv /usr/local/jdk1.8.0_171/ /usr/local/jdk
[root@localhost ~]# 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
#------------------------------------------------------------------

[root@localhost ~]# source /etc/profile
[root@localhost ~]# java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)

3.配置MariaDB开启lower_case_table_names选项,每台都要开启.

代码语言:javascript复制
[root@localhost ~]# yum install -y mariadb mariadb-server
[root@localhost ~]# vim /etc/my.cnf

[mysqld]
lower_case_table_names = 1

[root@localhost ~]# systemctl restart mariadb

4.安装zookeeper,其主要用来统计数据.

代码语言:javascript复制
[root@localhost ~]# tar -xzvf zookeeper-3.4.12.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/zookeeper-3.4.12/conf/
[root@localhost ~]# cp zoo_sample.cfg zoo.cfg
[root@localhost ~]# vim zoo.cfg

dataDir=/usr/local/...
dataLogDir=/usr/local/...

5.运行zookeeper

代码语言:javascript复制
[root@localhost ~]# cd /usr/local/zookeeper-3.4.12/bin/
[root@localhost ~]# ./zkServer.sh start
[root@localhost ~]# 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

6.最后一步,安装并运行Mycat-Web

代码语言:javascript复制
[root@localhost ~]# tar -xzvf Mycat-web-1.0-SNAPSHOT-20170102153329-linux.tar.gz -C /usr/local
[root@localhost ~]# cd /usr/local/mycat-web
[root@localhost ~]# ./start.sh &
[root@localhost ~]# netstat  -an | grep "8082"
tcp  0    0 :::8082       :::*           LISTEN 

[root@localhost ~]# curl http://localhost:8082/mycat

0 人点赞