测试springboot多个数据源配置时,需要安装多个mysql容器,由于资源限制,当前只有一台虚拟机,如果在一台机器上安装多个mysql实例,是可以的,但步骤比较繁琐,使用docker来安装MySQL容器,非常简单,只需要简单几步,对于测试非常方便

dcker部署多个mysql容器

1、下载mysql镜像,这里下载5.7.23版本

docker pull mysql:5.7.

2、启动一个mysql容器

docker run --name db001 -p : -e MYSQL_ROOT_PASSWORD= -d mysql:5.7.

3、启动第二个mysql容器

docker run --name db002 -p : -e MYSQL_ROOT_PASSWORD= -d mysql:5.7.

有时,启动的时候可能会出现以下提示,且容器未正常启动,此时可以重启下docker,然后在启动mysql容器来尝试解决该问题

[root@localhost ~]# docker run --name db002 -p : -e MYSQL_ROOT_PASSWORD= -d mysql 
4e9380ef9bb8d81ac686116ad266377eb0e97ab80ceb6b653cf4c8214f3c9718
/usr/bin/docker-current: Error response from daemon: driver failed programming external connectivity on endpoint mysql_test2 (00ccfe607d64b17f0ee91b426cbe5b814fea70bf110960e8621959d35a9cfc62): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d / --dport -j DNAT --to-destination 172.17.0.3: ! -i docker0: iptables: No chain/target/match by that name.

4、查看mysql容器启动情况

上述映射到mysql容器的端口均为3306,每个容器都有一个小型的linux系统

使用java连接mysql容器

java连接mysql容器与不使用docker容器相比,没有什么不同。只是如果没有使用ssl,则在java连接mysql容器时,需要添加参数useSSL=false,或者配置ssl

spring.datasource.url=jdbc:mysql://192.168.101.116:3307/myapp002?useSSL=false

否则会出现以下异常

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:827) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:447) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:237) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) [HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) [HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-3.2.0.jar:na]
at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:157) [spring-jdbc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:115) [spring-jdbc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:78) [spring-jdbc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:370) [spring-jdbc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:451) [spring-jdbc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:461) [spring-jdbc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.queryForList(JdbcTemplate.java:489) [spring-jdbc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at com.demo.bootdemo.MainBusi.run(MainBusi.java:25) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:780) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:764) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:319) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1214) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1203) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
at com.demo.bootdemo.MultidatasourceApplication.main(MultidatasourceApplication.java:10) [classes/:na]
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_172]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_172]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_172]
at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:1.8.0_172]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.protocol.a.NativeProtocol.negotiateSSLConnection(NativeProtocol.java:340) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.protocol.a.NativeAuthenticationProvider.negotiateSSLConnection(NativeAuthenticationProvider.java:777) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.protocol.a.NativeAuthenticationProvider.proceedHandshakeWithPluggableAuthentication(NativeAuthenticationProvider.java:486) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.protocol.a.NativeAuthenticationProvider.connect(NativeAuthenticationProvider.java:202) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.protocol.a.NativeProtocol.connect(NativeProtocol.java:1348) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.NativeSession.connect(NativeSession.java:163) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:947) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:817) ~[mysql-connector-java-8.0.17.jar:8.0.17]
... 24 common frames omitted
Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateNotYetValidException: NotBefore: Mon Sep 16 23:20:24 CST 2019
at sun.security.ssl.Alerts.getSSLException(Unknown Source) ~[na:1.8.0_172]
at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source) ~[na:1.8.0_172]
at sun.security.ssl.Handshaker.fatalSE(Unknown Source) ~[na:1.8.0_172]
at sun.security.ssl.Handshaker.fatalSE(Unknown Source) ~[na:1.8.0_172]
at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source) ~[na:1.8.0_172]
at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source) ~[na:1.8.0_172]
at sun.security.ssl.Handshaker.processLoop(Unknown Source) ~[na:1.8.0_172]
at sun.security.ssl.Handshaker.process_record(Unknown Source) ~[na:1.8.0_172]
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source) ~[na:1.8.0_172]
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) ~[na:1.8.0_172]
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) ~[na:1.8.0_172]
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) ~[na:1.8.0_172]
at com.mysql.cj.protocol.ExportControlled.performTlsHandshake(ExportControlled.java:316) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.protocol.StandardSocketFactory.performTlsHandshake(StandardSocketFactory.java:188) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.protocol.a.NativeSocketConnection.performTlsHandshake(NativeSocketConnection.java:99) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.protocol.a.NativeProtocol.negotiateSSLConnection(NativeProtocol.java:331) ~[mysql-connector-java-8.0.17.jar:8.0.17]
... 31 common frames omitted
Caused by: java.security.cert.CertificateNotYetValidException: NotBefore: Mon Sep 16 23:20:24 CST 2019
at sun.security.x509.CertificateValidity.valid(Unknown Source) ~[na:1.8.0_172]
at sun.security.x509.X509CertImpl.checkValidity(Unknown Source) ~[na:1.8.0_172]
at sun.security.x509.X509CertImpl.checkValidity(Unknown Source) ~[na:1.8.0_172]
at com.mysql.cj.protocol.ExportControlled$X509TrustManagerWrapper.checkServerTrusted(ExportControlled.java:363) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at sun.security.ssl.AbstractTrustManagerWrapper.checkServerTrusted(Unknown Source) ~[na:1.8.0_172]
... 43 common frames omitted

另外,docker容器启动一定要设置内存大小,根据-m参数指定,单位为b、k、m和g

docker部署多个mysql容器,并使用java连接的更多相关文章

  1. 在docker for windows建立mssql容器后,ssms连接mssql出现错误号码18456的问题

    在docker for windows建立mssql容器后,ssms连接mssql出现错误号码18456的问题 笔者提供一个可能会没考虑到的点. 请检查本机是否安装了mssql!!! 请检查本机的ms ...

  2. docker构建mysql容器及Navicat 远程连接

    1. MySQL部署 1.1拉取MySQL镜像 docker pull mysql 查看镜像 docker images 1.2创建MySQL容器 首先建立所需要的 文件夹: docker run - ...

  3. docker 部署 nginx+php+mysql

    系统:centos7 使用root账户 开放80端口 firewall-cmd --zone=public --add-port=80/tcp --permanent 安装docker yum ins ...

  4. Docker部署tomcat+nginx+mysql,创建一个tomcat集群

    Docker的部署可以选择虚拟机或者云服务器,我自己选择的是本机安装centos7虚拟机: 1.虚拟机安装登录成功后开始部署docker: 首先了解docker是一个开源应用容器引擎,基于Go语言遵循 ...

  5. 如何通过 Docker 部署 Logstash 同步 Mysql 数据库数据到 ElasticSearch

    在开发过程中,我们经常会遇到对业务数据进行模糊搜索的需求,例如电商网站对于商品的搜索,以及内容网站对于内容的关键字检索等等.对于这些高级的搜索功能,显然数据库的 Like 是不合适的,通常我们采用 E ...

  6. Docker学习之搭建MySql容器服务

    描述 MySQL 5.6 SQL数据库服务器Docker镜像,此容器映像包含用于OpenShift的MySQL 5.6 SQL数据库服务器和一般用法.用户可以选择RHEL和基于CentOS的图像.然后 ...

  7. 在docker中创建使用MySQL,并实现远程连接navicat

    在 docker 中使用 mysql 安装完docker之后,在命令行中输入docker images可以查看自己创建的image(安装下载docker的教程很多了,大家需要可以去查一下就可以了)这里 ...

  8. AspNetCore容器化(Docker)部署(三) —— Docker Compose容器编排

    一.前言 上一篇部署了一个最基础的helloworld应用,创建了两个容器和一个network,还算应付得过来. 如果该应用继续引入mysql.redis.job等若干服务,到时候发布一次得工作量之大 ...

  9. docker(部署常见应用):docker部署mysql

    上节回顾:docker(部署常见应用):docker部署nginx docker部署mysql:5.7.26 # 下载镜像 docker pull mysql: # 查看镜像 docker image ...

随机推荐

  1. 微信小程序跳到指定页面不生效

    wx.redirectTo({ url: './index', }) wx.navigateTo({ url: './index', }) 用了这两种都没生效,点击没跳转,也没报错 后来想了下是不是因 ...

  2. vue关于路由容易忽略的点

    1.去掉导航里的# 在router.js中 export default new Router{ mode:'history' } 2.指定激活项的class 在router.js中 export d ...

  3. ubuntu安装交叉编译器

    # sudo apt-get install gcc-arm-linux-gnueabi # sudo apt-get install g++-arm-linux-gnueabi 官方下载 https ...

  4. Redis和MemCache静态Map做缓存区别

    本地缓存和分布式缓存 本地缓存:使用自带的map或者guava实现的是本地缓存,最主要的特点是轻量以及快速,生命周期随着jvm的销毁而结束,并且在多实例的情况下,每个实例都需要各自保存一份缓存,缓存不 ...

  5. 在cmd下import cv2报错——OpenCV实现BRISK

    平台:win10 x64 +JetBrains PyCharm 2018.2.4 x64 +Anaconda3(python3.7.0+opencv3.4.5) Issue说明:同学发了个python ...

  6. Django项目上线的准备工作

    settings文件配置 添加上STATIC_ROOT = os.path.join(BASE_DIR, "/static/") 我的配置项目文件的最终 STATIC_URL = ...

  7. HLS manifest standard 翻译: HTTP Live Streaming draft-pantos-http-live-streaming-23

    我为什么要干这种事 Introduction to HTTP Live Streaming 1 OVerview Multimedia presentation : specified by a Un ...

  8. Java中Static关键字详解以及静态变量和成员变量的区别

    一.static关键字的特点 (1)修饰成员变量.成员方法(2)随着类的加载而加载(3)优先于对象存在(4)被所有对象共享(5)可以通过类名调用 它本身也可以通过对象名调用 例如:main()方法由j ...

  9. Django:常用字段、手动自动第三张表单、元信息

    一.常用字段和非常用字段 二.手动,自动创建第三张表 三.元信息 四.defer和only 一.常用字段和非常用字段 -常用字段 AutoField int自增列,必须填入参数 primary_key ...

  10. easyui tree选中指定节点,点击指定节点

    功能需求描述如下: A主页面,在datagrid的某行上,操作列,点击详情,Tab页面上加载B页面 B页面,左边是树tree,右边是左边树的详情列表 要求:由A页面链接到B页面,B页面的tree,默认 ...