first commit
This commit is contained in:
141
quick_oracle_test.py
Normal file
141
quick_oracle_test.py
Normal file
@@ -0,0 +1,141 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
快速Oracle连接测试脚本
|
||||
用于快速诊断Oracle连接问题
|
||||
"""
|
||||
|
||||
import oracledb
|
||||
import logging
|
||||
from config import DatabaseConfig
|
||||
|
||||
# 配置日志
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def test_oracle_connection():
|
||||
"""
|
||||
测试Oracle连接的多种方式
|
||||
"""
|
||||
# 获取Oracle配置
|
||||
config = DatabaseConfig.get_config("oracle")
|
||||
|
||||
print("=" * 60)
|
||||
print("快速Oracle连接测试")
|
||||
print("=" * 60)
|
||||
print(f"配置信息:")
|
||||
print(f" 主机: {config['host']}")
|
||||
print(f" 端口: {config['port']}")
|
||||
print(f" 用户名: {config['username']}")
|
||||
print(f" 服务名: {config['service_name']}")
|
||||
print("=" * 60)
|
||||
|
||||
# 测试方式列表
|
||||
test_methods = [
|
||||
{
|
||||
'name': '方式1: Easy Connect字符串',
|
||||
'params': {
|
||||
'user': config['username'],
|
||||
'password': config['password'],
|
||||
'dsn': f"{config['host']}:{config['port']}/{config['service_name']}"
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': '方式2: 分离参数 (service_name)',
|
||||
'params': {
|
||||
'user': config['username'],
|
||||
'password': config['password'],
|
||||
'host': config['host'],
|
||||
'port': config['port'],
|
||||
'service_name': config['service_name']
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': '方式3: makedsn (service_name)',
|
||||
'params': {
|
||||
'user': config['username'],
|
||||
'password': config['password'],
|
||||
'dsn': oracledb.makedsn(config['host'], config['port'], service_name=config['service_name'])
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': '方式4: makedsn (SID)',
|
||||
'params': {
|
||||
'user': config['username'],
|
||||
'password': config['password'],
|
||||
'dsn': oracledb.makedsn(config['host'], config['port'], sid=config['service_name'])
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
success_count = 0
|
||||
|
||||
for i, method in enumerate(test_methods, 1):
|
||||
print(f"\n🔍 测试 {method['name']}...")
|
||||
|
||||
try:
|
||||
# 显示连接参数
|
||||
if 'dsn' in method['params']:
|
||||
print(f" DSN: {method['params']['dsn']}")
|
||||
else:
|
||||
print(f" Host: {method['params']['host']}:{method['params']['port']}")
|
||||
if 'service_name' in method['params']:
|
||||
print(f" Service Name: {method['params']['service_name']}")
|
||||
|
||||
# 尝试连接
|
||||
connection = oracledb.connect(**method['params'])
|
||||
|
||||
# 测试查询
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SELECT 1 FROM DUAL")
|
||||
result = cursor.fetchone()
|
||||
|
||||
# 获取数据库信息
|
||||
cursor.execute("SELECT BANNER FROM V$VERSION WHERE ROWNUM = 1")
|
||||
version = cursor.fetchone()
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
print(f" ✅ 连接成功!")
|
||||
print(f" 📊 查询结果: {result}")
|
||||
print(f" 🔖 数据库版本: {version[0] if version else 'Unknown'}")
|
||||
|
||||
success_count += 1
|
||||
|
||||
except Exception as e:
|
||||
print(f" ❌ 连接失败: {str(e)}")
|
||||
|
||||
# 分析常见错误
|
||||
error_str = str(e).lower()
|
||||
if 'ora-12514' in error_str:
|
||||
print(f" 💡 提示: ORA-12514错误通常表示服务名不存在或未注册")
|
||||
elif 'ora-12541' in error_str:
|
||||
print(f" 💡 提示: ORA-12541错误通常表示监听器未运行")
|
||||
elif 'ora-01017' in error_str:
|
||||
print(f" 💡 提示: ORA-01017错误表示用户名或密码无效")
|
||||
elif 'ora-12170' in error_str:
|
||||
print(f" 💡 提示: ORA-12170错误表示连接超时")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print(f"测试结果: {success_count}/{len(test_methods)} 种方式成功")
|
||||
|
||||
if success_count == 0:
|
||||
print("\n🔧 所有连接方式都失败,可能的解决方案:")
|
||||
print("1. 检查Oracle数据库服务是否正在运行")
|
||||
print("2. 检查监听器状态: lsnrctl status")
|
||||
print("3. 验证服务名是否正确注册")
|
||||
print("4. 检查网络连接和防火墙设置")
|
||||
print("5. 确认用户名和密码正确")
|
||||
print("6. 尝试使用Oracle SQL Developer或其他客户端工具测试连接")
|
||||
elif success_count < len(test_methods):
|
||||
print("\n⚠️ 部分连接方式成功,建议使用成功的连接方式")
|
||||
else:
|
||||
print("\n🎉 所有连接方式都成功!")
|
||||
|
||||
print("=" * 60)
|
||||
|
||||
return success_count > 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_oracle_connection()
|
||||
Reference in New Issue
Block a user