https://www.wulaoer.org/?p=3079。下面配置一下mycat。
[root@clinent ~]# wget https://github.com/MyCATApache/Mycat-Server/releases/download/Mycat-server-1.6.7.4-release/Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz [root@clinent ~]# tar -zxcf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz [root@clinent ~]# mv mycat /usr/local/
因为mycat是通过java编写的,所以我们需要配置一下java环境,直接下载jdk到节点上配置环境变量即可。
[root@clinent ~]# vim /etc/profile ........................ export JAVA_HOME=/usr/local/jdk export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar export PATH=$PATH:${JAVA_HOME}/bin [root@clinent ~]# source /etc/profile [root@clinent ~]# java -version java version "1.8.0_361" Java(TM) SE Runtime Environment (build 1.8.0_361-b09) Java HotSpot(TM) 64-Bit Server VM (build 25.361-b09, mixed mode)
配置mycat,在mycat的conf目录下有两个比较重要的文件分别是schema.xml
和server.xml
,其中schema.xml
是配置逻辑库读写分离的文件,server.xml
是负责登录用户以及路由规则的配置文件。下面先看一下schema的配置内容,我这里把注释去掉了。
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> #这里如果checkSQLschema="flase"可以改成<schema name="wulaoer" checkSQLschema="flase" sqlMaxLimit="100" randomDataNode="dn1">如果是true不要在后面加dataNode="dn1" <schema name="wulaoer" checkSQLschema="true" sqlMaxLimit="100" randomDataNode="dn1" dataNode="dn1"> </schema> <schema name="wolf" checkSQLschema="true" sqlMaxLimit="100" randomDataNode="dn2" dataNode="dn2"> </schema> <dataNode name="dn1" dataHost="host1" database="wulaoer" /> #这里的dn1对应上面的dn1 <dataNode name="dn2" dataHost="host1" database="wolf" /> <dataHost name="host1" maxCon="1000" minCon="10" balance="3" #这里的host1对应上面的host1 writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <!-- can have multi write hosts --> <writeHost host="hostM1" url="10.211.55.40:3306" user="root" #写数据库 password="root"> <!-- can have multi read hosts --> #读数据库 <readHost host="hostS1" url="10.211.55.39:3306" user="root" password="root" /> </writeHost> </dataHost> </mycat:schema>
这里有几个注意事项,针对schema配置针对wulaoer这个库的读写分离,如果在schema里的name中不修改默认会进入一个虚拟的TESTDB库里,但是TESTDB库里的内容还是wulaoer库中的一样,不过配置项目连接时不能使用wulaoer,必须配置连接TESTDB才可以,如果name里配置wulaoer,那么应用程序里就可以直接使用wulaoer即可。不过针对schema的注意事项需要注意,下面的关键点。
balance balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的 writeHost 上。 balance="1",全部的 readHost 与 stand by writeHost 参与 select 语句的负载均衡,简单的说,当双主双从模式(M1->S1,M2->S2,并且 M1 与 M2 互为主备),正常情况下,M2,S1,S2 都参与 select 语句的负载均衡。 balance="2",所有读操作都随机的在 writeHost、readhost 上分发。 balance="3",所有读请求随机的分发到 wiriterHost 对应的 readhost 执行,writerHost 不负担读压力,注意 balance=3 只在 1.4 及其以后版本有,1.3 没有。 switchType switchType="1" 默认值,自动切换 switchType="2" 基于 MySQL 主从同步的状态决定是否切换 心跳语句为 show slave status switchType="3" 基于 MySQL galary cluster 的切换机制(适合集群)(1.4.1)心跳语句为 show status like ‘wsrep%’
针对server.xml的文件我们不需要修改过多,主要是针对mycat的管理配置,里面有注释,需要注意schemq.xml里配置的schemq的name和service.xml里的schemas必须要一致,要不启动失败。
<user name="root" defaultAccount="true"> <property name="password">123456</property> <property name="schemas">wulaoer,wolf</property> #这里需要注意,如果多个库配置读写分离需要用逗号分开
下面启动mycat服务,如果你的系统是arm64的需要下载一下wrapper,然后copy到bin目录下就可以了
[root@clinent ~]# /usr/local/mycat/bin/mycat start #如果出现 Unable to locate any of the following operational binaries: /etc/develop/mycat/bin/./wrapper-linux-aarch64-64 /etc/develop/mycat/bin/./wrapper-linux-aarch64-32 /etc/develop/mycat/bin/./wrapper 解决方法: [root@clinent ~]# wget https://download.tanukisoftware.com/wrapper/3.5.40/wrapper-linux-armhf-64-3.5.40.tar.gz tar zxvf wrapper-linux-armhf-64-3.5.40.tar.gz 将 bin/wrapper 拷贝至 mycat/bin 目录下 将 lib/libwrapper.so 拷贝至 mycat/lib 目录下
测试mycat的读写分离。
[root@mysql01 ~]# mysql -uroot -p123456 -h10.211.55.38 -P 8066 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 1 Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (OpenCloudDB) Copyright (c) 2000, 2023, Oracle and/or its affiliates. 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> use wulaoer; Database changed mysql> show tables; +-------------------+ | Tables_in_wulaoer | +-------------------+ | employees | | lhj | +-------------------+ 2 rows in set (0.02 sec) mysql> create table wolf(id int,name char(11)); Query OK, 0 rows affected (0.09 sec) mysql> insert wolf() values(1,'wolf27'),(2,'wolf'); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from wolf; +------+--------+ | id | name | +------+--------+ | 1 | wolf27 | | 2 | wolf | +------+--------+ 2 rows in set (0.01 sec) mysql> show databases; +----------+ | DATABASE | +----------+ | wolf | | wulaoer | +----------+ 2 rows in set (0.00 sec)
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏