当前位置:网站首页>. net mysql Too many connections

. net mysql Too many connections

2022-06-12 07:54:00 Ruoyun Liufeng

One 、 problem

        use .net Multithreaded mysql Data query and update , After the discovery program starts , You will report this error .

        Too many connections

Two 、 analysis

        1. Database side

                use show processlist; Check the connection list . Many connections are found sleep, And it keeps increasing .

                show variables like '%max_connections%'; see mysql The maximum number of connections found is only 100 many .

                When the connection increases to the maximum number of connections , Not increasing , There are no new database connections .

        2. Program side

                Frequently used in each thread .net establish mysql The connection of , But it happens every time close.

The code snippet is as follows :

            string ConnectStr = Connectconfig.GetReadNameConfig("MySql");
            MySqlConnection conn = new MySqlConnection(ConnectStr);
            connect.Open();
            // Do something 
            connect.Close();
            connect.Dispose();

        3. The problem is :

         Mingming closes and releases it every time , Why is it mysql Inside the connection , The connection is not automatically released , It's just sleep 了 .

        4. analysis :

         After analysis, as long as the connection is created , also open,mysql There is a discount to store this connection , Whether or not close. because mysql The mechanism is that if you use this connection next time , direct open Just fine . You don't have to create connections every time , Yes mysql It's also consumption .

3、 ... and 、 solve

        Scheme 1 ( Treat the symptoms, not the root cause ): utilize mysql The timeout mechanism of + Set the maximum number of connections

          take mysql Set the maximum number of connections to 1000, Set the timeout to 30( second ), That is to say, before the number of connections reaches the set maximum , The first connection has been released due to timeout . Here's a premise , The first connection will not be used in the future , But generally, this problem can be encountered , It is estimated that the first connection will be difficult to use again in the program .

        Set timeout method :

        max_connections=1024

        Set the maximum number of connections method , Add... To the configuration file :


wait_timeout=30
interactive_timeout=30

         Option two :

        This problem is first encountered frequently in threads   the new Connection operation , If only new once mysql The connection of , Then only use this connection , It can solve the problem .

原网站

版权声明
本文为[Ruoyun Liufeng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010553153441.html