error 2006 mysql server has gone away python
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more error 2006 mysql server has gone away while importing about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users general error 2006 mysql server has gone away magento Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each
Error 2006 (hy000): Mysql Server Has Gone Awayother. Join them; it only takes a minute: Sign up How to enable MySQL client auto re-connect with MySQLdb? up vote 29 down vote favorite 13 I came across PHP way of doing the trick: my_bool reconnect =
Python Mysqldb Ping1; mysql_options(&mysql, MYSQL_OPT_RECONNECT, &reconnect); but no luck with MySQLdb (python-mysql). Can anybody please give a clue? Thanks. python mysql share|improve this question asked Oct 16 '08 at 9:56 victorz 79431114 add a comment| 6 Answers 6 active oldest votes up vote 57 down vote I solved this problem by creating a function that wraps the cursor.execute() method since that's what was throwing the MySQLdb.OperationalError exception. The other example above implies that it is the conn.cursor() method that mysqldb python reconnect throws this exception. import MySQLdb class DB: conn = None def connect(self): self.conn = MySQLdb.connect() def query(self, sql): try: cursor = self.conn.cursor() cursor.execute(sql) except (AttributeError, MySQLdb.OperationalError): self.connect() cursor = self.conn.cursor() cursor.execute(sql) return cursor db = DB() sql = "SELECT * FROM foo" cur = db.query(sql) # wait a long time for the Mysql connection to timeout cur = db.query(sql) # still works share|improve this answer answered Jun 11 '09 at 18:38 Garret Heaton 67957 good ...nice answer!! –Grijesh Chauhan Jan 17 '13 at 5:53 1 @garret-heaton - why reconnect on AttributeError? Is that also thrown when the connection becomes stale? –aaa90210 Jul 14 '14 at 0:14 For quite some time I tried to figure out occasional mysql errors in my wrapper. Initially I thought conn.open will do the trick nope... but this worked. –marcin_koss Aug 21 '14 at 0:54 If you re-connect, you should be aware that you have lost any @variables and SETtings. Also, a half-finished transaction will have been ROLLBACK'd. This can lead to some messes. –Rick James Aug 4 '15 at 21:52 1 @aaa90210: The AttributeError is to catch the case where self.conn is still 'None' (the db hasn't been connected to yet) and the query method tries 'cursor = self.conn.cursor()'. –Mark R Jan 21 at 18:32 add a comment| Did you find this question interesting? Try our newslette
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About
Python Mysql OperationalerrorUs Learn more about Stack Overflow the company Business Learn more about hiring flask sqlalchemy mysql server has gone away developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join django wait_timeout the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Avoiding “MySQL server has gone http://stackoverflow.com/questions/207981/how-to-enable-mysql-client-auto-re-connect-with-mysqldb away” on infrequently used Python / Flask server with SQLAlchemy up vote 27 down vote favorite 10 How can Flask / SQLAlchemy be configured to create a new database connection if one is not present? I have an infrequently visited Python / Flask server which uses SQLAlchemy. It gets visited every couple of days, and on the first visit it often throws a "MySQL server has http://stackoverflow.com/questions/6471549/avoiding-mysql-server-has-gone-away-on-infrequently-used-python-flask-server gone away" error. Subsequent page views are fine, but it looks unprofessional to have this initial error. I'd like to know the correct way to handle this - advice like "make a really long time out", which would be about 4 days long in this case, doesn't seem correct. How can I test for the lack of a database connection and create one if needed? python mysql sqlalchemy flask database-connection share|improve this question edited Sep 4 '13 at 17:11 Paco 2,55411442 asked Jun 24 '11 at 17:34 Ollie Glass 6,218114577 add a comment| 3 Answers 3 active oldest votes up vote 26 down vote accepted I've had trouble with this before, and found that the way to handle it is by not keeping sessions around. The trouble is you are trying to keep a connection open for way too long. Instead, use a thread local scoped session like so either in __init__.py or in a utility package that you import everywhere: from sqlalchemy import scoped_session, sessionmaker Session = scoped_session( sessionmaker() ) Then set up your engines and metadata once. This allows you to skip configuration mechanics every time you connect/disconnect. After that, you can
the max_user_connections resource Are you having trouble with problems like this: OperationalError: (1226, "User '