使用Python oracledb连接Oracle Cloud数据库时遭遇SSL握手失败问题求助
Let’s walk through the most likely causes and fixes for your SSL handshake error when connecting to Oracle Autonomous Database (ADB) with python-oracledb:
1. The Most Likely Culprit: Using the Wrong Connection String
Since you’ve set Mutual TLS (mTLS) Authentication to "Not Required" in your ADB’s Network settings, you need to use a TLS-only connection string—not the mTLS one you copied initially.
mTLS connection strings include the ssl_server_cert_dn parameter, which expects a client certificate for authentication. Since you disabled mTLS, this parameter causes the SSL handshake to fail.
Fix Steps:
- Go back to your ADB’s DB Connection > Connection Strings page
- Switch to the TLS tab (not the mTLS tab)
- Copy the connection string from this tab. It should look similar to this:
(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=adb.uk-london-1.oraclecloud.com))(connect_data=(service_name=abc123xyzredacted.adb.oraclecloud.com))(security=(ssl_server_dn_match=yes))) - Update your
PYTHON_CONNECTSTRINGenvironment variable with this new string.
2. Verify Python's SSL Library Compatibility
The SSLV3_ALERT_HANDSHAKE_FAILURE error can also occur if your Python environment uses an outdated SSL library that doesn’t support TLS 1.2 or higher (Oracle Cloud requires TLS 1.2+ for ADB connections).
Check Your SSL Version:
Run this quick script to confirm your setup:
import ssl print("OpenSSL Version:", ssl.OPENSSL_VERSION)
- If your OpenSSL version is older than 1.0.2, you’ll need to upgrade your Python installation or the underlying OpenSSL library.
- For Python 3.9+, you should already have a compatible version, but double-check to rule this out.
3. ADMIN Account & Permissions: You're Good to Go
Your ADMIN account has full administrative privileges by default, so you don’t need to create a separate user for this connection test. The error you’re seeing is at the network/SSL layer, not a permission issue.
4. Quick Sanity Checks
- Confirm your public IP address is correctly added to the ADB’s Access Control List (ACL). If you’re using a VPN or proxy, your outgoing IP might differ from what you added—double-check with a tool like
curl ifconfig.meto get your current public IP. - Ensure your
PYTHON_USERNAME,PYTHON_PASSWORD, andPYTHON_CONNECTSTRINGenvironment variables are set correctly (no typos, extra spaces, or missing quotes).
Test the Fix
To rule out environment variable issues, try hardcoding the correct credentials and connection string temporarily (just for testing):
import oracledb un = "ADMIN" pw = "your_database_password" cs = "(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=adb.uk-london-1.oraclecloud.com))(connect_data=(service_name=abc123xyzredacted.adb.oraclecloud.com))(security=(ssl_server_dn_match=yes)))" with oracledb.connect(user=un, password=pw, dsn=cs) as connection: with connection.cursor() as cursor: sql = """select sysdate from dual""" for r in cursor.execute(sql): print(r)
If this works, you can safely switch back to using environment variables for production.
内容的提问来源于stack exchange,提问作者powlo

