从容器到容器连接到mysql时出错

时间:2019-06-15 12:21:34

标签: docker slick

从一个容器中的scala / slick应用程序连接到另一个容器中的mysql时,我遇到了一些困难。

这是我运行docker容器的方式:

// MySQL
docker container run --publish 3306:3306  --name mysqlDb -e MYSQL_ROOT_PASSWORD=123123 -d mysql

// Scala/slick app
docker container run -v /Users/userHome/Sources/slick-dev-box:/home --expose=3306 -it --link mysqlDb:mysql hseeberger/scala-sbt

$ docker ps -a
CONTAINER ID        IMAGE                  COMMAND                  CREATED              STATUS                         PORTS                               NAMES
f12b46821f69        hseeberger/scala-sbt   "bash"                   About a minute ago   Up About a minute              3306/tcp                            gracious_engelbart
afcd1902b741        mysql                  "docker-entrypoint.s…"   About an hour ago    Up 37 minutes                  0.0.0.0:3306->3306/tcp, 33060/tcp   mysqlDb

application.conf

mysql = {
  profile = "slick.jdbc.MySQLProfile$"
  dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"
  properties = {
    driver = "com.mysql.cj.jdbc.Driver"
    url  = "jdbc:mysql://172.17.0.2:3360/slick"
    user = "root"
    password = "123123"
  }
}

Error I am getting:

warn] There may be incompatibilities among your library dependencies; run 'evicted' to see detailed eviction warnings.
[info] Running com.avelimited.slick.demo.Runner
[error] (run-main-0) java.sql.SQLTransientConnectionException: mysql - Connection is not available, request timed out after 30005ms.
[error] java.sql.SQLTransientConnectionException: mysql - Connection is not available, request timed out after 30005ms.
[error]     at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:676)
[error]     at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:190)
[error]     at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:155)
[error]     at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:100)
[error]     at slick.jdbc.hikaricp.HikariCPJdbcDataSource.createConnection(HikariCPJdbcDataSource.scala:14)
[error]     at slick.jdbc.JdbcBackend$BaseSession.<init>(JdbcBackend.scala:494)
[error]     at slick.jdbc.JdbcBackend$DatabaseDef.createSession(JdbcBackend.scala:46)
[error]     at slick.jdbc.JdbcBackend$DatabaseDef.createSession(JdbcBackend.scala:37)
[error]     at slick.basic.BasicBackend$DatabaseDef.acquireSession(BasicBackend.scala:250)
[error]     at slick.basic.BasicBackend$DatabaseDef.acquireSession$(BasicBackend.scala:249)
[error]     at slick.jdbc.JdbcBackend$DatabaseDef.acquireSession(JdbcBackend.scala:37)
[error]     at slick.basic.BasicBackend$DatabaseDef$$anon$3.run(BasicBackend.scala:275)
[error]     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[error]     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
[error]     at java.lang.Thread.run(Thread.java:748)
[error] Caused by: java.lang.NullPointerException
[error]     at slick.jdbc.DriverDataSource.getConnection(DriverDataSource.scala:101)
[error]     at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369)
[error]     at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198)
[error]     at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467)
[error]     at com.zaxxer.hikari.pool.HikariPool.access$100(HikariPool.java:71)
[error]     at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:706)
[error]     at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:692)
[error]     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[error]     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[error]     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
[error]     at java.lang.Thread.run(Thread.java:748)
[error] Nonzero exit code: 1

到目前为止我尝试过的是: 我尝试使用不同的端口,例如3360和33060,但结果相同。 从网络上看,当我从应用程序容器运行ping时,我从mysql容器得到响应,而当我在应用程序容器上运行cat / etc / hosts时:

root@f12b46821f69:/home# cat /etc/hosts
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2  mysql afcd1902b741 mysqlDb
172.17.0.3  f12b46821f69

请告诉我是否还有其他方法可以解决连接问题。

1 个答案:

答案 0 :(得分:0)

好吧,显然问题出在myql-connector驱动程序上, 这是我必须应用的修复程序:

// Before
libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.34"

// After
libraryDependencies += "mysql" % "mysql-connector-java" % "8.0.11"