共计 9780 个字符,预计需要花费 25 分钟才能阅读完成。
这篇文章给大家分享的是有关 MyCat 如何实现 MySQL 双主一从读写分离的内容。丸趣 TV 小编觉得挺实用的,因此分享给大家做个参考,一起跟随丸趣 TV 小编过来看看吧。
1)配置 server.xml
user name= root defaultAccount= true
property name= password mysql /property
property name= schemas TESTDB /property
!-- 表级 DML 权限设置 --
!--
privileges check= false
schema name= TESTDB dml= 0110
table name= tb01 dml= 0000 /table
table name= tb02 dml= 1111 /table
/schema
/privileges
--
/user
配置用户 root,密码为 mysql,对 TESTDB 具有权限
2)配置 schema.xml
schema name= TESTDB checkSQLschema= false sqlMaxLimit= 100 dataNode= dn1
/schema
!-- dataNode name= dn1$0-743 dataHost= localhost1 database= db$0-743
/ --
dataNode name= dn1 dataHost= localhost1 database= test /
dataNode name= dn2 dataHost= localhost1 database= dedecms /
dataNode name= dn3 dataHost= localhost1 database= xcredit /
!-- dataNode name= dn4 dataHost= sequoiadb1 database= SAMPLE /
dataNode name= jdbc_dn1 dataHost= jdbchost database= db1 /
dataNode name= jdbc_dn2 dataHost= jdbchost database= db2 /
dataNode name= jdbc_dn3 dataHost= jdbchost database= db3 / --
dataHost name= localhost1 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= master1 url= 192.168.8.57:3306 user= root
password= mysql
!-- can have multi read hosts --
readHost host= slave url= 192.168.8.59:3306 user= root password= mysql /
/writeHost
writeHost host= master2 url= 192.168.8.58:3306 user= root
password= mysql /
schema 标签:schema name 属性指定逻辑库名,dataNode 属性指定下边的 dataNode
dataNode 标签:dataHost 指定下边的 dataHost,database 指定具体的 database
dataHost 标签:balance 指的负载均衡类型,switchType 指的是切换的模式
balance 指的负载均衡类型,目前的取值有 4 种:
1. balance= 0 , 不开启读写分离机制,所有读操作都发送到当前可用的 writeHost 上。
2. balance= 1,全部的 readHost 与 stand by writeHost 参与 select 语句的负载均衡。
3. balance= 2,所有读操作都随机的在 writeHost、readhost 上分发。
4. balance= 3,所有读请求随机的分发到 wiriterHost 对应的 readhost 执行,writerHost 不负担读压力
switchType 指的是切换的模式,目前的取值也有 4 种:
1. switchType= -1 表示不自动切换
2. switchType= 1 默认值,表示自动切换
3. switchType= 2 基于 MySQL 主从同步的状态决定是否切换, 心跳语句为 show slave status
4. switchType= 3 基于 MySQL galary cluster 的切换机制(适合集群)(1.4.1),心跳语句为 show status like wsrep%。
3)启动 mycat,并观察日志
/usr/local/mycat/bin/mycat start
[root@oneproxy logs]# tail -f wrapper.log
STATUS | wrapper | 2018/08/25 10:32:39 | -- Wrapper Started as Daemon
STATUS | wrapper | 2018/08/25 10:32:40 | Launching a JVM...
INFO | jvm 1 | 2018/08/25 10:32:40 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO | jvm 1 | 2018/08/25 10:32:40 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
INFO | jvm 1 | 2018/08/25 10:32:40 |
INFO | jvm 1 | 2018/08/25 10:32:41 | log4j:WARN No appenders could be found for logger (io.mycat.memory.MyCatMemory).
INFO | jvm 1 | 2018/08/25 10:32:41 | log4j:WARN Please initialize the log4j system properly.
INFO | jvm 1 | 2018/08/25 10:32:41 | log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
INFO | jvm 1 | 2018/08/25 10:32:41 | MyCAT Server startup successfully. see logs in logs/mycat.log
4)在 mycat 服务器登录 mysql
mysql -uroot -pmysql -P8066 -h292.168.8.63
mysql show databases;
+———-+
| DATABASE |
+———-+
| TESTDB |
+———-+
5)测试读写分离
mysql use TESTDB;
mysql create table t4(hostname1 varchar(12),hostname2 varchar(12),hostname3 varchar(12));
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql mysql insert into t4 values(@@hostname,@@hostname,@@hostname);
mysql select * from t4;
+-----------+-----------+-----------+
| hostname1 | hostname2 | hostname3 |
+-----------+-----------+-----------+
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
| master1 | master1 | master1 |
+-----------+-----------+-----------+
64 rows in set (0.00 sec)
插入的数据都是从 master1 插入的,通过查询 mycat 日志,发现所有的查询全部分发到了 slave 节点。
感谢各位的阅读!关于“MyCat 如何实现 MySQL 双主一从读写分离”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!