1.主从服务器的配置及实现原理
(1)主从服务的配置设计图
(2)主重服务器的配置
1.主服务器的配置
1.1打开配置文件
vim /etc/ my.cnf
2.2修改配置文件
server-id=1
log-bin=master-bin
log-bin-index=master-bin.index
2.3重启服务器
service mysqld restart
2.4查看服务器是否执行成功
SHOW MASTER STATUS
2.从服务器的配置
1.1打开配置文件
vim /etc/ my.cnf
2.2修改配置文件
server-id=2
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
2.3重启服务器
/etc/init.d/mysql stop
/etc/init.d/mysql start
2.4查看服务器是否执行成功
SHOW MASTER STATUS
2.5配置主服务的相关内容
create user repl;
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'115.28.159.6' IDENTIFIED BY 'mysql'
flush privileges;
3.主从服务器的连接
3.1. 打开数据库(从服务器)
mysql -uroot -proot
3.2. 从库连接主库
change master to master_host='120.73.23.86' ,master_port=3306,master_user='repl',master_password='mysql',master-log-file='master-bin.000001',master_log_pos=0
**主ip :120.73.23.86 主端口:master_port **
3.3. 查询从库状态(不正常)
show slave status \G;
出现以上:不能够正常同步
3.3.1 停止主重同步
stop slave;
3.4. 查询从库状态(正常)
4.数据远程导入到远程服务器上
4.1本地数据导入成本地文件
mysqldump -uroot -p o2o>o2o.sql
4.2导入到远程服务器上面
scp o2o.sql root@120.24.64.163:/root
4.3导入到远程服务器上面
//连接远程数据库
mysql -uroot -proot
//显示数据库信息
show databases;
//创建数据库
create database o2o;
//使用数据库
use o2o;
//引用o2o文件
source ~/o2o.sql
5.给主库设置用户,并设置访问权限
//创建用户,并赋予增、删、改、查的权限
GRANT select ,insert,update,delete ON *.* TO 'work'@'%' IDENTIFIED BY '230230' WITH GRANT OPTION
//刷新权限
flush privileges;
(3)配置java代码
3.1 配置本地路由
1.DynamicDataSource.java文件
package com.imooc.o2o.dao.split;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
return DynamicDataSourceHolder.getDbType();
}
}
2.DynamicDataSourceHolder.java文件
package com.imooc.o2o.dao.split;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DynamicDataSourceHolder {
private static Logger logger=LoggerFactory.getLogger(DynamicDataSourceHolder.class);
private static ThreadLocal<String> contextHolder=new ThreadLocal<String>();
public static final String DB_MASTER="master";
public static final String DB_SLAVE="slave";
/**
* 获取连接类型
* @return
*/
public static String getDbType() {
String db=contextHolder.get();
if(db==null) {
db=DB_MASTER;
}
return db;
}
/***
* 设置连接类型
* @param str
*/
public static void setDbType(String str) {
logger.debug("所使用的数据源为:"+str);
contextHolder.set(str);
}
/**
*清理连接类型
*/
public static void clearDbType() {
contextHolder.remove();
}
}
3.2 配置拦截器
package com.imooc.o2o.dao.split;
import java.util.Locale;
import java.util.Properties;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.keygen.SelectKeyGenerator;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
@Intercepts({@Signature(type=Executor.class,method = "update",args = {MappedStatement.class,Object.class}),
@Signature(type=Executor.class,method = "query",args = {MappedStatement.class,Object.class,ResultHandler.class})})
public class DynamicDataSourceInterceptor implements Interceptor{
private static Logger logger=LoggerFactory.getLogger(DynamicDataSourceInterceptor.class);
private static final String REGEX=".*insert\\u0020.|.*delete\\u0020.*|.*update\\u0020.*";
/**
* 拦截的方法
*/
public Object intercept(Invocation invocation) throws Throwable {
boolean synchronizationActive=TransactionSynchronizationManager.isActualTransactionActive();
Object[] objects=invocation.getArgs();
MappedStatement ms=(MappedStatement)objects[0];
String lookupKey=DynamicDataSourceHolder.DB_MASTER;
if(!synchronizationActive) {
//读方法
if(ms.getSqlCommandType().equals(SqlCommandType.SELECT)) {
//selectKey为自增id查询主键(SELECT LAST_INSERT_ID())方法,使用主库
if(ms.getId().contains(SelectKeyGenerator.SELECT_KEY_SUFFIX)) {
lookupKey=DynamicDataSourceHolder.DB_MASTER;
}else{
BoundSql boundSql=ms.getSqlSource().getBoundSql(objects[2]);
String sql=boundSql.getSql().toLowerCase(Locale.CHINA).replace("[\\t\\n\\r]", " ");
if(sql.matches(REGEX)){
lookupKey=DynamicDataSourceHolder.DB_MASTER;
}else{
lookupKey=DynamicDataSourceHolder.DB_SLAVE;
}
}
}
}else{
lookupKey=DynamicDataSourceHolder.DB_MASTER;
}
logger.debug("设置方法[{}] use [{}] Strategy,SqlCommanType[{}]..",
ms.getId(),lookupKey,ms.getSqlCommandType().name());
DynamicDataSourceHolder.setDbType(lookupKey);
return invocation.proceed();
}
/***
* 返回封装好的对象
*/
public Object plugin(Object target) {
if(target instanceof Executor) {
return Plugin.wrap(target, this);
}else {
return target;
}
}
public void setProperties(Properties properties) {
// TODO Auto-generated method stub
}
}
3.3 配置主从数据库配置文件(jdbc.properties)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.slave.url=jdbc:mysql://115.28.159.6:3306/o2o?useUnicode=true&characterEncoding=utf8
jdbc.master.url=jdbc:mysql://120.24.64.163:3306/o2o?useUnicode=true&characterEncoding=utf8
jdbc.usesname=root
jdbc.password=123
3.4 配置spring整合mybatis的配置文件(spring-dao.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置整合mybatis过程 -->
<!-- 1.配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- <bean class="com.imooc.myo2o.util.EncryptPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:redis.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8" />
</bean> -->
<!-- 修改部分 -->
<!-- 2.数据库连接池 -->
<bean id="abstractDataSource" abstract="true"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false" />
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000" />
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2" />
</bean>
<bean id="master" parent="abstractDataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.master.url}" />
<property name="user" value="${jdbc.usesname}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="slave" parent="abstractDataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.slave.url}" />
<property name="user" value="${jdbc.usesname}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置动态数据源,这儿targetDataSources就是路由数据源所对应的名称 -->
<bean id="dynamicDataSource" class="com.imooc.o2o.dao.split.DynamicDataSource">
<property name="targetDataSources">
<map>
<!-- value-ref与上面配置保持一致 key与实体类中保持一致-->
<entry value-ref="master" key="master"></entry>
<entry value-ref="slave" key="slave"></entry>
</map>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSources">
<ref bean="dynamicDataSource"/>
</property>
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 扫描entity包 使用别名 -->
<property name="typeAliasesPackage" value="com.imooc.o2o.entity" />
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>
<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.imooc.o2o.dao" />
</bean>
</beans>
3.5配置mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置全局变量 -->
<settings>
<!-- 使用jdbc的useGeneratedKeys获取数据库自增主键值 -->
<setting name="useGeneratedKeys" value="true"/>
<!-- 使用列标签替换列别名 默认;true -->
<setting name="useColumnLabel" value="true"/>
<!-- 开启驼峰命名转换 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--配置拦截器 -->
<plugins>
<plugin interceptor="com.imooc.o2o.dao.split.DynamicDataSourceInterceptor"></plugin>
</plugins>
</configuration>