码迷,mamicode.com
首页 > 数据库 > 详细

MariaDB MyCat实现读写分离(15)

时间:2018-12-30 19:01:27      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:mysqld   引擎   als   ...   mysql集群   ddb   last   分布   datanode   

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

[root@localhost ~]# yum install -y mariadb mariadb-server
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# systemctl enable mariadb

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

[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准备的使用数据库的授权用户.

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

[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读写分离策略,这里我们删除源文件,并自己创建一个,写入以下内容.

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

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

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

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

[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.

[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环境.

[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选项,每台都要开启.

[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,其主要用来统计数据.

[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

[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

[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


MariaDB MyCat实现读写分离(15)

标签:mysqld   引擎   als   ...   mysql集群   ddb   last   分布   datanode   

原文地址:https://www.cnblogs.com/LyShark/p/10200146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!