import pypyodbc as odbc
from configparser import ConfigParser
try:
config = ConfigParser()
config.read('config.ini')
hostname = config.get('Config', 'host')
database = config.get('Config', 'database')
username = config.get('Config', 'sql_login')
password = config.get('Config', 'sql_passw')
driver = '{/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.1.so.2.1}'
conn_string = f"DRIVER={driver};SERVER={hostname};DATABASE={database};UID={username};PWD={password}"
result = None
with odbc.connect(f'{conn_string}', timeout=60) as cnxn:
cursor = cnxn.cursor()
result = cursor.execute(query).fetchall()
print(conn)
if conn:
print('connected to the games database')
else:
print('no connection...')
except Exception as e:
print(e)
finally:
#conn.close()
print('db connection closed')
I am struggling with the odbc connection from my linux vm to a Windows vm running sqlserver. I am rly lost , I think I have tried everything. Port 1433 is open on the Windows vm , my linux server pings the windows VM.
I`ve tried to connect on Windows host to the sql server via sa login - its working.
I've turned off the Windows firewall and I still get the same error. First I was using pyodbc and later on i changed it to pypyodbc - nothing happend. trusted_connection=True - dosnt change anything.
After debugging this script the conn_string seems to be alright, also there is no difference if I connect to the IP adress or <computer_name>\SQLexpress.In the Microsoft documentation they suggested that the timeout timer was to short, I've changed it as well and I got still the same error.
Stackoverflow, please help :) !
Error :('HYT00', '[HYT00] [Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired')
Related
I'm having trouble connecting to a DB from a jupyter nb on a mac.
psycopg2 complains about an OperationalError. My code:
import sqlalchemy as db
# build connection string
connection_string = f"postgresql://{db_cred['username']}:{db_cred['password']}#{db_cred['host']}/{db_cred['database']}"
# create engine
engine = db.create_engine(connection_string)
# connect
conn = engine.connect()
returns
OperationalError: (psycopg2.OperationalError) could not translate host name "db.production.info" to address: nodename nor servname provided, or not known
Needless to say, the DB credentials are correct. The documentation on these errors isn't very helpful, and most proposed solutions revolve around Docker, which I'm not using in this case.
The only exception was this thread, which proposes using the IP address instead of the host name. I did that, and get the following different error:
OperationalError: (psycopg2.OperationalError) connection to server at "192.XXX.XX.XX", port XXXX failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
Any idea what am I doing wrong?
So the problem turned out to be that an SSH tunnel was necessary.
From a shell script, execute the command:
ssh -L 9999:db.production.info:[port number] -N [username]#192.XXX.XX.XX
this will tunnel db.production.info:[port number] to your localhost on port 9999.
You can then adapt the connection string as
'postgresql://username:password#localhost:9999/[the DB name]'
I have a running DBeaver connection to a DB2 database and now I would like to connect also in Python with the DB2 database securely over SSL. As authenticating I would like to use my windows account instead of giving a PW and UserID, to do so in DBeaver I changed:
javax.net.ssl.keyStoreType = Windows-ROOT
javax.net.ssl.trustStoreType = Windows-MY
Is this also possible with the Python connection?
I tried the following to set up the connection:
import ibm_db
connID = ibm_db.connect('DRIVER={IBM DB2 ODBC DRIVER};'
'DATABASE=XXXX;'
'HOSTNAME=XXXXYYY.net;'
'PORT=9797;'
'PROTOCOL=TCPIP;'
'SECURITY=ssl;'
'SSLSERVERCERTIFICATE=C:/Users/..../cert.cer;'
, '', '')
if connID is None:
print("\nERROR: Unable to connect.")
exit(-1)
print("Connected!")
I get the following error:
SQLCODE=-30082][CLI Driver] SQL30082N Security processing failed with reason "17" ("UNSUPPORTED FUNCTION"). SQLSTATE=08001
Some details:
M:\>"C:\Users\.conda\pkgs\ibm_db-3.0.1-py37hfa6e2cd_1\Lib\site-packages\clidriver\bin\db2level.exe"
DB21085I This instance or install (instance name, where applicable: "*") uses
"64" bits and DB2 code release "SQL11013" with level identifier "0204010F".
Informational tokens are "DB2 v11.1.3031.295", "s1804271300",
"DYN1804271300WIN64", and Fix Pack "3a".
Product is installed at
"C:\Users\.conda\pkgs\ibm_db-3.0.1-py37hfa6e2cd_1\Lib\site-packages\cli
driver" with DB2 Copy Name "IBM Data Server Driver For ODBC and CLI".
I'm using:
Python version 2.7.17
Ibm_db version 3.0.1
I use pyodbc to connect into a postgresql server over an ssh connection, the ssh connection goes well, but when I try to connect to the bd its returns a strange error:
encoding name too long
I don't know how to handle this!
Conn string - SERVER=127.0.0.1;DATABASE=xxxxx_core_xxx;UID=xxxxty;PWD=####$;PORT=12345;DRIVER={DRIVER_Postgresql};
odbc.ini
[DRIVER_Postgresql]
Driver=/usr/lib/x86_64-linux-gnu/odbc/psqlodbcw.so
odbcinst.ini
I am getting
cx_Oracle.DatabaseError: ORA-12170: TNS:Connect timeout occurred error while connecting oracle from python.
I have installed python 3.7.0 and instantclient_11_2.
Below are the process i am doing,
import cx_Oracle
dsn_tns = cx_Oracle.makedsn( '<ip>', 1521, service_name = '<given service name>')
connection = cx_Oracle.connect('user', 'pwd', dsn_tns)
I have set system veriable PATH where oci.dll is present.
What could be wrong?
Try:
connection = cx_Oracle.connect('user', 'pwd', cx_Oracle.makedsn( '<ip>', '1521',None,'<given service name>'))
Looks the same but works different in my ubuntu server.
Also make sure to put the port between ' '
You should also be able to use the following connect string if the database resides on the same machine:
connection = cx_Oracle.connect('user/pwd#localhost/service_name')
Note that a service_name value is required. You can't use the empty string!
PYTHON CODE
from pymongo import MongoClient
if __name__ == "__main__":
client = MongoClient('mongodb://localhost:27017/')
..I even tried
client = MongoClient()
getting this error..
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [WinError 10061] No connection could be made because the target machine actively refused it
I am using Windows 10, the version of pymongo is 3.0.3 and I am using python 3.5