0

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.

4
  • Edit: this is the errar displayed on webpage: Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application. Commented Dec 9, 2018 at 23:13
  • Also this error occurs: _mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away') Commented Dec 10, 2018 at 1:31
  • 3
    You should provide your code and traceback details. Otherwise, it's impossible to know reason why happened that. Commented Dec 10, 2018 at 5:33
  • code posted above Commented Dec 10, 2018 at 14:37

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.