Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

An Oracle client gives up before it reaches the database listener

Your error

ORA-12170: TNS:Connect timeout occurred2 more lines it can show

Fix it fast

Most common cause, 1 of 3

A firewall or cloud security rule drops traffic to the listener port

Allow inbound TCP on the listener port (1521 unless your DBA changed it) from the application server's address, on the database host's firewall and in any cloud rule in front of it.

Fix
# on a database host that uses firewalld (example)
sudo firewall-cmd --permanent --add-port=1521/tcp
sudo firewall-cmd --reload
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

ORA-12170: TNS:Connect timeout occurred (newer clients: Cannot connect. TCP connect timeout of ...) means the Oracle client could not finish connecting to the database host and port in the allowed time. Oracle names a firewall that drops the traffic, no network path between client and server, and slow networks as the causes. Test the port from the app host first.

  • Applies to Oracle Instant Client 23.26.4 and 19.33, Oracle Net sqlnet.ora parameters (Oracle AI Database 26ai docs), yajra/laravel-oci8 13.14.1 on Laravel 13, PHP 8.3+
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

From the machine or container where PHP, Python or Java runs, try the database port directly. A probe that hangs and then fails points to cause 1. A host that does not resolve, or resolves to an address you did not expect, points to cause 2. A probe that connects at once while the application still times out points to cause 3.

Run this first
getent hosts db.example.com
nc -vz -w 10 db.example.com 1521

Then find the line you see in this list and open its fix:

  1. 1A firewall or cloud security rule drops traffic to the listener portnc waits for the full -w time and then reports a timeout, never Connection refused
  2. 2The host in the connect string is not reachable from where the app runsThe host_port in the message is a private address, localhost inside a container, or a name that resolves differently on the app server
  3. 3The network is slow and the client's connect timeout is shortThe port probe connects, but the message names an Outbound connect timeout or a very small TCP timeout value

What does the error look like?

  • ORA-12170: TNS:Connect timeout occurred
  • ORA-12170: Cannot connect. TCP connect timeout of time_interval for host_port or key. (CONNECTION_ID=ID_string).
  • ORA-12170: Cannot connect. Outbound connect timeout of time_interval for host_port or key.

Cause 1 of 3

A firewall or cloud security rule drops traffic to the listener port

You seenc waits for the full -w time and then reports a timeout, never Connection refused

Check

ORA-12170 with a TCP connect timeout is what a silently dropped packet looks like: the client sends its request to port 1521 and no answer ever comes. Oracle's error help lists a firewall that does not allow the connection as a cause, next to delays and missing connectivity. Host firewalls, cloud security groups and network ACLs on either side all count.

Check
nc -vz -w 10 db.example.com 1521

Fix

Allow inbound TCP on the listener port (1521 unless your DBA changed it) from the application server's address, on the database host's firewall and in any cloud rule in front of it. Then repeat the probe from the app host.

Fix
# on a database host that uses firewalld (example)
sudo firewall-cmd --permanent --add-port=1521/tcp
sudo firewall-cmd --reload

Verify

nc prints that the connection to port 1521 succeeded, and the application connects without ORA-12170.

Verify
nc -vz -w 10 db.example.com 1521

Confirmed by docs.oracle.com

Did this fix it?

Cause 2 of 3

The host in the connect string is not reachable from where the app runs

You seeThe host_port in the message is a private address, localhost inside a container, or a name that resolves differently on the app server

Check

The newer ORA-12170 text prints the host and port the client tried, so read it first. A private IP used from outside its network, a VPN that is down, or DB_HOST=localhost inside a Docker container all leave no network path between client and server, which Oracle lists as a cause.

Check
grep -E "^DB_(HOST|PORT|SERVICE_NAME|TNS)=" .env

Fix

Point the connection at an address the app host can route to. With yajra/laravel-oci8, the oracle connection reads DB_HOST, DB_PORT and DB_SERVICE_NAME (or DB_TNS) from .env, so correct those values and clear the cached config.

Fix
# .env
DB_HOST=db.internal.example.com
DB_PORT=1521
DB_SERVICE_NAME=ORCLPDB1
# then
php artisan config:clear

Verify

The host_port in any new error matches the address you set, and a test query from the app succeeds.

Verify
php artisan tinker --execute="DB::connection('oracle')->select('select 1 from dual');"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

The network is slow and the client's connect timeout is short

You seeThe port probe connects, but the message names an Outbound connect timeout or a very small TCP timeout value

Check

When the path exists but the handshake takes longer than allowed, ORA-12170 reports the timeout type and value it hit. TCP.CONNECT_TIMEOUT defaults to 60 seconds; SQLNET.OUTBOUND_CONNECT_TIMEOUT has no default and also covers reaching the instance, and a CONNECT_TIMEOUT in the connect string overrides it. Someone may have set one of them very low.

Check
grep -iE "CONNECT_TIMEOUT|RECV_TIMEOUT" "$TNS_ADMIN/sqlnet.ora" /opt/oracle/instantclient_*/network/admin/sqlnet.ora 2>/dev/null

Fix

Raise the value in the client's sqlnet.ora. With Instant Client that file lives in the network/admin folder of the Instant Client directory, or wherever TNS_ADMIN points. Keep it bounded so a real outage still fails fast.

Fix
# <Instant Client directory>/network/admin/sqlnet.ora, or $TNS_ADMIN/sqlnet.ora
TCP.CONNECT_TIMEOUT=20 sec
SQLNET.OUTBOUND_CONNECT_TIMEOUT=30 sec

Verify

Connections succeed on the slow path, and a deliberately wrong host still fails after the new limit rather than hanging.

Verify
php artisan tinker --execute="DB::connection('oracle')->select('select 1 from dual');"

Confirmed by docs.oracle.com

Did this fix it?

How do I stop it coming back?

  • Add a start-up check that opens one database connection with a short timeout, so a closed port fails the deploy instead of the first request.
  • Keep the database address in one place (.env or tnsnames.ora) and record which firewall rule allows each app server to reach it.
  • Set TCP.CONNECT_TIMEOUT and SQLNET.OUTBOUND_CONNECT_TIMEOUT on purpose in the client sqlnet.ora rather than leaving the 60-second TCP default in place.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Spotless Laundry, EZ Estimater.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.