java - IO 错误:将 oracle DB 作为 docker 容器运行并尝试通过 Tomcat 连接时,网络适配器无法建立连接
问题描述
我已经使用以下 yml 设置了一个带有 oracle db 的 docker 容器。
version: '3'
services:
oracle:
image: "carloscastillo/rgt-oracle-xe-11g"
ports:
- "49160:22"
- "49161:1521"
- "49162:8080"
volumes:
- "dbdata:/u01/app/oracle carloscastillo/rgt-oracle-xe-11g"
environment:
- ORACLE_ALLOW_REMOTE=true
- ORACLE_ENABLE_XDB=true
volumes:
dbdata:
我正在尝试访问 tomcat 中的数据库。这是 Context.xml 数据库配置。
<Resource name="ORACLE" auth="Container" type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
maxActive="100" maxIdle="30" maxWait="10000"
username="system" password="oracle" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:49161/xe"/>
也将相关驱动程序添加到类路径中。仍然收到错误“IO错误:网络适配器无法建立连接”。还尝试将 url 设置为 jdbc:oracle:thin:@localhost:49161:xe。这是我第一次这样做。无法解决我做错了什么。
解决方案
我试过你的 docker-compose。在我的 MacOS 上开箱即用。我建议您在连接字符串中添加双斜杠。
jdbc:oracle:thin:@//localhost:49161/xe
双斜线是告诉你的 JDBC 驱动程序,“嘿,除了我在这一行上给你的以外,不要尝试使用任何环境信息” - EZConnect (EasyConnect)”
建议始终确保您可以从 sqlplus 或 sqlcl 连接,然后再继续使用任何 app-lib(java、python、php 等)。像这样:
sqlplus system/oracle@//localhost:49161/xe
SQL*Plus: Release 19.0.0.0.0 - Production on Wed Nov 11 13:11:30 2020
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
system@XE> @inst
INSTANCE INFORMATION
Ins Stat Para DB Shdn
# Instance Host Version Up since us llel Th# Arch Status Pend Logins SGA Size (GB)
--- -------------------- ---------------------- ---------- ----------------- ---- ---- --- ------- ------ ---- ------- -------------
1 XE 5600801e8a25 11.2.0.2.0 11.11.20 11:58:33 OPEN NO 1 STOPPED ACTIVE NO ALLOWED 1
Elapsed: 00:00:00.02
尽管容器和数据库进程正在运行,但我已经看到很多次数据库侦听器没有启动。我的方法是
# restart the container until it works. Second or third time it normally works....
docker-container restart oracle
# fire up the listener. Remember to run as 'oracle' and wait for 10s until the service_name 'XE' is exposed in the listener process
docker-compose exec -u oracle oracle sh -c "lsnrctl stop;lsnrctl start;echo 'alter database register' | sqlplus / as sysdba; sleep 10; lsnrctl status"
# For real world CI/CD scenarios I will add a healthcheck to the docker-compose service.
healthcheck:
test: sql -L -S sys/oracle@localhost:1521/XE as sysdba < /dev/null |grep 'ORA-'; if [[ $$? == 1 ]]; then echo 0; else echo 1; fi
interval: 1m
timeout: 10s
retries: 20
start_period: 40s
祝你好运!
推荐阅读
- flutter - Flutter:更新滚动视图物理而不重建
- iframe - iframe 为 Grafana 显示“拒绝连接”
- java - 如何在正则表达式 Jlex 中选择特定单词?
- mysql - 将字符串从 MySQL 转换为 JSON,意外的令牌 N
- reactjs - React.js:登录时如何获取并显示当前用户?
- python - 从 Cygwin 扫描 Windows 中的驱动器
- macos - 为什么 /usr/libexec/java_home 不打印任何内容?
- python - Python Pandas:使用 apply 方法从另一个数据框中更新多个列值
- c++ - 如何检测和处理算法中不受支持的语言环境?
- batch-file - 我正在尝试编写一个批处理命令以在工作区中自动运行 VSCode,但是当我关闭 CMD 时,VSCode 也会关闭