数据库中间件:CentOS7下MyCat实现读写分离(二)

CentOS7下MyCat实现读写分离

环境说明
Centos版本: CentOS Linux release 7.7.1908 (Core)
Linux连接工具:SecureCRT
MySQL Version: 5.7.28
主库主机地址:192.168.163.61
从库主机地址:192.168.163.62
参考文档:https://dev.mysql.com/doc/refman/5.7/en/replication.html
MyCat安装在主库主机
MyCat版本:Mycat-server-1.6.7.3-release
MyCat参考文档:http://www.mycat.io 里面有下载地址

安装配置MyCat

  • 把下载好的MyCat上传并解压到指定目录
#上传到opt目录,解压到/usr/local目录(我们自己的软件一般都安装到这个目录)
[root@localhost opt]# ls
Mycat-server-1.6.7.3-release-20190927161129-linux.tar.gz
[root@localhost opt]# pwd
/opt
#解压到指定目录
[root@localhost opt]# tar -xzvf Mycat-server-1.6.7.3-release-20190927161129-linux.tar.gz -C /usr/local/
[root@localhost opt]# cd /usr/local/
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  Mycat-1.6.7.3  mysql  sbin  share  src

详细讲解几个主要的配置文件:
conf目录下面:
server.xml:这里面可以配置mycat的端口号,和连接用户和密码
schema.xml:中配置逻辑库、数据节点,数据主机
rule.xml:
log目录下面
wrapper.log:tail -n 100 logs/wrapper.log 可以查看mycat的日志信息:MyCAT Server startup successfully. see logs in logs/mycat.log代表mycat启动成功

  • 为MyCat创建单独的系统用户
#创建用户
[root@localhost Mycat-1.6.7.3]# useradd mycat
#设置密码
[root@localhost Mycat-1.6.7.3]# passwd mycat  
Retype new password: 
passwd: all authentication tokens updated successfully.
修改mycat安装目录的所有者为mycat
[root@localhost Mycat-1.6.7.3]# chown -R mycat:mycat /usr/local/Mycat-1.6.7.3
#切换当前用户为mycat
[root@localhost Mycat-1.6.7.3]# su mycat
#启动MyCat
[root@localhost Mycat-1.6.7.3]# ./bin/mycat start
Starting Mycat-server...
#查看日志信息,启动成功
[root@localhost Mycat-1.6.7.3]# tail -n 10 logs/wrapper.log 
STATUS | wrapper  | 2020/01/02 18:08:50 | --> Wrapper Started as Daemon
STATUS | wrapper  | 2020/01/02 18:08:50 | Launching a JVM...
INFO   | jvm 1    | 2020/01/02 18:08:50 | OpenJDK 64-Bit Server VM warning: ignoring option MaxPermSize=64M; support was removed in 8.0
INFO   | jvm 1    | 2020/01/02 18:08:51 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO   | jvm 1    | 2020/01/02 18:08:51 |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
INFO   | jvm 1    | 2020/01/02 18:08:51 | 
INFO   | jvm 1    | 2020/01/02 18:09:01 | MyCAT Server startup successfully. see logs in logs/mycat.log

MyCat目录/bin命令有:
启动MyCat: ./mycat start 
查看启动状态:./mycat status 
停止: ./mycat stop 
重启: ./mycat restart 
启动并控制台打印日志:./mycat console

MyCat配置读写分离

  • 修改schema.xml 文件


<mycat:schema xmlns:mycat="http://io.mycat/">
    
	
    <schema name="myorders" checkSQLschema="false" sqlMaxLimit="100"
	dataNode="mydn1">
		
	schema>
    
	<dataNode name="mydn1" dataHost="dhhost1" database="orders" />
	<dataNode name="mydn2" dataHost="dhhost2" database="orders" />
	
	
    <dataHost name="dhhost1" maxCon="1000" minCon="10" balance="1"
			  writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
		<heartbeat>select user()heartbeat>
		<writeHost host="hostM1" url="192.168.163.61:3306" user="root" password="Root2020.">
			<readHost host="hostS1" url="192.168.163.62:3306" user="root" password="Root2020.">
			readHost>
		writeHost>
	dataHost>
	
	<dataHost name="dhhost2" maxCon="1000" minCon="10" balance="1"
			  writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
		<heartbeat>select user()heartbeat>
		<writeHost host="hostM2" url="192.168.163.61:3306" user="root" password="Root2020.">			
		writeHost>
		<writeHost host="hostS2" url="192.168.163.62:3306" user="root" password="Root2020.">			
		writeHost>
	dataHost>
mycat:schema>


<user name="root" defaultAccount="true">
		<property name="password">Root2020.property>
        
		<property name="schemas">myordersproperty>
		
		
		
	user>
  • 重启MyCat服务
#启动MyCat
[root@localhost Mycat-1.6.7.3]# ./bin/mycat start
Starting Mycat-server...
#查看日志信息,启动成功
[root@localhost Mycat-1.6.7.3]# tail -n 10 logs/wrapper.log 
STATUS | wrapper  | 2020/01/02 18:08:50 | --> Wrapper Started as Daemon
STATUS | wrapper  | 2020/01/02 18:08:50 | Launching a JVM...
INFO   | jvm 1    | 2020/01/02 18:08:50 | OpenJDK 64-Bit Server VM warning: ignoring option MaxPermSize=64M; support was removed in 8.0
INFO   | jvm 1    | 2020/01/02 18:08:51 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO   | jvm 1    | 2020/01/02 18:08:51 |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
INFO   | jvm 1    | 2020/01/02 18:08:51 | 
INFO   | jvm 1    | 2020/01/02 18:09:01 | MyCAT Server startup successfully. see logs in logs/mycat.log

MyCat用户和数据库密码加密

在mycat-1.4.1版本以后开始支持对密码加密,在/usr/local/Mycat-1.6.7.3/lib目录下有一个jar包:Mycat-server-1.6.7.3-release.jar提> 供了加密方法:
用户加密: java -cp Mycat-server-1.6.7.3-release.jar io.mycat.util.DecryptUtil 0:root:Root2020.
dataHost加密:java -cp Mycat-server-1.6.7.3-release.jar io.mycat.util.DecryptUtil 1:hostS2:root:Root2020.
输入命令后,会有相应的提示信息

#加密命令
[mycat@localhost lib]$ java -cp Mycat-server-1.6.7.3-release.jar io.mycat.util.DecryptUtil 0:root:Root2020.       
其中 0:user:password是加密字符串,有两种格式
dataHost加密格式
1:hostM1:root:123456
1代表是dataHost加密
hostM1是<writeHost host="hostM1" 
root是user="root"
123456是 password=明文密码(123456)
对应writeHost配置
                <writeHost host="hostM1" url="localhost:3306" user="root"                                   password="BpkNIjF7LfzS1C76HT7B1bJgmGIDtPihqIvHBlC92L1IFqsMfoJEMk1EkxSzjasWB4GWoUcODYO4AaJstdAp5w==" usingDecrypt="1">
                        <!-- can have multi read hosts -->
                </writeHost>
mycat用户登录密码加密格式
0:root:123456
0代表mycat用户登录密码加密
        <user name="root" defaultAccount="true">
                <property name="usingDecrypt">1</property>
                <property name="password">d6D+pOmkuUoY09p4/aivwMsScLa7zfjIwAxvkEhr3v7en06mEXoX9DTTjQNug5CfvGf7Wy9oLcthYI3yLMSjIg==</property>
                <property name="schemas">TESTDB</property>
##生成的加密密文
A+OX6CDyAa/ipM9DvV2us9l9p/FRQLPaesX9LM39+U94Nk11HWNZ+kieVkpoCxqzsqxCGJ0UUxirZQDBbf5jBA==

你可能感兴趣的:(数据库中间件)