centos8

服务器共三台

client                  10.0.0.88        mariadb-10.4.24

mycat-server     10.0.0.18        #内存建立2G以上

mysql-master     10.0.0.28          mariadb-10.4.24

mysql-master      10.0.0.38       mariadb-10.4.24

关闭防火墙和SELinux

systemctl stop firewalld
setenforce 0
时间同步

1、实现主从复制

修改master和slave上配置文件

master

[root@master ~]#cat /etc/my.cnf
[mysqld]
datadir=/data/mysql/data
pid-file=/data/mysql/mysqll.pid
server-id=28                      添加ID编号
log-bin=/data/mysql/mysql-bin            开启二进制日志并指定路径
relay_log=/data/mysql/logs/mysql_relay.log
[client]
socket=/tmp/mysql.sock

[root@master ~]#cd /data/                         #mkdir /data/mysql
[root@master /data]#ll
total 0
drwxrwx--- 5 mysql mysql 59 May 15 20:23 mysql    #确认目录属性为    修改命令chown -R mysql.mysql /data/mysql
[root@master /data]#systemctl restart mysqld      #重启服务


lave

[root@slave ~]#cat /etc/my.cnf
[mysqld]
datadir=/data/mysql/data
pid-file=/data/mysql/mysqll.pid
server-id=38
relay_log=/data/mysql/logs/mysql_relay.log
[client]
socket = /tmp/mysql.sock

[root@slave ~]#systemctl restart mysqld

(2)master上创建复制用户

[root@master ~]#mysql -uroot -p
Enter password:

MariaDB [(none)]> create user repluser@'10.0.0.%' identified by '123456';      创建复制用户
Query OK, 0 rows affected (0.002 sec)


MariaDB [(none)]> grant replication slave on *.* to repluser@'10.0.0.%';       授权
Query OK, 0 rows affected (0.001 sec)


MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)

 

MariaDB [(none)]> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 351 |
| mysql-bin.000002 | 915 |
+------------------+-----------+
2 rows in set (0.000 sec)

(3)slave上执行

MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='10.0.0.28',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='123456',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000002',
-> MASTER_LOG_POS=915;
Query OK, 0 rows affected (0.006 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.002 sec)

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.28
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 915
Relay_Log_File: mysql_relay.000002
Relay_Log_Pos: 555
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
.........................
Slave_Transactional_Groups: 0
1 row in set (0.000 sec)

  主从复制检查

  主节点加载数据库

MariaDB [(none)]> source /root/hellodb_innodb.sql
MariaDB [hellodb]> show databases;
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)

  从节点同布

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)

2、在MySQL代理服务器10.0.0.18安装mycat并启动

[root@mycat ~]#yum -y install java

#确认安装成功

[root@mycat ~]#java -version
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)

(1)下载工具,创建目录 , 解压缩

[root@mycat ~]#wget http://dl.mycat.org.cn/1.6.7.6/20211221142218/Mycat-server-1.6.7.6-release-20211221142218-linux.tar.gz
--2022-05-15 21:59:05-- http://dl.mycat.org.cn/1.6.7.6/20211221142218/Mycat-server-1.6.7.6-release-20211221142218-linux.tar.gz
Resolving dl.mycat.org.cn (dl.mycat.org.cn)... 210.51.26.184
Connecting to dl.mycat.org.cn (dl.mycat.org.cn)|210.51.26.184|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 27570028 (26M) [application/octet-stream]
Saving to: ‘Mycat-server-1.6.7.6-release-20211221142218-linux.tar.gz’ Mycat-server-1.6.7.6-release- 100%[================================================>] 26.29M 9.72MB/s in 2.7s 2022-05-15 21:59:08 (9.72 MB/s) - ‘Mycat-server-1.6.7.6-release-20211221142218-linux.tar.gz’ saved [27570028/27570028]
[root@mycat ~]#mkdir /apps/
[root@mycat ~]#tar xf Mycat-server-1.6.7.6-release-20211221142218-linux.tar.gz -C /apps/

(2)配置环境变量

[root@mycat ~]#echo 'PATH=/apps/mycat/bin:$PATH' > /etc/profile.d/mycat.sh
[root@mycat ~]#source /etc/profile.d/mycat.sh

(3)启动mycat

[root@mycat ~]#mycat start                       #启动前内容调为大于2G,否则启动不了
Starting Mycat-server...
[root@mycat ~]# [root@mycat ~]#tail -f /apps/mycat/logs/wrapper.log #查看日志,是否运行成功
STATUS | wrapper | 2022/05/15 22:02:30 | --> Wrapper Started as Daemon
STATUS | wrapper | 2022/05/15 22:02:30 | Launching a JVM...
INFO | jvm 1 | 2022/05/15 22:02:31 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO | jvm 1 | 2022/05/15 22:02:31 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
INFO | jvm 1 | 2022/05/15 22:02:31 |
INFO | jvm 1 | 2022/05/15 22:02:32 | MyCAT Server startup successfully. see logs in logs/mycat.log
[root@mycat ~]#ss -tnl
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 [::]:22 [::]:*
LISTEN 0 50 *:43575 *:*
LISTEN 0 50 *:1984 *:*
LISTEN 0 128 *:8066 默认监听8066端口 *:*
LISTEN 0 50 *:43369 *:*
LISTEN 0 128 *:9066 *:*

3、在mycat 服务器上修改server.xml文件配置Mycat的连接信息

vim /apps/mycat/conf/server.xml 找到以下内容取消注释

                <!--
<property name="serverPort">8066</property>
<property name="managerPort">9066</property>
<property name="idleTimeout">300000</property>
<property name="authTimeout">15000</property>
<property name="bindIp">0.0.0.0</property>
<property name="dataNodeIdleCheckPeriod">300000</property> 5 * 60 * 1000L; //连接空闲检查
<property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> -->

修改serverport端口为3306

<property name="serverPort">3306</property>
<property name="managerPort">9066</property>
<property name="idleTimeout">300000</property>
<property name="authTimeout">15000</property>
<property name="bindIp">0.0.0.0</property>
<property name="dataNodeIdleCheckPeriod">300000</property>
<property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> 修改mycat用户root登录面膜为atech
<user name="root" defaultAccount="true">
<property name="password">atech</property>
<property name="schemas">TESTDB</property>
<property name="defaultSchema">TESTDB</property>
<!--No MyCAT Database selected 错误前会尝试使用该schema作为schema,不设置则为null,报错 -->

重启观察日志是否成功

[root@mycat ~]#mycat restart
Stopping Mycat-server...
Stopped Mycat-server.
Starting Mycat-server...

备份文件  schema.xml

[root@mycat ~]#cp /apps/mycat/conf/schema.xml{,.back}
[root@mycat ~]#vim /apps/mycat/conf/schema.xml
[root@mycat ~]#mycat restart    #重启程序,注意观察日志是否成功
Stopping Mycat-server...
Stopped Mycat-server.
Starting Mycat-server...

[root@mycat ~]#tail -f /apps/mycat/logs/wrapper.log
STATUS | wrapper | 2022/05/15 22:02:30 | --> Wrapper Started as Daemon
STATUS | wrapper | 2022/05/15 22:02:30 | Launching a JVM...
INFO | jvm 1 | 2022/05/15 22:02:31 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO | jvm 1 | 2022/05/15 22:02:31 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
INFO | jvm 1 | 2022/05/15 22:02:31 |
INFO | jvm 1 | 2022/05/15 22:02:32 | MyCAT Server startup successfully. see logs in logs/mycat.log
STATUS | wrapper | 2022/05/15 22:08:53 | TERM trapped. Shutting down.
STATUS | wrapper | 2022/05/15 22:08:54 | <-- Wrapper Stopped
STATUS | wrapper | 2022/05/15 22:08:55 | --> Wrapper Started as Daemon
STATUS | wrapper | 2022/05/15 22:08:55 | Launching a JVM...
INFO | jvm 1 | 2022/05/15 22:08:55 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO | jvm 1 | 2022/05/15 22:08:55 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
INFO | jvm 1 | 2022/05/15 22:08:55 |
INFO | jvm 1 | 2022/05/15 22:08:56 | MyCAT Server startup successfully. see logs in logs/mycat.log
STATUS | wrapper | 2022/05/15 22:11:37 | TERM trapped. Shutting down.
STATUS | wrapper | 2022/05/15 22:11:38 | <-- Wrapper Stopped
STATUS | wrapper | 2022/05/15 22:11:39 | --> Wrapper Started as Daemon
STATUS | wrapper | 2022/05/15 22:11:39 | Launching a JVM...
INFO | jvm 1 | 2022/05/15 22:11:40 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO | jvm 1 | 2022/05/15 22:11:40 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
INFO | jvm 1 | 2022/05/15 22:11:40 |
INFO | jvm 1 | 2022/05/15 22:11:41 | MyCAT Server startup successfully. see logs in logs/mycat.log

wrapper.log 日志观察

vim编辑   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="hellodb" />
<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.28:3306" user="root"
password="123456">
<readHost host="host2" url="10.0.0.38:3306" user="root"
password="123456" />
</writeHost>
</dataHost>
</mycat:schema>

4、在后端主服务器创建用户并对mycat授权

master

MariaDB [hellodb]> create database mycat;
Query OK, 1 row affected (0.000 sec)

MariaDB [hellodb]> create user root@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected (0.000 sec)

MariaDB [hellodb]> grant all on *.* to root@'10.0.0.%'
-> ;
Query OK, 0 rows affected (0.000 sec)

  从服务器同步检查

MariaDB [mysql]> select user,host from user;
+-------------+---------------------------+
| User | Host |
+-------------+---------------------------+
| root | 10.0.0.% |
| magedu | 192.168.1.0/255.255.255.0 |
| mariadb.sys | localhost |
| mysql | localhost |
| root | localhost |
+-------------+---------------------------+
5 rows in set (0.001 sec)

5、主从节点都开启通用日志

MariaDB [mysql]> show variables like 'general_log';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| general_log | OFF |
+---------------+-------+
1 row in set (0.001 sec) MariaDB [mysql]> set global general_log=on;
Query OK, 0 rows affected (0.000 sec) MariaDB [mysql]> show variables like 'general_log';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| general_log | ON |
+---------------+-------+
1 row in set (0.000 sec)

6客户端登录

MySQL [TESTDB]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 28 |
+-------------+
1 row in set (0.001 sec) MySQL [TESTDB]> select @@hostname;
+------------+
| @@hostname |
+------------+
| master.org |
+------------+
1 row in set (0.001 sec) MySQL [TESTDB]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.001 sec) MySQL [TESTDB]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 28 |
+-------------+
1 row in set (0.002 sec) MySQL [TESTDB]> select @@hostname;
+------------+
| @@hostname |
+------------+
| master.org |
+------------+
1 row in set (0.001 sec) MySQL [TESTDB]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.002 sec) MySQL [TESTDB]> select * from students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.002 sec)

Linux-Mycat实现MySQL的读写分离的更多相关文章

  1. MyCAT实现MySQL的读写分离

    在MySQL中间件出现之前,对于MySQL主从集群,如果要实现其读写分离,一般是在程序端实现,这样就带来一个问题,即数据库和程序的耦合度太高,如果我数据库的地址发生改变了,那么我程序端也要进行相应的修 ...

  2. 通过mycat实现mysql的读写分离

    mysql的主从配置沿用上一篇博客的配置:https://www.cnblogs.com/MasterSword/p/9434169.html mycat下载地址:http://www.mycat.i ...

  3. Mycat实现Mysql数据库读写分离

    Linux和Windows环境下搭建Mycat数据读写分离 前提需要:1.服务器装有JVM虚拟机,就是JDK.2.两个Mysql数据库已经实现主从复制,参考:https://www.cnblogs.c ...

  4. 利用mycat实现mysql数据库读写分离

    1.这里是在mysql主从复制实现的基础上,利用mycat做读写分离,架构图如下: 2.Demo 2.1 在mysql master上创建数据库创建db1 2.2 在数据库db1创建表student ...

  5. Mycat实现Mysql主从读写分离

    一.概述 关于Mycat的原理网上有很多,这里不再详述,对于我来说Mycat的功能主要有如下几种: 1.Mysql主从的读写分离 2.Mysql大表分片 3.其他数据库例如Oracle,MSSQL,D ...

  6. Mycat实现mysql主从复制(读写分离)

    数据库性能瓶颈主要原因: 随着用户数的增多,带来的是数据库连接的大幅度增长 随着业务体量的增长,表数据量(空间存储的问题)的大幅增长,其中涉及到索引的优化,mysql默认的索引是硬盘级别的,BTREE ...

  7. mycat配置MySQL主从读写分离

    1.安装java 1.8 mycat 1.6要求的Java需要Java 1.8或1.8以上,安装Java参考以下链接: https://blog.csdn.net/weixin_43893397/ar ...

  8. Mycat - 实现数据库的读写分离与高可用

    前言 开心一刻 上语文课,不小心睡着了,坐在边上的同桌突然叫醒了我,并小声说道:“读课文第三段”.我立马起身大声读了起来.正在黑板写字的老师吓了一跳,老师郁闷的看着我,问道:“同学有什么问题吗?”,我 ...

  9. MyCAT部署及实现读写分离(转)

    MyCAT是mysql中间件,前身是阿里大名鼎鼎的Cobar,Cobar在开源了一段时间后,不了了之.于是MyCAT扛起了这面大旗,在大数据时代,其重要性愈发彰显.这篇文章主要是MyCAT的入门部署. ...

  10. linux下mysql基于mycat做主从复制和读写分离之基础篇

    Linux下mysql基于mycat实现主从复制和读写分离1.基础设施 两台虚拟机:172.20.79.232(主) 172.20.79.233(从) 1.1软件设施 mysql5.6.39 , my ...

随机推荐

  1. 学习Tomcat(一)

    一.jdk和tomcat基础 1.web应用对比 apache: 两种方式运行php,一是用模块,二是用fastcgi nginx: 通过fastcgi处理动态请求,也可转发到tomcat 2.tom ...

  2. 决策树3:基尼指数--Gini index(CART)

    既能做分类,又能做回归.分类:基尼值作为节点分类依据.回归:最小方差作为节点的依据. 节点越不纯,基尼值越大,熵值越大 pi表示在信息熵部分中有介绍,如下图中介绍 方差越小越好. 选择最小的那个0.3 ...

  3. .NET面试题整理

    .NET..NET Framework..NET Core和C#的解释各是什么? ASP.NET MVC和ASP.NET Web API的区别是什么? C#中的委托是什么?事件是不是一种委托? 简述P ...

  4. 什么是RESTFUL?REST的请求方法有哪些,有什么区别?

    这里是修真院前端小课堂,每篇分享文从 [背景介绍][知识剖析][常见问题][解决方案][编码实战][扩展思考][更多讨论][参考文献] 八个方面深度解析前端知识/技能,本篇分享的是: [什么是REST ...

  5. ES6-11学习笔记--Reflect

    Reflect 映射 将Object属于语言内部的方法放到Reflect上 修改某些Object方法的返回结果,让其变得更合理 让Object操作编程函数行为 Reflect对象的方法与Proxy对象 ...

  6. EF框架基础

    ORM概述: ORM全称是"对象 - 关系映射" . ORM是将关系数据库中的数据用对象的形式表现出来,并通过面向对象的方式将这些对象组织起来,实现系统业务逻辑的过程. Entit ...

  7. for循环打印九九乘法表

    学习目标: 熟练掌握 for 循环的使用 例题: 需求:打印九九乘法表 代码如下: // 九九乘法表 // row 为行,col为列 for(int row = 1; row < 10; row ...

  8. 控制反转 IOC 理论推导

    控制反转 IOC 理论推导 按照我们传统的开发,我们会先去 dao 层创建一个接口,在接口中定义方法. public interface UserDao { void getUser(); } 然后再 ...

  9. Java学习day36

    元注解:负责注解其他注解,Java定义了4个标准的meta-annotation类型,用来提供对其它annotation类型作说明 1.@Target:用于描述注解的使用范围(即被描述的注解可以用在什 ...

  10. ES 文档与索引介绍

    在之前的文章中,介绍了 ES 整体的架构和内容,这篇主要针对 ES 最小的存储单位 - 文档以及由文档组成的索引进行详细介绍. 会涉及到如下的内容: 文档的 CURD 操作. Dynamic Mapp ...