mysql主从同步springboot_Mysql8.0主从复制搭建,shardingsphere+springboot+mybatis读写分离...

本文详细介绍了在Linux环境下安装MySQL 8.0的步骤,包括解决权限和文件问题,以及配置主从同步。通过创建用户、设置权限和修改配置,成功实现主从复制。接着,利用SpringBoot、MyBatis和ShardingSphere搭建了读写分离的示例,展示了如何配置数据源和检查读写分离效果。
摘要由CSDN通过智能技术生成

1、安装mysql8.0

首先需要在192.167.3.171上安装JDK。

下载mysql安装包,https://dev.mysql.com/downloads/,找到以下页面下载。

7b485f92a676a8738399e6a3404cb46a.png

14822207120e35f959fb8aa4d7281e4e.png

下载后放到linux系统中

官网说需要先查看本机是否已安装mysql,删除mysql这里不介绍。

yum install libaio  yum install openssl,安装mysql所需的软件包:libaiohe openssl。

tar -xvf mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz,解压。

mv mysql-8.0.13-linux-glibc2.12-x86_64 /usr/local/mysql,将解压文件移动到local下。

groupadd mysql

useradd -r -g mysql -s /bin/false mysql

cd /usr/local/mysql

mkdir mysql-files

chown mysql:mysql mysql-files

chmod 750 mysql-files

bin/mysqld --initialize --user=mysql,初始化数据库,注意此处随机生成的密码,第一次登陆mysql的时候要使用。

c7ae206f1549f4d232487e7b3424962d.png

bin/mysql_ssl_rsa_setup,安装ssl。

cp support-files/mysql.server /etc/init.d/mysql.server    将服务文件复制到开机启动目录,实现服务开机自启动。

bin/mysqld_safe --user=mysql &     开启服务,&是后台运行的意思,执行命令之后,终端会卡在一个位置,再按一下Enter即可。

如果上面命令报错,什么log,pid文件未找到之类的,就需要执行下面方法。由于mysql服务启动时,会去读取/etc/my.cnf文件中的配置内容,我们打开文件来看,文件内容如下(我已修改):

3398fde466d2aef9ef7bedac56f9937f.png

注意,[mysqld]或[mysqld_safe]下配置了socket路径时,会以此为准,而本地登陆mysql的时候,如果不用 -S指定mysql.sock文件位置,会报错,不能找到/tmp/mysql.sock。所以我们只在[client]里面配置。

eddf59f69be1278a0a281319777a9c90.png

原来的内容地址就是启动mysql服务时报错的路径,我们可以知道,文件不存在问题导致启动失败。这时,就需要新建文件,并设置文件权限了。我这里修改了红框内的路径,具体命令如下:

cd /usr/local/mysql/mysql-files

mkdir log ;  mkdir pid

touch log/mysql.log ; touch pid/mysql.pid

cd /usr/local/mysql

chown -R mysql:mysql mysql-files

chmod -R 750 mysql-files

然后再次执行:bin/mysqld_safe --user=mysql &

bin/mysql -uroot -p   登陆mysql,回车后粘贴之前初始密码。

ALTER USER 'root'@'localhost' IDENTIFIED BY 'ibethfy;

flush privileges

执行完成后,我又想使用navicat登陆,那好,试试呗,navicat连接直接报找不到服务,2003 cannot。。。。。,行嘛,排除原因,总结如下:

1、建立供外部连接的mysql用户。

mysql -uroot -pibethfy

create user 'ibethfy'@'%' identified by 'ibethfy';

grant all on *.* to 'ibethfy'@'%';     注意,mysql8的grant语句和之前版本有差别。

flush privileges;

2、防火墙关了,我用的centos。

firewall-cmd --state          查看防火墙状态,发现时running;

systemctl stop firewalld.service     关闭防火墙

firewall-cmd --state         再次查看,发现not running;

然后再用navicat连接,好嘛,又报错,caching-sha2-password,看来一下,应该是mysql的加密策略变了,navicat版本没跟上呗,那行,执行下面命令,然后再连接,没问题了!

mysql -uroot -pibethfy

alter user 'ibethfy'@'%' identified by 'ibethfy' password expire never;

alter user 'ibethfy'@'%' identified with mysql_native_password by 'ibethfy';

flush privileges;

ps -ef |grep mysql,可以看到mysql服务有两个,mysqld_safe和mysqld。说明启动成功了。

大家在linux装mysql8.0的时候,如果按照步骤来,还出现问题,就分析一下,主要导致的一些原因就是权限问题和文件问题,依次解决一下再试试。

2、mysql主从安装

分别按以上方发安装两个mysql服务,分别为192.167.3.171(主),192.167.3.172(从)。

配置主服务

my.conf 文件修改。添加log-bin与server-id,具体配置和上面一致。

重启mysql,service mysql restart;也可用mysqladmin -uroot -pibethfy shutdown;mysqld_safe --user=mysql &;如果没有找到服务,直接用ps -ef|grep mysql,找到对应进程,kill -9强行终止后(不建议),使用mysqld_safe --user=mysql &  重启。

登陆mysql,赋予外部连接的ibethfy用户权限并刷新。grant replication slave on *.* to 'ibethfy'@'%';         flush privileges;

查看主服务信息,从服务配置时需要用到,show master status;

2837a2e3ac7a9cca11064c9c22add2cb.png

修改从mysql服务配置

修改my.cnf,vi /etc/my.cnf

66e10ed55771729b2c8c1935d391b9a6.png

配置从服务,先登陆后,执行  CHANGE MASTER TO MASTER_HOST='192.167.3.171', MASTER_USER='ibethfy', MASTER_PASSWORD=ibethfy',MASTER_LOG_FILE='binlog.000010',  MASTER_LOG_POS=1179;

start slave;

show slave status\G;查看从服务状态,如果内容中有Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the –replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it);错误,需要执行下面语句:

show variables like 'server_id';可以看见server_id=1,这里与主服务id相同,则执行,set global server_id=2;与my.cnf内容相同即可。

重新启动start slave;

如果得到以下信息,则提示主从复制配置成功。

如果启动slave报错,什么文件未删除之类的,可以执行reset slave,然后重新执行上面步骤。

175093818dfffedc455aae83b32271b9.png

现在可以测试了,在主服务建一个database,从服务可以看见,代表配置成功。

3、springboot+mybatis+shardingsphere搭建主从分离

使用idea创建springboot工程

pom.xml依赖jar配置,具体配置如下

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.2.RELEASE

com.example

demo

0.0.1-SNAPSHOT

SharingJDBCDemo

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

2.0.0

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-starter-test

test

io.shardingsphere

sharding-jdbc-spring-boot-starter

3.0.0

org.mybatis.generator

mybatis-generator-core

1.3.5

org.springframework.boot

spring-boot-maven-plugin

org.mybatis.generator

mybatis-generator-maven-plugin

1.3.5

src/main/resources/generateMybatis.xml

true

true

application.yml配置,具体如下

server:

port: 8888

spring:

application:

name: SharingJdbc

mybatis:

type-aliases-package: com.example.demo.entity

mapper-locations: classpath:mapper/*.xml

sharding:

jdbc:

datasource:

names: master1,slave1

master1:

type: com.zaxxer.hikari.HikariDataSource

driver-class-name: com.mysql.cj.jdbc.Driver

jdbc-url: jdbc:mysql://192.167.3.171:3306/ibethfy?useSSL=false&useUnicode=true&characterEncoding=utf8&autoReconnect=true

username: ibethfy

password: ibethfy

maxPoolSize: 20

slave1:

type: com.zaxxer.hikari.HikariDataSource

driver-class-name: com.mysql.cj.jdbc.Driver

jdbc-url: jdbc:mysql://192.167.3.172:3306/ibethfy?useSSL=false&useUnicode=true&characterEncoding=utf8&autoReconnect=true

username: ibethfy

password: ibethfy

maxPoolSize: 20

config:

masterslave:

load-balance-algorithm-type: round_robin

name: db_m1_s1

master-data-source-name: master1

slave-data-source-names: slave1

sharding:

props:

sql:

show: true

mybatis自动生成器配置

/p>

PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"

"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

项目demo结构图,比较简单,只实现功能

c87f694ca1141b3fa0ece1188432fefd.png

自动生成器执行方法,打开maven project,点击运行,具体参考下图

351d87f36f3654ec8a6e75566aa3282b.png

SharingJdbcDemoApplication,配置mapper扫描路径,也可直接在mapper类上增加@Mapper.

packagecom.example.demo;importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@MapperScan("com.example.demo.mapper")public classSharingJdbcDemoApplication {public static voidmain(String[] args) {

SpringApplication.run(SharingJdbcDemoApplication.class, args);

}

}

DemoController类

packagecom.example.demo;importcom.example.demo.entity.People;importcom.example.demo.mapper.PeopleMapper;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importjava.util.List;

@RestControllerpublic classDemoController {

@Autowired

PeopleMapper peopleMapper;

@RequestMapping("/{id}")public People getPeople(@PathVariable(value = "id") intid)

{returnpeopleMapper.selectByPrimaryKey(id);

}

@RequestMapping("/insert/{name}")public void insert(@PathVariable(value = "name") String name)

{

People p= newPeople();

p.setName(name);

peopleMapper.insert(p);

}

}

启动工程后,即可测试读写分离。关于如何查看读写分离效果,可以开启mysql的查询日志,开启方法如下

登陆mysql,执行语句查询日志记录开启情况:show variables like "%general%";

+------------------+------------------------------+

| Variable_name    | Value                        |

+------------------+------------------------------+

| general_log      | OFF                          |

| general_log_file | /var/lib/mysql/localhost.log |

+------------------+------------------------------+

set global general_log = "ON";开启日志记录,可以在/var/lib/mysql中查看日志。

测试:访问http://localhost:8888/1,查询数据,在日志中,可以看到,172从服务日志记录查询语句,171没有日志。

测试:访问http://localhost:8888/insert/ibethfy,插入语句,可以看到171主服务有日志,172从服务没有日志。

注意:io.shardingsphere用3.0.0版本即可,3.1.0引入maven会报关联错误。

之前想使用mycat实现读写分离等,结果发现mycat只支持mysql5版本,其余版本未在其支持列表,并且mycat很久没更新啦!我自己试了很久都没搭建好mycat的环境,哎!

好啦,基本的都搞完了,之后实施shardingsphere的分库分表!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值