MySQL 数据库主从同步与读写分离

1、MySQL 数据库主服务器配置

root@master ~]# ifconfig
eth1	Link encap:Ethernet HWaddr 00:16:3E:00:47:83
		inet addr:120.24.64.163  Bcast:120.24.67.255  Mask:255.255.252.0

打开 master 的 binary log:

root@master ~]# vim /etc/my.cnf

[mysqld]
# datadir=/var/lib/mysql
server-id=1
log-bin=master-bin
log-bin-index=master-bin.index
datadir=/usr/local/yanyan/mysql_data
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
default-character-set=utf8
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysql]
default-character-set=utf8

重启master上的mysql服务:

root@master ~]# service mysqld restart

显示master状态:

mysql> show master status;
+------------------+-----------------+-------------------+-----------------+
| File			   | Position		 | Binlog_Do_DB		 | Binlog_Ignore_DB|
+------------------+-----------------+-------------------+-----------------+
| master-bin.000001|			6485 |					 |				   |
+------------------+-----------------+-------------------+-----------------+
1 row in set (0.00 sec)

指定master访问slave的账号,并授权REPLICATION SLAVE权限。

mysql> create user repl;
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'115.28.159.6' IDENTIFIED BY 'mysql';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

2、MySQL 数据库从服务器配置

root@slave ~]# ifconfig
eth1	Link encap:Ethernet HWaddr 00:16:3E:00:47:83
		inet addr:115.28.159.6  Bcast:115.28.159.255  Mask:255.255.252.0

打开slave的 relay log:

root@master ~]# vim /etc/my.cnf

# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.

# The following options will be passed to all MySQL clients
[client]
# password	= your_password
port		= 3306
socket		= /var/lib/mysql/mysql.sock
# Here follows entries for some specific programs.

# The MySQL server
[mysqld]
server-id	= 2
relay-log	= slave-relay-bin
relay-log-index	= slave-relay-bin.index
port		= 3306
socket		= /var/lib/mysql/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M

重启slave上的mysql服务:

root@slave ~]# service mysqld restart

或者使用以下命令重启:

root@slave ~]# /etc/init.d/mysql stop
root@slave ~]# /etc/init.d/mysql start

3、在 slave 上将 master 与 slave 联系起来

mysql> change master to master_host='120.24.64.163', master_port=3306, master_user='repl', master_password='mysql', master_log_file='master-bin.000001', master_log_pos=0;
Query OK, 0 rows affected (0.06 sec)

开启slave:

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

查看slave状态:(\G表示将输出竖向显示)

mysql> show slave status \G;

停止slave:

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

4、验证主库和从库同步

主库添加数据:

mysql> create database o2o;
Query OK, 1 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database			 |
+--------------------+
| information_schema |
| mysql				 |
| o2o				 |
| performance_schema |
+--------------------+

从库显示数据:

mysql> show databases;
+--------------------+
| Database			 |
+--------------------+
| information_schema |
| mysql				 |
| o2o				 |
| performance_schema |
+--------------------+

主库删除数据:

mysql> drop database o2o;
Query OK, 1 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database			 |
+--------------------+
| information_schema |
| mysql				 |
| performance_schema |
+--------------------+

从库也删除了数据:

mysql> show databases;
+--------------------+
| Database			 |
+--------------------+
| information_schema |
| mysql				 |
| performance_schema |
+--------------------+

5、将本地数据库导出到主库

在本地执行如下命令:

~]# mysqldump -uroot -p o2o > o2o.sql
Enter password:
~]# ls
o2o.sql
~]# scp o2o.sql root@120.24.64.163:/root
root@120.24.64.163's password:
o2o.sql						100%	13KB 	12.8KB/s	00:00

在master中导入o2o.sql

root@master ~]# mysql -uroot -p
Enter password:

mysql> create database o2o;
mysql> use o2o;
mysql> source ~/o2o.sql;

这时,master和slave中的o2o数据库都已创建,并导入数据。

mysql> show tables;
+------------------------+
| Tables_in_o2o			 |
+------------------------+
| tb_area				 |
| tb_head_line			 |
| tb_local_auth			 |
| tb_person_info		 |
| tb_product			 |
| tb_product_category	 |
| tb_product_img		 |
| tb_shop				 |
| tb_shop_category		 |
| tb_wechat_auth		 |
+------------------------+
10 rows in set (0.00 sec)

6、在项目中配置读写分离

在master和slave中分别新建用户work,并授予权限(master和slave中都执行以下操作)。work账号就是项目代码访问数据库master和slave的账号。

mysql> create user work;
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'work'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

在项目代码的jdbc.properties中做如下配置:
(连接master数据库,可读可写;连接slave数据库,只可读)

## jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.master.url=jdbc:mysql://120.24.64.163:3306/o2o?useUnicode=true&characterEncoding=utf8
jdbc.slave.url=jdbc:mysql://115.28.159.6:3306/o2o?useUnicode=true&characterEncoding=utf8
jdbc.username=work
jdbc.password=123456
## 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();
    }
}
## 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<>();
    public static final String DB_MASTER = "master";
    public static final String DB_SLAVE = "slave";

    // 获取线程的dbType
    public static String getDbType(){
        String db = contextHolder.get();
        if (db == null){
            db = DB_MASTER;
        }
        return db;
    }

    // 设置线程的dbType
    public static void setDbType(String str){
        logger.debug("所使用的数据源为:" + str);
        contextHolder.set(str);
    }

    // 清理连接类型
    public static void clearDBType(){
        contextHolder.remove();
    }
}
## DynamicDataSourceInterceptor.java

package com.imooc.o2o.dao.split;

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.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.util.Locale;
import java.util.Properties;

@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
	@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.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.*";
    @Override
    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 != true){
            // 读方法
            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[1]);
                    String sql = boundSql.getSql().toLowerCase(Locale.CHINA).replaceAll("[\\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, SqlCommandType [{}]",
                ms.getId(), lookupKey, ms.getSqlCommandType().name());
        DynamicDataSourceHolder.setDbType(lookupKey);
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        if (target instanceof Executor){
            return Plugin.wrap(target, this);
        } else {
            return target;
        }
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
## 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">

    <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.username}" />
        <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.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
	<!--配置动态数据源,targetDataSources是路由数据源所对应的名称-->
    <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="targetDataSource">
            <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>
## 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>
        <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>
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页