MariaDB服务器在600秒后使客户端连接超时

时间:2018-07-24 19:43:04

标签: mysql sql database mariadb libmysql

我的MariaDB服务器在不活动600秒(10分钟)后,正在使C ++客户端(使用libmariadb)超时,我不确定为什么,因为我找不到任何配置的超时来指定该数字。

这是我的代码,在这里我执行一个简单的SELECT查询,等待11分钟,然后再次运行相同的查询,并收到“服务器不可用”错误:

#include <iostream>
#include <unistd.h>

#include <errmsg.h>
#include <mysql.h>

int main(int, char**)
{
    // connect to the database
    MYSQL* connection = mysql_init(NULL);
    my_bool reconnect = 0;
    mysql_options(connection, MYSQL_OPT_RECONNECT, &reconnect);  // don't implicitly reconnect
    mysql_real_connect(connection, "127.0.0.1", "testuser", "password",
                       "my_test_db", 3306, NULL, 0);

    // run a simple query
    mysql_query(connection, "select 5");
    mysql_free_result(mysql_store_result(connection));
    std::cout << "First query done...\n";

    // sleep for 11 minutes
    sleep(660);

    // run the query again
    if(! mysql_query(connection, "select 5"))
    {
        std::cout << "Second query succeeded after " << seconds << " seconds\n";
        mysql_free_result(mysql_store_result(connection));
    }
    else
    {
        if(mysql_errno(connection) == CR_SERVER_GONE_ERROR)
        {
            // **** this happens every time ****
            std::cout << "Server went away after " << seconds << " seconds\n";
        }
    }

    // close the connection
    mysql_close(connection);
    connection = nullptr;

    return 0;
}

服务器进程的标准输出报告说我的连接超时:

$ sudo journalctl -u mariadb
...
Jul 24 17:58:31 myhost mysqld[407]: 2018-07-24 17:58:31 139667452651264 [Warning] Aborted connection 222 to db: 'my_test_db' user: 'testuser' host: 'localhost' (Got timeout reading communication packets)
...

看看tcpdump捕获,我还可以看到服务器向客户端发送了一个TCP FIN数据包,该数据包关闭了连接。

我感到难过的原因是因为我没有更改任何默认超时值,甚至没有600秒的默认超时值。

MariaDB [(none)]> show variables like '%timeout%';
+-------------------------------------+----------+
| Variable_name                       | Value    |
+-------------------------------------+----------+
| connect_timeout                     | 10       |
| deadlock_timeout_long               | 50000000 |
| deadlock_timeout_short              | 10000    |
| delayed_insert_timeout              | 300      |
| innodb_flush_log_at_timeout         | 1        |
| innodb_lock_wait_timeout            | 50       |
| innodb_print_lock_wait_timeout_info | OFF      |
| innodb_rollback_on_timeout          | OFF      |
| interactive_timeout                 | 28800    |
| lock_wait_timeout                   | 31536000 |
| net_read_timeout                    | 30       |
| net_write_timeout                   | 60       |
| slave_net_timeout                   | 3600     |
| thread_pool_idle_timeout            | 60       |
| wait_timeout                        | 28800    |
+-------------------------------------+----------+

那服务器为什么超时我的连接?根据文档,我本来以为是wait_timeout服务器变量,但是默认保留了8小时……

顺便说一句,我正在使用MariaDB 10.0和libmariadb 2.0(来自Ubuntu Xenial Universe存储库)


编辑:这是捕获断开连接的tcpdump捕获的图像。我的Wireshark过滤器为tcp.port == 55916,因此我正在查看往返于此客户端连接的流量。服务器发送的FIN数据包是数据包1199,恰好在前一个数据包(884)之后600秒。 pcap opened in wireshark

3 个答案:

答案 0 :(得分:1)

wait_timeout很棘手。通过相同连接执行

SHOW SESSION VARIABLES LIKE '%timeout%';
SHOW SESSION VARIABLES WHERE VALUE BETWEEN 500 AND 700;

您应该能够通过执行

解决此问题
mysql_query("SET @@wait_timeout = 22222");

您是否以“ root”身份连接?

更多连接器详细信息:

请参阅: https://dev.mysql.com/doc/refman/5.5/en/mysql-options.html

  

CLIENT_INTERACTIVE:在关闭连接之前,允许Interactive_timeout秒处于不活动状态(而不是wait_timeout秒)。客户端的会话wait_timeout变量设置为会话Interactive_timeout变量的值。

https://dev.mysql.com/doc/relnotes/connector-cpp/en/news-1-1-5.html(MySQL Connector / C ++ 1.1.5)

  

还可以使用MySQL_Statement :: getQueryTimeout()和MySQL_Statement :: setQueryTimeout()方法获取并设置语句的执行时间限制。

可能还会有TCP / IP超时。

答案 1 :(得分:1)

I'm not sure about the exact reason. But I'm sure wait_timeout is not the only thing which has an effect on this. According to the only error message you have included in your question, it seems like there was a problem reading the packet.

Got timeout reading communication packets

I believe it was more like MariaDB had an issue reading the packet rather than attempting to connect or so. I also had a look at the MariaDB client library, and found this block;

if (ma_net_write_command(net,(uchar) command,arg,
            length ? length : (ulong) strlen(arg), 0))
  {
    if (net->last_errno == ER_NET_PACKET_TOO_LARGE)
    {
      my_set_error(mysql, CR_NET_PACKET_TOO_LARGE, SQLSTATE_UNKNOWN, 0);
      goto end;
    }
    end_server(mysql);
    if (mariadb_reconnect(mysql))
      goto end;
    if (ma_net_write_command(net,(uchar) command,arg,
              length ? length : (ulong) strlen(arg), 0))
    {
      my_set_error(mysql, CR_SERVER_GONE_ERROR, SQLSTATE_UNKNOWN, 0);
      goto end;
    }
}

https://github.com/MariaDB/mariadb-connector-c/blob/master/libmariadb/mariadb_lib.c

So it seems like it sets the error code to server gone away when it get a packet size issue. I suggest you to change the max_allowed_packet variable to some large value and see whether it has any effect.

SET @@global.max_allowed_packet = <some large value>;

https://mariadb.com/kb/en/library/server-system-variables/#max_allowed_packet

I hope it will help, or at least it will set you in some path to solve the problem :) and finally, I think you should handle the disconnects in your code rather than relying on the timeouts.

答案 2 :(得分:0)

具有Haproxy负载平衡的Galera群集。在haproxy设置上更改此参数 默认值     超时连接10秒     超时客户端30秒     超时服务器30秒