ShadingJDBC之mysql主从复制
Posted Timous
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ShadingJDBC之mysql主从复制相关的知识,希望对你有一定的参考价值。
ShardingJDBC
yum安装mysql
下载mysql的rpm地址
配置mysql扩展源
rpm -ivh http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm
yum安装mysql
yum install mysql-community-server -y
启动mysql设置开机自启动
systemctl start mysqld
systemctl status mysqld
systemctl stop mysqld
systemctl enable mysqld
使用mysql初始密码进行登录
gerp "password" /var/log/mysqld.log
mysql -uroot -p出来的密码
或者是以下的做法
mysql -uroot -p$(awk '/temporary password/{print $NF}' /var/log/mysqld.log)
修改数据库的密码
数据库的默认的规则是必须携带大小写字母、特殊符号,字符串长度大于8,否则会报错
因此设定较为简单的密码是首先要修改set gloable validate_password_policy
和length
set global validate_password_policy=0;
set global validate_password_length=1;
修改密码
set password for root@localhost = password('123456');
# 或者
alter user 'root'@'localhost' identified by '123456';
# 给用户授权
grant all on *.* to root@'%' identified by '123456';
# 刷新
flush privileges;
登录测试
通过测试,能够进行登录
主从复制的配置
Master节点的配置
vim /etc/my.cnf
[mysqld]
## 同一局域网内,注意要唯一
server-id=100
### 开启二进制日志 可以随便取(关键)
log-bin=mysql-bin
### 复制过滤: 不需要备份的数据库不输出(mysql库一般不同步)
binlog-ignore-db=mysql
### 为每个session分配内存,在事务过程中用来存储二进制日志的缓存
binlog_cache_size=1M
### 主从复制的格式(mixed , statement , row 默认格式是statement)
binlog_format=mixed
2.2slave节点的配置
vim /etc/my.cnf
[mysqld]
## 同一局域网内,注意要唯一
server-id=102
### 开启二进制日志功能 , 已备份slave作为其他slave的Master时使用
log-bin=mysql-slave-bin
### relay_log配置中继日志
relay_log=edu-mysql-relay-bin
### 复制过滤: 不需要备份的数据库不输出(mysql库一般不同步)
binlog-ignore-db=mysql
### 如果需要同步函数或者是存储过程
log_bin_trust_function_creators=true
### 为每个session分配内存,在事务过程中用来存储二进制日志的缓存
binlog_cache_size=1M
### 主从复制的格式(mixed , statement , row 默认格式是statement)
binlog_format=mixed
[mysqld]
## 同一局域网内,注意要唯一
server-id=101
### 开启二进制日志功能 , 已备份slave作为其他slave的Master时使用
log-bin=mysql-slave-bin
### relay_log配置中继日志
relay_log=edu-mysql-relay-bin
### 复制过滤: 不需要备份的数据库不输出(mysql库一般不同步)
binlog-ignore-db=mysql
### 如果需要同步函数或者是存储过程
log_bin_trust_function_creators=true
### 为每个session分配内存,在事务过程中用来存储二进制日志的缓存
binlog_cache_size=1M
### 主从复制的格式(mixed , statement , row 默认格式是statement)
binlog_format=mixed
在master服务器上进行授权
## 登录数据库
mysql -uroot -p123456
## 进行授权
grant replication slave , replication client on *.* to 'root'@'47.105.129.77' identified by '123456';
## 刷新权限
flush privileges;
##查看mysql具有哪些用户及对应的IP权限(可以不执行,只是一个查看)
select user , host from mysql.user;
查看对应的binlog文件名和位置(master节点执行)
show master status;
配置从服务器的连接
## 连接对应的主数据库
change master to master_host='81.69.232.6' , master_user='root' , master_password='123456' , master_port=3306, master_log_file='mysql-bin.000001' , master_log_pos=1247;
## 启动
经过测试,主从复制就会完成了
读写分离的springboot实现
pom.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.timous</groupId>
<artifactId>shadingjdbctest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shadingjdbctest</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<shading-sphere.version>4.0.0-RC1</shading-sphere.version>
</properties>
<dependencies>
<!--web的基本依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis的jar包-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--lombok简化实体类的包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--测试的包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--shardingjdbc 的starter包-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>${shading-sphere.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-core-common</artifactId>
<version>${shading-sphere.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>druid</artifactId>-->
<!-- <version>1.2.3</version>-->
<!-- </dependency>-->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>top.timous.ShadingjdbctestApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
yml文件配置
server:
port: 8080
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
props:
sql:
show: true
#配置数据源
datasource:
# ds1 一个主的mysql ds2 和 ds3都是slave从库
names: ds1,ds2,ds3
# 配置数据源的具体的信息
ds1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://81.69.232.6:3306/ksd_order_db?useUnicide=true&characterEncoding=utf-8&tinyIntlisBit=false&useSSL=false&serverTimeZone=GMT%2b8
username: root
password: 123456
maxPoolSize: 100
minPoolSize: 5
ds2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://121.4.161.181:3306/ksd_order_db?useUnicide=true&characterEncoding=utf-8&tinyIntlisBit=false&useSSL=false&serverTimeZone=GMT%2b8
username: root
password: 123456
maxPoolSize: 100
minPoolSize: 5
ds3:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://47.105.129.77:3306/ksd_order_db?useUnicide=true&characterEncoding=utf-8&tinyIntlisBit=false&useSSL=false&serverTimezone=GMT%2b8
username: root
password: 123456
maxPoolSize: 100
minPoolSize: 5
# 不配置下面的话写入的时候会出现问题 , 会报错 下面才是读写分离的
sharding:
default-data-source-name: ds1
# 之后下面配置的就是我们的策略等规则的东西 可以不用配置
masterslave:
name: ms
master-data-source-name: ds1
slave-data-source-names: ds2,ds3
load-balance-algorithm-type: round_robin
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: top.timous.entity
代码的基本结构
controller层
package top.timous.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.timous.entity.User;
import top.timous.mapper.UserMapper;
import java.util.Date;
import java.util.List;
/**
* @author Timous
* @create 2021-03-23 18:33
**/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/save")
public String insert(){
User user = new User();
user.setAge(12);
user.setBirthday(new Date());
user.setNickname("timous1");
user.setPassword("123456");
userMapper.addUser(user);
return "success";
}
@GetMapping("/listuser")
public List<User> listUser(){
return userMapper.findUser();
}
}
mapper层
package top.timous.mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
import top.timous.entity.User;
import java.util.List;
/**
* @author Timous
* @create 2021-03-23 18:29
**/
@Mapper
public interface UserMapper {
/**
* 插入用户
*/
@Insert("insert into ksd_user(nickname , password , age , birthday) values (#{nickname} , " +
"#{password} , #{age} ,#{birthday})")
void addUser(User user);
/**
* 查找所有的用户
*/
@Select("select * from ksd_user")
List<User> findUser();
}
mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.timous.mapper.UserMapper">
</mapper>
entity层
package top.timous.entity;
import lombok.Data;
import java.util.Date;
/**
* @author Timous
* @create 2021-03-23 18:27
**/
@Data
public class User {
//id
private Integer id;
//昵称
private String nickname;
//密码
private String password;
//年龄
private Integer age;
//出生日期
private Date birthday;
}
数据库的表
测试的结果
在读的时候是ds2和ds3轮询的规则 , 在写的时候是写入ds1的。
分库分表
为什么分库分表
单个的mysql在超过2k的时候就会出现问题 , 最好是并发量控制在1k左右。
目的: 解决高并发,和数据量大的问题
水平拆分和垂直拆分
水平拆分: 同一个拆分为多个数据表
垂直拆分: 一般是针对各个服务进行拆分,也就是业务的模块
表的垂直拆分: 字段的使用频率不一,可以拆分为两个表
以上是关于ShadingJDBC之mysql主从复制的主要内容,如果未能解决你的问题,请参考以下文章