读写分离-mycat:
安装mycat:
#下载mycat: wget http://dl.mycat.io/1.6.7.1/Mycat-server-1.6.7.1-release-20190627191042linux.tar.gz #解压: tar xf Mycat-server-1.6.7.1-release-20190627191042-linux.tar.gz -C /usr/local/ #安装java-jdk: yum install java-1.8.0-openjdk -y #配置mycat环境变量: echo "export PATH=/usr/local/mycat/bin:$PATH" > /etc/profile.d/mycat.sh . /etc/profile.d/mycat.sh
数据库授权mycat中间件:
#在主库执行授权信息,从库会自动同步: Mariadb [(nane)]> grant all on *.* to "mycat-proxy"@"192.168.1.20" ientified by "123456";
备份mycat的核心文件:
cp /mycat-proxy/conf/server.xml /mycat-proxy/conf/server.xml.bak cp /mycat-proxy/conf/schema.xml /mycat-proxy/conf/schema.xml.bak
配置server.xml文件:
vim /mycat-proxy/conf/server.xml
配置schema.xml文件:
vim /mycat-proxy/conf/schema.xml # 设置逻辑库以及数据库节点 <schema name="lutixiadb" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"> </schema> # 配置数据库节点对应的后端真实的数据库: <dataNode name="dn1" dataHost="localhost1" database="students" /> # 配置读写库以及均衡: <dataHost name="localhost1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <!-- can have multi write hosts --> <writeHost host="hostM1" url="192.168.1.200:3306" user="mysql-proxy" password="123456"> <!-- can have multi read hosts --> <readHost host="hostS2" url="192.168.1.201:3306" user="mysql-proxy" password="123456" /> </writeHost> <writeHost host="hostS1" url="192.168.1.201:3306" user="mysql-proxy" password="123456" />
案例:
启动mycat并且查看端口: mycat start #需先创建用户 mycat/ mycat-proxy ps -ef|grep mycat #端口8066 9066 netstat -nutlp
连接测试:(需要安装mysql客户端工具)
mysql -umycat -p123456 -P8066 -h127.0.0.1 Mariadb [(nane)]> show databases; mysql -umycat -p123456 -P9066 -h127.0.0.1 Mariadb [(nane)]> show databases;
查看日志:
ps -aux |grep mycat #查看mycat启动的目录 tailf /usr/local/mycat/logs/mycat.log #查看错误日志
查看状态:
show @@datasource;
在后端主库创建表插入数据:
Mariadb[(nane)]> show databases; Mariadb[(nane)]> use students; Mariadb[(nane)]> create table t1(id int(10) auto0increment primary key, name varchar(20) jod varchar(20)); Mariadb[(nane)]> insert ti values(1,"xiaoming","it");
mycat继续查询:
mysql -umycat -p123456 -P8066 -h127.0.0.1
show databases;
use lutixiadb;
show tables;