每当我测试我的网站时,它会随机崩溃,并出现以下错误:
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()
回溯:
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永远不会超时或解决方案,将允许这个错误永远不会发生。
1条答案
按热度按时间zynd9foi1#
空闲时间超过数据库服务器上的连接超时设置的连接将被关闭;下次尝试使用关闭的连接时,您将收到该错误消息。pythonanywhere的超时时间是300秒。
这里有一个pythonanywhere帮助页面,提供了一些关于如何管理mysql连接的提示和技巧,这样就不会引起问题,所以这可能是寻找解决方案的好地方。