MYSQL-Mycat 도구를 사용하여 mysql 마스터-슬레이브 읽기-쓰기 분리 실현

MYSQL-Mycat 도구를 사용하여 mysql 마스터-슬레이브 읽기-쓰기 분리 실현

1. 읽기-쓰기 분리 원칙 소개

프로덕션 환경 아키텍처 : 마스터-슬레이브 복제를 사용하여 데이터를 동기화 한 다음 읽기-쓰기 분리를 사용하여 데이터베이스의 동시로드 용량을 향상시킵니다.

읽기-쓰기 분리의 장점 :
① 접근 압력 증가로 인해 단일 서버의 성능에 병목 현상이 발생하고 부하를 공유해야합니다.
② 메인 라이브러리는 쓰기를 담당하고 슬레이브 라이브러리는 읽기를 담당하므로 X 잠금 (전용 잠금)과 S 잠금 (공유 잠금)의 점유가 완화됩니다.
③ myisam 엔진을 라이브러리에서 사용하여 쿼리 성능을 향상시키고 시스템 오버 헤드를 줄일 수 있습니다.
④ 중복성을 높이고 가용성을 향상시킵니다.

읽기와 쓰기의 분리 실현 :
① 응용 계층의 실현 : 애플리케이션과 커넥터에서 읽기와 쓰기의 분리를 실현합니다. [읽기-쓰기 분리의 내부 구현, 손쉬운 배포, 접근이 크지 않을 때 성능 향상, 아키텍처 조정 코드도 조정해야 함, 고급 애플리케이션 구현이 어렵고 대규모 애플리케이션 시나리오에 적용 할 수 없음]
미들웨어 구현 : 외부 미들웨어 프로그램에서 읽기 분리 쓰기. [Mycat, 많이 소개하지 않음]

2. Mycat 데이터베이스 하위 데이터베이스 하위 테이블 미들웨어

2.1 mycat 소개

mycat은 엔터프라이즈 애플리케이션 개발을위한 오픈 소스 데이터베이스 클러스터입니다. 지원 트랜잭션, ACID는 새로운 유형의 데이터베이스 미들웨어 제품입니다.

mysql은 읽기-쓰기 분리 토폴로지를 실현합니다.
여기에 사진 설명 삽입
mycat은 windows, linux, mac, solaris 및 기타 시스템에 설치하고 실행할 수 있습니다.

mycat 공식 웹 사이트 : http://www.mycat.org.cn/

2.2 Mycat 미들웨어는 읽기-쓰기 분리 배포를 실현합니다.

CPU 이름 IP 주소 마스터-슬레이브 데이터베이스 유형 mysql 버전 mycat 설치 호스트
석사 192.168.10.10 메인 라이브러리 mysql 5.7.31 192.168.10.10
노예 192.168.10.20 슬레이브 라이브러리 mysql 5.7.31

①mycat 다운로드 및 압축 해제 :

# 下载tar包并解压到/usr/local/mycat目录
[root@master ~]# ll Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz 
-rw-r--r--. 1 root root 21760812 9月  23 15:36 Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz
[root@master ~]# tar xzf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz -C /usr/local/
[root@master ~]# ll /usr/local/mycat/
总用量 12
drwxr-xr-x. 2 root root  190 9月  23 15:37 bin
drwxrwxrwx. 2 root root    6 10月 22 2019 catlet
drwxrwxrwx. 4 root root 4096 9月  23 15:37 conf
drwxr-xr-x. 2 root root 4096 9月  23 15:37 lib
drwxrwxrwx. 2 root root    6 1月   5 2020 logs
-rwxrwxrwx. 1 root root  227 1月   5 2020 version.txt

# 创建mycat用户,并设置密码
[root@master ~]# useradd mycat
[root@master ~]# echo 123456 | passwd --stdin mycat
更改用户 mycat 的密码 。
passwd:所有的身份验证令牌已经成功更新。

# 设置/usr/local/mycat目录的所有者和所属组为mycat
[root@master ~]# chown -R mycat:mycat /usr/local/mycat/
[root@master ~]# ll /usr/local/mycat/
总用量 12
drwxr-xr-x. 2 mycat mycat  190 9月  23 15:37 bin
drwxrwxrwx. 2 mycat mycat    6 10月 22 2019 catlet
drwxrwxrwx. 4 mycat mycat 4096 9月  23 15:37 conf
drwxr-xr-x. 2 mycat mycat 4096 9月  23 15:37 lib
drwxrwxrwx. 2 mycat mycat    6 1月   5 2020 logs
-rwxrwxrwx. 1 mycat mycat  227 1月   5 2020 version.txt

bin程序目录
conf配置文件目录
lib目录主要存放mycat依赖的一些jar文件
logs目录存放日志

# 查看版本信息 1.6.7.4-release
[root@master ~]# cd /usr/local/mycat/
[root@master mycat]# cat version.txt
BuildTime  2020-01-05 08:41:01
GitVersion   f929f96a16852869bc9dc63f4c0f192ee02818e0
MavenVersion 1.6.7.4-release
GitUrl https://github.com/MyCATApache/Mycat-Server.git
MyCatSite http://www.mycat.org.cn

② 적어도 JDK1.7보다 높은 버전의 JDK를 설치합니다.

# 卸载原来的openjdk,安装oracle-jdk
# oracle-jdk性能比openjdk好,兼容性大。
[root@master ~]# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
[root@master ~]# yum remove -y java-* 

# 下载oracle-jdk,解压到/usr/local/下
[root@master ~]# tar xzvf /usr/local/src/jdk-8u191-linux-x64.tar.gz -C /usr/local/

# 在/usr/local目录下做一个软连接
[root@master ~]# cd /usr/local/
oot@master local]# ln -s jdk1.8.0_191 jdk1.8

# 配置环境变量
[root@master ~]# vim /etc/profile.d/jdk8.sh
[root@master ~]# cat /etc/profile.d/jdk8.sh 
export JAVA_HOME=/usr/local/jdk1.8.0_191
export CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/jar/tools.jar:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

# 使环境变量生效
[root@master ~]# chmod +x /etc/profile.d/jdk8.sh
[root@master ~]# /etc/profile.d/jdk8.sh
[root@master ~]# bash

# 查看jdk版本
# 不配置环境变量的话可以做一个软连接
[root@master local]# ln -s /usr/local/jdk1.8/bin/java /usr/bin/java
[root@master local]# java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

③ 시작하려면 mycat을 배포합니다.

# 配置环境变量,在/etc/profile.d目录下创建mycat.sh脚本,添加环境变量
[root@master ~]# vim /etc/profile.d/mycat.sh
[root@master ~]# cat /etc/profile.d/mycat.sh 
MYCAT_HOME=/usr/local/mycat
PATH=$MYCAT_HOME/bin:$PATH

# 使环境变量立即生效
[root@master ~]# chmod +x /etc/profile.d/mycat.sh
[root@master ~]# source /etc/profile.d/mycat.sh 

# 定义mycat集群中各服务器的IP地址和主机名的映射关系
[root@master ~]# vim /etc/hosts
192.168.10.10 master
192.168.10.20 slave

# 修改mycat的用户信息和授权信息
[root@master ~]# cd /usr/local/mycat/conf/
[root@master conf]# cp server.xml server.xml.bak
[root@master conf]# echo "" > server.xml
[root@master conf]# vim server.xml
# wabong用户用于写,rabong用户用于读
[root@master conf]# cat server.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
	<system>
	<property name="defaultSqlParser">druidparser</property>
	</system>
	<!--以下设置为应用访问帐号权限 -->
	<!--可写账号 -->
	<user name="wabong">
	<property name="password">123456</property>
	<property name="schemas">test</property>
	</user>
	<!--可读账号 -->
	<user name="rabong">    # 读库账号
	<property name="password">123456</property>
	<property name="schemas">test</property>
	<property name="readOnly">true</property>
	</user>
</mycat:server>

# 配置mycat架构
[root@master conf]# cp schema.xml schema.xml.bak
[root@master conf]# echo "" > schema.xml
[root@master conf]# vim schema.xml
[root@master conf]# cat schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
 <schema name="test" checkSQLschema="false" sqlMaxLimit="100" dataNode='dn1'> </schema>
 <dataNode name="dn1" dataHost="dthost" database="test"/>
 <dataHost name="dthost" maxCon="500" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="-1" slaveThreshold="100">
 <heartbeat>select user()</heartbeat>
 <!--后端写库信息 -->
 <writeHost host="master" url="192.168.10.10:3306" user="mycat" password="Abong123.">
	 <!--后端读库信息 -->
	<readHost host="slave" url="192.168.10.20:3306" user="mycat" password="Abong123." />
 </writeHost>
 </dataHost>
</mycat:schema>

# 启动mycat
[root@master ~]# mycat 
Usage: /usr/local/mycat/bin/mycat {
    
     console | start | stop | restart | status | dump }
[root@master ~]# mycat start
Starting Mycat-server...
[root@master ~]# cat /usr/local/mycat/logs/wrapper.log     # 看到这个日志记录,就说明已经启动成功了。
INFO   | jvm 1    | 2020/09/23 16:23:27 | MyCAT Server startup successfully. see logs in logs/mycat.log
[root@master ~]# mycat status;
Mycat-server is running (9115).

mysql 마스터-슬레이브 동기 복제를 구성합니다.

# 配置主库/etc/my.cnf
[root@master ~]# vim /etc/my.cnf
log-bin=mysql-bin-master
server-id=1
binlog-do-db=test
binlog-ignore-db=mysql
[root@master ~]# systemctl restart mysqld

# 在主库上创建用户mycat,并授予all privileges的权限,允许在所有主机的客户端上使用mycat用户进行登录。
[root@master ~]# mysql -uroot -p
Enter password: 

mysql> grant all privileges on *.* to mycat@'%' identified by 'Abong123.';   # 用于读写分离的用户
Query OK, 0 rows affected, 1 warning (0.04 sec)

mysql> grant replication slave on *.* to [email protected] identified by 'Abong123.';   # 用于主从复制的用户
Query OK, 0 rows affected, 1 warning (0.03 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

mysql> exit
Bye

# 主库上导出test数据库,并导入到从库。保持主从数据一致性。
[root@master ~]# mysqldump -uroot -p test > test.sql
Enter password: 
[root@master ~]# ll test.sql 
-rw-r--r--. 1 root root 6815 9月  24 11:24 test.sql
[root@master ~]# scp test.sql [email protected]:/root/

[root@slave ~]# mysql -uroot -p     # 导入test到从库前的检测
Enter password: 
mysql> show databases;    # 从库上没有test数据库,要先创建。
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

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

mysql> exit
Bye
[root@slave ~]# mysql -uroot -p test < test.sql
Enter password: 

# 配置从库上的/etc/my.cnf
[root@slave ~]# vim /etc/my.cnf
[mysqld]
server-id=2
[root@slave ~]# systemctl restart mysqld

# 在从库上创建用户mycat,并授予all privileges的权限,允许在所有主机的客户端上使用mycat用户进行登录。
[root@slave ~]# mysql -uroot -p
Enter password: 

mysql> grant all privileges on *.* to mycat@'%' identified by 'Abong123.';
Query OK, 0 rows affected, 1 warning (0.04 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

mysql> exit
Bye

# 开放主库上的3306端口号
[root@master ~]# firewall-cmd --permanent --zone=public --add-port=3306/tcp
success
[root@master ~]# firewall-cmd --reload 
success

# 建立主从关系
mysql> show master status\G;    # 主库信息
*************************** 1. row ***************************
             File: mysql-bin-master.000001
         Position: 154
     Binlog_Do_DB: test
 Binlog_Ignore_DB: mysql
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified

# 配置从库对应主库信息:
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host='192.168.10.10',master_port=3306,master_user='slave',master_password='Abong123.',master_log_file='mysql-bin-master.000001',master_log_pos= 154;
Query OK, 0 rows affected, 2 warnings (0.06 sec)

mysql> start slave;
Query OK, 0 rows affected (0.02 sec)

mysql> show slave status\G;    # 可以看到主从同步复制的关系已经建立起来。
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

2.3 Mycat 읽기 및 쓰기 분리 테스트

로그인 형식 : mysql -urabong -p123456 -h 192.168.10.10 -P8066
mycat은 192.168.10.10에 설치되므로 -h도 192.168.10.10입니다.
mycat의 경우 8066은 데이터 포트이고 9066은 관리 포트입니다.
클라이언트는 mysql 데이터베이스에 로그인하고 포트 8066을 사용합니다.
클라이언트는 포트 9066을 사용하여 mycat을 관리합니다.
① 읽기 전용 사용자 rabong은 mycat을 통해 mysql 데이터베이스에 로그인합니다.

[root@master ~]# mysql -urabong -p123456 -h 192.168.10.10 -P8066
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 10
Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (OpenCloudDB)   # 可以看到是通过中间件mycat登录的

Copyright (c) 2000, 2020, 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> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> desc emp;     # 可以看到rabong是没有写权限的,即只能执行select语句。
ERROR 1495 (HY000): User readonly
mysql> select * from emp;
+----+------+--------+
| id | name | deptno |
+----+------+--------+
|  1 | haha |     20 |
|  2 | xixi |     20 |
|  3 | ohoh |     20 |
|  5 | yeye |     20 |
+----+------+--------+
4 rows in set (0.10 sec)

② 쓰기 가능한 사용자 wabong은 mycat을 통해 mysql 데이터베이스에 로그인합니다.

[root@master ~]# mysql -uwabong -p123456 -h 192.168.10.10 -P8066
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 11
Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (OpenCloudDB)   # 可以看到是通过中间件mycat登录的

Copyright (c) 2000, 2020, 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> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> desc emp;      # 可以看到用户wabong是具有读写权限的
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(10) | YES  |     | NULL    |       |
| deptno | int(11)     | YES  | MUL | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.40 sec)

mysql> select  * from emp;
+----+------+--------+
| id | name | deptno |
+----+------+--------+
|  1 | haha |     20 |
|  2 | xixi |     20 |
|  3 | ohoh |     20 |
|  5 | yeye |     20 |
+----+------+--------+
4 rows in set (0.01 sec)

③ 주 라이브러리 또는 슬레이브 라이브러리의 실패가 사용자의 데이터베이스 액세스에 미치는 영향을 테스트합니다.

메인 라이브러리가 정상적으로 실행되고 슬레이브 라이브러리를 중지합니다.

[root@slave ~]# systemctl stop mysqld

[root@master ~]# mysql -uwabong -p123456 -h 192.168.10.10 -P8066
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 12
Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (OpenCloudDB)

Copyright (c) 2000, 2020, 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> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> desc emp;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(10) | YES  |     | NULL    |       |
| deptno | int(11)     | YES  | MUL | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (1.66 sec)

mysql> select * from emp;
+----+------+--------+
| id | name | deptno |
+----+------+--------+
|  1 | haha |     20 |
|  2 | xixi |     20 |
|  3 | ohoh |     20 |
|  5 | yeye |     20 |
+----+------+--------+
4 rows in set (0.32 sec)

mysql> insert into emp values(4,'lala',20);
Query OK, 1 row affected (0.08 sec)

mysql> select * from emp;
+----+------+--------+
| id | name | deptno |
+----+------+--------+
|  1 | haha |     20 |
|  2 | xixi |     20 |
|  3 | ohoh |     20 |
|  4 | lala |     20 |
|  5 | yeye |     20 |
+----+------+--------+
5 rows in set (0.00 sec)

测试结果:主库上仍然可以进行读写操作。对主库的写操作会在从库恢复后再次进行同步复制,保证主从数据的一致性。

메인 라이브러리가 중단되고 슬레이브 라이브러리가 정상적으로 실행됩니다.

[root@master ~]# systemctl stop mysqld

[root@master ~]# mysql -uwabong -p123456 -h 192.168.10.10 -P8066
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 13
Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (OpenCloudDB)

Copyright (c) 2000, 2020, 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> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> desc emp;
ERROR 1184 (HY000): java.net.ConnectException: 拒绝连接
mysql> select * from emp;
+----+------+--------+
| id | name | deptno |
+----+------+--------+
|  1 | haha |     20 |
|  2 | xixi |     20 |
|  3 | ohoh |     20 |
|  4 | lala |     20 |
|  5 | yeye |     20 |
+----+------+--------+
5 rows in set (0.00 sec)

测试结果:由于主库挂了,故不再对主库进行写操作,为保持主从数据库的一致性,从库上也不能进行写操作,但是还可以进行读操作。

추천

출처blog.csdn.net/weixin_36522099/article/details/108724983