测试容器+ db2问题

v7pvogib  于 2022-11-07  发布在  DB2
关注(0)|答案(1)|浏览(175)

我在使用测试容器时遇到了一个问题。我一直收到一个连接被拒绝的错误。
使用以下命令运行db2时:

  • docker run我可以连接dbvis。
  • 我使用fabric 8 maven插件启动db2容器,并且能够再次连接dbvis

我在junit 5测试中设置了一个断点,并尝试访问db2,但连接被拒绝。
我的db2测试容器配置:

@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参数是正确的......所以我有点不知道哪里出错了。
EDIT 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

EDIT 2: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参数,所以JDBCURL的端口不是50000。
编辑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.
vktxenjb

vktxenjb1#

请确保在启动容器后使用DB2.getJdbcUrl()或类似的方法访问容器。Testcontainers将容器的公开端口发布到一个随机的空闲主机端口,并且需要在运行时将此动态端口注入到您的测试系统中。根据框架和库的不同,有不同的方法来实现这些,或者在Spring中配置属性,或者在最坏的情况下,通过模板化配置文件。

相关问题