OperationalError When Connecting - Psycopg2 - python

I want to interact with some data from an SQL database in Python, but am having connection issues. I've verified that the information below is correct, as I'm able to use these credentials (stored in a YAML file) to log in to the database via MySQL Workbench 6.3 CE.
user_db : *******
user_host : **********
user_port : 3306
user_username: username
user_password: password
Here is the connection code within my Ipython Notebook:
prod_db = psycopg2.connect(database=credentials['user_db'],
user=credentials['user_username'],
password=credentials['user_password'],
host=credentials['user_host'],
port=credentials['user_port'])
Regardless of whether or not I try to connect using the YAML file or just use the values within it, I get this error in my Ipython Notebook. The line indicated is the 'port' line above.
OperationalError: could not send data to server: Software caused connection
abort (0x00002745/10053)
could not send startup packet: Software caused connection abort
(0x00002745/10053)
I'm able to use identical code to log in to a Redshift database using port 5439, but I'm wondering what is causing this particular error.

It seems like you're trying to connect to MySQL (port 3306) using a PostgreSQL client library (psycopg2), which also works for Redshift because Redshift is based on Postgres.
Please try installing PyMySQL or any of the alternatives.

Related

Oracle 19c connection with python Listener refused connection

I'm using oracledb library as the cx_oracle is not working now, using the command oracledb.connect(), and it always gives error
here is my code:
connection = oracledb.connect(
user='myusername',
password='mypassword',
dsn='xx.xx.xxx.xxx:portnumber/dsnname')
print("Successfully connected to Oracle Database")
oracledb.exceptions.OperationalError: DPY-6000: cannot connect to database. Listener refused connection. (Similar to ORA-12660)
and if I set the parameters like this
connection = oracledb.connect(
user='myusername',
password='mypassword',
dsn='xx.xx.xxx.xxx:portnumber:dsnname')
print("Successfully connected to Oracle Database")
it returns error
oracledb.exceptions.DatabaseError: DPY-4027: no configuration directory to search for tnsnames.ora
the database administrator approved the variables are correct and we are using thin client which is the default in the code parameters so I don't know what is making the problem?
The error (ORA-12660) indicates that you have encryption or checksumming parameters set on the database. These are set up in the server side sqlnet.ora and look something like this:
SQLNET.ENCRYPTION_SERVER=REQUIRED
SQLNET.CRYPTO_CHECKSUM_SERVER=REQUIRED
SQLNET.ENCRYPTION_TYPES_SERVER=(AES256,AES192,AES128)
SQLNET.CRYPTO_CHECKSUM_TYPES_SERVER=(SHA1)
SQLNET.ENCRYPTION_CLIENT=REQUIRED
SQLNET.CRYPTO_CHECKSUM_CLIENT=REQUIRED
SQLNET.ENCRYPTION_TYPES_CLIENT=(AES256,AES192,AES128)
SQLNET.CRYPTO_CHECKSUM_TYPES_CLIENT=(SHA1)
This has been noted in the documentation. Your only option is to either disable the server requirement for native network encryption (NNE) or enable thick mode (which works the same way as cx_Oracle).
You can follow along with this enhancement request to see when NNE support is added to thin mode.

Connecting to Cloud SQL postgres instance with SSL in python

I am trying to connect to a postgres instance I have in cloud sql. I have everything set up and am able to connect to it if ssl encryption is turned off. But now that I have it on I am trying to connect but running into some error.
def run():
connector = Connector()
def getconn():
conn = connector.connect(
os.getenv("CONNECTION_NAME"),
"pg8000",
user = os.getenv('DB_USERNAME'),
password = os.getenv("DB_PASSWORD"),
db=os.getenv('DB_NAME'),
ip_type= IPTypes.PRIVATE
)
return conn
pool = sqlalchemy.create_engine(
"postgresql+pg8000://",
creator=getconn,
pool.execute("CREATE TABLE........;")
All the certs are stored in secret manager as strings so I am using env variables to grab them, which is why I used cadata for example. But running into this error cadata does not contain a certificate why is this error coming up?
I'd recommend using the Cloud SQL Python Connector to connect to Cloud SQL from Python as it will generate the SSL context for you, meaning no need to manage SSL certificates! It also has additional benefits of not needing to authorize networks etc.
You can find a code sample for the Python Connector similar to the one you are using for establishing a TCP connection.
There is also an interactive getting started Colab Notebook that will walk you through using the Python Connector without you needing to change a single line of code!
It makes connecting to Cloud SQL both easy and secure.

Cannot connect AS400 by python

I am using idb_bm module to build the connection to the AS400 by using the following code.
from ibm_db import connect
connection = connect('DATABASE=DATABASE;'
'HOSTNAME=xxx.xxx.xxx.xxx;'
'PORT=446;'
'PROTOCOL=TCPIP;'
'UID=uid;'
'PWD=pwd;', '', '')
After execute the code, it shows the following error,
connection = connect('DATABASE=DATABASE;'
SQLCODE=-30061][CLI Driver] SQL30061N The database alias or database name "DATABASE " was not found at the remote node. SQLSTATE=08004
The AS400 structure is complicated with tons of library and tons of table in each library. What's the database alias or database name actually? I have stuck to it for few days....Thanks all.
Assuming you actually have a modern POWER server running IBM i and not a 20 year old AS/400...
Download IBM Access Client Solutions (ACS)..
Set up a connection to your IBM i.
The "Schemas" component of ACS will show you a list of databases on the server.
Assuming you're connecting to the local database and not an iASP, you should be able to use *LOCAL.

Extract data from client database

I have to send multiple data through Python from a DB allocated in the client to the Main DB in the server, whats the best solution to this? i currently have my web server up and functioning, i can fill my DB locally but i dont really know how to do it remotely, im using Python in my hardware, here is what i have so far in the client:
import mysql.connector
cnx = mysql.connector.connect(user='user', password='pass', host='url?', database='db') #im able to enter with this
cursor = cnx.cursor()
query = ("INSERT INTO IGNORE table " "(id,date,son) " "VALUES( (%s,%s,%s)")
for row in data: #ive already extracted data from the other DB
cursor.execute(query, (row[0],row[1],row[2]))
wich yealds an error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the righ syntax to use near 'IGNORE table(id,date,son) VALUES (number, '2017-11-09 14:33:15', 18.987)' at line 1
One possibility - Make sure your mysqld service is binding to your external ip address (set in your mysql config file on the server). By default I believe it binds to 127.0.0.1 or localhost. If it is binding to localhost, your db will never respond to external requests.
I fixed the issue with the MySQL port in the following way: I opened the port internally from my VM instead of from my Virtual Network (I'm using the Google Cloud Platform).

OpenShift mysql connection issues in python

I previously wrote my app using local development servers, and now that I have moved it onto an openshift small gear almost all works except for mysql connections.
In my code I have the line:
self.db = MySQLdb.connect(host, username, password, dbname)
When I review the openshift error log, the following error is reported:
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")
I think that python is trying to connect using a UNIX socket as opposed to an INET one, but I'm not sure how to change this behavior. Any help is much appreciated.
Not specific to MySQLdb: if you use localhost as hostname, a MySQL client using the MySQL C libraries will try to connect using UNIX socket (or named pipe on Windows). There are 2 ways around this, but you'll need to grant extra permissions to make it work for both:
Use IP address 127.0.0.1
Use IP address 127.0.0.1 instead of the localhost hostname. This will make MySQL client connect using TCP/IP.
Use option files
The other way is to force the protocol using using option files. For example, in your ~/.my.cnf (or any file you want), add the following:
[python]
protocol=tcp
Now use the connection arguments to read the option file and group:
import MySQLdb
cnx = MySQLdb.connect(host='localhost', user='scott', passwd='tiger',
read_default_file='~/.my.cnf',
read_default_group='python')
The group name does not need to be python, but it is good not to use mysql or client as it might interfere with other MySQL tools (unless you want that of course).
For setting up permissions, you'll need to use the IP address of localhost, something like:
mysql> GRANT SELECT TO yourdb.* TO 'scott'#'127.0.0.1' IDENTIFIED BY ...;
(Site note: MySQL database drivers such as MySQL Connector/Python do not consider localhost to be special and connect through TCP/IP right away and you have to explicitly use the unix_socket.)
As I later discovered, while the database server runs on localhost, it runs on a very specific localhost bind address. In my case it was an address that I would never have though to try if I hadn't noticed how phpmyadmin was connecting.

Categories

Resources