python - 错误 2006 : "MySQL server has gone away" using Python, Bottle Microframework 和 Apache

标签 python mysql apache mod-wsgi bottle

使用以下方法访问我的网络应用程序后:
- Python 2.7
- Bottle 微框架 v. 0.10.6
- Apache 2.2.22
- mod_wsgi
- 在 Ubuntu 服务器 12.04 64 位上;几个小时后我收到此错误:

OperationalError: (2006, 'MySQL server has gone away')

我正在使用 MySQL - Python 中包含的 native 版本。它通常发生在我不访问服务器时。我已经尝试使用以下方法关闭所有连接:

cursor.close()
db.close()

其中 db 是标准的 MySQLdb.Connection() 调用。

my.cnf 文件看起来像这样:

key_buffer      = 16M
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

这是默认的配置文件,除了 max_allowed_pa​​cket 是 128M 而不是 16M。

对数据库的查询非常简单,最多检索大约 100 条记录。

谁能帮我解决这个问题?我确实有一个想法是使用 try/except,但我不确定这是否真的有效。

提前致谢

杰米

更新:try/except 调用无效。

最佳答案

这是 MySQL 的错误,不是 Python 的。

此处列出了可能的原因和可能的解决方案:MySQL 5.5 Reference Manual: C.5.2.9. MySQL server has gone away .

可能的原因包括:

  • 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 side and the automatic reconnection in the client is disabled (the reconnect flag in the MYSQL structure is equal to 0).
  • You can also get these errors if you send a query to the server that is incorrect or too large. If mysqld receives a packet that is too large or out of order, it assumes that something has gone wrong with the client and closes the connection. If you need big queries (for example, if you are working with big BLOB columns), you can increase the query limit by setting the server's max_allowed_packet variable, which has a default value of 1MB. You may also need to increase the maximum packet size on the client end. More information on setting the packet size is given in Section C.5.2.10, “Packet too large”.
  • You also get a lost connection if you are sending a packet 16MB or larger if your client is older than 4.0.8 and your server is 4.0.8 and above, or the other way around.
  • and so on...

换句话说,有很多可能的原因。浏览该列表并检查所有可能的原因。

关于python - 错误 2006 : "MySQL server has gone away" using Python, Bottle Microframework 和 Apache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12444272/

相关文章:

python - 将 Pandas 日期时间列 yyyy-mm-dd 转换为 YYYYMMDD

python - Netflow v9 中的字段 ID 不正确

python - PlaySound仅播放默认的 “SystemAsterisk”声音(Windows 10,C++,Python)

mysql - 如何在不给出任何键的情况下选择表中所有重复的数据?

PhpMyAdmin 错误 : 403 forbidden

python - 服务器上的 django 应用程序 : apache runs with python 2. 7 而不是 python 3.5

Python,使用列表

mysql - 对 MYSQL 存储过程调用使用 EXPLAIN

java - Spring - 如何在集成测试中将图像插入数据库

apache - "make_sock: could not bind to address [::]:443"重启 apache 时(安装 trac 和 mod_wsgi)