I have a Django program that is connecting to an Oracle database. In my settings.py file I have this configuration:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.oracle',
'NAME': 'xe',
'USER': 'MY_USER_NAME',
'PASSWORD': 'abcdefghijklmnopqrstuvwxyz',
'HOST': 'db_server.example.com',
'PORT': '1234',
}
}
I received a strange error when attempting to load the website:
ORA-28547: connection to server failed, probable Oracle Net admin error
After further investigation, I sniffed the TCP traffic between the webserver and the database server. I discovered this text in the network communication, which I reformatted for this post:
(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=1.2.3.4)
(PORT=1234)
)
(CONNECT_DATA=
(SID=xe)
(CID=
(PROGRAM=httpd@webserver_hostname)
(HOST=webserver_hostname)
(USER=apache)
)
)
)
So my question is: why is Django attempting to connect to the Oracle database with different credentials than the ones I specified? Notably, it is attempting to use user ‘apache’ instead of ‘MY_USER_NAME’. The database host IP, port, and SID are correct and what I specified. It just appears to be the user name that is different.
(As a side note, I suppose the password is transmitted separately in a later portion of the log in process?)
Installing the full Oracle client (with Administrator tools) seems to have solved the problem. There are some nuances to take care of though:
wsgi.pyrequires the location of ORACLE_HOME, as it is not passed in from the shell. In my case, this is whatwsgi.pylooks like:In Oracle 11.2.0 client there is a bug with the library linking. To fix Oracle library linking:
Additionally, it is important to have the Linux loader set up properly. (Note: this is equivalent to setting LD_LIBRARY_PATH, but I think the following is a cleaner solution). Create a file
/etc/ld.so.conf.d/oracle.confwith a single line entry to the Oracle Home library path. In my case, it’s/client/oracle/product/11.2.0/db/lib. Then runldconfig. To verify that the loader is configured correctly you can check the shared object paths for cx_Oracle:To find the file: as superuser, execute
updatedband thenlocate cx_Oracle.so | grep cx_Oracle\.so$To test the file:
ldd <path>The output should look similar to this (below). If you see the phrase “not found” then something is wrong with the loader paths.
For convenience, you’ll probably also want to create the file
/etc/profile.d/oracle.shwith these contents (note, change ORACLE_HOME to your specific install path):Reboot for these global environment variables to take effect.
After that, Oracle connections should work in any scenario. I hope this information helps others who’ve had trouble with Oracle!