简体   繁体   中英

Python Script to oracle remote database connection

First time posting here, and I've no idea if I'm in the right area or not, so forgive me if I'm way off.

I have a project that requires being able to connect a python script to a remote database to be able to run queries through the script. The uni is running Oracle Database 12C and have provided the tnsnames.ora file, and InstantClient10_1. The only other requirement they give is to be VPN connected to the uni network, which I've done.

I'm able to ssh connect to the database and run queries through SQLPlus, and have seen this script working on others computers remotely, but if I try and run any connection script then I get a mixture of messages, ranging from "TNS: No Listener" to "Connection Timeout". This is after having placed the tnsnames file in the instant client directory and changing the path and tns_admin environment variables to point towards that directory.

I have a feeling I need to install more than just that, but I'm struggling to find an idiots guide to getting it working.

Any advice is much appreciated.

Welcome to SO!

Before diving into all sorts of Python library issues, make sure you can establish a connection from your client to the db.

Firewall: make sure the port is open. nc -vz dbnode 1521

If sqlplus is available on client side the alias way:

export TNS_ADMIN=/path/to/where/my/tnsnames.ora-file-is-located
sqlplus user/pass@TNS_ALIAS 

If sqlplus is available on client side the non-alias way:

sqlplus user/pass@"THE_WHOLE_TNS_STRING_IN_HERE_NO_SPACES_STARTS_AND_ENDS_WITH_PARENTESIS" 

If sqlplus is available on client side the EZ way (EasyConnect):

sqlplus user/pass@dbnode:1521/MYDATABASE_SERVICE_NOT_SID

If you don't have sqlplus installed on the client side, make sure you get it installed or it's cousin SQLcl or SQL Developer (GUI). Also, version 10 client is not compatible with db 12c so you need a newer client.

After you are able to establish a connection, you are ready to move on with your python library. There are tons of tutorials how to connect to an Oracle Database using python.

Best of luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM