Springboot基于Mycat的读写分离

Mysql

安装

环境准备

# 1.卸载mariadb,否则安装mysql会出现冲突
# 2.执行命令rpm -qa | grep mariadb
# 3.列出所有被安装的mariadb rpm 包;
# 4.执行命令rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64

本地安装

  • 将下载好的软件包传到linux系统中并解压
tar -zxvf 软件包 -C 安装目录
  • 按照顺序执行以下命令
rpm -ivh perl-*
rpm -ivh net-tools-2.0-0.22.20131004git.el7.x86_64.rpm
rpm -ivh mysql-community-common-5.6.42-2.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.6.42-2.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.6.42-2.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.6.42-2.el7.x86_64.rpm
  • 设置root用户密码
# 1.启动mysql数据库
	[root@localhost mysql]# systemctl start mysqld

# 2.修改mysql数据库密码
	mysqladmin -u root -p password 回车 输入原始密码 在输入新的密码
	
	注意:5.7之前版本安装完成之后没有密码,mysql5.7之后的版本的初始密码是随机生成的,放在了 /var/log/mysqld.log
			使用命令 grep ‘temporary password’ /var/log/mysqld.log 读出来即可
# 3.登录mysql
	[root@localhost mysql]# mysql -u root -p

# 4.在/etc/my.cnf配置文件中添加以下命令可以不用输入密码
	skip-grant-tables

Mysql主从复制

准备环境

三台机器 (一主两从)

10.15.0.10 mysql1
10.15.0.11 mysql2
10.15.0.12 mysql3

前提:

  • jdk正常运行

  • 修改主机与ip的映射

  • 修改主机名后需要重启

  • 修改三台机器mysql的配置文件
[root@mysql1 ~]# vim /etc/my.cnf

# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables
#加入如下配置
server-id=1
log-bin=mysql-bin
log-slave-updates
slave-skip-errors=all

[root@mysql2 ~]# vim /etc/my.cnf
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables
#加入如下配置
server-id=2
log-bin=mysql-bin
log-slave-updates
slave-skip-errors=all

[root@mysql3 ~]# vim /etc/my.cnf

# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables
#加入如下配置
server-id=3
log-bin=mysql-bin
log-slave-updates
slave-skip-errors=all

注意:server-id 必须是唯一的

  • 重启mysql服务
[root@mysql1 ~]# systemctl restart mysqld
[root@mysql2 ~]# systemctl restart mysqld
[root@mysql3 ~]# systemctl restart mysqld
  • 分别登陆mysql检测配置是否生效
[root@mysql1 ~]# mysql -u root
mysql> SHOW VARIABLES like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1     |
+---------------+-------+
1 row in set (0.00 sec)
[root@mysql2 ~]# mysql -u root
mysql> SHOW VARIABLES like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 2     |
+---------------+-------+
1 row in set (0.00 sec)
[root@mysql2 ~]# mysql -u root
mysql> SHOW VARIABLES like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 3     |
+---------------+-------+
1 row in set (0.00 sec)
  • 登陆master节点执行如下命令
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000008 |      727 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
  • 分别登陆mysql2,mysql3从节点执行如下命令
change master to 
master_host='10.15.0.10',
master_user='root',
master_log_file='mysql-bin.000008',
master_log_pos=120;
mysql> change master to
    -> master_host='10.15.0.10',
    -> master_user='root',
    -> master_log_file='mysql-bin.000008',
    -> master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
  • 开启mysql2,mysql3的从节点
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
  • 查看从节点状态
mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.15.0.10
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000008
          Read_Master_Log_Pos: 727
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 890
        Relay_Master_Log_File: mysql-bin.000008
            ` Slave_IO_Running: Yes
            ` Slave_SQL_Running: Yes

注意: 出现Slave_IO_Running: YesSlave_SQL_Running: Yes说明成功

  • 关闭主从复制节点(在从节点执行)
stop slave;

注意: 如果出现Slave I/O: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. Error_code: 1593错误

请执行如下命令:rm -rf /var/lib/mysql/auto.cnf删除这个文件,之所以出现会出现这样的问题,是因为我的从库主机是克隆的主库所在的主机,所以auto.cnf文件中保存的UUID会出现重复

  • 测试
    Springboot基于Mycat的读写分离_第1张图片

读写分离

安装Mycat

  • 下载mycat
http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
  • 解压mycat
tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C 指定安装目录
  • 配置mycat中conf下的配置schema.xml(将原先的配置删除)
[root@mycat mycat]# vim /usr/soft/mycat/conf/schema.xml
<mycat:schema xmlns:mycat="http://io.mycat/">
<!-- 定义MyCat的逻辑库 名字可以随意   -->
   <schema name="yt" checkSQLschema="false" sqlMaxLimit="100" dataNode="testNode"></schema>
    <!-- 定义MyCat的数据节点 database是真实库的名字-->
    <dataNode name="testNode" dataHost="dtHost" database="project" />
   <dataHost name="dtHost" maxCon="1000" minCon="10" balance="1"
                writeType="0" dbType="mysql" dbDriver="native" switchType="-1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <!--写节点-->
                <writeHost host="hostM1" url="10.15.0.10:3306" user="root" password="root" >
                        <!--从节点-->
                        <readHost host="hostS1" url="10.15.0.11:3306" user="root" password="root"/>
                        <readHost host="hostS1" url="10.15.0.12:3306" user="root" password="root"/>
                </writeHost>
   </dataHost>

</mycat:schema>
  • 配置登陆mycat的权限server.xml
[root@mycat mycat]# vim /usr/soft/mycat/conf/server.xml
<mycat:server xmlns:mycat="http://io.mycat/">
        <system>
            <!-- 这里配置的都是一些系统属性,可以自己查看mycat文-->
            <property name="defaultSqlParser">druidparser</property>
            <property name="charset">utf8</property>
        </system>

        <user name="root">
            <property name="password">root</property>
            <!-- 写schema.xml中配置的逻辑库表名字 -->
            <property name="schemas">yt</property>
        </user>
</mycat:server>
  • 修改超时时间 conf/wrapper.conf配置
wrapper.startup.timeout=7200   #2小时

注意: 5秒未启动成功则报错,所有建议修改以上配置

  • 启动mycat
mycat console
  • 查看日志
tail -f ../logs/mycat.log

集成Springboot

配置文件

  • application.yaml
#服务基本配置
server:
  port: 8888
  servlet:
    context-path: /
#数据源
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://10.15.0.13:8066/yt
    username: root
    password: root
  • 测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = UserModelApplication.class)
public class TestUser {

@Autowired
private UserService userService;


@Test
//分页查询
public void queryUserByPageTests(){
    for(int i=0;i<3;i++){
        List<User> userList = userService.queryUserByPage(1, 2, "name","z");
        assertFalse(userList.isEmpty());
    }
}   

为了方便测试 建议把数据库中两个slave丛节点的数据进行修改将两个节点的name分别改为zs-slave1 zs-slave2

  • 测试结果
DEBUG org.springframework.jdbc.datasource.DataSourceUtils#doGetConnection 2020-03-19 16:02:14 Fetching JDBC Connection from DataSource
DEBUG com.baizhi.dao.UserDAO.queryUserByPage#debug 2020-03-19 16:02:14 ==>  Preparing: select id,name,password,sex,photo,birthDay,email from t_user WHERE name like concat(concat('%',?),'%') limit 0,? 
DEBUG com.baizhi.dao.UserDAO.queryUserByPage#debug 2020-03-19 16:02:14 ==> Parameters: z(String), 2(Integer)
TRACE com.baizhi.dao.UserDAO.queryUserByPage#trace 2020-03-19 16:02:14 <==    Columns: id, name, password, sex, photo, birthDay, email
TRACE com.baizhi.dao.UserDAO.queryUserByPage#trace 2020-03-19 16:02:14 <==        Row: 1, zs-slave1, 1234, 0, null, 2020-03-11, yt@qq.com
DEBUG com.baizhi.dao.UserDAO.queryUserByPage#debug 2020-03-19 16:02:14 <==      Total: 1
DEBUG org.springframework.jdbc.datasource.DataSourceUtils#doGetConnection 2020-03-19 16:02:14 Fetching JDBC Connection from DataSource
DEBUG com.baizhi.dao.UserDAO.queryUserByPage#debug 2020-03-19 16:02:14 ==>  Preparing: select id,name,password,sex,photo,birthDay,email from t_user WHERE name like concat(concat('%',?),'%') limit 0,? 
DEBUG com.baizhi.dao.UserDAO.queryUserByPage#debug 2020-03-19 16:02:14 ==> Parameters: z(String), 2(Integer)
TRACE com.baizhi.dao.UserDAO.queryUserByPage#trace 2020-03-19 16:02:14 <==    Columns: id, name, password, sex, photo, birthDay, email
TRACE com.baizhi.dao.UserDAO.queryUserByPage#trace 2020-03-19 16:02:14 <==        Row: 1, zs-slave2, 1234, 0, null, 2020-03-11, yt@qq.com
DEBUG com.baizhi.dao.UserDAO.queryUserByPage#debug 2020-03-19 16:02:14 <==      Total: 1

你可能感兴趣的:(Springboot基于Mycat的读写分离)