sharding-jdbc 支持配置主从库,本章主要介绍如何配置主从,及mysql 主从配置
mysql主从配置参考文章:https://blog.csdn.net/soslinken/article/details/97764119
版本:
springboot: 2.4.1
mybatis-plus-boot-starter: 2.2.0
sharding-jdbc-spring-boot-starter: 4.1.1
druid: 1.2.4官网:https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/
Demo gitee: https://gitee.com/old_guys/sharding-test-demo
目录:
- MySQL 主从配置
- sharding-jdbc主从配置
MySQL 主从配置
目录结构
.
├── docker-compose.yml
├── master
│ ├── Dockerfile
│ └── my.cnf
└── slave
├── Dockerfile
└── my.cnf
docker-compose.yml
version: '3'
services:
mysql-master:
build:
context: ./
dockerfile: master/Dockerfile
environment:
- "MYSQL_ROOT_PASSWORD=root"
links:
- mysql-slave-1
- mysql-slave-2
ports:
- "33065:3306"
restart: always
hostname: mysql-master
mysql-slave-1:
build:
context: ./
dockerfile: slave/Dockerfile
environment:
- "MYSQL_ROOT_PASSWORD=root"
ports:
- "33066:3306"
restart: always
hostname: mysql-slave-1
mysql-slave-2:
build:
context: ./
dockerfile: slave/Dockerfile
environment:
- "MYSQL_ROOT_PASSWORD=root"
ports:
- "33067:3306"
restart: always
hostname: mysql-slave-2
查看数据库状态 使用navicat 或者命令行都可以
mysql -uroot -h127.0.0.1 -P33066 -p [密码]
mysql> show master status;
+----------------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------------------+----------+--------------+------------------+-------------------+
| replicas-mysql-slave1-bin.000005 | 154 | | mysql | |
+----------------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
配置slave
master_log_file 对应 master status 的log 文件名称
master_log_pos 对应 master status 的偏移量
STOP SLAVE;
CHANGE MASTER TO master_host = 'mysql-master',
master_port = 3306,
master_user = 'root',
master_password = 'root',
master_log_file = 'replicas-mysql-bin.000008',
master_log_pos = 11357;
START SLAVE;
SHOW SLAVE STATUS;
查看配置效果,都为yes 才是有效
Slave_IO_Running: Yes
Slave_SQL_Running: Yes注意! mysql主从库的数据库必须保证一致,如果操作主库中 从库没有的 数据库表,会抛异常,直接导致主从同步失败。
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: mysql-master
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: replicas-mysql-bin.000005
Read_Master_Log_Pos: 357
Relay_Log_File: replicas-mysql-relay-bin.000002
Relay_Log_Pos: 329
Relay_Master_Log_File: replicas-mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 357
Relay_Log_Space: 545
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 100
Master_UUID: 95827688-4a4c-11eb-abdc-0242ac130003
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
配置完成之后,操作主库,从库就会跟着同步。
sharding-jdbc主从配置
主从配置的时候类似于逻辑数据源
配置实际的数据源
spring.shardingsphere.datasource.names=ds-0,ds-1m,ds-2m,ds-1s1,ds-2s1,ds->1s2,ds-2s2
配置映射的数据源
spring.shardingsphere.sharding.master-slave-rules.ds-1.master-data-source->name=ds-1m spring.shardingsphere.sharding.master-slave-rules.ds-1.slave-data-source->names[0]=ds-1s1 spring.shardingsphere.sharding.master-slave-rules.ds-1.slave-data-source->names[1]=ds-1s2 spring.shardingsphere.sharding.master-slave-rules.ds-2.master-data-source->name=ds-2m spring.shardingsphere.sharding.master-slave-rules.ds-2.slave-data-source->names=ds-2s1,ds-2s2
使用映射的数据源配置分库分表
spring.shardingsphere.sharding.tables.test_user.actual-data-nodes=ds-$->>{1..2}.test_user_$->{0..3} spring.shardingsphere.sharding.tables.test_user.database-strategy.inline.sharding->column=scope spring.shardingsphere.sharding.tables.test_user.database->strategy.inline.algorithm-expression=ds-$->{scope%2+1} spring.shardingsphere.sharding.tables.test_user.table-strategy.inline.sharding->column=id spring.shardingsphere.sharding.tables.test_user.table-strategy.inline.algorithm->expression=test_user_$->{id%4}
完整的环境变量
spring.shardingsphere.datasource.names=ds-0,ds-1m,ds-2m,ds-1s1,ds-2s1,ds-1s2,ds-2s2
#打开sql显示
spring.shardingsphere.props.sql.show=true
spring.shardingsphere.datasource.ds-0.url=jdbc:mysql://127.0.0.1:3306/ds-0?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT
spring.shardingsphere.datasource.ds-0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-0.username=root
spring.shardingsphere.datasource.ds-0.password=root
spring.shardingsphere.datasource.ds-1m.url=jdbc:mysql://192.168.62.165:33065/test_db_01?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT
spring.shardingsphere.datasource.ds-1m.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds-1m.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-1m.username=root
spring.shardingsphere.datasource.ds-1m.password=root
spring.shardingsphere.datasource.ds-2m.url=jdbc:mysql://192.168.62.165:33065/test_db_02?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT
spring.shardingsphere.datasource.ds-2m.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds-2m.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-2m.username=root
spring.shardingsphere.datasource.ds-2m.password=root
spring.shardingsphere.datasource.ds-1s1.url=jdbc:mysql://192.168.62.165:33066/test_db_01?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT
spring.shardingsphere.datasource.ds-1s1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds-1s1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-1s1.username=root
spring.shardingsphere.datasource.ds-1s1.password=root
spring.shardingsphere.datasource.ds-2s1.url=jdbc:mysql://192.168.62.165:33066/test_db_02?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT
spring.shardingsphere.datasource.ds-2s1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds-2s1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-2s1.username=root
spring.shardingsphere.datasource.ds-2s1.password=root
spring.shardingsphere.datasource.ds-1s2.url=jdbc:mysql://192.168.62.165:33067/test_db_01?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT
spring.shardingsphere.datasource.ds-1s2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds-1s2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-1s2.username=root
spring.shardingsphere.datasource.ds-1s2.password=root
spring.shardingsphere.datasource.ds-2s2.url=jdbc:mysql://192.168.62.165:33067/test_db_02?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT
spring.shardingsphere.datasource.ds-2s2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds-2s2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-2s2.username=root
spring.shardingsphere.datasource.ds-2s2.password=root
## 自定义参数
my-sharding-table.config=0,1,2,3
## demo2_test_user 如果有多个数据源,但是不配置(配置失效)的时候,就会随机获取一个数据源进行处理。
spring.shardingsphere.sharding.tables.demo2_test_user.actual-data-nodes=ds-0.test_user
##test_user
spring.shardingsphere.sharding.tables.test_user.actual-data-nodes=ds-$->{1..2}.test_user_$->{0..3}
spring.shardingsphere.sharding.tables.test_user.database-strategy.inline.sharding-column=scope
spring.shardingsphere.sharding.tables.test_user.database-strategy.inline.algorithm-expression=ds-$->{scope%2+1}
spring.shardingsphere.sharding.tables.test_user.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.test_user.table-strategy.inline.algorithm-expression=test_user_$->{id%4}
##course
spring.shardingsphere.sharding.tables.course.actual-data-nodes=ds-$->{[1,2]}.course_$->{[${my-sharding-table.config}]}
spring.shardingsphere.sharding.tables.course.database-strategy.inline.sharding-column=scope
spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=ds-$->{scope%2+1}
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{id%4}
##course_selected
spring.shardingsphere.sharding.tables.course_selected.actual-data-nodes=ds-$->{1..2}.course_selected_$->{0..3}
spring.shardingsphere.sharding.tables.course_selected.database-strategy.inline.sharding-column=scope
spring.shardingsphere.sharding.tables.course_selected.database-strategy.inline.algorithm-expression=ds-$->{scope%2+1}
spring.shardingsphere.sharding.tables.course_selected.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.course_selected.table-strategy.inline.algorithm-expression=course_selected_$->{user_id%4}
spring.shardingsphere.sharding.binding-tables[0]=test_user,course_selected,course
spring.shardingsphere.sharding.master-slave-rules.ds-1.master-data-source-name=ds-1m
spring.shardingsphere.sharding.master-slave-rules.ds-1.slave-data-source-names[0]=ds-1s1
spring.shardingsphere.sharding.master-slave-rules.ds-1.slave-data-source-names[1]=ds-1s2
spring.shardingsphere.sharding.master-slave-rules.ds-2.master-data-source-name=ds-2m
spring.shardingsphere.sharding.master-slave-rules.ds-2.slave-data-source-names=ds-2s1,ds-2s2
结果
查询会直接根据从库进行负载均衡
2021-01-14 16:24:08.861 INFO 7400 --- [ main] ShardingSphere-SQL : Logic SQL: SELECT a.id,name,b.course_id, CASE a.scope WHEN 0 THEN 100 ELSE 200 END scope FROM test_user a JOIN course_selected b ON a.id = b.user_id
2021-01-14 16:24:08.861 INFO 7400 --- [ main] ShardingSphere-SQL : SQLStatement: SelectStatementContext(super=CommonSQLStatementContext(sqlStatement=org.apache.shardingsphere.sql.parser.sql.statement.dml.SelectStatement@34e990cc, tablesContext=org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext@c82d925), tablesContext=org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext@c82d925, projectionsContext=ProjectionsContext(startIndex=7, stopIndex=76, distinctRow=false, projections=[ColumnProjection(owner=a, name=id, alias=Optional.empty), ColumnProjection(owner=null, name=name, alias=Optional.empty), ColumnProjection(owner=b, name=course_id, alias=Optional.empty), ExpressionProjection(expression=CASEa.scopeWHEN0THEN100ELSE200END, alias=Optional[scope])]), groupByContext=org.apache.shardingsphere.sql.parser.binder.segment.select.groupby.GroupByContext@55c03e4, orderByContext=org.apache.shardingsphere.sql.parser.binder.segment.select.orderby.OrderByContext@14df5253, paginationContext=org.apache.shardingsphere.sql.parser.binder.segment.select.pagination.PaginationContext@605eb072, containsSubquery=false)
2021-01-14 16:24:08.861 INFO 7400 --- [ main] ShardingSphere-SQL : Actual SQL: ds-1s1 ::: SELECT a.id,name,b.course_id, CASE a.scope WHEN 0 THEN 100 ELSE 200 END scope FROM test_user_0 a JOIN course_selected_0 b ON a.id = b.user_id
2021-01-14 16:24:08.861 INFO 7400 --- [ main] ShardingSphere-SQL : Actual SQL: ds-1s2 ::: SELECT a.id,name,b.course_id, CASE a.scope WHEN 0 THEN 100 ELSE 200 END scope FROM test_user_1 a JOIN course_selected_1 b ON a.id = b.user_id
2021-01-14 16:24:08.861 INFO 7400 --- [ main] ShardingSphere-SQL : Actual SQL: ds-1s1 ::: SELECT a.id,name,b.course_id, CASE a.scope WHEN 0 THEN 100 ELSE 200 END scope FROM test_user_2 a JOIN course_selected_2 b ON a.id = b.user_id
2021-01-14 16:24:08.861 INFO 7400 --- [ main] ShardingSphere-SQL : Actual SQL: ds-1s2 ::: SELECT a.id,name,b.course_id, CASE a.scope WHEN 0 THEN 100 ELSE 200 END scope FROM test_user_3 a JOIN course_selected_3 b ON a.id = b.user_id
2021-01-14 16:24:08.861 INFO 7400 --- [ main] ShardingSphere-SQL : Actual SQL: ds-2s1 ::: SELECT a.id,name,b.course_id, CASE a.scope WHEN 0 THEN 100 ELSE 200 END scope FROM test_user_0 a JOIN course_selected_0 b ON a.id = b.user_id
2021-01-14 16:24:08.861 INFO 7400 --- [ main] ShardingSphere-SQL : Actual SQL: ds-2s2 ::: SELECT a.id,name,b.course_id, CASE a.scope WHEN 0 THEN 100 ELSE 200 END scope FROM test_user_1 a JOIN course_selected_1 b ON a.id = b.user_id
2021-01-14 16:24:08.861 INFO 7400 --- [ main] ShardingSphere-SQL : Actual SQL: ds-2s1 ::: SELECT a.id,name,b.course_id, CASE a.scope WHEN 0 THEN 100 ELSE 200 END scope FROM test_user_2 a JOIN course_selected_2 b ON a.id = b.user_id
2021-01-14 16:24:08.861 INFO 7400 --- [ main] ShardingSphere-SQL : Actual SQL: ds-2s2 ::: SELECT a.id,name,b.course_id, CASE a.scope WHEN 0 THEN 100 ELSE 200 END scope FROM test_user_3 a JOIN course_selected_3 b ON a.id = b.user_id
插入 只会使用主库进行操作
2021-01-14 17:10:23.860 INFO 3968 --- [ main] ShardingSphere-SQL : Logic SQL: INSERT INTO course
( id,
`name`,
`scope` ) VALUES
( ?,
?,
? )
2021-01-14 17:10:23.860 INFO 3968 --- [ main] ShardingSphere-SQL : SQLStatement: InsertStatementContext(super=CommonSQLStatementContext(sqlStatement=org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement@6eabe718, tablesContext=org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext@38d525aa), tablesContext=org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext@38d525aa, columnNames=[id, name, scope], insertValueContexts=[InsertValueContext(parametersCount=3, valueExpressions=[ParameterMarkerExpressionSegment(startIndex=59, stopIndex=59, parameterMarkerIndex=0), ParameterMarkerExpressionSegment(startIndex=63, stopIndex=63, parameterMarkerIndex=1), ParameterMarkerExpressionSegment(startIndex=67, stopIndex=67, parameterMarkerIndex=2)], parameters=[1349645022316584961, 测试-7743ee152931455491f6bd9dc20a6cb2, 0])], generatedKeyContext=Optional.empty)
2021-01-14 17:10:23.861 INFO 3968 --- [ main] ShardingSphere-SQL : Actual SQL: ds-1m ::: INSERT INTO course_1
( id,
`name`,
`scope` ) VALUES
(?, ?, ?) ::: [1349645022316584961, 测试-7743ee152931455491f6bd9dc20a6cb2, 0]
2021-01-14 17:10:23.912 INFO 3968 --- [ main] ShardingSphere-SQL : Logic SQL: INSERT INTO test_user
( id,
`name`,
`scope` ) VALUES
( ?,
?,
? )
2021-01-14 17:10:23.912 INFO 3968 --- [ main] ShardingSphere-SQL : SQLStatement: InsertStatementContext(super=CommonSQLStatementContext(sqlStatement=org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement@32e697ac, tablesContext=org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext@3866c96e), tablesContext=org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext@3866c96e, columnNames=[id, name, scope], insertValueContexts=[InsertValueContext(parametersCount=3, valueExpressions=[ParameterMarkerExpressionSegment(startIndex=62, stopIndex=62, parameterMarkerIndex=0), ParameterMarkerExpressionSegment(startIndex=66, stopIndex=66, parameterMarkerIndex=1), ParameterMarkerExpressionSegment(startIndex=70, stopIndex=70, parameterMarkerIndex=2)], parameters=[1349645025416175617, 测试用户-9f56aa0505334005b323cb561293b8b5, 0])], generatedKeyContext=Optional.empty)
2021-01-14 17:10:23.912 INFO 3968 --- [ main] ShardingSphere-SQL : Actual SQL: ds-1m ::: INSERT INTO test_user_1
( id,
`name`,
`scope` ) VALUES
(?, ?, ?) ::: [1349645025416175617, 测试用户-9f56aa0505334005b323cb561293b8b5, 0]
TestUserEntity(id=1349645025416175617, name=测试用户-9f56aa0505334005b323cb561293b8b5, scope=0)
CourseEntity(id=1349645022316584961, name=测试-7743ee152931455491f6bd9dc20a6cb2, scope=0)
2021-01-14 17:10:23.920 INFO 3968 --- [ main] ShardingSphere-SQL : Logic SQL: INSERT INTO course_selected
( id,
user_id,
course_id,
`scope` ) VALUES
( ?,
?,
?,
? )
2021-01-14 17:10:23.920 INFO 3968 --- [ main] ShardingSphere-SQL : SQLStatement: InsertStatementContext(super=CommonSQLStatementContext(sqlStatement=org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement@5aab5b31, tablesContext=org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext@5408d4b3), tablesContext=org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext@5408d4b3, columnNames=[id, user_id, course_id, scope], insertValueContexts=[InsertValueContext(parametersCount=4, valueExpressions=[ParameterMarkerExpressionSegment(startIndex=81, stopIndex=81, parameterMarkerIndex=0), ParameterMarkerExpressionSegment(startIndex=85, stopIndex=85, parameterMarkerIndex=1), ParameterMarkerExpressionSegment(startIndex=89, stopIndex=89, parameterMarkerIndex=2), ParameterMarkerExpressionSegment(startIndex=93, stopIndex=93, parameterMarkerIndex=3)], parameters=[1349645025458118657, 1349645025416175617, 1349645022316584961, 0])], generatedKeyContext=Optional.empty)
2021-01-14 17:10:23.921 INFO 3968 --- [ main] ShardingSphere-SQL : Actual SQL: ds-1m ::: INSERT INTO course_selected_1
( id,
user_id,
course_id,
`scope` ) VALUES
(?, ?, ?, ?) ::: [1349645025458118657, 1349645025416175617, 1349645022316584961, 0]