MariaDB MyCat实现读写分离(15)

Posted LyShark

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MariaDB MyCat实现读写分离(15)相关的知识,希望对你有一定的参考价值。

MariaDB数据库管理系统是mysql的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQL的代替品.在存储引擎方面,使用XtraDB来代替MySQL的InnoDB,MariaDB由MySQL的创始人Michael Widenius主导开发,他早前曾以10亿美元的价格,将自己创建的公司MySQL AB卖给了SUN,此后,随着SUN被甲骨文收购MySQL的所有权也落入Oracle的手中.MariaDB名称来自MichaelWidenius的女儿Maria的名字.

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

MyCat 读写分离

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不需要初始化.

[[email protected] ~]# yum install -y mariadb mariadb-server
[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# systemctl enable mariadb

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

[[email protected] ~]# tar -xzvf jdk-8u171-linux-x64.tar.gz -C /usr/local/
[[email protected] ~]# mv /usr/local/jdk1.8.0_171/ /usr/local/jdk
[[email protected] ~]# 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
#------------------------------------------------------------------

[[email protected] ~]# source /etc/profile
[[email protected] ~]# 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准备的使用数据库的授权用户.

[[email protected] ~]# 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登陆账户,这里配置文件有很多选项,我们不需要动,只需要修改以下几个地方就行.

[[email protected] ~]# wget http://dl.mycat.io/1.6.5/Mycat-server-1.6.5-release-20180122220033-linux.tar.gz
[[email protected] ~]# tar -xzvf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz -C /usr/local/
[[email protected] ~]# 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读写分离策略,这里我们删除源文件,并自己创建一个,写入以下内容.

[[email protected] ~]# 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若无报错则说明启动成功,查看端口看虚拟端口是否开启成功.

[[email protected] ~]# /usr/local/mycat/bin/mycat start
[[email protected] ~]# netstat -an |grep "9066"                 #虚拟schema管理端口
[[email protected] ~]# netstat -an |grep "8066"                 #虚拟schema登陆端口

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

[[email protected] ~]# 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代理端,测试读写分离服务.

[[email protected] ~]# 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.

[[email protected] ~]# wget http://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz
[[email protected] ~]# wget http://dl.mycat.io/mycat-web-1.0/Mycat-web-1.0-SNAPSHOT-20170102153329-linux.tar.gz

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

[[email protected] ~]# tar -xzvf jdk-8u171-linux-x64.tar.gz -C /usr/local/
[[email protected] ~]# mv /usr/local/jdk1.8.0_171/ /usr/local/jdk
[[email protected] ~]# 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
#------------------------------------------------------------------

[[email protected] ~]# source /etc/profile
[[email protected] ~]# 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选项,每台都要开启.

[[email protected] ~]# yum install -y mariadb mariadb-server
[[email protected] ~]# vim /etc/my.cnf

[mysqld]
lower_case_table_names = 1

[[email protected] ~]# systemctl restart mariadb

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

[[email protected] ~]# tar -xzvf zookeeper-3.4.12.tar.gz -C /usr/local/
[[email protected] ~]# cd /usr/local/zookeeper-3.4.12/conf/
[[email protected] ~]# cp zoo_sample.cfg zoo.cfg
[[email protected] ~]# vim zoo.cfg

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

5.运行zookeeper

[[email protected] ~]# cd /usr/local/zookeeper-3.4.12/bin/
[[email protected] ~]# ./zkServer.sh start
[[email protected] ~]# 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

[[email protected] ~]# tar -xzvf Mycat-web-1.0-SNAPSHOT-20170102153329-linux.tar.gz -C /usr/local
[[email protected] ~]# cd /usr/local/mycat-web
[[email protected] ~]# ./start.sh &
[[email protected] ~]# netstat  -an | grep "8082"
tcp  0    0 :::8082       :::*           LISTEN 

[[email protected] ~]# curl http://localhost:8082/mycat





以上是关于MariaDB MyCat实现读写分离(15)的主要内容,如果未能解决你的问题,请参考以下文章

Mariadb安装,主从热备操作步骤,MyCat配置安装实现读写分离

Linux 部署 Mycat 实现 MariaDB 分库分表

Linux 部署 Mycat 实现 MariaDB 分库分表

Mycat读写分离

Mycat读写分离

Mycat实现读写分离