SSL connection error: SSL certificate validation failure (2026) (SQLDriverConnect) pyodbc? - python

I am trying to connect to Azure MySQL Database Service. I am using
pyodbc==3.0.3
python 2.7
My Connection string looks like this
connection_string = "DRIVER={MySQL};SERVER={server_name}.mysql.database.azure.com;PORT=3306;DATABASE" \
"={my_database_name};UID={username#server_name};PWD={password};CHARSET=UTF8;" \
"sslca=/home/sachin/BaltimoreCyberTrustRoot.crt.pem;sslverify=1"
cnxn = pyodbc.connect(connection_string)
But the same configuration works with mymysql.connector is working fine.
import mysql.connector
cnx = mysql.connector.connect(user="{username#servername}", password="{password}",
host="{server_name}.mysql.database.azure.com",
port=3306, database="{database_name}", ssl_ca="/home/sachin//root.crt",
ssl_verify_cert=True)
cnx.cursor()
I can not use mysql.connector for now. So need some suggestion if any one have faced this issue.

I had the same problem and fixed it by setting sslverify=1 to sslverify=0.
e.g. for Azure:
;sslca={BaltimoreCyberTrustRoot.crt.pem}; sslverify=0; Option=3;'

Related

How to connect with oracle database?

I am using this code to connect with oracle database:
import cx_Oracle
conn_str = u"jbdc:oracle:thin:#****_***.**.com"
conn = cx_Oracle.connect(conn_str)
c = conn.cursor()
However, I am getting this error:
ORA-12560: TNS:protocol adapter error
How can I resolve it?
Thank you
You cannot use a JDBC thin connect string to connect with cx_Oracle (or the new python-oracledb). You must use either an alias found in a tnsnames.ora file, or the full connect descriptor (such as that found in a tnsnames.ora) file or an EZ+ Connect string. An example follows:
conn_str = "user/password#host:port/service_name"
With the new python-oracledb driver you can also do this:
import oracledb
conn = oracledb.connect(user="USER", password="PASSWORD", host="my_host",
port=1521, service_name="my_service")
See the documentation for more details.

Connecting to Oracle Database Using Python with cx_oracle

I am using cx_oracle to connect the Oracle DB using python.I need your assistance in connecting DB. I'm using following code till date it worked well
Code-1:
import cx_Oracle as cx
dsn_tns = cx.makedsn(HOST,PORT, service_name=SERVICE_NAME)
conn = cx.connect(user=USER, password=PW, dsn=dsn_tns)
Recently TCP port is disabled and we are using jdbc connection string to connect DB which has TCPS port number.can you help where I need to pass the protocol in above/ suggest any other method.
i tried following code
code-2:
dsn_tns='(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = tcps)(HOST =hostdetails)(PORT =port))) (CONNECT_DATA = (SERVICE_NAME = servicename)))'
conn = cx.connect(user=USER, password=PW, dsn=dsn_tns)
print(cx.version)
I got error: ORA-28759: failure to open file.
Any suggestion is appreciated. Thank you in advance.
Does your sqlnet.ora have a WALLET_LOCATION defined?
Source.

Cannot connect to GearHost database

I am trying to connect to my GearHost Database in python, I followed the instructions here on GearHost. My python code looks like:
from mysql.connector import connection
server = 'den1.mssql6.gear.host'
db = 'testmenow'
user = 'testmenow'
psword = 'TEST!!'
cnxn = connection.MySQLConnection(host=server, user=usr, password=psword, database=db)
cursor = cnxn.cursor()
cnxn.close()
I get the following error:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'den1.mssql6.gear.host:3306' (10061 No connection could be made because the target machine actively refused it)
I have also tried to connect to GearHost through mysql workbench, as instructed in GearHost's instruction page, which also cannot connect to the database:
Connecting to MySQL server ...
Can't connect to MySQL server on 'den1.mssql6.gear.host' (10060)
This is what my GearHost shows:
Tried (unsuccessfully)
Messing with and without a port number, as mentioned here: Issue connecting to gearhost database
Trying connecting through MySQL workbench
Considering the conversation here: Cannot Connect to Database Server mysql workbench , but does not seem applicable to my circumstances.
Different SQL packages in python, including mysql-python and mysql-connector-python
Question
Am I missing something obvious that is preventing me from connecting to GearHost, either with python or mysql workbench?
UPDATE
as #SMor pointed out, I had mistaken MSSQL for MySQL - I was able to successfully connect to my database using:
import pyodbc
server = 'den1.mssql6.gear.host'
db = 'testmenow'
usr = 'testmenow'
psword = 'TEST!!'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';'
'DATABASE='+db+';UID='+usr+';PWD=' + psword)
cursor = cnxn.cursor()

Connect to Oracle database using pyodbc

I would like to connect to an Oracle database with python through pyodbc. I have installed oracle driver and I tried the following script:
import pyodbc
connectString = """
DRIVER={Oracle in OraClient12Home1};
SERVER=some_oracle_db.com:1521;
SID=oracle_test;
UID=user_name;
PWD=user_pass
"""
cnxn = pyodbc.connect(connectString)
I got the following error message:
cnxn = pyodbc.connect(connectString)
Error: ('HY000', '[HY000] [Oracle][ODBC][Ora]ORA-12560: TNS:protocol adapter error\n (12560) (SQLDriverConnect)')
What's wrong here?
Why keyword DBQ works and SID/Service Name does not, see the section 21.4.1 Format of the Connection String in Oracle 12c documentation.
https://docs.oracle.com/database/121/ADFNS/adfns_odbc.htm#ADFNS1183/
or google keywords for odbc oracle 12c
Looks Like your missing a PORT
Try this way
NOTE:
Depending on your Server the syntax can be different this will work for Windows without DSN using an SQL Server Driver.
connectString = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;PORT=1433;DATABASE=testdb;UID=me;PWD=pass')
This is the connection, you still need a cursor and to use execute along with an SQL Statement..
You have to specify server or hostname (or IP address in connection string for your database server is running.
So close...
connectString = """
DRIVER={Oracle in OraClient12Home1};
SERVER=some_oracle_db.com:1521;
DBQ=oracle_test;
Uid=user_name;
Pwd=user_name
"""
I replaced SID with DBQ

Connection to external MySQL instance using Python

I have tried the following code:
MySQLdb.connect(host='xxx', port=3306, user='yyy')
But I get the following error:
(2005, "Unknown MySQL server host ...
I have tried to remove all firewall restrictions on the external MySQL instance, as a test. I am able to connect to the instance from my developing machine.
I believe this should be possible now that the App Engine supports sockets, or am I wrong?
I think this connection is not allowed (no external socket support in MySQLdb) :
https://developers.google.com/appengine/docs/python/cloud-sql/?hl=en#Python_Using_a_local_MySQL_instance_during_development
You have to use localhost/127.0.0.1 or CloudSQL socket (unix_socket='/cloudsql/'):
if (os.getenv('SERVER_SOFTWARE') and
os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):
db = MySQLdb.connect(unix_socket='/cloudsql/' + _INSTANCE_NAME, db='guestbook', user='root')
else:
db = MySQLdb.connect(host='127.0.0.1', port=3306, user='root')
# Alternately, connect to a Google Cloud SQL instance using:
# db = MySQLdb.connect(host='ip-address-of-google-cloud-sql-instance', port=3306, user='root')

Categories

Resources