vim /etc/ my.cnf
server-id=1
log-bin=master-bin
log-bin-index=master-bin.index
service mysqld restart
SHOW MASTER STATUS
vim /etc/ my.cnf
server-id=2
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
/etc/init.d/mysql stop
/etc/init.d/mysql start
SHOW MASTER STATUS
create user repl;
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'115.28.159.6' IDENTIFIED BY 'mysql'
flush privileges;
mysql -uroot -proot
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 **
show slave status \G;
stop slave;
mysqldump -uroot -p o2o>o2o.sql
scp o2o.sql [email protected]:/root
//连接远程数据库
mysql -uroot -proot
//显示数据库信息
show databases;
//创建数据库
create database o2o;
//使用数据库
use o2o;
//引用o2o文件
source ~/o2o.sql
//创建用户,并赋予增、删、改、查的权限
GRANT select ,insert,update,delete ON *.* TO 'work'@'%' IDENTIFIED BY '230230' WITH GRANT OPTION
//刷新权限
flush privileges;
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();
}
}
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();
}
}
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
}
}
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
<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">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="abstractDataSource" abstract="true"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<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>
<bean id="dynamicDataSource" class="com.imooc.o2o.dao.split.DynamicDataSource">
<property name="targetDataSources">
<map>
<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>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="typeAliasesPackage" value="com.imooc.o2o.entity" />
<property name="mapperLocations" value="classpath:mapper/*.xml" />
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.imooc.o2o.dao" />
bean>
beans>
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="useGeneratedKeys" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
settings>
<plugins>
<plugin interceptor="com.imooc.o2o.dao.split.DynamicDataSourceInterceptor">plugin>
plugins>
configuration>