我在Asp .Net Mvc应用程序中遇到“太多连接”问题,当我通过Mysql v6.56 IDE手动终止进程时得到修复,但是在远程主机计算机上我每次都无法杀死进程怎么能我修复了这个错误。
我尝试连接到information_schema
DB的PROCESSLIST
表,但是当连接即将执行命令时,会出现“root @ loclahost information_schema的访问被拒绝”错误。我也试图授予root @ loclahost所有权限,但我仍然无法解决这个问题。我在过去的两年里一直以相同的方式进行编码,但是在这个应用程序中,我遇到了这个问题,我使用了每个方法中的每个连接。如果有人遇到过这个问题或者知道答案的话。请帮帮我。 Thanx提前
答案 0 :(得分:3)
您需要通过在连接对象上调用close()来关闭代码中的连接。通过MySQL服务器杀死线程是一个可怕的想法
答案 1 :(得分:0)
' code in vb.net you can convert it to c# easily
Dim ConnectionString As String = "your connection string"
Public Function KillAllMySQL() As Integer
Dim query As String = "SHOW FULL PROCESSLIST"
'
Try
Dim mySqlConnection As MySqlConnection = New MySqlConnection(ConnectionString)
' 2. open connection to database using connectionString
mySqlConnection.Open()
' 3. Create a command to hold values
Dim objCmd As New MySqlCommand(query, mySqlConnection)
' 4. Add parameters for sqlCommand
mySqlDataReader = objCmd.ExecuteReader()
If mySqlDataReader.HasRows Then
Do While mySqlDataReader.Read()
' kill processes with elapsed time > 200 seconds and in Sleep
If mySqlDataReader.GetInt32(5) > 200 And mySqlDataReader.GetString(4) = "Sleep" Then
KillMySqlProcess("KILL " & mySqlDataReader.GetInt32(0))
End If
Loop
End If
If Not mySqlDataReader Is Nothing Then mySqlDataReader.Close()
If Not mySqlConnection Is Nothing Then mySqlConnection.Close()
Catch ex As MySqlException
Return -1
End Try
Return 0
End Function
Public Function KillMySqlProcess(ByVal myQuery As String) As Integer
'1. Create a query
Dim query As String = myQuery
'
Try
Dim mySqlConnection As MySqlConnection = New MySqlConnection(ConnectionString)
' 2. open connection to database using connectionString
mySqlConnection.Open()
' 3. Create a command to hold values
Dim objCmd As New MySqlCommand(query, mySqlConnection)
objCmd.ExecuteNonQuery()
mySqlConnection.Close()
Catch ex As MySqlException
Return -1
End Try
Return 0
End Function