(pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query) - python

I am having problem querying to a MariaDB database that is hosted on a Virtual Machine. I am using the dataset Python module.
This is my code:
import dataset
db = dataset.connect("mysql+pymysql://....")
Q = "select * from mytable Limit 1"
print(db.query(Q))
However I would get this error message:
(pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server
during query ([WinError 10060] A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to
respond)') (Background on this error at: http://sqlalche.me/e/e3q8)
I have tried to configure the timeout variables such as; connect_timeout, wait_timeout etc within the my.cnf file and sending a very simple query, but the issue still exist.
Any solutions or suggestions would be highly appreciated. Thank you

Related

make connection with azure sql database

I have DataGrip program, I tried to connect with it to azure sql database and it connect successfully.
I try to connect to azure sql database using sqlalchemy but it fail, here is the code:
from sqlalchemy import create_engine
try:
connection = create_engine("mysql+pymysql://<username>:<password>#<dbserver>.database.windows.net:1433/<dbname>").connect()
with connection:
with connection.begin():
print("OK")
except Exception as err:
print(err)
then the result was:
(pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query ([WinError 10054] An existing connection was forcibly closed by the remote host)')
(Background on this error at: https://sqlalche.me/e/14/e3q8)
I tried to use pyodbc instead of pymysql but the result was:
(pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
(Background on this error at: https://sqlalche.me/e/14/rvf5)
the final goal is deploy my fastapi project into azure as webapp then use azure sql.
how can I make this working ?

OperationalError pymysql.connections in _read_bytes error(2013, 'Lost connection to MySQL server during query')

Now I'm in big hard trouble because of mysql connection loss.
sometimes server starts to make some mysql related errors.
Develope envrioments are as following.
AWS EC2, AWS DB t2.small, MySQL(CONN MAX AGE 60sec, WAIT_TIMEOUT 300sec)
Ubuntu 18, Django 1.11, Python3.6
While apscheduler threads are running, it abruptly happens while writing data on AWS mysql DB.
Thank you so much in advance!
pymysql/connections.py in _read_bytes at line 707
self._force_close()
raise
if len(data) < num_bytes:
self._force_close()
raise err.OperationalError(
CR.CR_SERVER_LOST, "Lost connection to MySQL server during query")
return data
def _write_bytes(self, data):
self._sock.settimeout(self._write_timeout)
try:
Django SQL query is as following:
SELECT `django_apscheduler_djangojob`.`id`, `django_apscheduler_djangojob`.`job_state` FROM `django_apscheduler_djangojob` WHERE `django_apscheduler_djangojob`.`next_run_time` <= %s ORDER BY `django_apscheduler_djangojob`.`next_run_time` ASC
OperationalError: (2013, 'Lost connection to MySQL server during query')

PyMySQL: Connection was aborted by the software in your host machine

I executed the below code and it shows this error.
Here is the code snippet.
import pymysql
mydb = pymysql.connect(
database = "q11",
user = "111",
password = "11111",
host = "localhost"
)
The error I am getting is:
pymysql.err.OperationalError: (2006, "MySQL server has gone away (ConnectionAbortedError(10053, 'An established connection was aborted by the software in your host machine', None, 10053, None))")```
I have a problem when I connect to my MySQL database and after a long time, it hangs and my code is not getting anything from the database.
I found a solution to my problem and put the DB connection in the main message handler

Cannot Connect Using SQL Alchemy Unknown MySQL server host

I am trying to connect to a MySQL database on my local machine. I am able to connect using MySQL Workbench, but when I try to use sqlalchemy it does not allow me to connect.
The user has the correct permissions. I can connect using MySQL bench so I know the credentials are correct.
engine = create_engine('mysql://username:password!# ***.***.***.**:3306/ceesmart')
connection = engine.connect()
OperationalError: (_mysql_exceptions.OperationalError) (2006, "Unknown MySQL server host ' ..*.' (2)") (Background on this error at: http://sqlalche.me/e/e3q8)
I don't know if it was on copying and pasting here, but you have an "space" between the # and the first octect:
# ***.*
^ here
That could make the engine to try to resolve it as a DNS name instead of an IP address, therefore, raising Unknown Host exception

can't connect to Azure SQL using SQLAlchemy with pymssql

Using SQLAlchemy to connect to SQL Server on Azure.
I can access it via JDBC using SQL Workbench/J from same IP.(opened the IP in Azure)
Using the string:
mssql+pymssql://username:password#SQLservername.database.windows.net
Getting:
(pymssql.OperationalError) (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (nibisql1.database.windows.net:1433)\n') (Background on this error at: http://sqlalche.me/e/e3q8)
I tried all variations, but none of them work.
any idea why?
How do I pass parameters such as databasename?
Try to change
mssql+pymssql://username#SQLservername:password#SQLservername.database.windows.net

Categories