DB优化mycatapp开发

6.MyCat实现MySQL主从读写分离

2019-06-15  本文已影响264人  一枼落知天下

MySQL读写分离的概述

MySQl作为目前世界上使用最广泛的免费数据库,相信所有从事系统运维的工程师都一定接触过。但在实际的生产环境中,由单台MySQL作为独立的数据库是完全不能满足实际需求的,无论是在安全性,高可用性以及高并发等各个方面。

因此,一般来说都是通过 主从复制(Master-Slave)的方式来同步数据,再通过读写分离(MySQL-Proxy/Amoeba)来提升数据库的并发负载能力,这样的方案来进行部署与实施的。

读写分离工作原理

基本的原理是让主数据库处理事务性增、改、删操作(INSERT、UPDATE、DELETE),而从数据库处理SELECT查询操作。数据库复制被用来把事务性操作导致的变更同步到集群中的从数据库。


为什么要读写分离

1.面对越来越大的访问压力,单台的服务器的性能成为瓶颈,需要分担负载
2.主从只负责各自的写和读,极大程度的缓解X锁和S锁争用
3.从库可配置myisam引擎,提升查询性能以及节约系统开销
4.增加冗余,提高可用性

实现读写分离的方式

1. 应用程序层实现,网站的程序实现

应用程序层实现指的是在应用程序内部及连接器中实现读写分离

2.中间件层实现:

中间件层实现是指在外部中间件程序实现读写分离

常见的中间件程序


什么是MyCAT

MyCAT官方地址


MyCat服务安装与配置

MyCat有提供编译好的安装包,支持Windows、Linux、Mac、Solaris等系统上安装与运行。 官方下载主页http://www.mycat.io
Linux下可以下载Mycat-server-xxxxx.linux.tar.gz 解压在某个目录下,注意目录不能有空格,在Linux(Unix)下,建议放在usr/local/Mycat目录下,
如下:

[root@ShuaiGuo ~]# wget http://dl.mycat.io/1.6.7.1/Mycat-server-1.6.7.1-release-20190213150257-linux.tar.gz
[root@ShuaiGuo ~]# ll
total 17152
-rw-------. 1 root root     1742 Jun 12 16:54 anaconda-ks.cfg
-rw-r--r--  1 root root 17558357 Feb 22 16:20 Mycat-server-1.6.7.1-release-20190213150257-linux.tar.gz
[root@ShuaiGuo ~]# tar zvxf Mycat-server-1.6.7.1-release-20190213150257-linux.tar.gz -C /usr/local/
[root@ShuaiGuo ~]# cd /usr/local/
[root@ShuaiGuo local]# ls
bin  etc  games  include  lib  lib64  libexec  mycat  sbin  share  src
[root@ShuaiGuo local]# 
[root@ShuaiGuo local]# ls /usr/local/mycat/
bin  catlet  conf  lib  version.txt
[root@ShuaiGuo local]# useradd mycat 
[root@ShuaiGuo local]# passwd mycat
Changing password for user mycat.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@ShuaiGuo local]# 
[root@ShuaiGuo local]# chown -R mycat:mycat /usr/local/mycat/

[root@ShuaiGuo local]# ll /usr/local/mycat/
total 12
drwxr-xr-x 2 mycat mycat  190 Jun 15 18:53 bin
drwxrwxrwx 2 mycat mycat    6 Feb 13 14:46 catlet
drwxrwxrwx 4 mycat mycat 4096 Jun 15 18:53 conf
drwxr-xr-x 2 mycat mycat 4096 Jun 15 18:53 lib
-rwxrwxrwx 1 mycat mycat  185 Feb 13 15:02 version.txt
[root@ShuaiGuo local]# 


[root@ShuaiGuo ~]# cd /usr/local/mycat/bin/
[root@ShuaiGuo bin]# ll
total 384
-rwxr-xr-x 1 mycat mycat   3567 Feb 13 14:46 dataMigrate.sh
-rwxr-xr-x 1 mycat mycat   1225 Feb 13 14:46 init_zk_data.sh
-rwxr-xr-x 1 mycat mycat  15714 Feb 13 15:03 mycat
-rwxr-xr-x 1 mycat mycat   2941 Feb 13 14:46 rehash.sh
-rwxr-xr-x 1 mycat mycat   2496 Feb 13 14:46 startup_nowrap.sh
-rwxr-xr-x 1 mycat mycat 140198 Feb 13 15:03 wrapper-linux-ppc-64
-rwxr-xr-x 1 mycat mycat  99401 Feb 13 15:03 wrapper-linux-x86-32
-rwxr-xr-x 1 mycat mycat 111027 Feb 13 15:03 wrapper-linux-x86-64
[root@ShuaiGuo bin]# 

[root@ShuaiGuo ~]# cd /usr/local/mycat/
[root@ShuaiGuo mycat]# mkdir logs
[root@ShuaiGuo mycat]# chmod -R a+rwx ./logs/
[root@ShuaiGuo mycat]# chown -R mycat.mycat ./logs/
[root@ShuaiGuo mycat]# cd bin/
[root@ShuaiGuo bin]# ./mycat console 
Running Mycat-server...
wrapper  | --> Wrapper Started as Console
wrapper  | Launching a JVM...
wrapper  | JVM exited while loading the application.
jvm 1    | wrapper  | Unable to start JVM: No such file or directory (2)
wrapper  | Launching a JVM...
wrapper  | JVM exited while loading the application.
jvm 2    | wrapper  | Unable to start JVM: No such file or directory (2)
^Cwrapper  | INT trapped.  Shutting down.
wrapper  | <-- Wrapper Stopped
[root@ShuaiGuo bin]# 

Mycat 需要安装JDK 1.7 或者以上版:

[root@ShuaiGuo ~]# wget http://dl.mycat.io/jdk-8u20-linux-x64.tar.gz

第二步:新建/usr/java文件夹,将/jdk-8u20-linux-x64.tar.gz解压到该目录中
[root@ShuaiGuo ~]#  mkdir /usr/java
[root@ShuaiGuo ~]#  tar xf /jdk-8u20-linux-x64.tar.gz -C /usr/java/

[root@ShuaiGuo ~]# ll
total 157108
-rw-------. 1 root root      1742 Jun 12 16:54 anaconda-ks.cfg
-rw-r--r--  1 root root 160872342 Jun 27  2015 jdk-8u20-linux-x64.tar.gz
[root@ShuaiGuo ~]# mkdir /usr/java
[root@ShuaiGuo ~]# tar zvxf jdk-8u20-linux-x64.tar.gz -C /usr/java/
[root@ShuaiGuo ~]# vim /etc/profile.d/java.sh
JAVA_HOME=/usr/java/jdk1.8.0_20
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar
export PATH JAVA_HOME CLASSPATH


[root@ShuaiGuo ~]# vim /etc/profile.d/java.sh
[root@ShuaiGuo ~]# ls /etc/profile.d/java.sh 
/etc/profile.d/java.sh
[root@ShuaiGuo ~]# source /etc/profile.d/java.sh   #使环境变量生效
[root@ShuaiGuo ~]# java -version
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
[root@ShuaiGuo ~]# 

MyCat服务启动与启动设置

MyCAT在Linux中部署启动时,首先需要在Linux系统的环境变量中配置MYCAT_HOME,操作方式如下:

[root@ShuaiGuo ~]# vim /etc/profile.d/mycat.sh

MYCAT_HOME=/usr/local/mycat
PATH=$MYCAT_HOME/bin:$PATH

[root@ShuaiGuo ~]# source /etc/profile.d/mycat.sh   #使环境变量生效
[root@ShuaiGuo ~]# mycat -v
Usage: /usr/local/mycat/bin/mycat { console | start | stop | restart | status | dump }
[root@ShuaiGuo ~]# 

如果是在多台Linux系统中组建的MyCAT集群,那需要在MyCAT Server所在的服务器上配置对其他IP和主机名的映射,配置方式如下:

vim /etc/hosts
例如:我有4台机器,4台机器主机配置如下:
IP 主机名:
192.168.31.62 ShuaiJhou
192.168.31.63 ZhouShuai
192.168.31.64 ShuaiZhou
192.168.31.65 ShuaiGuou
编辑完后,保存文件。


mycat的用户账号和授权信息是在conf/server.xml文件中配置

[root@ShuaiGuo ~]# cd /usr/local/mycat/conf/
[root@ShuaiGuo conf]# vim server.xml 

                        <property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> -->
        </system>
        <!--以下设置为应用访问帐号权限 -->
        <user name="zhoushuai">
                <property name="password">123456</property>
                <property name="schemas">test</property>
        </user>
         <!--以下设置为应用只读帐号权限 -->
        <user name="shuaishuai">
                <property name="password">123456</property>
                <property name="schemas">test</property>
                <property name="readOnly">true</property>
        </user>

编辑MyCAT的配置文件schema.xml,关于dataHost的配置信息如下:

[root@ShuaiGuo conf]# cp schema.xml schema.xml.bak  #备份原文件。
[root@ShuaiGuo conf]# vim schema.xml

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">

        <schema name="test" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1" >
        </schema>
        <dataNode name="dn1" dataHost="dbHost" database="test" />
        <dataHost name="dbHost" maxCon="1000" minCon="10" balance="1"
                writeType="0" dbType="mysql" dbDriver="native" switchType="-1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <!-- can have multi write hosts -->
                <writeHost host="ShuaiJhou" url="192.168.31.62:3306" user="mycat"
                        password="123456">
                </writeHost>
                <writeHost host="ZhouShuai" url="192.168.31.63:3306" user="mycat"
                        password="123456" />
        </dataHost>
</mycat:schema>

经过以上两个步骤的配置,就可以到/usr/local/mycat/bin 目录下执行 ./mycat start,即可启动mycat服务!

[root@ShuaiGuo conf]# /usr/local/mycat/bin/mycat start
Starting Mycat-server...
Mycat-server is already running.
[root@ShuaiGuo conf]# /usr/local/mycat/bin/mycat restart
Stopping Mycat-server...
Stopped Mycat-server.
Starting Mycat-server...
[root@ShuaiGuo conf]# cat /usr/local/mycat/logs/wrapper.log


在主从数据库上:授权给mycat登陆数据库使用的帐号

[root@ShuaiZhou ~]# mysql -uroot -p123456
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'mycat'@"%" IDENTIFIED BY "123456"; 
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

启动服务Mycat服务:

[root@ShuaiGuo ~]# mycat start
Starting Mycat-server...
Mycat-server is already running.
[root@ShuaiGuo ~]# mycat restart
Stopping Mycat-server...
Stopped Mycat-server.
Starting Mycat-server...
[root@ShuaiGuo ~]# tail -5 /usr/local/mycat/logs/wrapper.log 
INFO   | jvm 1    | 2019/06/15 22:51:57 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO   | jvm 1    | 2019/06/15 22:51:57 |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
INFO   | jvm 1    | 2019/06/15 22:51:57 | 
INFO   | jvm 1    | 2019/06/15 22:51:58 | log4j 2019-06-15 22:51:58 [./conf/log4j.xml] load completed.
INFO   | jvm 1    | 2019/06/15 22:51:58 | MyCAT Server startup successfully. see logs in logs/mycat.log
[root@ShuaiGuo ~]# 
[root@ShuaiGuo ~]# netstat -ant | grep 8066
tcp6       0      0 :::8066                 :::*                    LISTEN     
tcp6       0      0 192.168.31.65:8066      192.168.31.14:58150     ESTABLISHED
tcp6       0      0 192.168.31.65:8066      192.168.31.14:58154     ESTABLISHED
tcp6       0      0 192.168.31.65:8066      192.168.31.14:58156     ESTABLISHED
[root@ShuaiGuo ~]# 


[root@ShuaiGuo ~]# mysql -uzhoushuai -p123456  -h 192.168.31.65 -P8066
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.8-mycat-1.5-RELEASE-20160301083012 MyCat Server (OpenCloundDB)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> 


测试读写分离


上一篇下一篇

猜你喜欢

热点阅读