Всякий раз, когда я тестирую свой веб-сайт, он случайно падает со следующей ошибкой:
mysql_exceptions.OperationalError: (2006, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
Единственное решение - перезапустить программу.
Извлечение кода:
#####################################################
#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')
Ошибка возникает в течение первых 5 минут после перезагрузки веб-сайта и иногда происходит мгновенно при его перезагрузке.Ищите решение, позволяющее MySQL никогда не превышать время ожидания, или решение, которое позволит этой ошибке никогда не возникать.