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.
Related
`db = MySQLdb.connect(
host = '12.34.567.891',
user = 'root',
passwd = '',
db = 'testdb',
port = "something-that-works")`
Very Simple Can I somehow make it so that it connects only to the ip '12.34.567.891'. Google is forwarding the port to 80 but you can't request port 80 or it ends up in an endless loop.
port=null or port = none will cause and error.
I have no issues connecting from my cli mysql client
Thank you,
I expected to be able to connect to the server no issues if I am able to do so from my cli - I need some way to send the connecting request to the raw IP no port. It may be possible python-mysql can't do this
3306 is the default MySQL port and it seems that you are using MySQL, so that should work. https://cloud.google.com/sql/docs/mysql/connect-overview
You will have an easier time connecting with the Cloud SQL Python Connector a library built purely for connecting to Cloud SQL with Python.
Looks like this:
from google.cloud.sql.connector import Connector
# build connection
def getconn() -> pymysql.connections.Connection:
with Connector() as connector:
conn = connector.connect(
"project:region:instance", # Cloud SQL instance connection name
"pymysql",
user="my-user",
password="my-password",
db="my-db-name"
)
return conn
# create connection pool
pool = sqlalchemy.create_engine(
"mysql+pymysql://",
creator=getconn,
)
# insert statement
insert_stmt = sqlalchemy.text(
"INSERT INTO my_table (id, title) VALUES (:id, :title)",
)
# interact with Cloud SQL database using connection pool
with pool.connect() as db_conn:
# insert into database
db_conn.execute(insert_stmt, id="book1", title="Book One")
# query database
result = db_conn.execute("SELECT * from my_table").fetchall()
# Do something with the results
for row in result:
print(row)
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.
I am using PyMySQL to connect to a database running on localhost. I can access the database just fine using the username/password combiunation in both the command line and adminer so the database does not appear to be the probem here.
My code is as follow. However, when using the host="127.0.0.1" options, I get an OperationalError and an Errno 111. Using the same code, but connecting via the socket Mariadb runs on is fine.
import pymysql.cursors
from pprint import pprint
# This causes an OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")
# connection = pymysql.connect(
# host="127.0.0.1",
# port=3306,
# user="root",
# password="S3kr37",
# db="my_test",
# )
# This works.
connection = pymysql.connect(
user="root",
password="S3kr37",
db="my_test",
unix_socket="/var/lib/mysql/mysql.sock"
)
try:
with connection.cursor() as cursor:
sql = "select * from MySuperTable"
cursor.execute(sql)
results = cursor.fetchall()
pprint(results)
finally:
connection.close()
What am I doing wrong?
PS: Note that this question has the same problem but the solution offered is the socket. That is no good enough: I want to know why I cannot use the hostname as the documentation suggests.
Errorcode 2003 (CR_CONN_HOST_ERROR) is returned by the client library, in case the client wasn't able to establish a tcp connection to the server.
First you should check, if you can connect via telnet or mysql command line client to your server.
If not, check the server configuration file:
does the server run on port 3306?
is IPv4 disabled?
is skip-networking enabled?
is bind-address activated (with another IP?
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;'
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