数据库优化-主从同步、读写分离

准备环境
至少需要两台服务器,肯定是独立两台电脑,当然也可以使用虚拟机。
1)分别构造主、从数据库并输出日志(方便定位问题)
配置日志路径
2)安装及启动:
修改master中的my.ini 路径改为master的路径
mysqld --install MySQLXY --defaults-file=“C:\Program Files\MySQL\MySQL Server X.Y\my.ini”
(install/remove of the service denied 权限不足 以管理身份运行)
net start MySQLXY
Sc delete master/slave 弄错了可以删除服务重新操作
开启日志(非必须)

(非必须)开启sql语句的日志,生产环境不建议开启:

查看日志目录,并开启sql语句的日志:
mysql>show variables like ‘%general_log%’;
mysql>set global general_log=on;
开启后,重启Mysql ,上述开启日志配置将失效。

master服务器配置

1)修改master方的mysql.ini

 log-bin=mysql-bin
 server-id=1
 innodb_flush_log_at_trx_commit=1
 sync_binlog=1
 binlog_ignore_db=mysql
 binlog_checksum=none

2)重启master服务,登录
3)授权savle服务器的使用的账号及权限
场景:
master主服务器: 192.168.1.101
slave从服务器 : 192.168.1.102
1、权给slave数据库服务器192.168.10.131(master用户,只对slave服务器开放)
语法为:GRANT REPLICATION SLAVE ON . to ‘用户名’@‘192.168.0.102’ identified by ‘密码’;

 Mysql>  GRANT REPLICATION SLAVE ON *.* to 'lvtest'@'192.168.77.128'identified   by 'admin';

参数说明:
lvtest:slave连接master使用的账号
IDENTIFIED BY ‘admin’ :slave连接master使用的密码
192.168.77.128:slave IP

2询主数据库状态
Mysql> show master status;
±-------------------±---------±--------------±-----------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
±-------------------±---------±--------------±-----------------------------+
| mysql-bin.000002 | 226 | | mysql |
±-------------------±---------±--------------±-----------------------------+
记录 File 和 Position的值,在slave端使用

slave配置

1、修改slave服务器的配置文件my.ini将 server-id = 1修改为 server-id = 10,并确保这个
ID没有被别的MySQL服务所使用。
2、启动slave服务器,登录
3、在slave端,配置master链接信息 (执行语句)
1) 配置
Mysql> change master to
master_host=‘192.168.77.1’, #master IP
master_user=‘lvtest’, #master数据库通过GRANT授权的账号
master_password=‘admin’, #master数据库通过GRANT授权的密码
master_port=3307, #master数据库的密码
master_log_file=‘mysql-bin.000001’,
#master数据库中通过show master status显示的File名称
master_log_pos=296
#master数据库的通过show master status显示的Position的值
2) (重启)连接
Mysql> start slave;

        3)主从同步检查
        show slave status;
        其中Slave_IO_Running 与 Slave_SQL_Running 的值都必须为YES,才表明状态正常。

4、 测试
1) 在master上,建库、建表、添加数据
2) 刷新slave库,记录也存在
由此,整个MySQL主从复制的过程就完成了,接下来,我们进行MySQL读写分离的安装与配置。

读写分离-sharding-jdbc

数据库优化-主从同步、读写分离_第1张图片
配置文件
基础配置–>application.yml

server:
  port: 8018

spring:
  application:
    name: bj-sharding-jdbc
  main:
    allow-bean-definition-overriding: true
  profiles:
    # rw-读写分离配置  table-数据分表+读写分离   dt-分库分表+读写分离
    active: dt
mybatis:
  mapper-locations: classpath:/top/qrainly/**/dao/**/*.xml

读写分离配置–>application-rw.yml

sharding:
  jdbc:
    dataSource:
      names: db-test0,db-test1
      db-test0:
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/bj_sharding?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
        username: root
        password: 123456
        maxPoolSize: 20
      db-test1:
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3307/bj_sharding?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
        username: root
        password: 123456
        maxPoolSize: 20
    config:
        # 仅配置读写分离时打开此配置
      masterslave:
        # 配置从库选择策略,提供轮询与随机,这里选择用轮询//random 随机 //round_robin 轮询
        load-balance-algorithm-type: round_robin
        name: db1s2
        master-data-source-name: db-test0
        slave-data-source-names: db-test1
    props:
      sql:
        # 开启SQL显示,默认值: false,注意:仅配置读写分离时不会打印日志!!!
        show: true

数据分表+读写分离配置–>application-table.yml

sharding:
  jdbc:
    dataSource:
      names: db-test0,db-test1
      db-test0:
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/bj_sharding?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
        username: root
        password: 123456
        maxPoolSize: 20
      db-test1:
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3307/bj_sharding?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
        username: root
        password: 123456
        maxPoolSize: 20
    config:
      # 配置数据分表
      sharding:
        tables:
          user:
            table-strategy:
              standard:
                sharding-column: id
                precise-algorithm-class-name: top.qrainly.sharding.jdbc.config.MyPreciseShardingAlgorithm
            # 读取ds_0数据源的user_0、user_1、user_2、user_3
            actual-data-nodes: ds_0.user_$->{0..3}
        master-slave-rules:
          ds_0:
            master-data-source-name: db-test0
            slave-data-source-names: db-test1
    props:
      sql:
        # 开启SQL显示,默认值: false,注意:仅配置读写分离时不会打印日志!!!
        show: true

分库分表+读写分离配置–>application-dt.yml

---
sharding:
  jdbc:
    datasource:
      names: ds-master-0,ds-master-1,ds-master-0-slave-0,ds-master-1-slave-0
      # 主库0
      ds-master-0:
        password: 123456
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/bj_sharding?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
        username: root
      # 主库0-从库0
      ds-master-0-slave-0:
        password: 123456
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3307/bj_sharding?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
        username: root
      # 主库1
      ds-master-1:
        password: 123456
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/bj_sharding1?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
        username: root
      # 主库1-从库0
      ds-master-1-slave-0:
        password: 123456
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3307/bj_sharding1?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
        username: root
    config:
      sharding:
        tables:
          user:
            table-strategy:
              inline:
                sharding-column: id
                algorithm-expression: user_$->{id % 5}
            key-generator-column-name: id
            actual-data-nodes: ds_$->{0..1}.user_$->{0..4}
        default-database-strategy:
          inline:
            # 置的分库的字段,本案例是根据id进行分
            sharding-column: id
            # 置的分库的逻辑,根据id%2进行分
            algorithm-expression: ds_$->{id % 2}
        master-slave-rules:
          ds_1:
            slave-data-source-names: ds-master-1-slave-0
            master-data-source-name: ds-master-1
          ds_0:
            slave-data-source-names: ds-master-0-slave-0
            master-data-source-name: ds-master-0

注:分库分表配置下需要在@SpringBootApplication上添加参数exclude={DataSourceAutoConfiguration.class}

你可能感兴趣的:(数据库,mysql)