python3 loses connection to mysql connector during query - python

I am running the latest version of python 3 with the latest connector/python for Mac OS from the Oracle MySQL site. But when I test the connection with the simple commands in a python script file:
import mysql.connector
cnx = mysql.connector.connect(user='spUser', password='123',
host='127.0.0.1',
database='stellapeers_v2')
cnx.close()
I get the following errors, where the most important line is the final one that says :
"mysql.connector.errors.InterfaceError: 2013: Lost Connection to MySQL Server during query"
I have found many complaints about this issue on Google but no actual resolution.
Has anyone found out what the problem is and how to fix it?
Thank you
Here is the entire error output:

Related

How can I use Python 3.10 to connect to a MariaDB database on a NAS from a Linux machine?

Good day,
I have a Synology NAS DS120J hosting a MariaDB 10 (10.3.32-1040) database, on the same local network, I have a Linux machine running Ubuntu 22.04.
I am trying to connect the Linux machine to the MariaDB database using Python 3.10 and the MariaDB connector. However, it seems that I am missing a step.
When I access this database using the command line, I SSH from the Linux machine to the NAS, mentioning the port where the database is located on the NAS.
Then I populate my credentials to login to the database, this works fine.
But when using Python 3.10 and the MariaDB connector, I only provide with my credentials to login to the database, so I feel like I am missing the SSH step to the NAS, at least this is my assumption.
How could I achieve that ? Can someone please help ?
Here is my script in Python:
import mariadb
import json
import sys
with open('config.json') as config_file:
config = json.load(config_file)['mariadb']
try:
conn = mariadb.connect(
user=config['raspi-svr']['user'],
password=config['raspi-svr']['password'],
host=config['raspi-svr']['host'],
port=config['raspi-svr']['port'],
database="test_db"
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
cur = conn.cursor()
Here is the error message I am getting when running my script:
Error connecting to MariaDB Platform: Lost connection to server at 'handshake: reading initial communication packet', system error: 0
I tried running the MariaDB connector for Python.

ibm_db.connect string stating error with database name

I'm running python on a server that will eventually do my ETL and one of the databases that I have to connect to is running Informix (I believe its version 14). I have access to the Informix DB and have an active ODBC connection on a Windows 10 machine that connects no problem. I'm trying to get the basic connection down to the Informix Server and keep getting the following error:
Exception: [IBM][CLI Driver] SQL1013N The database alias name or database name "" could not be found. SQLSTATE=42705 SQLCODE=-1013
My code to connect:
import ibm_db
import pandas as pd
conn_str = ibm_db.connect('database=mydatabase;host=myservername;port=9088;protocol=onsoctcp;uid=myusername;pwd=mypassword','','')
df = pd.read_sql('SELECT * FROM schema.table', conn_str)
print(df)
With the error, I know the name is correct and I've tried a few different variations on the DB name as I know that its case sensitive with informix. I've also tried connecting to some of the configuration databases that are available in the drop down from the ODBC connection and everything matches up. I've also tried running it in a virtual environment and treating the server as a strictly python server. OS is Ubuntu 20.04LTS.
I tried the IfxPy install again and had an error showing a missing files/sources and an error on the use_2to3 file that was giving me issue. I managed to get it installed once I found this post: (Cannot connect to Informix DB using python) which shows one other thing that has to be declared than the GitHub page for IfxPy.
export CSDK_HOME=$INFORMIXDIR
Once I followed that and declared it on my system it compiled no problem and I've been able to extract some data. I'm still playing with it but I'm at least getting data out, now I just have to get it in a useable format. Appreciate all the help.

Issues connecting to a DB2 server using Python. Possible issue identifying the driver

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.

Windows Authentication Frequently Failing with pymssql

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}')

Python + cx_oracle connection failed

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.

Categories