mysql TIME_WAIT ; too many connections problem

后端 未结 3 1996
心在旅途
心在旅途 2021-02-06 16:47

When i was checking for mysql load time on site. i got result showing connections as TIME_WAIT. Even though i close connection on every page. Sometimes the site doesnt load say

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-06 17:39

    If a client connects to a MySQL-Server, it usually opens a local port, example:

     localhost:12345 -> mysqlserver:3306
    

    If the client closes the connection, the client gets a TIME_WAIT. Due to TCP routing, a packet might arrive late on the temporary port. A connection in TIME_WAIT just discards these packets. Without a TIME_WAIT, the local port might be reused for another connection and might receive packets from a former connection.

    On an high frequent application on the web which opens a mysql-connection per request, a high amount of TIME_WAIT connections is expectable. There is nothing wrong with it.

    Problems can occur, if your local port range is too low, so you cannot open outgoing connections any more. The usual timeout is set to 60 seconds. So a problem can already occur on more than 400 requests per second on low ranges.

    Check:

    To check the amount of TIME_WAIT, you can use the following command:

    $ cat /proc/net/sockstat
    sockets: used 341
    TCP: inuse 12 orphan 0 tw 33365 alloc 23 mem 16
    UDP: inuse 9 mem 2
    UDPLITE: inuse 0
    RAW: inuse 0
    FRAG: inuse 0 memory 0
    

    The value after "tw", in this case 33365, shows the amount of TIME_WAIT.

    Solutions:

    a. TIME_WAIT tuning (Linux based OS examples):

    Reduce the timeout for TIME_WAIT:

    # small values are ok, if your mysql server is in the same local network
    echo 15 > /proc/sys/net/ipv4/tcp_fin_timeout
    

    Increase the port range for local ports:

    # check, what you highest listening ports are, before setting this
    echo 15000 65000 > /proc/sys/net/ipv4/ip_local_port_range
    

    The settings /proc/sys/net/ipv4/tcp_tw_recycle and /proc/sys/net/ipv4/tcp_tw_reuse might be interesting, too. (But we experienced strange side effects with these settings, so better avoid them. More informations in this answer)

    b. Persistent Connections

    Some programming languages and libraries support persistent connections. Another solution might be using a locally installed proxy like "ProxySQL". This reduces the amount of new and closed connections.

提交回复
热议问题