Python Error 2006 Mysql Server Has Gone Away
Contents |
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 about hiring python mysqldb ping developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask
Python Mysql Reconnect
Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join
Flask Operationalerror: (_mysql_exceptions.operationalerror) (2006, 'mysql Server Has Gone Away')
them; it only takes a minute: Sign up Error 2006: “MySQL server has gone away” using Python, Bottle Microframework and Apache up vote 2 down vote favorite 2 After accessing my web app using: - Python 2.7 - the
Sqlalchemy Mysql Server Has Gone Away
Bottle micro framework v. 0.10.6 - Apache 2.2.22 - mod_wsgi - on Ubuntu Server 12.04 64bit; I'm receiving this error after several hours: OperationalError: (2006, 'MySQL server has gone away') I'm using MySQL - the native one included in Python. It usually happens when I don't access the server. I've tried closing all the connections, which I do, using this: cursor.close() db.close() where db is the standard MySQLdb.Connection() call. The my.cnf file looks something like this: key_buffer = 16M python mysql operationalerror max_allowed_packet = 128M thread_stack = 192K thread_cache_size = 8 # This replaces the startup script and checks MyISAM tables if needed # the first time they are touched myisam-recover = BACKUP #max_connections = 100 #table_cache = 64 #thread_concurrency = 10 It is the default configuration file except max_allowed_packet is 128M instead of 16M. The queries to the database are quite simple, at most they retrieve approximately 100 records. Can anyone help me fix this? One idea I did have was use try/except but I'm not sure if that would actually work. Thanks in advance, Jamie Update: try/except calls didn't work. python mysql apache mod-wsgi bottle share|improve this question edited Sep 18 '12 at 4:59 asked Sep 16 '12 at 3:37 Jamie 4039 Check this one: stackoverflow.com/questions/207981/… –Jakub M. Nov 22 '12 at 7:53 add a comment| 2 Answers 2 active oldest votes up vote 14 down vote This is MySQL error, not Python's. The list of possible causes and possible solutions is here: MySQL 5.5 Reference Manual: C.5.2.9. MySQL server has gone away. Possible causes include: You tried to run a query after closing the connection to the server. This indicates a logic error in the application that should be corrected. A client application running on a different host does not have the necessary privileges to connect to the MySQL server from that host. You have encountered a timeout on the server
here for a quick overview of the site Help Center Detailed answers to any questions you django conn_max_age might have Meta Discuss the workings and policies of this site mysql server has gone away flask About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or django mysql auto reconnect posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of http://stackoverflow.com/questions/12444272/error-2006-mysql-server-has-gone-away-using-python-bottle-microframework-and 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to enable MySQL client auto re-connect with MySQLdb? up vote 30 down vote favorite 13 I came across PHP way of doing the trick: my_bool reconnect = 1; mysql_options(&mysql, MYSQL_OPT_RECONNECT, &reconnect); but no luck with http://stackoverflow.com/questions/207981/how-to-enable-mysql-client-auto-re-connect-with-mysqldb MySQLdb (python-mysql). Can anybody please give a clue? Thanks. python mysql share|improve this question asked Oct 16 '08 at 9:56 victorz 79931114 add a comment| 6 Answers 6 active oldest votes up vote 58 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 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 68957 good ...nice answer!! –Grijesh Chauhan Jan 17 '13 at 5:53 1 @garret-heaton - why
look into this ASAP? deleted-user-101937 | 2 posts | Aug. 27, 2013, 10:08 p.m. | permalink This error is typically caused by MySQL closing the connection due to idleness, although it can also https://www.pythonanywhere.com/forums/topic/858/ be caused by intermediate network equipment closing the connection. For example, overly aggressive firewalls and load balancers can sometimes kill off connections after a relatively short idle period. Is this a web application, a scheduled script, something http://serverfault.com/questions/407612/error-2006-mysql-server-has-gone-away you're running from the command-line or a long-running daemon? Particularly in web applications a connection pool is often used, to cache database connections for performance reasons. If you're using a pool, be aware that you'll need mysql server to test the connection as part of removing it from the pool. You can use the MySQL ping function for this - see this blog post for some more detail. If this error occurs on an actively used connections (i.e. where there's been a successful operation less than, say, 60 seconds before) then that's definitely something to let the admins know about. Please indicate the frequency with which it occurs as well. If, however, mysql server has it's an occasional thing then it's something you may just have to deal with in your code. Keep your transactions short because a connection close midway through a transaction will cause an implicit rollback and your changes will be lost. Always test a potentially idle connection (especially one that might have been sitting in a connection pool for hours) just before starting a new transaction and reconnect it or make a new connection if necessary. EDIT By the way, you might wonder why MySQLdb doesn't just automatically reconnect for you without all this rubbish. The reason is that if you have a disconnect, you need to be aware of it because any pending transactions may have not been committed. In fact, if the disconnection happens at the point you commit your transaction, I'm not aware of any way to tell whether your transaction was committed or not. Hence, libraries can't just quietly reconnect for you because it may hide underlying issues. This post has some more discussion (although it's targetted at Java users, the principles remain the same). Of course, as long as you keep your transactions short, the chance of a disconnection at commit time should be very low since the connection should never be idle - that would only occur due to loss of network connec
Start 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 about hiring developers or posting ads with us Server Fault Questions Tags Users Badges Unanswered Ask Question _ Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Error 2006: MySQL server has gone away up vote 7 down vote favorite 5 I'm running a Python Pyramid app on a CentOS server using uWSGI and nginx. I'm using SQLAlchemy as an ORM, MySQLdb as the API, and MySQL as a database. The site hasn't gone live yet, so the only traffic is me and some other employees of the company. We purchased some data to populate the database, so the largest (and most frequently queried) table is ~150,000 rows. Yesterday I opened four new tabs of the website in quick succession, and I got back a couple 502 Bad Gateway errors. I looked in the uWSGI log, and found the following: sqlalchemy.exc.OperationalError: (OperationalError) (2006, 'MySQL server has gone away') 'SELECT ge... Important note: This error is not due to MySQL's wait_timeout. Been there, done that. I wondered if the issue was caused by concurrent requests being served simultaneously. I made myself a poor man's load tester: for i in {1..10}; do (curl -o /dev/null http://domain.com &); done; Sure enough, within those ten requests at least one would throw a 2006 error, oftentimes more. Sometimes the errors would get even stranger, for example: sqlalchemy.exc.NoSuchColumnError: "Could not locate column in row for column 'table.id'" When the column most definitely exists and worked fine on all the other identical requests. Or, this one: sqlalchemy.exc.ResourceClosedError: This result object does not return rows. It has been closed automatically. When, once again, it worked fine for all the other requests. To further verify that the problem stemmed from concurrent database connections, I set uWSGI to a single worker and multi-threading disabled, forcing the requests to be processed one at a time. Sure enough, the problems disapp