Mycat实现mysql读写分离

Mycat实现mysql读写分离

Mycat 官网:http://www.mycat.org.cn/

所有主机系统环境

Rocky就是以前的CentOS
[root@client ~]# cat /etc/os-release 
NAME="Rocky Linux"
VERSION="8.6 (Green Obsidian)"
ID="rocky"
关闭防火墙,selinux,并且确保时间同步
mycat	建议内存2G以上,mycat启动比较慢,先ss -ntl 查看端口是否打开了,端口打开了再使用mysql客户端连接
项目主机:
mysql-master:	10.0.0.100		安装mysql-server
mysql-slave1:	10.0.0.101		安装mysql-server
mysql-slave2:	10.0.0.102		安装mysql-server
mycat:			10.0.0.103		安装Java+mycat
client:			10.0.0.104		安装mysql
项目实现如下:

Mycat实现mysql读写分离_第1张图片

一、MySQL一主二从实现

1、安装mysql-server

master:	10.0.0.100		安装mysql-server
slave1:	10.0.0.101		安装mysql-server
slave2:	10.0.0.102		安装mysql-server
三台主机安装mysql-server:
yum -y install mysql-server
启动数据库(开机自启动)
systemctl enable --now mysqld

2、修改mysql服务器配置文件

(1)vim /etc/my.cnf

master
[mysqld]
server_id=100
log-bin

slave1
[mysqld]
server_id=101
log-bin

slave2
[mysqld]
server_id=102
log-bin

(2)重启数据库

三台都重启操作
systemctl restart mysqld

3、登录数据库配置主从

(1)master

登录数据库:mysql8.0安装好后默认没有密码
[root@master ~]# mysql -uroot -p
记录二进制日志文件和二进制记录位置起始点
mysql> show master status;
mysql> show master logs;
+------------------+-----------+-----------+
| Log_name         | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000001 |       179 | No        |
| mysql-bin.000002 |       156 | No        |
+------------------+-----------+-----------+
2 rows in set (0.00 sec)

创建用户repluser允许远程登录的ip地址10.0.0.%密码为123456
授予所有权限,可访问所有数据库
mysql> create user repluser@'10.0.0.%' identified by '123456';
mysql> grant all on *.* to repluser@'10.0.0.%';

(2)slave1、slave2一样

登录
[root@slave1 ~]# mysql -uroot -p
主从同步配置
mysql> CHANGE MASTER TO
       MASTER_HOST='10.0.0.100',
       MASTER_USER='repluser',
       MASTER_PASSWORD='123456',
       MASTER_PORT=3306,
       MASTER_LOG_FILE='mysql-bin.000002',
       MASTER_LOG_POS=156,
       MASTER_CONNECT_RETRY=10;

开启主从同步
mysql> start slave;
查看状态
mysql> show slave status\G;

同步的是创建用户之前的数据,所以用户也同步过来了
mysql> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| repluser         | 10.0.0.%  |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

(3)在master创建数据库,创建表,插入数据

创建数据库db1
mysql> create database db1;
mysql> use db1;

创建表
mysql> create table student(id int unsigned auto_increment primary key,
	name varchar(20) not null,
	age tinyint unsigned,
	gender enum('M','F')default 'M');
	
查看创建的表
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| student       |
+---------------+

查看表结构
mysql> desc student;
+--------+------------------+------+-----+---------+----------------+
| Field  | Type             | Null | Key | Default | Extra          |
+--------+------------------+------+-----+---------+----------------+
| id     | int unsigned     | NO   | PRI | NULL    | auto_increment |
| name   | varchar(20)      | NO   |     | NULL    |                |
| age    | tinyint unsigned | YES  |     | NULL    |                |
| gender | enum('M','F')    | YES  |     | M       |                |
+--------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

插入数据
mysql> insert student(name,age) values('xiaoming',18);
Query OK, 1 row affected (0.01 sec)

查看表数据
mysql> select * from student;
+----+----------+------+--------+
| id | name     | age  | gender |
+----+----------+------+--------+
|  1 | xiaoming |   18 | M      |
+----+----------+------+--------+
1 row in set (0.00 sec)

(4)查看slave1、slave2中的数据是否同步

查看所有数据库
mysql> show databases;
mysql> use db1;
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| student       |
+---------------+
1 row in set (0.00 sec)

mysql> select * from db1.student;
+----+----------+------+--------+
| id | name     | age  | gender |
+----+----------+------+--------+
|  1 | xiaoming |   18 | M      |
+----+----------+------+--------+
1 row in set (0.00 sec)

二 、Mycat实现

1、安装登录

mycat: 10.0.0.103 安装Java+mycat
(1)安装java

[root@mycat ~]# yum -y install java
[root@mycat ~]# java -version
openjdk version "1.8.0_342"

(2)下载mycat

wget http://dl.mycat.org.cn/1.6.7.4/Mycat-server-1.6.7.4-release/Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz

(3)创建文件夹,解压到创建的文件夹

[root@mycat ~]# mkdir /apps
[root@mycat ~]# tar xf Mycat-server-1.6.7.6-release-20220524173810-linux.tar.gz -C /apps
[root@mycat ~]# tree -d /apps/
/apps/
└── mycat
    ├── bin
    ├── catlet
    ├── conf
    │   ├── zkconf
    │   └── zkdownload
    ├── lib
    └── logs
8 directories

(4)加入到环境变量

加入到环境变量
[root@mycat ~]# vim /etc/profile.d/mycat.sh
PATH=/apps/mycat/bin:$PATH
使脚本生效
[root@mycat ~]# source /etc/profile.d/mycat.sh
可以看到任何目录都可以执行mycat了
[root@mycat ~]# mycat
Usage: /apps/mycat/bin/mycat { console | start | stop | restart | status | dump }

(5)启动mycat

[root@mycat ~]# mycat start
Starting Mycat-server...
查看端口确保起来了
[root@mycat ~]# ss -ntl
State        Recv-Q       Send-Q             Local Address:Port              Peer Address:Port       Process       
LISTEN       0            128                      0.0.0.0:22                     0.0.0.0:*                        
LISTEN       0            1                      127.0.0.1:32000                  0.0.0.0:*                        
LISTEN       0            128                            *:9066                         *:*                        
LISTEN       0            50                             *:44621                        *:*                        
LISTEN       0            128                         [::]:22                        [::]:*                        
LISTEN       0            50                             *:1984                         *:*                        
LISTEN       0            128                            *:8066                         *:*                        
LISTEN       0            50                             *:46823                        *:*  

日志位置
[root@mycat ~]# ls /apps/mycat/logs/
mycat.log  mycat.pid  switch.log  wrapper.log

查看日志,启动成功
[root@mycat ~]# cat /apps/mycat/logs/wrapper.log
STATUS | wrapper  | 2022/09/06 16:37:19 | TERM trapped.  Shutting down.
STATUS | wrapper  | 2022/09/06 16:37:20 | <-- Wrapper Stopped
STATUS | wrapper  | 2022/09/06 16:37:21 | --> Wrapper Started as Daemon
STATUS | wrapper  | 2022/09/06 16:37:21 | Launching a JVM...
INFO   | jvm 1    | 2022/09/06 16:37:21 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO   | jvm 1    | 2022/09/06 16:37:21 |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
INFO   | jvm 1    | 2022/09/06 16:37:21 | 
INFO   | jvm 1    | 2022/09/06 16:37:22 | MyCAT Server startup successfully. see logs in logs/mycat.log

(6)查看mycat默认账号密码

配置文件路径
[root@mycat ~]# cd /apps/mycat/conf/
[root@mycat conf]# ll schema.xml server.xml 
-rwxrwxrwx 1 root root  976 Sep  3 21:27 schema.xml
-rwxrwxrwx 1 root root 6541 Sep  3 21:25 server.xm

连接账号密码,默认端口是8066
[root@mycat ~]# vim server.xml

Mycat实现mysql读写分离_第2张图片
Mycat实现mysql读写分离_第3张图片

(7)使用安装mysql客户端主机连接mycat

客户机client安装mysql客户端
[root@client ~]# yum -y install mysql

连接mycat:用户名root密码123456主机10.0.0.103端口8066
[root@client ~]# mysql -uroot -p123456 -h 10.0.0.103 -P8066

查看数据库、登录成功
mysql> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB   |
+----------+

2、修改mycat配置文件

(1)修改默认端口

[root@mycat ~]# vim server.xml

定位到8066端口那行,拷贝到没有注释的行,保存后,重启mycat端口修改完成
Mycat实现mysql读写分离_第4张图片
(2)测试端口是否修改好

重启mycat
[root@mycat conf]# mycat restart
mysql客户端测试
[root@client ~]# mysql -uroot -p123456 -h 10.0.0.103

(3)修改schema.xml

[root@mycat conf]# vim schema.xml

Mycat实现mysql读写分离_第5张图片
Mycat实现mysql读写分离_第6张图片
(4)修改后的schema.xml

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
    <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
    </schema>

    <dataNode name="dn1" dataHost="localhost1" database="db1" />

    <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
              writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>select user()</heartbeat>
        <writeHost host="host1" url="10.0.0.100:3306" user="repluser" password="123456">
                <readHost host="host2" url="10.0.0.101:3306" user="repluser" password="123456" />
                <readHost host="host3" url="10.0.0.102:3306" user="repluser" password="123456" />
        </writeHost>
    </dataHost>
</mycat:schema>

三、 测试

(1)连接mycat

重启mycat
[root@mycat ~]# mycat restart
mysql客户端测试
[root@client ~]# mysql -uroot -p123456 -h 10.0.0.103
查看数据库
mysql> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB   |
+----------+
1 row in set (0.01 sec)
进入数据库、区分大小写
mysql> use TESTDB
查看表
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| student       |
+---------------+
1 row in set (0.00 sec)

(2)读数据测试

查询@@server_id确定写入的数据库为slave1或slave2
随机的,多操作几次就能查看到
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|         102 |
+-------------+
1 row in set (0.00 sec)

查询@@server_id确定写入的数据库为slave1或slave2
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|         101 |
+-------------+
1 row in set (0.01 sec)

(3)写数据测试

插入数据,写操作,都是在master上
mysql> insert student(name,age) values('test1',@@server_id);
Query OK, 1 row affected (0.04 sec)

mysql> insert student(name,age) values('test2',@@server_id);
Query OK, 1 row affected (0.00 sec)

mysql> insert student(name,age) values('test3',@@server_id);
Query OK, 1 row affected (0.00 sec)

mysql> insert student(name,age) values('test4',@@server_id);
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+----------+------+--------+
| id | name     | age  | gender |
+----+----------+------+--------+
|  1 | xiaoming |   18 | M      |
|  2 | test1    |  100 | M      |
|  3 | test2    |  100 | M      |
|  4 | test3    |  100 | M      |
|  5 | test4    |  100 | M      |
+----+----------+------+--------+
5 rows in set (0.00 sec)

(3)测试slave2挂了

[root@rocky86-102 ~]# systemctl stop mysqld

客户机操作

多次操作,发现前面报错几次,后面就一直是101
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|         101 |
+-------------+

(4)测试slave1也挂了
此时100负责读写

mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|         100 |
+-------------+
1 row in set (0.01 sec)

(5)master挂了
如果master数据库挂了,就全挂了,
如需要高可使用
方法1:使用MHA
方法2:PXC
方法3:mysql组复制

三(其他)

(1)多库实现
在这里插入图片描述
下面的用户也要修改,不然mycat起不来
Mycat实现mysql读写分离_第7张图片
Mycat实现mysql读写分离_第8张图片
(2)mysql操作

查看线程
mysql> show processlist;

mysql主从,从服务器操作错误后可使用下方法

停止slave同步线程
mysql> strat slave
清空slave信息
mysql> reset slave all;

你可能感兴趣的:(mysql,mysql,linux,数据库)