mysql TIME_WAIT ; too many connections problem

后端 未结 3 1995
心在旅途
心在旅途 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:23

    If you are getting alot of TIME_WAIT connections on the Mysql Server then that means that Mysql server is closing the connection. The most likely case in this instance would be that a host or several hosts got on a block list. You can clear this by running

    mysqladmin flush-hosts
    

    to get a list of the number of connections you have per ip run,

     netstat -nat | awk {'print $5'} | cut -d ":" -f1 | sort | uniq -c | sort -n
    

    you can also confirm this is happening by going to one of your clients that is having trouble connecting and telnet to port 3306. It will thow a message with something like,

    telnet mysqlserver 3306
    Trying 192.168.1.102...
    Connected to mysqlserver.
    Escape character is '^]'.
    sHost 'clienthost.local' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'Connection closed by foreign host.
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-06 17:44

    As @Zimbabao suggested in the comment, debug your code for any potential errors that may halt the execution of closing the Mysql connection.

    If nothing works, check your my.cnf for a system variable called wait_timeout. If its not present add it to the section [mysqld] and restart your Mysql server.

    [mysqld]
    wait_timeout = 3600
    

    Its the number of seconds the server waits for activity on a noninteractive connection before closing it. Further information can be found http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_wait_timeout

    Tune the figure 3600 (1 hour) to your requirements.

    HTH

    0 讨论(0)
提交回复
热议问题