java主从服务器的配置

1.主从服务器的配置及实现原理

(1)主从服务的配置设计图

java主从服务器的配置_第1张图片

(2)主重服务器的配置

1.主服务器的配置

1.1打开配置文件

在这里插入图片描述

vim /etc/ my.cnf

2.2修改配置文件

java主从服务器的配置_第2张图片

server-id=1
log-bin=master-bin
log-bin-index=master-bin.index

2.3重启服务器

service mysqld restart

2.4查看服务器是否执行成功

SHOW MASTER STATUS

java主从服务器的配置_第3张图片

2.从服务器的配置

1.1打开配置文件

在这里插入图片描述

vim /etc/ my.cnf

2.2修改配置文件

java主从服务器的配置_第4张图片

server-id=2
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin

2.3重启服务器

java主从服务器的配置_第5张图片

/etc/init.d/mysql stop
/etc/init.d/mysql start

2.4查看服务器是否执行成功

SHOW MASTER STATUS

java主从服务器的配置_第6张图片

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;

java主从服务器的配置_第7张图片
出现以上:不能够正常同步

3.3.1 停止主重同步
stop slave;

在这里插入图片描述

3.4. 查询从库状态(正常)

java主从服务器的配置_第8张图片

4.数据远程导入到远程服务器上

4.1本地数据导入成本地文件

mysqldump -uroot -p o2o>o2o.sql

4.2导入到远程服务器上面

scp o2o.sql [email protected]:/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)


<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>

3.5配置mybatis-config.xml文件


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>

你可能感兴趣的:(服务器,java,运维)