测试容器数据库问题
我在使用 testcontainers 时遇到 db2 问题。我不断收到连接拒绝错误。
当使用以下命令运行 db2 时:
docker run
我能够与 dbvis 连接。- 使用fabric8 maven插件启动db2容器,我再次能够与dbvis连接
我在junit5测试中放置了一个断点并尝试访问db2,但我收到连接被拒绝。
我的 db2 testcontainers 配置:
@Testcontainers
public class ArchiveTest {
@Container
private static final Db2Container DB2 = new Db2Container("ibmcom/db2:11.5.7.0").withPrivilegedMode(true)
.acceptLicense().withUsername("db2inst1").withPassword("password").withDatabaseName("BPMF")
.withEnv("ARCHIVE_LOGS", "false").withEnv("PERSISTENT_HOME", "false");
来自 docker 的 db2 日志:
(*) Previous setup has not been detected. Creating the users...
(*) Creating users ...
(*) Creating instance ...
DB2 installation is being initialized.
Total estimated time for all tasks to be performed: 309 second(s)
Total number of tasks to be performed: 4
Estimated time 1 second(s)
Description: Setting default global profile registry variables
Task #1 start
Task #1 end
Estimated time 5 second(s)
Description: Initializing instance list
Task #2 start
Task #2 end
Estimated time 300 second(s)
Description: Configuring DB2 instances
Task #3 start
Task #3 end
The execution completed successfully.
Task #4 end
Estimated time 3 second(s)
Description: Updating global profile registry
Task #4 start
For more information see the DB2 installation log at "/tmp/db2icrt.log.72".
(*) Fixing /etc/services file for DB2 ...
DBI1070I Program db2icrt completed successfully.
DBI1446I The db2icrt command is running.
chown: cannot access '/database/config/db2inst1/sqllib/adm/fencedid': No such file or directory
03/16/2022 10:26:18 0 0 SQL1032N No start database manager command was issued.
SQL1032N No start database manager command was issued. SQLSTATE=57019
(*) Cataloging existing databases
(*) Applying Db2 license ...
ls: cannot access /database/data/db2inst1/NODE0000: No such file or directory
LIC1426I This product is now licensed for use as outlined in your License Agreement. USE OF THE PRODUCT CONSTITUTES ACCEPTANCE OF THE TERMS OF THE IBM LICENSE AGREEMENT, LOCATED IN THE FOLLOWING DIRECTORY: "/opt/ibm/db2/V11.5/license/en_US.iso88591"
LIC1402I License added successfully.
(*) Updating DBM CFG parameters ...
(*) Saving the checksum of the current nodelock file ...
successfully.
DB20000I The UPDATE DATABASE MANAGER CONFIGURATION command completed
successfully.
DB20000I The UPDATE DATABASE MANAGER CONFIGURATION command completed
successfully.
DB20000I The UPDATE DATABASE MANAGER CONFIGURATION command completed
(*) Remounting /database with suid...
No Cgroup memory limit detected, instance memory will follow automatic tuning
(*) Nothing appears in the Db2 directory. will skip update/upgrade.
(*) Code level is the same. No update/upgrade needed.
DB2 State : Operable
Starting DB2...
DB2 has not been started
SQL1063N DB2START processing was successful.
03/16/2022 10:26:29 0 0 SQL1063N DB2START processing was successful.
(*) Creating database BPMF ...
(*) User chose to create BPMF database
DB20000I The CREATE DATABASE command completed successfully.
DB20000I The ACTIVATE DATABASE command completed successfully.
(*) Instance and database will not be auto configured. AUTOCONFIG has been set to false.
(*) Log archiving will not be configured as ARCHIVE_LOGS has been set to false.
(*) Skipping TEXT_SEARCH setup for database BPMF because TEXT_SEARCH is not configured for the instance ...
ssh-keygen: generating new host keys: RSA1 RSA DSA ECDSA ED25519
(*) All databases are now active.
(*) Setup has completed.
2022-03-16 12:27:28 | INFO | [main] d.5.7.0]:503 - Container ibmcom/db2:11.5.7.0 started in PT1M27.212S
来自 java 的错误是:
Caused by: com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][4.25.13] Exception java.net.ConnectException: Error opening socket to server /127.0.0.1 on port 50,000 with message: Connection refused: connect. ERRORCODE=-4499, SQLSTATE=08001
at com.ibm.db2.jcc.am.b6.a(b6.java:338)
at com.ibm.db2.jcc.am.b6.a(b6.java:435)
at com.ibm.db2.jcc.t4.a0.a(a0.java:445)
at com.ibm.db2.jcc.t4.a0.<init>(a0.java:96)
at com.ibm.db2.jcc.t4.a.b(a.java:366)
at com.ibm.db2.jcc.t4.b.newAgent_(b.java:2148)
at com.ibm.db2.jcc.am.Connection.initConnection(Connection.java:839)
at com.ibm.db2.jcc.am.Connection.<init>(Connection.java:784)
at com.ibm.db2.jcc.t4.b.<init>(b.java:350)
at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:233)
at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:200)
at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:471)
at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:113)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at org.osjava.datasource.SJDataSource.getConnection(SJDataSource.java:115)
at org.osjava.datasource.SJDataSource.getConnection(SJDataSource.java:106)
at org.osjava.datasource.SJDataSource.getConnection(SJDataSource.java:88)
at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:48)
... 105 common frames omitted
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
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.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:607)
at com.ibm.db2.jcc.t4.x.run(x.java:49)
at java.security.AccessController.doPrivileged(Native Method)
at com.ibm.db2.jcc.t4.a0.a(a0.java:431)
... 121 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'migration' threw exception; nested exception is org.flywaydb.core.internal.exception.FlywaySqlException: Unable to obtain connection from database: [jcc][t4][2043][11550][4.25.13] Exception java.net.ConnectException: Error opening socket to server /127.0.0.1 on port 50,000 with message: Connection refused: connect. ERRORCODE=-4499, SQLSTATE=08001
我已经确认我的 JDBC 参数是正确的...所以我对出错的地方有点茫然。
编辑1:db2正在运行:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7046334e6c8 ibmcom/db2:11.5.0.0a "/var/db2_setup/lib/…" About a minute ago Up About a minute 22/tcp, 55000/tcp, 60006-60007/tcp, 0.0.0.0:53444->50000/tcp wizardly_cartwright
ccfe6845bfb1 testcontainers/ryuk:0.3.3 "/app" About a minute ago Up About a minute 0.0.0.0:53439->8080/tcp testcontainers-ryuk-99222438-9340-47ca-b6d2-0a13bfe50f9d
编辑2:docker-for-java命令参数:
AbstrDockerCmd:34 - Cmd: org.testcontainers.shaded.com.github.dockerjava.core.command.CreateContainerCmdImpl@7df60067[name=<null>,hostName=<null>,domainName=<null>,user=<null>,attachStdin=<null>,attachStdout=<null>,attachStderr=<null>,portSpecs=<null>,tty=<null>,stdinOpen=<null>,stdInOnce=<null>,env={DB2INSTANCE=db2inst1,AUTOCONFIG=false,ARCHIVE_LOGS=false,DB2INST1_PASSWORD=password,PERSISTENT_HOME=false,DBNAME=BPMF,LICENSE=accept},cmd={},healthcheck=<null>,argsEscaped=<null>,entrypoint=<null>,image=ibmcom/db2:11.5.0.0a,volumes=Volumes(volumes=[]),workingDir=<null>,macAddress=<null>,onBuild=<null>,networkDisabled=<null>,exposedPorts=ExposedPorts(exposedPorts=[50000/tcp]),stopSignal=<null>,stopTimeout=<null>,hostConfig=HostConfig(binds=[], blkioWeight=null, blkioWeightDevice=null, blkioDeviceReadBps=null, blkioDeviceWriteBps=null, blkioDeviceReadIOps=null, blkioDeviceWriteIOps=null, memorySwappiness=null, nanoCPUs=null, capAdd=null, capDrop=null, containerIDFile=null, cpuPeriod=null, cpuRealtimePeriod=null, cpuRealtimeRuntime=null, cpuShares=null, cpuQuota=null, cpusetCpus=null, cpusetMems=null, devices=null, deviceCgroupRules=null, deviceRequests=null, diskQuota=null, dns=null, dnsOptions=null, dnsSearch=null, extraHosts=[], groupAdd=null, ipcMode=null, cgroup=null, links=[], logConfig=LogConfig(type=null, config=null), lxcConf=null, memory=null, memorySwap=null, memoryReservation=null, kernelMemory=null, networkMode=null, oomKillDisable=null, init=null, autoRemove=null, oomScoreAdj=null, portBindings={50000/tcp=[Lcom.github.dockerjava.api.model.Ports$Binding;@393881f0}, privileged=true, publishAllPorts=null, readonlyRootfs=null, restartPolicy=null, ulimits=null, cpuCount=null, cpuPercent=null, ioMaximumIOps=null, ioMaximumBandwidth=null, volumesFrom=[], mounts=null, pidMode=null, isolation=null, securityOpts=null, storageOpt=null, cgroupParent=null, volumeDriver=null, shmSize=null, pidsLimit=null, runtime=null, tmpFs=null, utSMode=null, usernsMode=null, sysctls=null, consoleSize=null),labels={org.testcontainers=true, org.testcontainers.sessionId=090442b0-8cc4-4f6e-b07e-1afdfed5ec15},shell=<null>,networkingConfig=<null>,ipv4Address=<null>,ipv6Address=<null>,aliases=<null>,authConfig=<null>,platform=<null>]
由于我正在使用smplie-jndi并且属性文件中有JDBC参数,因此JDBC URL的端口不是50000。忙着寻找如何将其设置为DB2Container 类中指定了默认的特定端口
编辑 1:文档中提到了
Note that this exposed port number is from the perspective of the container.
From the host's perspective Testcontainers actually exposes this on a random free port. This is by design, to avoid port collisions that may arise with locally running software or in between parallel test runs.
I am having an issue with db2 using testcontainers. I keep receiving a connection refused error.
When running db2 with:
docker run
I am able to connect with dbvis.- using fabric8 maven plugin to start the db2 container and again I am able to connect with dbvis
I put a breakpoint in the junit5 test and attempt access db2 and I receive the connection refused.
My db2 testcontainers configuration:
@Testcontainers
public class ArchiveTest {
@Container
private static final Db2Container DB2 = new Db2Container("ibmcom/db2:11.5.7.0").withPrivilegedMode(true)
.acceptLicense().withUsername("db2inst1").withPassword("password").withDatabaseName("BPMF")
.withEnv("ARCHIVE_LOGS", "false").withEnv("PERSISTENT_HOME", "false");
The db2 logs from docker:
(*) Previous setup has not been detected. Creating the users...
(*) Creating users ...
(*) Creating instance ...
DB2 installation is being initialized.
Total estimated time for all tasks to be performed: 309 second(s)
Total number of tasks to be performed: 4
Estimated time 1 second(s)
Description: Setting default global profile registry variables
Task #1 start
Task #1 end
Estimated time 5 second(s)
Description: Initializing instance list
Task #2 start
Task #2 end
Estimated time 300 second(s)
Description: Configuring DB2 instances
Task #3 start
Task #3 end
The execution completed successfully.
Task #4 end
Estimated time 3 second(s)
Description: Updating global profile registry
Task #4 start
For more information see the DB2 installation log at "/tmp/db2icrt.log.72".
(*) Fixing /etc/services file for DB2 ...
DBI1070I Program db2icrt completed successfully.
DBI1446I The db2icrt command is running.
chown: cannot access '/database/config/db2inst1/sqllib/adm/fencedid': No such file or directory
03/16/2022 10:26:18 0 0 SQL1032N No start database manager command was issued.
SQL1032N No start database manager command was issued. SQLSTATE=57019
(*) Cataloging existing databases
(*) Applying Db2 license ...
ls: cannot access /database/data/db2inst1/NODE0000: No such file or directory
LIC1426I This product is now licensed for use as outlined in your License Agreement. USE OF THE PRODUCT CONSTITUTES ACCEPTANCE OF THE TERMS OF THE IBM LICENSE AGREEMENT, LOCATED IN THE FOLLOWING DIRECTORY: "/opt/ibm/db2/V11.5/license/en_US.iso88591"
LIC1402I License added successfully.
(*) Updating DBM CFG parameters ...
(*) Saving the checksum of the current nodelock file ...
successfully.
DB20000I The UPDATE DATABASE MANAGER CONFIGURATION command completed
successfully.
DB20000I The UPDATE DATABASE MANAGER CONFIGURATION command completed
successfully.
DB20000I The UPDATE DATABASE MANAGER CONFIGURATION command completed
(*) Remounting /database with suid...
No Cgroup memory limit detected, instance memory will follow automatic tuning
(*) Nothing appears in the Db2 directory. will skip update/upgrade.
(*) Code level is the same. No update/upgrade needed.
DB2 State : Operable
Starting DB2...
DB2 has not been started
SQL1063N DB2START processing was successful.
03/16/2022 10:26:29 0 0 SQL1063N DB2START processing was successful.
(*) Creating database BPMF ...
(*) User chose to create BPMF database
DB20000I The CREATE DATABASE command completed successfully.
DB20000I The ACTIVATE DATABASE command completed successfully.
(*) Instance and database will not be auto configured. AUTOCONFIG has been set to false.
(*) Log archiving will not be configured as ARCHIVE_LOGS has been set to false.
(*) Skipping TEXT_SEARCH setup for database BPMF because TEXT_SEARCH is not configured for the instance ...
ssh-keygen: generating new host keys: RSA1 RSA DSA ECDSA ED25519
(*) All databases are now active.
(*) Setup has completed.
2022-03-16 12:27:28 | INFO | [main] d.5.7.0]:503 - Container ibmcom/db2:11.5.7.0 started in PT1M27.212S
The error from java is:
Caused by: com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][4.25.13] Exception java.net.ConnectException: Error opening socket to server /127.0.0.1 on port 50,000 with message: Connection refused: connect. ERRORCODE=-4499, SQLSTATE=08001
at com.ibm.db2.jcc.am.b6.a(b6.java:338)
at com.ibm.db2.jcc.am.b6.a(b6.java:435)
at com.ibm.db2.jcc.t4.a0.a(a0.java:445)
at com.ibm.db2.jcc.t4.a0.<init>(a0.java:96)
at com.ibm.db2.jcc.t4.a.b(a.java:366)
at com.ibm.db2.jcc.t4.b.newAgent_(b.java:2148)
at com.ibm.db2.jcc.am.Connection.initConnection(Connection.java:839)
at com.ibm.db2.jcc.am.Connection.<init>(Connection.java:784)
at com.ibm.db2.jcc.t4.b.<init>(b.java:350)
at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:233)
at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:200)
at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:471)
at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:113)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at org.osjava.datasource.SJDataSource.getConnection(SJDataSource.java:115)
at org.osjava.datasource.SJDataSource.getConnection(SJDataSource.java:106)
at org.osjava.datasource.SJDataSource.getConnection(SJDataSource.java:88)
at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:48)
... 105 common frames omitted
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
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.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:607)
at com.ibm.db2.jcc.t4.x.run(x.java:49)
at java.security.AccessController.doPrivileged(Native Method)
at com.ibm.db2.jcc.t4.a0.a(a0.java:431)
... 121 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'migration' threw exception; nested exception is org.flywaydb.core.internal.exception.FlywaySqlException: Unable to obtain connection from database: [jcc][t4][2043][11550][4.25.13] Exception java.net.ConnectException: Error opening socket to server /127.0.0.1 on port 50,000 with message: Connection refused: connect. ERRORCODE=-4499, SQLSTATE=08001
I have confirmed my JDBC parameters are correct...so I am at a bit of a loss where it is going wrong.
EDIT 1: db2 is running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7046334e6c8 ibmcom/db2:11.5.0.0a "/var/db2_setup/lib/…" About a minute ago Up About a minute 22/tcp, 55000/tcp, 60006-60007/tcp, 0.0.0.0:53444->50000/tcp wizardly_cartwright
ccfe6845bfb1 testcontainers/ryuk:0.3.3 "/app" About a minute ago Up About a minute 0.0.0.0:53439->8080/tcp testcontainers-ryuk-99222438-9340-47ca-b6d2-0a13bfe50f9d
EDIT2: docker-for-java command parameters:
AbstrDockerCmd:34 - Cmd: org.testcontainers.shaded.com.github.dockerjava.core.command.CreateContainerCmdImpl@7df60067[name=<null>,hostName=<null>,domainName=<null>,user=<null>,attachStdin=<null>,attachStdout=<null>,attachStderr=<null>,portSpecs=<null>,tty=<null>,stdinOpen=<null>,stdInOnce=<null>,env={DB2INSTANCE=db2inst1,AUTOCONFIG=false,ARCHIVE_LOGS=false,DB2INST1_PASSWORD=password,PERSISTENT_HOME=false,DBNAME=BPMF,LICENSE=accept},cmd={},healthcheck=<null>,argsEscaped=<null>,entrypoint=<null>,image=ibmcom/db2:11.5.0.0a,volumes=Volumes(volumes=[]),workingDir=<null>,macAddress=<null>,onBuild=<null>,networkDisabled=<null>,exposedPorts=ExposedPorts(exposedPorts=[50000/tcp]),stopSignal=<null>,stopTimeout=<null>,hostConfig=HostConfig(binds=[], blkioWeight=null, blkioWeightDevice=null, blkioDeviceReadBps=null, blkioDeviceWriteBps=null, blkioDeviceReadIOps=null, blkioDeviceWriteIOps=null, memorySwappiness=null, nanoCPUs=null, capAdd=null, capDrop=null, containerIDFile=null, cpuPeriod=null, cpuRealtimePeriod=null, cpuRealtimeRuntime=null, cpuShares=null, cpuQuota=null, cpusetCpus=null, cpusetMems=null, devices=null, deviceCgroupRules=null, deviceRequests=null, diskQuota=null, dns=null, dnsOptions=null, dnsSearch=null, extraHosts=[], groupAdd=null, ipcMode=null, cgroup=null, links=[], logConfig=LogConfig(type=null, config=null), lxcConf=null, memory=null, memorySwap=null, memoryReservation=null, kernelMemory=null, networkMode=null, oomKillDisable=null, init=null, autoRemove=null, oomScoreAdj=null, portBindings={50000/tcp=[Lcom.github.dockerjava.api.model.Ports$Binding;@393881f0}, privileged=true, publishAllPorts=null, readonlyRootfs=null, restartPolicy=null, ulimits=null, cpuCount=null, cpuPercent=null, ioMaximumIOps=null, ioMaximumBandwidth=null, volumesFrom=[], mounts=null, pidMode=null, isolation=null, securityOpts=null, storageOpt=null, cgroupParent=null, volumeDriver=null, shmSize=null, pidsLimit=null, runtime=null, tmpFs=null, utSMode=null, usernsMode=null, sysctls=null, consoleSize=null),labels={org.testcontainers=true, org.testcontainers.sessionId=090442b0-8cc4-4f6e-b07e-1afdfed5ec15},shell=<null>,networkingConfig=<null>,ipv4Address=<null>,ipv6Address=<null>,aliases=<null>,authConfig=<null>,platform=<null>]
As I am using smplie-jndi and have the JDBC parameters in property files, the port for the JDBC URL is not 50000. Busy looking how to set it to a specific port as the default is specified in the DB2Container class
EDIT 1: It's mentioned in the docs
Note that this exposed port number is from the perspective of the container.
From the host's perspective Testcontainers actually exposes this on a random free port. This is by design, to avoid port collisions that may arise with locally running software or in between parallel test runs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请确保在启动容器后使用
DB2.getJdbcUrl()
或类似方式访问容器。 Testcontainers 将容器的公开端口发布到随机的空闲主机端口,并且需要在运行时将该动态端口注入到正在测试的系统中。根据框架和库的不同,有不同的方法来实现这些,要么是在 Spring 中配置属性,要么是最坏的情况,通过模板化配置文件。Please make sure to use
DB2.getJdbcUrl()
or similar access the container after starting it. Testcontainers publishes the exposed ports of the container to a random free host port and this dynamic port needs to be injected into your system under test at runtime. Depending on framework and libraries, there are different ways to achieve these, either configuration properties in Spring, or worst case, by templating config files.