mysql--mycat读写分离

发布时间:2023-06-11 15:30

mycat读写分离

Mycat 数据库中间件

mysql--mycat读写分离_第1张图片

www.mycat.io
http://www.oracle.com/technetwork/java/javase/overview/index.html{.underline}

部署

4台机器

user: 192.168.240.191

Mycat: 192.168.240.140

master: 192.168.240.190

slave: 192.168.240.192

  1. mycat服务器配置(将java 包和mycat包上传到/jdk目录中)

注意:mycat中间件是由java开发的所以要在java环境下才能运行。

# mkdir /jdk

# cd /jdk/

查看是否有安装过java 低于1.8就删掉重装(最小化是没有安装java)

# rpm -qa | grep java

# tar -xvf jdk-8u261-linux-x64.tar.gz -C /usr/local/

修改解压后的目录 改包为java (也可以不改,做软链接也行)

# mv /usr/local/jdk1.8.0_261/ /usr/local/java

添加环境变量

# vim /etc/profile.d/jdk.sh

JAVA_HOME=/usr/local/java PATH=$JAVA_HOME/bin:$PATH

export JAVA_HOME PATH

#source /etc/profile.d/jdk.sh # java -version

java version “1.8.0_261”

Java™ SE Runtime Environment (build 1.8.0_261-b12)

Java HotSpot™ 64-Bit Server VM (build 25.261-b12, mixed mode)

  1. 解压mycat安装包并配置

解压安装包

# tar -xvf Mycat-server-1.6.7.1-release-20200209222254-linux.tar.gz
-C /usr/local/

配置MyCat账号密码和数据库名(server.xml

# cd /usr/local/mycat/

# ls

bin catlet conf lib logs version.txt

#cd conf # ls

autopartition-long.txt index_to_charset.properties rule.xml server.xml

auto-sharding-long.txt log4j2.xml schema.xml sharding-by-enum.txt

auto-sharding-rang-mod.txt migrateTables.properties
sequence_conf.properties wrapper.conf cacheservice.properties
myid.properties sequence_db_conf.properties zkconf

dbseq.sql partition-hash-int.txt sequence_distributed_conf.properties
zkdownload ehcache.xml

#vim server.xml

100.

101.123456

±-----±--------------------------------------------------------------+ |
102 | > TESTDB
|
++=========================================================+ | 103 |
|
±-----±--------------------------------------------------------------+ |
104 | > |
±-----±--------------------------------------------------------------+ |
105 | > |
±-----±--------------------------------------------------------------+ |
106 | >
|
±-----±--------------------------------------------------------------+ |
107 | >
|
±-----±--------------------------------------------------------------+ |
108 |


|
±-----±--------------------------------------------------------------+ |
109 |

|
±-----±--------------------------------------------------------------+ |
110 | >
|
±-----±--------------------------------------------------------------+ |
111 | >
|
±-----±--------------------------------------------------------------+ |
112 | > -->
|
±-----±--------------------------------------------------------------+ |
113 | >
|
±-----±--------------------------------------------------------------+

user节点配置用户信息,详细说明如下: user 用户配置节点

name 登录的用户名,也就是连接Mycat的用户名

password 登录的密码,也就是连接Mycat的密码

schemas 数据库名,这里会和schema.xml中的配置关联,多个库用逗号分开

修改后的server.xml

±----±---------------------------------------------------±--------------------------------------+ | 100 | > | >
客户端登录Mycat的用户名 |
+=+==========+=+ | 101 | > 123 | >
客户端登录Mycat用户名的密码 |
±----±---------------------------------------------------±--------------------------------------+ | 102 | > test_db | >
客户端登录Mycat数据库后的虚拟库名称 |
±----±---------------------------------------------------±--------------------------------------+ | 103 | |
|
±----±---------------------------------------------------±--------------------------------------+ | 104 | > |
|
±----±---------------------------------------------------±--------------------------------------+ | 105 | >

113 114

115

116 user

117TESTDB

118 true

119 120 -->

121

schema.xml 文件中配置读写分离

# vim schema.xml

1

2
3 ]{.underline}
4
5

33

34

36

37

38

39

40

41

42 -->

43

44 writeType=“0” dbType=“mysql” dbDriver=“native” switchType=“1” slaveThreshold=“100”>

45select user()

46

47

49

50

51

52 53 password=“123456” />

54

55

修改后的schema.xml

# cat -n conf/schema.xml

  1. ]{.underline}

4

  1. writeType=“0” dbType=“mysql” dbDriver=“native” switchType=“1” slaveThreshold=“100”>

  2. select user()

  3. password=“1”>

18

19

20

配置内容小解

schema name=“test_db” //虚拟库名称与server.xml对应

checkSQLschema=“false” // 不启用检查

sqlMaxLimit=“100” //最大连接数dataNode=“mycat_db” // 数据节点名称

dataNode name=“mycat_db” //数据节点名称

dataHost=“localhost1” //主机组

database=“test” //真实数据库的名称 在数据库中必须先创建出来.

dataHost name=“localhost1” //主机组maxCon=“1000” minCon=“10” // 最大任务和最小任务

balance=“3” //负载均衡

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 没有。

writeType=“0”, 所有写操作发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties .

writeType=“1”,所有写操作都随机的发送到配置的 writeHost。

writeType=“2”,没实现。-1 表示不自动切换1 默认值,自动切换2 基于MySQL 主从同步的状态决定是否切换

writeHost host=“hostM1” // 写主机的主机名自定义

url=“192.168.240.190:3306” // 写主机的ip地址及数据库端口

user=“writeuser” // 写主机要授权的用户名

password=“1” // 写主机授权用户(writeuser)的密码

readHost host=“hostS2” // 读主机的主机名自定义url=“192.168.240.192:3306” //读主机的ip地址及数据库端口user=“readuser” //读主机要授权的用户名

password=“1” //读主机授权用户(readuser)的密码

配置 AB复制(略)和授权相应的用户

master :192.168.240.190

mysql> grant all on *.* to ‘writeuser’@‘192.168.240.%’ identified by ‘1’; mysql> flush privileges;

mysql> create database test; 此数据库是schema.xml配置文件中mycat_db节点连接的真实库名。

slave :192.168.240.192

mysql> grant all on *.* to ‘readeuser’@‘192.168.240.%’ identified by ‘1’; mysql> flush privileges;

客户端验证用户是否能正常登录

# mysql -h 192.168.240.190 -uwriteuser -P3306 -p1

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 257

Server version: 5.7.26-log Source distribution

Copyright © 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>

# mysql -h 192.168.240.192 -ureaduser -P3306 -p1

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 248

Server version: 5.7.26-log Source distribution

Copyright © 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>

启动mycat

# cd /usr/local/mycat/

# ./bin/mycat start 启动mycat Starting Mycat-server…

# ./bin/mycat status 查看状态

Mycat-server is running (5083).

查看端口(mycat的端口8066)

# ss -tanlp |grep java

±-------±–±------±------------------±------------±------------------------------------+
| LISTEN | 0 | > 1 | > 127.0.0.1:32000 | > * | > users:((“java”,pid=5085,fd=4)) |
++=+=+=+=+===============================+
| LISTEN | 0 | > 50 | > [::]:1984 | > [::] | > users:((“java”,pid=5085,fd=59)) |
±-------±–±------±------------------±------------±------------------------------------+
| LISTEN | 0 | > 100 | > [::]:8066 | > [::] | > users:((“java”,pid=5085,fd=85)) |
±-------±–±------±------------------±------------±------------------------------------+
| LISTEN | 0 | > 50 | > [::]:33638 | > [::] | > users:((“java”,pid=5085,fd=62)) |
±-------±–±------±------------------±------------±------------------------------------+
| LISTEN | 0 | > 100 | > [::]:9066 | > [::] | > users:((“java”,pid=5085,fd=81)) |
±-------±–±------±------------------±------------±------------------------------------+
| LISTEN | 0 | > 50 | > [::]:37491 | > [::] | > users:((“java”,pid=5085,fd=58)) |
±-------±–±------±------------------±------------±------------------------------------+

==================================================================

防火墙放行端口

mycat 机器上

# firewall-cmd --add-port=8066/tcp --permanent # firewall-cmd --reload

success

# firewall-cmd --info-zone=public

====================================================================

master 和slave 放行3306端口

# firewall-cmd --add-port=3306/tcp --permanent # firewall-cmd --reload

success

=====================================================================

验证:

# mysql -u’mycat’ -h’192.168.240.140’ -P8066 -p’123’

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 6

Server version: 5.6.29-mycat-1.6.7.1-release-20200209222254 MyCat Server (OpenCloudDB) Copyright © 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>

用户为server.xml中授权的用户和密码

mysql> show databases;

±---------+

| DATABASE |

±---------+

| test_db |

±---------+

1 row in set (0.00 sec)
mysql> create database test1;

ERROR 3000 (HY000): No MyCAT Database selected 要切换到test_db虚拟库实际连接的是mysql的test库

mysql> use test_db;

Database changed

mysql> create database test2; Query OK, 1 row affected (0.00 sec)

mysql> create table t1(id int, name varchar(10));

Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values (1,‘jim’); Query OK, 1 row affected
(0.07 sec)

mysql> select * from t1;

±-----±-----+

| id | name |

±-----±-----+

| 1 | jim |

±-----±-----+

1 row in set (0.00 sec)

masterslave上查看是否有新创的库

master:

mysql> show databases;

±-------------------+

| Database |

±-------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

| test |

| test1 |

| test2 |

±-------------------+

7 rows in set (0.00 sec)

mysql> select * from test.t1;

±-----±-----+

| id | name |

±-----±-----+

| 1 | jim |

±-----±-----+

1 row in set (0.00 sec)

slave:

mysql> show databases;

±-------------------+

| Database |

±-------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

| test |

| test1 |

| test2 |

±-------------------+

7 rows in set (0.00 sec)

mysql> select * from test.t1;

±-----±-----+

| id | name |

±-----±-----+

| 1 | jim |

±-----±-----+

1 row in set (0.00 sec)

可以看出我们默认的虚拟库test_db 其实连接的真实库是test库 在创建表时可以看到数据是在test库中的并且

masterslave 已经同步数据。

=====================================================================

mycat 日志

位置:

…/logs/

/usr/local/mycat/logs/

  • wrapper.log 为程序启动的日志,启动时的问题看这个

  • mycat.log 为脚本执行时的日志,SQL脚本执行报错后的具体错误内容,查看这个文件。mycat.log是最新的错误日志,历史日志会根据时间生成目录保存

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号