读写分离,分库分表

数据切分

将存储在一台数据库上的数据,分散到多台数据库中,以此达到降低单台数据库负载的效果

垂直切分

水平切分

数据库读写分离

安装

wget https://dev..com/get/mysql80-community-release-el7-3.noarch.rpm
rpm -ivh 80-community-release-el7-3.noarch.rpm
yum install -community-server
systemctl start d
# 查看临时密码
grep password /var/log/d.log
# 登录修改密码
alter user 'root'@'localhost' identified by 'Learn@123456';
flush privileges;
# 新建用户
create user 'learn'@'%' identified with _native_password by 'Learn@123456';
# 授权
grant all on *.* to 'learn'@'%';
flush privileges;

主从配置

# 主
vim /etc/my.cnf
log-bin=learn_
server-id=1
# 从
server-id=2
create user 'repl'@'%' identified by 'Learn@123456';
grant replication slave on *.* to 'repl'@'%';
flush privileges;
change master to
-> master_host='192.168.150.90',
-> master_user='repl',
-> master_password='Learn@123456',
-> master_log_file='learn_mysql.000001',
-> master_log_pos=857;

mycat

应用场景

基本概念

配置

# server.xml
# 配置mycat的用户名,密码,权限,schema等
# 如同给新建用户
# 客户端连接mycat与连接无异
<mycat:server>
    ......
    <user name="root" defaultAccount="true">
        <property name="password">123456</property>
        <property name="schemas">TESTDB</property>
    </user>
    <user name="user">
        <property name="password">123456</property>
        <property name="schemas">TESTDB</property>
        <property name="readOnly">true</property>
    </user>
</mycat:server>
# 配置dataHost(节点主机),包括读host,写host
# 配置dataNode(数据节点),自定到具体的数据库
# 配置schema,表名,数据节点,分片规则
# balance:负载均衡类型:0不开启读写分离;1(双主双从:读在后3台,写在第一台)和
# 2(所有读操作随机分配)均读写分离;3读落在readHost上
# schema标签中,checkSQLschema默认保留就好
# sqlMaxLimit:select默认的limit值,仅对分片有效
# rule属性:定义分片表的分片规则,必须与rule.xml中的tableRule对应
# ruleRequired属性,是否绑定分片规则,如果为true,没有绑定分片规则,程序报错

分片规则

# 适合特定的场景,通过在配置文件中配置可能的枚举id,自己配置分片
# schema.xml
<table name="user" dataNode="dn90,dn91" rule="sharding-by-intfile" />
# rule.xml
<tableRule name="sharding-by-intfile">
    <rule>
        <columns>province_id</columns>
        <algorithm>hash-int</algorithm>
    </rule>
</tableRule>
<function name="hash-int"
    class="io.mycat.route.function.PartitionByFileMap">
    <property name="mapFile">partition-hash-int.txt</property>
    # 设置默认节点
    <property name="defaultNode">0</property>
</function>
# partition-hash-int.txt
DEFAULT_NODE=0
# schema.xml
<table name="user" dataNode="dn90,dn91" rule="mod-long" />
# rule.xml
<tableRule name="mod-long">
    <rule>
        <columns>user_id</columns>
        <algorithm>mod-long</algorithm>
    </rule>
</tableRule>
<function name="mod-long"
    class="io.mycat.route.function.PartitionByMod">
    # 设置节点数量
    <property name="count">2</property>
</function>

全局表

子表

# schema.xml
<table name="o_order" dataNode="dn90,dn91" rule="auto-sharding-long">
    <childTable name="order_item" joinKey="order_id" parentKey="id"/>
</table>

高可用

yum -y install haproxy.x86_64
vi /etc/haproxy/haproxy.cfg
# 部分配置
defaults
    mode                    tcp
backend app
    balance     roundrobin
    server  app1 192.168.150.90:8066 check
    server  app1 192.168.150.92:8066 check
# 启动
haproxy -f /etc/haproxy/haproxy.cfg
yum install -y keepalived.x86_64
vi /etc/keepalived/keepalived.conf
# 部分配置
参考keepalived配置

sharding-jdbc

## 数据源
spring.shardingsphere.datasource.names=ds0,ms1,slave0

spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbcUrl=jdbc:mysql://192.168.150.90:3306/sharding_order
spring.shardingsphere.datasource.ds0.username=learn
spring.shardingsphere.datasource.ds0.password=Learn@123456

spring.shardingsphere.datasource.ms1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ms1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ms1.jdbcUrl=jdbc:mysql://192.168.150.91:3306/shard_order
spring.shardingsphere.datasource.ms1.username=learn
spring.shardingsphere.datasource.ms1.password=Learn@123456

spring.shardingsphere.datasource.slave0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.slave0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.slave0.jdbcUrl=jdbc:mysql://192.168.150.92:3306/shard_order
spring.shardingsphere.datasource.slave0.username=learn
spring.shardingsphere.datasource.slave0.password=Learn@123456

## 表
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ms$->{0..1}.t_order${1..2}
spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.algorithm-expression=ms${user_id%2}
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_${id%2+1}

## 全局表
spring.shardingsphere.sharding.broadcast-tables=area

## 读写分离
spring.shardingsphere.sharding.master-slave-rules.ms0.master-data-source-name=ds0
spring.shardingsphere.sharding.master-slave-rules.ms0.slave-data-source-names.=slave0
spring.shardingsphere.sharding.master-slave-rules.ms0.load-balance-algorithm-type=RANDOM

## mybatis
mybatis.mapper-locations=/mybatis/*.xml

发表评论

发表
Table of Contents