I am trying to connect to a new database using the below method I have used many times previously:
import pymssql
server = "servername.database.windows.net"
user = "user"
password = "pwd"
conn = pymssql.connect(server, user, password, "DB")
I am getting this error message:
MSSQLDatabaseException: (20004, b'DB-Lib error message 20004, severity 9:\nRead from the server failed (servername.database.windows.net:1433)\nNet-Lib error during Connection reset by peer (54)\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (servername.database.windows.net:1433)\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (servername.database.windows.net:1433)\nNet-Lib error during Operation timed out (60)\n')
One of the first suggestions from other posts is to check the install of FreeTDS. My FreeTDS is up to date and I have had no problem connecting to another server: "server2.cloudapp.azure.com". Both are azure sqlserver's so I am unsure what the connection issue could be. Why would this be? Any help is appreciated.
I searched a lot, and found a solution.
Seems that the pymssql module you installed directly with pip from pypi.org cannot work normallywith PaaS Azure SQL. You need to follow the official tutorial (Configure development environment for pymssql Python development) to download specific whl file and install it manually.
In my case, I use Windows OS with Python 3.7, so I download pymssql-2.1.4-cp37-cp37m-win_amd64.whl. cp37 for python version 3.7. win for Windows OS. amd64 for 64 bit version. You need to choose right one based on your environment.
Then install the downloaded module with pip install {your_whl_file}. After that, I can connect to my Azure SQL:
import pymssql
server = 'jackdemo.database.windows.net'
database = 'jackdemo'
username = 'jack#jackdemo'
password = '***********'
cnxn = pymssql.connect(server, username, password, database)
cursor = cnxn.cursor()
cursor.execute("select * from Users")
row = cursor.fetchone()
while row:
print(str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()
Result:
By the way, I may try to explain why you can connect to sql server with *.cloudapp.azure.com. Based on the URI path, it seems to be a DNS name for Azure VM. If so, the pymssql module would woek fine with the sql server on that VM, because it is not the PaaS Azure SQL.
Another suggestion is to use pyodbc. I see the samples from Azure SQL documentation use it. It may cause less trouble.
Related
So I'm trying to establish a connection to a remote Oracle Database in python, from a Linux CentOS machine, but I'm getting the error-
cx_Oracle.DatabaseError: ORA-12170: TNS:Connect timeout occurred.
I'm using a virtual environment to run my code, wherein I have installed the cx-Oracle==8.3.0.
My python version is 3.6.2 and below is my code
import cx_Oracle
username ="username"
password ="password"
tns_dsn = cx_Oracle.makedsn("examplehost.com", 1528, "SID")
connection = cx_Oracle.connect(
username,
password,
tns_dsn)
print(connection.version)
I've also tried
connection = cx_Oracle.connect("username/password#examplehost.com:1528/SID") and possibly many other ways of connecting to the database, all have given me the same error.
I read other similar questions, some said to modify tnsnames.ora which I can't find anywhere on the system, as I'm using a virtual to implement this.
Please let me know where I'm going wrong or if I'm missing something.
Thanks
Can you please let me know how to connect to DB2 on IBM Cloud using python?
I have tried the below steps.
installed ibm_db using pip install ibm_db
Created a free tier Db2 service on IBM cloud
Generated Service credentials key
Trying to establish a connection with the database with the credential details (Database, host, port, user id, and password) extracted from the Service credentials key
import ibm_db
print("Creating connection.......")
conn_string = "DATABASE=bludb;HOSTNAME=54a2f15b-5c0f-46df-8954-7e38e612c2bd.c1ogj3sd0tgtu0lqde00.databases.appdomain.cloud;PORT=32733;PROTOCOL=TCPIP;UID=<userId>;PWD=<password>;"
conn = ibm_db.connect(conn_string,"","")
if conn:
print("Connection ...... [SUCCESS]")
else:
print("Connection ...... [FAILURE]")
I am getting below error message:
SQLCODE=-30082n: [IBM][CLI Driver] SQL30082N Security processing failed with reason "17" ("UNSUPPORTED FUNCTION"). SQLSTATE=08001
It seems like you are on the new Db2 on Cloud lite plan with non-standard ports and SSL enforced. When you connect to Db2 using the Python driver and use SSL, you have to add the SECURITY=SSL property, e.g.:
conn_string = "DATABASE=bludb;HOSTNAME=yourhostname;PORT=<port>;PROTOCOL=TCPIP;UID=<userId>;PWD=<password>;SECURITY=SSL"
I think the easy way to do this is with SQL Magic. This way, you can just type a SQL statement just by adding %sql before your query
First install the packages
!pip install sqlalchemy==1.3.9
!pip install ibm_db_sa
Then,
%load_ext sql
Finally, run the following code by replacing your username, password, hostname and SSL. You can find these in your IBM DB2 under credentials.
%sql ibm_db_sa://my-username:my-password#hostname:port/BLUDB?security=SSL
Now you can run any SQL by using %sql before or %%sql if the whole cell is going to be SQL. Example:
%sql SELECT * FROM TABLENAME;
or
%%sql
SELECT *
FROM TABLENAME;
There's a python package called ibm_db, does this link or (cited in the first one) this one help?
I had written code to connect to a DB2 server using Python that was working fine until my company had me upgrade the IBM DB2 Driver to 11.5. Now, its no longer working and I'm getting the following error message:
SQLCODE=-30082n: [IBM][CLI Driver] SQL30082N Security processing failed with reason "15" ("PROCESSING FAILURE"). SQLSTATE=08001
I verified that I have the right password. Also, I confirmed that I still have access to the Database by going through the ODBC Manager in Windows and testing a connection that way. I've checked several pages here on Stackoverflow and other sites and nothing seems to work. Most of the issues people are having on this site involve different error messages.
Here is the original code using the ibm_db module:
import ibm_db
import ibm_db_dbi
con = ibm_db_dbi.Connection(ibm_db.connect("ABC1011","rsmith","Passw123"))
When that stopped working i tried connecting using pyodbc as a connection:
import pyodbc
cnxn = pyodbc.connect('DRIVER={IBM DB2 ODBC DRIVER - DB2COPY2};Database=ABC1011;UID=rsmith; PWD=Passw123')
Again, I already verified in the ODBC Datasource Administrator (64-bit) that I am using the correct password, I have the correct database, and that I am using the correct driver's name. Does anyone know what else I may need to include to get a connection to work?
Thank you.
I am having a problem where Windows Authentication has almost completed stopped working for pymssql 2.1.3. It was working last week, but now when I try and use my AD login, it fails almost every time with the error (20009, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (878240-SQLdb4:1433)\n'), though it does manage to succeed about 1 in every 100 attempts. I have Sql Server Management Studios installed on the server where my Python code is running from, and Windows Authentication appears to be working fine. I checked the sql log for failed logins and I couldn't see any attempts.
Here is my code:
import pymssql
conn = pymssql.connection(server='myserver',database='database1')
Any ideas to make it connect consistently?
*Edit: It appears to work more consistently if it is the first time I have tried logging in after a few minutes.
So I just had to switch to pyodbc which has better support for Windows authentication. Keep in mind that the latest release of pyodbc (4.0.21) does not support Python 3.6, so you need to use 3.5. Apparently there is a wheel for python 3.6, but I haven't tested it and it appears pip is unable to natively install it on Windows. I also had to change a few things such as the connection string and how stored procedures are called (pyodbc does not have the callproc function). Here is my connection string now:
import pyodbc
conn = pyodbc.connect(server='server',database='database',Trusted_Connection='yes', driver = '{ODBC Driver 13 for SQL Server}')
I have 2 servers:
1st - Oracle Server, 2nd - Server with SQL Developer.
SQL Developer has connection with Oracle, it works good.
I am trying to get connection to Oracle wia python + cx_oracle, i have the following code:
import cx_Oracle
try:
dsnStr = cx_Oracle.makedsn("Oracle_server_ip", "1521", "Oracle_server_sid")
con = cx_Oracle.connect(user="Oracle_user", password="Oracle_password", dsn=dsnStr)
print ('CONNECTED TO ORACLE, VER: ' + con.version)
cur = con.cursor()
except:
print ('Connection Failed')
It works good on the server, where Oracle is located.
But it doesn't work on another server, where SQL Developer is located.
Can you help me, please?
First, I would suggest upgrading to cx_Oracle 6 as the error message that is returned is likely going to be better. You can do that via this command:
python -m pip install cx_Oracle --upgrade
At a guess, however, you are missing an Oracle Client on the second machine. The easiest to install is the instant client which you can find here.