mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away') - python

I got this error when I execute my query
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in de
faulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away')
I read this question, this one and this one and this one
I read the Doc and I know that I got this error because image_name in my query is too large
You can also get these errors if you send a query to the server that
is too large.
I set a limite for the length of my variable
image_name = (image_name[:180]) if len(image_name) > 180 else image_name
cmd = "UPDATE `banners` SET `checked` = '1',`local_dir` = %s , `image_name` = %s WHERE `unique_id` = %s "
cursor.execute(cmd,[local_addr,image_name,unique_id])
And I set also max_allowed_packet in /etc/my.cnf
[mysqld]
max_allowed_packet=16M
Do you have any idea how can I avoid this error?

Related

How can a background process access the database in Flask?

A MySQL server is initialized in Flask (with connexion) on service startup.
service.app.datastore = DatastoreMySQL(service.config)
class DatastoreMySQL(Datastore):
def __init__(self, config):
...
self.connection_pool = pooling.MySQLConnectionPool(
database=self.database,
host=self.hostname,
username=self.username,
password=self.password,
pool_name="pool_name",
pool_size=self.pool_size,
autocommit=True
)
def exec_query(self, query, params=None):
try:
connection = self.connection_pool.get_connection()
connection.ping(reconnect=True)
with closing(connection.cursor(dictionary=True, buffered=True)) as cursor:
if params:
cursor.execute(query, params)
else:
cursor.execute(query)
finally:
connection.close()
The view functions use the database by passing the DB reference from current_app.
def new():
do_something_in_db(current_app.datastore, request.get_json())
def do_something_in_db(db, data):
db.create_new_item(data)
...
However, a background process (run with APScheduler) must also run do_something_in_db(), but when passed a datastore reference an mysql.connector.errors.OperationalError error is thrown.
My understanding is that this error comes from two sources:
The server timed out and closed the connection. However, in this service the exec_query() function obtains a connection and executes right away, so there should be no reason that it times out. The monitor is also initialized at service startup with a datastore reference, but I am not sure how that can time out given that a new connection is created each time exec_query() is called.
The server dropped an incorrect or too large packet. However, there are no packets here - the process is run by a local background scheduler.
The error in full:
Job "Monitor.monitor_running_queries" raised an exception
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 509, in cmd_query
raw_as_string=raw_as_string)
_mysql_connector.MySQLInterfaceError: MySQL server has gone away
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/k8s-service/lib/datastore/datastore_mysql.py", line 88, in exec_query
cursor.execute(query, params)
File "/usr/local/lib/python3.6/site-packages/mysql/connector/cursor_cext.py", line 276, in execute
raw_as_string=self._raw_as_string)
File "/usr/local/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 512, in cmd_query
sqlstate=exc.sqlstate)
mysql.connector.errors.DatabaseError: 2006 (HY000): MySQL server has gone away
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/apscheduler/executors/base.py", line 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "/opt/k8s-service/lib/background.py", line 60, in monitor_running_queries
self.handle_process_state(query.id, datastore, hive)
File "/opt/k8s-service/lib/background.py", line 66, in handle_process_state
query = datastore.get_item(query_id)
File "/opt/k8s-service/lib/datastore/datastore.py", line 48, in get_item
return_results=True)
File "/opt/k8s-service/lib/datastore/datastore.py", line 97, in exec_query
connection.close()
File "/usr/local/lib/python3.6/site-packages/mysql/connector/pooling.py", line 131, in close
cnx.reset_session()
File "/usr/local/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 768, in reset_session
raise errors.OperationalError("MySQL Connection not available.")
mysql.connector.errors.OperationalError: MySQL Connection not available.

raise errors.InterfaceError("No result set to fetch from.") mysql.connector.errors.InterfaceError: No result set to fetch from. Error

I have had this MySQL error for quite some time now. I cant seem to understand why it keeps occurring, I have imported my database file with all the right information to make the connection but for some reason the error still occurs. I have similar functionality scattered all over my project and I have never got this error, it is literally word for word copying and yet I still get this error. If someone could point me in the right direction it would be much appreciated as I have had no luck online.
Thanks.
Code:
def membership_reactivation(self):
connection.connect(user="root", password="")
email = self.EmailBox.get()
password = self.PasswordBox.get()
query = "UPDATE users SET Status = 'ACTIVE' WHERE Email = %s AND Password = %s;"
cursor.execute(query, (email, password,))
result = cursor.fetchall()
if result:
connection.commit()
connection.close()
self.controller.show_frame("ThankYou")
else:
print("Credentials Not Found in Database.")
connection.close()
Error Message:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\AJE\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:\Users\AJE\PycharmProjects\ReactivateMembership.py", line 58, in membership_reactivation
result = cursor.fetchall()
File "C:\Users\AJE\AppData\Local\Programs\Python\Python36-32\lib\site-packages\mysql\connector\cursor.py", line 1002, in fetchall
raise errors.InterfaceError("No result set to fetch from.")
mysql.connector.errors.InterfaceError: No result set to fetch from.

Lost connection to MySQL server during Python connection

Relatively new to Python and MySQL, but I'm performing a simple query of a DB in a dev environment using the MySQL Python Connector. I've created a buffered cursor to return results as dictionaries. When I perform the simple query:
family_query = ("SELECT * FROM family as FF")
...I get a list of errors, all around this idea of error 2013: Lost Connection to MySQL server.
>python "FitMatch v1.5.py"
Traceback (most recent call last):
File "FitMatch v1.5.py", line 505, in <module>
fit_match_cursor.execute(fit_family_query, () )
File "C:\Python34\lib\site-packages\mysql\connector\cursor.py", line 507, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\Python34\lib\site-packages\mysql\connector\cursor.py", line 421, in _handle_result
self._handle_resultset()
File "C:\Python34\lib\site-packages\mysql\connector\cursor.py", line 895, in _handle_resultset
(self._rows, eof) = self._connection.get_rows()
File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 671, in get_rows
rows = self._protocol.read_text_result(self._socket, count)
File "C:\Python34\lib\site-packages\mysql\connector\protocol.py", line 309, in read_text_result
packet = sock.recv()
File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 226, in recv_plain
raise errors.InterfaceError(errno=2013)
mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query
I've tried increasing my connection_timeout to well over 10,000 (as I read on other stack overflow posts that could be the issue) but it had no effect.
Any ideas what could be causing the "Lost connection to MySQL server" error?
As a print of nested query result I face this problem
my Python query would fail with the error described in the question after returning just a subset of results.
As my way you Switched to PyMySQL and things work as you like
PyMySQL Link
My Sample Code For printing data
import pymysql
connection = pymysql.connect(user='XYZ', passwd='XYZ',host='XYZ',database='XYZ')
cursor = connection.cursor()
query = ("YOUR_QUERY")
cursor.execute(query)
for item in cursor:
print item
If any problem with this answer must comment me....:)

to strip ' or " while using string in python

I have column names in string , now to update table in mysql in the following code :
cursor.execute("""update websites SET %s = %s where weblink = %s""",(key,value,x))
gives error:
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''blog' = 1 where weblink = 'http://blogspot.com/'' at line 1")
key,value = 'blog',2
in cursor.execute key is string and sql table columns are without string , how to solve this problem
Traceback (most recent call last):
File "pgrank.py", line 28, in <module>
cursor.execute("""update websites SET %s = %s where weblink = %s""",(key,value,x))
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'blog\'' = 1 where weblink = 'http://blogspot.com/' at line 1')
The "inherent" replacing works fine for data, but not for table names.
In SET %s = %s, the first %s gets replaced by 'blog' while it should be blog or even `blog`.
You should do
cursor.execute("""update websites SET `%s` = %%s where weblink = %%s""" % key, (value,x))
because these are two distinct technologies.
Better readability would be provided by
cursor.execute("update websites SET `" + key +
"` = %s where weblink = %s", (value,x))
and safety is increased if you check if key contains the ` character.

Python, MySQL and a weird error

I have a bug that I don't know how to fix or even reproduce:
query = "SELECT id, name FROM names ORDER BY id"
results = database.execute(query)
where the class Database contains:
def execute(self, query):
cursor = self.db.cursor()
try:
cursor.execute(query)
return cursor.fetchall()
except:
import traceback
traceback.print_exc(file=debugFile)
return []
This is how I open the database connection:
self.db = MySQLdb.connect(
host=mysqlHost,
user=mysqlUser,
passwd=mysqlPasswd,
db=mysqlDB
)
This is the stacktrace of the error:
File "foo.py", line 169, in application results = config.db.execute(query)
File "Database.py", line 52, in execute
return cursor.fetchall()
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 340, in fetchall
self._check_executed()
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 70, in _check_executed
self.errorhandler(self, ProgrammingError, "execute() first")
File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
ProgrammingError: execute() first
Do you have any ideas of why this is happening and how can I fix it? I searched on the internet and I found out that the reason may be having 2 cursors, but I have only one.
try this in your traceback it's for debugging:
except ProgrammingError as ex:
if cursor:
print "\n".join(cursor.messages)
# You can show only the last error like this.
# print cursor.messages[-1]
else:
print "\n".join(self.db.messages)
# Same here you can also do.
# print self.db.messages[-1]

Categories

Resources