1、docker安装mysql5.7版本
- 拉取mysql的镜像
docker pull mysql:5.7
- 创建mysql的配置目录,日志目录,数据存储的目录
mkdir -p /home/sunyuhua/docker/mysql/conf
mkdir -p /home/sunyuhua/docker/mysql/logs
mkdir -p /home/sunyuhua/docker/mysql/data
- 启动mysql的容器
docker run -p 3306:3306 --name mysql --restart=always -v /home/sunyuhua/docker/mysql/conf:/etc/mysql/conf.d -v /home/sunyuhua/docker/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
- 修改 mysql的配置
/home/sunyuhua/docker/mysql/conf/my.cnf
[client]
port =3306
socket =/var/run/mysqld/mysqld.sock
[mysql]
socket =/var/run/mysqld/mysqld.sock
[mysql_safe]
socket =/var/run/mysqld/mysqld.sock
nice =0
[mysqld]
server-id = 1
datadir = /var/lib/mysql
socket = /var/run/mysqld/mysqld.sock
port = 3306
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
log-bin=mysql-bin
binlog-format=row
binlog-row-image=full
max_connections=600
- 重新mysql
docker restart mysql
- 链接mysql,测试
docker exec -it mysql /bin/bash
mysql -uroot -proot
2、docker 安装zookeeper
- 拉取zookeeper镜像
docker pull zookeeper
- 运行zookeeper
docker run -d -e TZ="Asia/Shanghai" -p 2181:2181 -v /home/sunyuhua/docker/zookeeper:/data --name zookeeper --restart always zookeeper
- 检查zookeeper安装成功
docker exec -it zookeeper bash
./bin/zkCli.sh
3、docker安装shardingsphere-proxy
- 获取镜像中的配置到挂载目录下
docker run -d --name tmp --entrypoint=bash apache/shardingsphere-proxy
docker cp tmp:/opt/shardingsphere-proxy/conf /home/sunyuhua/docker/shardingsphere-proxy/conf
docker rm tmp
- 启动shardingsphere-proxy
docker run -d -v /home/sunyuhua/docker/shardingsphere-proxy/conf:/opt/shardingsphere-proxy/conf -v /home/sunyuhua/docker/shardingsphere-proxy/ext-lib:/opt/shardingsphere-proxy/ext-lib -v /home/sunyuhua/docker/shardingsphere-proxy/logs:/opt/shardingsphere-proxy/logs -e PORT=3308 -p3308:3308 --restart always --name sharding-proxy apache/shardingsphere-proxy
- 验证是否安装成功
docker exec -it sharding-proxy /bin/bash
4、配置分库分表的准备mysql的表结构
create database demo_ds_0;
show databases;
use demo_ds_0;
CREATE TABLE `t_order_0` (
`order_id` bigint(20) unsigned NOT NULL COMMENT '主键ID',
`user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
`total_money` int(10) unsigned NOT NULL COMMENT '订单总金额',
PRIMARY KEY (`order_id`),
KEY `idx_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单总表';
CREATE TABLE `t_order_1` (
`order_id` bigint(20) unsigned NOT NULL COMMENT '主键ID',
`user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
`total_money` int(10) unsigned NOT NULL COMMENT '订单总金额',
PRIMARY KEY (`order_id`),
KEY `idx_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单总表';
CREATE TABLE `t_order_item_0` (
`order_item_id` bigint(20) unsigned NOT NULL COMMENT '子订单ID',
`order_id` bigint(20) unsigned NOT NULL COMMENT '主键ID',
`user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
`money` int(10) unsigned NOT NULL COMMENT '子订单金额',
PRIMARY KEY (`order_item_id`),
KEY `idx_order_id` (`order_id`) USING BTREE,
KEY `idx_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单子表';
CREATE TABLE `t_order_item_1` (
`order_item_id` bigint(20) unsigned NOT NULL COMMENT '子订单ID',
`order_id` bigint(20) unsigned NOT NULL COMMENT '主键ID',
`user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
`money` int(10) unsigned NOT NULL COMMENT '子订单金额',
PRIMARY KEY (`order_item_id`),
KEY `idx_order_id` (`order_id`) USING BTREE,
KEY `idx_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单子表';
create database demo_ds_1;
show databases;
use demo_ds_1;
CREATE TABLE `t_order_0` (
`order_id` bigint(20) unsigned NOT NULL COMMENT '主键ID',
`user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
`total_money` int(10) unsigned NOT NULL COMMENT '订单总金额',
PRIMARY KEY (`order_id`),
KEY `idx_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单总表';
CREATE TABLE `t_order_1` (
`order_id` bigint(20) unsigned NOT NULL COMMENT '主键ID',
`user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
`total_money` int(10) unsigned NOT NULL COMMENT '订单总金额',
PRIMARY KEY (`order_id`),
KEY `idx_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单总表';
CREATE TABLE `t_order_item_0` (
`order_item_id` bigint(20) unsigned NOT NULL COMMENT '子订单ID',
`order_id` bigint(20) unsigned NOT NULL COMMENT '主键ID',
`user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
`money` int(10) unsigned NOT NULL COMMENT '子订单金额',
PRIMARY KEY (`order_item_id`),
KEY `idx_order_id` (`order_id`) USING BTREE,
KEY `idx_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单子表';
CREATE TABLE `t_order_item_1` (
`order_item_id` bigint(20) unsigned NOT NULL COMMENT '子订单ID',
`order_id` bigint(20) unsigned NOT NULL COMMENT '主键ID',
`user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
`money` int(10) unsigned NOT NULL COMMENT '子订单金额',
PRIMARY KEY (`order_item_id`),
KEY `idx_order_id` (`order_id`) USING BTREE,
KEY `idx_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单子表';
5、配置shardingsphere-proxy分库分表的策略
修改server.xml文件,文件地址:/home/sunyuhua/docker/shardingsphere-proxy/conf
mode:
type: Cluster
repository:
type: ZooKeeper
props:
namespace: governance_ds
server-lists: 127.0.0.1:2181
retryIntervalMilliseconds: 500
timeToLiveSeconds: 60
maxRetries: 3
operationTimeoutMilliseconds: 500
# overwrite: false
#
rules:
- !AUTHORITY
users:
- root@%:root
- sharding@:sharding
provider:
type: ALL_PRIVILEGES_PERMITTED
# - !TRANSACTION
# defaultType: XA
# providerType: Atomikos
props:
# max-connections-size-per-query: 1
# kernel-executor-size: 16 # Infinite by default.
# proxy-frontend-flush-threshold: 128 # The default value is 128.
# proxy-opentracing-enabled: false
# proxy-hint-enabled: false
sql-show: true
# check-table-metadata-enabled: false
# show-process-list-enabled: false
# # Proxy backend query fetch size. A larger value may increase the memory usage of ShardingSphere Proxy.
# # The default value is -1, which means set the minimum value for different JDBC drivers.
# proxy-backend-query-fetch-size: -1
# check-duplicate-table-enabled: false
# sql-comment-parse-enabled: false
# proxy-frontend-executor-size: 0 # Proxy frontend executor size. The default value is 0, which means let Netty decide.
# # Available options of proxy backend executor suitable: OLAP(default), OLTP. The OLTP option may reduce time cost of writing packets to client, but it may increase the latency of SQL execution
# # if client connections are more than proxy-frontend-netty-executor-size, especially executing slow SQL.
# proxy-backend-executor-suitable: OLAP
# proxy-frontend-max-connections: 0 # Less than or equal to 0 means no limitation.
# sql-federation-enabled: false
修改config-sharding.yaml 文件,文件地址/home/sunyuhua/docker/shardingsphere-proxy/conf
schemaName: sharding_db
dataSources:
ds_0:
url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
ds_1:
url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
rules:
- !SHARDING
tables:
t_order:
actualDataNodes: ds_${0..1}.t_order_${0..1}
tableStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: t_order_inline
keyGenerateStrategy:
column: order_id
keyGeneratorName: snowflake
t_order_item:
actualDataNodes: ds_${0..1}.t_order_item_${0..1}
tableStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: t_order_item_inline
keyGenerateStrategy:
column: order_item_id
keyGeneratorName: snowflake
bindingTables:
- t_order,t_order_item
defaultDatabaseStrategy:
standard:
shardingColumn: user_id
shardingAlgorithmName: database_inline
defaultTableStrategy:
none:
shardingAlgorithms:
database_inline:
type: INLINE
props:
algorithm-expression: ds_${user_id % 2}
t_order_inline:
type: INLINE
props:
algorithm-expression: t_order_${order_id % 2}
t_order_item_inline:
type: INLINE
props:
algorithm-expression: t_order_item_${order_id % 2}
keyGenerators:
snowflake:
type: SNOWFLAKE
props:
worker-id: 123
重新启动shardingsphere
docker restart sharding-proxy
检查shardingsphere-proxy是不是启动
docker logs sharding-proxy
或者查看日志
/opt/shardingsphere-proxy/logs/stdout.log
[INFO ] 2023-06-28 05:46:54.015 [main-SendThread(host.docker.internal:2181)] org.apache.zookeeper.ClientCnxn - Session establishment complete on server host.docker.internal/192.168.65.2:2181, session id = 0x100000045ca0001, negotiated timeout = 40000
[INFO ] 2023-06-28 05:46:54.024 [main-EventThread] o.a.c.f.state.ConnectionStateManager - State change: CONNECTED
[INFO ] 2023-06-28 05:46:54.052 [main-EventThread] o.a.c.framework.imps.EnsembleTracker - New config event received: {}
[INFO ] 2023-06-28 05:46:54.052 [main-EventThread] o.a.c.framework.imps.EnsembleTracker - New config event received: {}
Thanks for using Atomikos! Evaluate http://www.atomikos.com/Main/ExtremeTransactions for advanced features and professional support
or register at http://www.atomikos.com/Main/RegisterYourDownload to disable this message and receive FREE tips & advice
[INFO ] 2023-06-28 05:46:56.075 [main] o.apache.curator.utils.Compatibility - Using org.apache.zookeeper.server.quorum.MultipleAddresses
[INFO ] 2023-06-28 05:46:56.093 [main] o.a.s.p.i.BootstrapInitializer - Database name is `MySQL`, version is `5.7.42-log`
[INFO ] 2023-06-28 05:46:56.283 [main] o.a.s.p.frontend.ShardingSphereProxy - ShardingSphere-Proxy start success
6、插入和查询数据,检查分库分表策略是不是生效
7、检查物理库是不是已经插入成功
错误汇总:
1、如果出现zookeeper 2181端口链接不上,是因为docker容器之间的网络通信问题
需要修改server.xml 中 zookeeper的链接地址
[WARN ] 2023-06-28 01:53:18.091 [main-SendThread(127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn - Session 0x0 for sever localhost/127.0.0.1:2181, Closing socket connection. Attempting reconnect except it is a SessionExpiredException.
java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:716)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:342)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1262)
[INFO ] 2023-06-28 01:53:19.194 [main-SendThread(127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn - Opening socket connection to server localhost/127.0.0.1:2181.
[INFO ] 2023-06-28 01:53:19.194 [main-SendThread(127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn - SASL config status: Will not attempt to authenticate using SASL (unknown error)
[WARN ] 2023-06-28 01:53:19.195 [main-SendThread(127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn - Session 0x0 for sever localhost/127.0.0.1:2181, Closing socket connection. Attempting reconnect except it is a SessionExpiredException.
java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:716)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:342)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1262)
[INFO ] 2023-06-28 01:53:19.592 [Curator-Framework-0] o.a.c.f.imps.CuratorFrameworkImpl - backgroundOperationsLoop exiting
[INFO ] 2023-06-28 01:53:20.296 [main-SendThread(127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn - Opening socket connection to server localhost/127.0.0.1:2181.
[INFO ] 2023-06-28 01:53:20.296 [main-SendThread(127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn - SASL config status: Will not attempt to authenticate using SASL (unknown error)
[WARN ] 2023-06-28 01:53:20.297 [main-SendThread(127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn - An exception was thrown while closing send thread for session 0x0.
java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:716)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:342)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1262)
[INFO ] 2023-06-28 01:53:20.400 [main] org.apache.zookeeper.ZooKeeper - Session: 0x0 closed
[INFO ] 2023-06-28 01:53:20.400 [main-EventThread] org.apache.zookeeper.ClientCnxn - EventThread shut down for session: 0x0
Exception in thread "main" org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryException: org.apache.zookeeper.KeeperException$OperationTimeoutException: KeeperErrorCode = OperationTimeout
at org.apache.shardingsphere.mode.repository.cluster.zookeeper.handler.CuratorZookeeperExceptionHandler.handleException(CuratorZookeeperExceptionHandler.java:51)
at org.apache.shardingsphere.mode.repository.cluster.zookeeper.CuratorZookeeperRepository.initCuratorClient(CuratorZookeeperRepository.java:128)
at org.apache.shardingsphere.mode.repository.cluster.zookeeper.CuratorZookeeperRepository.init(CuratorZookeeperRepository.java:82)
at org.apache.shardingsphere.mode.manager.cluster.ClusterContextManagerBuilder.createClusterPersistRepository(ClusterContextManagerBuilder.java:119)
at org.apache.shardingsphere.mode.manager.cluster.ClusterContextManagerBuilder.beforeBuildContextManager(ClusterContextManagerBuilder.java:94)
at org.apache.shardingsphere.mode.manager.cluster.ClusterContextManagerBuilder.build(ClusterContextManagerBuilder.java:84)
at org.apache.shardingsphere.proxy.initializer.BootstrapInitializer.initContext(BootstrapInitializer.java:77)
at org.apache.shardingsphere.proxy.initializer.BootstrapInitializer.init(BootstrapInitializer.java:68)
at org.apache.shardingsphere.proxy.Bootstrap.main(Bootstrap.java:47)
Caused by: org.apache.zookeeper.KeeperException$OperationTimeoutException: KeeperErrorCode = OperationTimeout
at org.apache.shardingsphere.mode.repository.cluster.zookeeper.CuratorZookeeperRepository.initCuratorClient(CuratorZookeeperRepository.java:125)
... 7 more
[ERROR] 2023-06-28 05:38:44.350 [main] com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Exception during pool initialization.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:342)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2197)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2230)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2025)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:778)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:354)
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:202)
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473)
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:554)
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81)
at org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.decorator.HikariJDBCParameterDecorator.decorate(HikariJDBCParameterDecorator.java:49)
at org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.decorator.HikariJDBCParameterDecorator.decorate(HikariJDBCParameterDecorator.java:30)
at org.apache.shardingsphere.infra.config.datasource.JDBCParameterDecoratorHelper.decorate(JDBCParameterDecoratorHelper.java:41)
at org.apache.shardingsphere.infra.config.datasource.DataSourceConverter.getDataSource(DataSourceConverter.java:48)
at org.apache.shardingsphere.infra.config.datasource.DataSourceConverter.lambda$getDataSourceMap$0(DataSourceConverter.java:68)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1321)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:566)
at org.apache.shardingsphere.infra.config.datasource.DataSourceConverter.getDataSourceMap(DataSourceConverter.java:68)
at org.apache.shardingsphere.proxy.initializer.BootstrapInitializer.getDataSourcesMap(BootstrapInitializer.java:86)
at org.apache.shardingsphere.proxy.initializer.BootstrapInitializer.initContext(BootstrapInitializer.java:76)
at org.apache.shardingsphere.proxy.initializer.BootstrapInitializer.init(BootstrapInitializer.java:68)
at org.apache.shardingsphere.proxy.Bootstrap.main(Bootstrap.java:47)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:607)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:301)
... 38 common frames omitted