I'm getting a MySQL "Too many connections" error in a C++ program running on Ubuntu Linux.
在Ubuntu Linux上运行的c++程序中,我遇到了一个“太多连接”错误。
This is the code that gets the error (it's inside a method that returns the mysql error, if any):
这是获得错误的代码(它位于返回mysql错误(如果有的话)的方法内):
MYSQL connect;
mysql_init(&connect);
if (!mysql_real_connect(&connect,SERVER,USER,PASSWORD,DATABASE,0,NULL,0))
{
return mysql_error(&connect);
}
This code keeps returning the string "Too many connections."
此代码不断返回字符串“太多连接”。
I'm wondering if this is actually some other error. This program has been working for months before I got this error. When the error first appeared it was because I had run the program against several thousand updates/reads and so yes, it's highly likely that I used up the available connections. The problem is, I can't find a way to release them, if that's what it is.
我想知道这是不是另一个错误。这个程序已经运行了好几个月,我才发现这个错误。当错误第一次出现的时候,是因为我在几千次更新/读取中运行了程序,所以是的,很有可能我耗尽了可用的连接。问题是,我找不到释放它们的方法,如果真的是这样的话。
Here is what I have tried:
以下是我尝试过的:
- FLUSH HOSTS;
- 冲洗主机;
- FLUSH TABLES;
- 刷新表;
- restarting MYSQL
- 重新启动MYSQL
- rebooting the machine altogether
- 完全重新启动机器
It has been over 12 hours since this error first appeared, so if it is the connections then nothing is being reset/released. I would have thought rebooting the machine would have released something.
这个错误第一次出现已经超过12个小时了,所以如果是连接,那么没有什么是重置/释放的。我本以为重启机器会释放一些东西。
2 个解决方案
#1
3
See C.5.2.7. Too many connections.
看到C.5.2.7。太多的连接。
View all MySQL connections.
查看所有MySQL连接。
netstat -apn | grep mysql | grep -i established
Tips
- Build and return connection object only when connection pointer is null or connection to DB is unavailable.
- 仅当连接指针为null或连接到DB时才建立和返回连接对象。
- Use one connection pool for the entirety of the session.
- 整个会话使用一个连接池。
- Close the connection at the end of each session and release/clean the connection pointer.
- 在每次会话结束时关闭连接,并释放/清理连接指针。
- Increase
max_connections=#
in /etc/mysql/my.cnf or restart MySQL with--max_connections=#
- 在/etc/mysql/my.cnf中增加max_connections=#,或者使用—max_connections=#重新启动MySQL
#2
0
Make sure you close connection when you are done with them.
当你用完它们的时候,一定要保持紧密的联系。
Consider reusing connections or connection pooling.
考虑重用连接或连接池。
#1
3
See C.5.2.7. Too many connections.
看到C.5.2.7。太多的连接。
View all MySQL connections.
查看所有MySQL连接。
netstat -apn | grep mysql | grep -i established
Tips
- Build and return connection object only when connection pointer is null or connection to DB is unavailable.
- 仅当连接指针为null或连接到DB时才建立和返回连接对象。
- Use one connection pool for the entirety of the session.
- 整个会话使用一个连接池。
- Close the connection at the end of each session and release/clean the connection pointer.
- 在每次会话结束时关闭连接,并释放/清理连接指针。
- Increase
max_connections=#
in /etc/mysql/my.cnf or restart MySQL with--max_connections=#
- 在/etc/mysql/my.cnf中增加max_connections=#,或者使用—max_connections=#重新启动MySQL
#2
0
Make sure you close connection when you are done with them.
当你用完它们的时候,一定要保持紧密的联系。
Consider reusing connections or connection pooling.
考虑重用连接或连接池。