Friday, January 17, 2014

Does the formatting of a tnsnames.ora file really make a difference?

I recently set up Oracle on my Mac so I could develop an application that uses Oracle for the database tier. I use the option of running Oracle server on a VM and configuring it to be available to my Mac using port-forwarding.

I pulled together some notes as I knew I would be going through the procedure again. Going through that procedure the second time, I encountered the following common error:

TNS:could not resolve the connect identifier specified

I checked to make sure I had set up port forwarding correctly on VirtualBox

I verified that I could telnet to the VM on port 1521 (from my Mac)

telnet 127.0.0.1 1521

I verified that I could successfully log in through sqlplus from the VM

sqlplus my_admin@orcl

I verified that the listener was up (from the VM)

lsnrctl status

I verified that my Mac had a tnsnames.ora file and that it was located at $ORACLE_HOME/network/admin/tnsnames.ora and that read permissions were open.

ORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )

I verified my environment was configured to properly use the sqlplus client on my Mac.

export NLS_LANG="AMERICAN_AMERICA.UTF8"
export ORACLE_HOME=/opt/oracle/instantclient
export DYLD_LIBRARY_PATH=$ORACLE_HOME

Everything seemed to be correct, but I was still getting errors. As I compared the tnsnames.ora on my one Mac that was working correctly and the other, I noticed that the one that wasn't had the entire configuration block indented. I had indented the code in my notes and so when it was copied over, the indent had remained. It couldn't really be that simple, could it?!

It could.

As soon as I removed the indentation so that the service declaration was bumped up directly with the left edge without any spaces, I could successfully connect.

Wow.