mysql - MySQL 重新连接标志值是什么意思?

标签 mysql c mysql-connector

我的代码看起来很像这样,只有错误处理(不会被触发)。它在多线程应用程序中调用(每个线程需要连接一次),在 Ubuntu 13.10.1 上使用 MySQL 5.5.37-0:

DBConnection = mysql_init(NULL);
mysql_options((MYSQL *) DBConnection, MYSQL_SET_CHARSET_NAME, "utf8");
mysql_real_connect(DBConnection, ...);
my_bool reconnect = true;
mysql_options(DBConnection, MYSQL_OPT_RECONNECT, &reconnect);
mysql_query(DBConnection, "SET wait_timeout=60"); //!!!
printf("reconnect flag %d", DBConnection->reconnect);

我已将断开连接超时时间缩短,以便我可以测试重新连接代码(1 分钟而不是默认的 8 小时)。编辑:我在 real_connect 之后设置 _RECONNECT 选项 this page因为它对我来说更可靠。

在mysql.h中相关代码说:

my_bool reconnect;  /* set to 1 if automatic reconnect */

“重新连接”字段的值似乎是随机的。从上面的日志中抓取值我看到:100、104、106、108、108、38、43、56、59、63、64、88、92、98

这似乎没有记录,the relevant page只是说:

MYSQL This structure represents a handle to one database connection. It is used for almost all MySQL functions. Do not try to make a copy of a MYSQL structure. There is no guarantee that such a copy will be usable.

A "lost connection" help page说:

Prior to MySQL 5.0.19, even if the reconnect flag in the MYSQL structure is equal to 1,

我认为我从未见过它实际设置为 1,无论我是否使用上面的 mysql_options() 调用来设置它。有谁知道这意味着什么,对实际值是否有任何意义?

重新连接后一段时间以来,我一直在追踪稍微奇怪的行为,我想知道这是否与它有关(有时自动重新连接失败,但手动断开连接并重新连接有效)。


我已经交换了顺序并使用了 mysql_options 而不是 SQL,“从不 0 或 1”结果没有明显变化。新代码:

DBConnection = mysql_init(NULL);
mysql_options((MYSQL *) DBConnection, MYSQL_SET_CHARSET_NAME, "utf8");
my_bool reconnect = true;
mysql_options(DBConnection, MYSQL_OPT_RECONNECT, &reconnect);
mysql_options(DBConnection, MYSQL_OPT_CONNECT_TIMEOUT, "60");
mysql_real_connect(DBConnection, ...);
printf("reconnect flag %d", DBConnection->reconnect);

相同的结果,但您的确切值似乎会有所不同(我在下面的评论使用了不同的运行并得到了不同的值)。使用 30 个线程启动我的代码:101、101、101、106、109、112、112、115、117、118、120、123、47、56、56、59、62、68、80、81, 81, 88, 88, 88, 90, 91, 91, 92, 93, 94, 96(从日志文件中取出这些的简单方法涉及对其进行排序,因此排序不是原来的顺序)。主要的是它不是手册上说的“1”。位 0 并不总是设置。

最佳答案

根据 MySQL 的版本,自动重新连接行为略有不同;例如,有 --skip-reconnect选项也。 This page解释一下。

希望这对您有所帮助。

关于mysql - MySQL 重新连接标志值是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23124643/

相关文章:

C# MySQL 中的连接太多

mysql - 如何在两个表之间拆分、计数和插入?

C 使用伯克利套接字发送文件

jdbc - 连接到数据源 phpstorm

mysql - 从 CASE 中的不同行返回值

mysql - 当列确实存在时没有这样的列错误

c - UInt8 数组 Swift 到 const char* C

c++ - 在 Windows 上的 Qt Creator 中编译 libmodbus 会引发 undefined reference to '_imp_....' 错误

c# - 如何在没有 SSL 的情况下连接到 MySQL 数据库

c++ - MySQL Connect/C++ 64 位构建错误