简体   繁体   中英

MySQL connection errors Python, Flask, MySQL

Whenever I test my website, It randomly crashes with the following error:

mysql_exceptions.OperationalError: (2006, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

The only solution is to restart the program.

Code extract:

#####################################################
#Initialize database
db = MySQLdb.connect(host="<Name removed>.mysql.pythonanywhere-services.com", user="<Name removed>", passwd="<Password removed>", connect_timeout=3600, db="<Name removed>$main")
cur = db.cursor()

@app.route('/form', methods = ['GET', 'POST'])
def form():

 if request.method == 'POST':
    #Import data from the form
    name = request.form['name']
    first_name = request.form['first_name']
    last_name = request.form['last_name']
    email = request.form['email']
    password = request.form['password']
    country = request.form['country']
    account_type = 'admin'

    #Encrypt the password
    password = sha256_crypt.encrypt(password)

    #Generate the country code based off of the input
    loop = cur.execute("SELECT * FROM table;")
    cur.execute("SELECT code, list FROM table;")
    row = cur.fetchone()

Traceback:

2018-12-10 14:07:24,036: Exception on /form [POST] 
Traceback (most recent call last):
  File "/home/<Link removed for posting>/.virtualenvs/my-virtualenv/lib/python3.4/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/<Link removed for posting>/.virtualenvs/my-virtualenv/lib/python3.4/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/<Link removed for posting>/.virtualenvs/my-virtualenv/lib/python3.4/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/<Link removed for posting>/.virtualenvs/my-virtualenv/lib/python3.4/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/<Link removed for posting>/.virtualenvs/my-virtualenv/lib/python3.4/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/<Link removed for posting>/.virtualenvs/my-virtualenv/lib/python3.4/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/<Link removed for posting>/mysite/app.py", line 235, in form
    loop = cur.execute("SELECT * FROM table;")
  File "/home/<Link removed for posting>/.virtualenvs/my-virtualenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 255, in execute
    self.errorhandler(self, exc, value)
  File "/home/<Link removed for posting>/.virtualenvs/my-virtualenv/lib/python3.4/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/home/<Link removed for posting>/.virtualenvs/my-virtualenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 252, in execute
    res = self._query(query)
  File "/home/<Link removed for posting>/.virtualenvs/my-virtualenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 378, in _query
    db.query(q)
  File "/home/<Link removed for posting>/.virtualenvs/my-virtualenv/lib/python3.4/site-packages/MySQLdb/connections.py", line 280, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away')

The error occurs within first 5 mins of reloading the website and sometimes happens instantly as it is reloaded. Looking for a solution to allow MySQL to never timeout or a solution that will allow this error to never occur.

Connections that have been idle for longer than the connection timeout setting on the database server will be closed; you'll get that error message when you next try to use the closed connection. The timeout is 300 seconds on PythonAnywhere.

There's a PythonAnywhere help page with some hints and tips on how to manage your MySQL connections so that this doesn't cause problems, so that's probably a good place to look for a solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM