10.211.55.4:主库master
10.211.55.5:从库slave
10.211.55.6:从库slave
service mysqld start|stop|restart
systemctl start|stop|restart mysqld
log-bin=mysql-bin # 非必需
server-id=1 # 必需
1.4.3.1、启动MySQL
1.4.3.2、登录MySQL客户端
1.4.3.3、依次执行命令
CREATE USER 'repl’@’10.211.55.%’ IDENTIFIED BY ‘Aa123123.’;
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.211.55.%';
FLUSH PRIVILEGES;
show variables like ‘log_bin%’;
show master status;
Ps:这里需要记住File和Position字段的值,在从库的设置中会用到
server-id=2
1.5.3.1、启动MySQL、登录MySQL
1.5.3.2、执行命令
change master to master_host='10.211.55.4', master_user='repl', master_password=‘Aa123123.’, master_log_file='mysql-bin.000001', master_log_pos=767;
Ps:由于两个从库的Linux服务器是从主库Linux服务器克隆来的,所以会导致主库和2个从库的MySQL数据库的UUID相同,这时我们需要修改2个从库的UUID否则从库slave_io_runnint值一致为no;只要保证3个数据库UUID值不一样就行
vi /var/lib/mysql/auto.cnf
1.5.5.1、重启mysql,再次登录MySQL
1.5.5.2、查看从库状态
show slave status\G
登录MySQL客户端
create user ‘pig’@‘%’ identified by ‘Aa123123.’;
grant select on test.* to ‘pig’@‘%’
flush privileges;
当我们配置好主从同步后,如果重启服务器或者其他原因导致服务器宕机,则数据将无法同步,以下两种情况最常见
负责自己的slave mysql进程,用于update数据
解决办法如下,进入mysql客户端,依次执行命令:
stop slave;
SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE;
start slave;
show slave status\G
至此,问题解决
负责与主机(master)的io通信
由于两台从服务器(slave)是通过主服务器(master)克隆来的,所以三台机器安装MySQL的UUIDs是相同的,我们需要修改两台从服务器(slave)的UUIDs的值,让3台服务器的UUID值都不相同
解决办法:修改从库UUIDs
vi /var/lib/mysql/auto.cnf
解决办法如下,进入mysql客户端:
2.2.2.1、查看主服务器(master)
show master status\G
2.2.2.2、查看从服务器(slave)
show slave status\G
2.2.2.3、找到问题:发现Master_Log_File没有对应
2.2.2.4、解决问题:在出现Slave_IO_Running:No的从服务器上操作
slave stop;
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000026', MASTER_LOG_POS=0;
slave start;
show slave status\G
至此,问题解决
参考:SpringBoot第 1 讲:HelloWorld_秦毅翔的专栏-CSDN博客
4.0.0
org.personal.qin.demos
mysql_master_slave
0.0.1-SNAPSHOT
jar
mysql_master_slave
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-aop
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
org.apache.commons
commons-lang3
org.springframework.boot
spring-boot-starter-test
test
mysql
mysql-connector-java
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
org.mybatis
mybatis-spring
1.3.1
org.mybatis
mybatis
3.5.0
com.jolbox
bonecp-spring
0.8.0.RELEASE
org.springframework.boot
spring-boot-maven-plugin
这里,我们配置了4个数据源,1个master,2两个slave,1个路由数据源。前3个数据源都是为了生成第4个数据源,而且后续我们只用这最后一个路由数据源。
application.yum
spring:
datasource:
master:
jdbc-url: jdbc:mysql://10.211.55.4:3306/test_datasource
username: root
password: Aa123123.
driver-class-name: com.mysql.jdbc.Driver
slave1:
jdbc-url: jdbc:mysql://10.211.55.5:3306/test_datasource
username: pig # 只读账户
password: Aa123123.
driver-class-name: com.mysql.jdbc.Driver
slave2:
jdbc-url: jdbc:mysql://10.211.55.6:3306/test_datasource
username: pig # 只读账户
password: Aa123123.
driver-class-name: com.mysql.jdbc.Driver
package demo.datasource.config;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import demo.datasource.config.bean.MyRoutingDataSource;
import demo.datasource.config.enums.DBTypeEnum;
/**
* 多数据源配置
*/
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource.master")
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.slave1")
public DataSource slave1DataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.slave2")
public DataSource slave2DataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public DataSource myRoutingDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
@Qualifier("slave1DataSource") DataSource slave1DataSource,
@Qualifier("slave2DataSource") DataSource slave2DataSource) {
Map
由于Spring容器中现在有4个数据源,所以我们需要为事务管理器和MyBatis手动指定一个明确的数据源。
package demo.datasource.config;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* MyBatis配置
*/
@EnableTransactionManagement
@Configuration
public class MyBatisConfig {
@Resource(name = "myRoutingDataSource")
private DataSource myRoutingDataSource;
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// 设置数据源
sqlSessionFactoryBean.setDataSource(myRoutingDataSource);
// 添加一行如下代码,解决setTypeAliasesPackage别名设置不起作用问题
sqlSessionFactoryBean.setVfs(SpringBootVFS.class);
//设置mybatis的主配置文件
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
org.springframework.core.io.Resource mybatisConfigXml = resourceResolver.getResource(Cast.MYBATIS_CONFIG);
sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);
// 设置别名包
sqlSessionFactoryBean.setTypeAliasesPackage(Cast.PO_PACKAGE);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(Cast.MAPPER_SCANNER));
return sqlSessionFactoryBean.getObject();
}
@Bean
public PlatformTransactionManager platformTransactionManager() {
return new DataSourceTransactionManager(myRoutingDataSource);
}
}
关于MyBatis的详细设置请参考:SpringBoot第 7 讲:SpringBoot+MyBatis_秦毅翔的专栏-CSDN博客
目标数据源就是那前3个这个我们是知道的,但是使用的时候是如果查找数据源的呢?
首先,我们定义一个枚举来代表这三个数据源
package com.cjs.example.enums;
public enum DBTypeEnum {
MASTER, SLAVE1, SLAVE2;
}
接下来,通过ThreadLocal将数据源设置到每个线程的上下文中
package demo.datasource.config.bean;
import java.util.concurrent.atomic.AtomicInteger;
import demo.datasource.config.enums.DBTypeEnum;
import demo.datasource.utils.Log;
/**
* 通过ThreadLocal将数据源设置到每个线程上下文中
*/
public class DBContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal<>();
private static final AtomicInteger counter = new AtomicInteger(-1);
public static void set(DBTypeEnum dbType) {
contextHolder.set(dbType);
}
public static DBTypeEnum get() {
return contextHolder.get();
}
public static void master() {
set(DBTypeEnum.MASTER);
Log.i(DBContextHolder.class, "切换到master");
}
public static void slave() {
// 轮询
int index = counter.getAndIncrement() % 2;
if (counter.get() > 9999) {
counter.set(-1);
}
Log.i(DBContextHolder.class, "轮询index:"+index);
if (index == 0) {
set(DBTypeEnum.SLAVE1);
Log.i(DBContextHolder.class, "切换到slave1");
} else {
set(DBTypeEnum.SLAVE2);
Log.i(DBContextHolder.class, "切换到slave2");
}
}
}
设置路由key
package com.cjs.example.bean;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.lang.Nullable;
public class MyRoutingDataSource extends AbstractRoutingDataSource {
@Nullable
@Override
protected Object determineCurrentLookupKey() {
return DBContextHolder.get();
}
}
package demo.datasource.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import demo.datasource.config.bean.DBContextHolder;
/**
* 设置路由key
* 默认情况下,所有的查询都走从库,插入/修改/删除走主库。我们通过方法名来区分操作类型(CRUD)
*/
@Aspect
@Component
public class DataSourceAop {
@Pointcut("!@annotation(demo.datasource.annotation.Master) " +
"&& (execution(* demo.datasource.service..*.select*(..)) " +
"|| execution(* demo.datasource.service..*.get*(..)))")
public void readPointcut() {
}
@Pointcut("@annotation(demo.datasource.annotation.Master) " +
"|| execution(* demo.datasource.service..*.insert*(..)) " +
"|| execution(* demo.datasource.service..*.add*(..)) " +
"|| execution(* demo.datasource.service..*.update*(..)) " +
"|| execution(* demo.datasource.service..*.edit*(..)) " +
"|| execution(* demo.datasource.service..*.delete*(..)) " +
"|| execution(* demo.datasource.service..*.remove*(..))")
public void writePointcut() {
}
@Before("readPointcut()")
public void read() {
DBContextHolder.slave();
}
@Before("writePointcut()")
public void write() {
DBContextHolder.master();
}
/**
* 另一种写法:if...else... 判断哪些需要读从数据库,其余的走主数据库
*/
// @Before("execution(* demo.datasource.service.impl.*.*(..))")
// public void before(JoinPoint jp) {
// String methodName = jp.getSignature().getName();
//
// if (StringUtils.startsWithAny(methodName, "get", "select", "find")) {
// DBContextHolder.slave();
// }else {
// DBContextHolder.master();
// }
// }
}
有一般情况就有特殊情况,特殊情况是某些情况下我们需要强制读主库,针对这种情况,我们定义一个注解,用该主句标注的Service方法就读主库
package com.cjs.example.annotation;
public @interface Master {
}
例如:我们有一张表member
package demo.datasource.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import demo.datasource.annotation.Master;
import demo.datasource.entity.Member;
import demo.datasource.mapper.MemberMapper;
import demo.datasource.service.MemberService;
@Service
public class MemberServiceImpl implements MemberService {
@Autowired
private MemberMapper memberMapper;
// @Transactional
@Override
public int insert(Member member) {
return memberMapper.insert(member);
}
@Master
@Override
public int save(Member member) {
return memberMapper.insert(member);
}
@Override
public List selectAll() {
return memberMapper.selectByExample();
}
@Master
@Override
public String getToken(String appId) {
// 有些读操作必须读主数据库
// 比如,获取微信access_token,因为高峰时期主从同步可能延迟
// 这种情况下就必须强制从主数据读
return appId;
}
}
package demo.datasource.test;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import demo.datasource.entity.Member;
import demo.datasource.service.MemberService;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DatasourceDemoApplicationTests {
@Autowired
private MemberService memberService;
@Test
public void testWrite() {
Member member = new Member();
member.setName("mike");
memberService.insert(member);
}
@Test
public void testRead() {
for (int i = 0; i < 4; i++) {
List selectAll = memberService.selectAll();
for(Member m : selectAll) {
System.out.println(m);
}
}
}
@Test
public void testSave() {
Member member = new Member();
member.setName("wangwu");
memberService.save(member);
}
@Test
public void testReadFromMaster() {
memberService.getToken("1234");
}
}
数据库效果:
执行查询时控制台效果,会在两个从库中轮询:
https://download.csdn.net/download/qzc70919700/30565891