MySQL服务器消失了

发布于 2024-09-17 00:18:00 字数 546 浏览 4 评论 0原文

这里是我的代码片段:

query.next();
qDebug()<<query.lastError();
qlonglong res=query.value(0).toLongLong();
qDebug()<<query.lastError();

以及相应的日志:

Debug: QSqlError(2006, "QMYSQL: Unable to execute query", "MySQL server has gone away") 
Warning: QSqlQuery::value: not positioned on a valid record
Debug: QSqlError(2006, "QMYSQL: Unable to execute query", "MySQL server has gone away")

通常我的程序工作得很好(它在服务器上工作并接受来自客户端的连接),但是每天早上当我尝试连接它时,我都会收到上面的消息。

MySQL 服务器可能出现什么问题?

Here my code snippet:

query.next();
qDebug()<<query.lastError();
qlonglong res=query.value(0).toLongLong();
qDebug()<<query.lastError();

and the corresponding log I have:

Debug: QSqlError(2006, "QMYSQL: Unable to execute query", "MySQL server has gone away") 
Warning: QSqlQuery::value: not positioned on a valid record
Debug: QSqlError(2006, "QMYSQL: Unable to execute query", "MySQL server has gone away")

Normally my program works just fine (it works on a server and accepts connections from clients), but every morning when I tried to connect it, I'm getting messages above.

What can be the problem with MySQL server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凑诗 2024-09-24 00:18:54

我知道这已经很旧了,但碰巧是谷歌第一次点击“MySQL 服务器已经消失 QMYSQL:无法执行查询”。

即使我们不再有连接,QSqlDatabase::isOpen() 似乎仍然会返回 true。这是我捕获它的方法:

QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
QString q = "SELECT * FROM myTable;";
if (!query.exec(q))
{
    int err = query.lastError().number();
    if (err == 2006) // Might want to do #2013 here also?
    {
        db.close();
        if (db.open() && !query.exec(q))
        {
            // handle error here we still failed...
        }
    }
    else
    {
        // handle normal query errors here
    }
}

我能够通过使用“/etc/init.d/mysql restart”重新启动服务器来模拟这种情况,并继续向其发送查询,它最终会抛出此错误。我觉得这不应该在服务器端更改,事实上 8 小时对于保持空闲连接打开来说似乎非常长。

I know this is old but happens to be the first google hit for "MySQL server has gone away QMYSQL: Unable to execute query".

It seems QSqlDatabase::isOpen() will still return true even though we don't have a connection anymore. Here is how I catch it:

QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
QString q = "SELECT * FROM myTable;";
if (!query.exec(q))
{
    int err = query.lastError().number();
    if (err == 2006) // Might want to do #2013 here also?
    {
        db.close();
        if (db.open() && !query.exec(q))
        {
            // handle error here we still failed...
        }
    }
    else
    {
        // handle normal query errors here
    }
}

I was able to mimic this situation by restarting the server with "/etc/init.d/mysql restart" and keep sending queries to it and it eventually throws this error. I feel this shouldn't be changed server side, in fact 8 hours seems extremely long to keep an idle connection open.

-黛色若梦 2024-09-24 00:18:46

来自 MySQL 手册

MySQL服务器消失错误的最常见原因是服务器超时并关闭了连接。

...

默认情况下,如果没有发生任何事情,服务器会在八小时后关闭连接。您可以在启动 mysqld 时通过设置wait_timeout变量来更改时间限制。< /p>

...

如果您有脚本,则只需再次发出查询,客户端即可自动重新连接。这假设您在客户端中启用了自动重新连接(这是 mysql 命令行客户端的默认设置)。

有关此错误的更多详细信息,请参阅本手册页

From the MySQL Manual:

The most common reason for the MySQL server has gone away error is that the server timed out and closed the connection.

...

By default, the server closes the connection after eight hours if nothing has happened. You can change the time limit by setting the wait_timeout variable when you start mysqld.

...

If you have a script, you just have to issue the query again for the client to do an automatic reconnection. This assumes that you have automatic reconnection in the client enabled (which is the default for the mysql command-line client).

See this manual page for more details on this error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文