While I am browsing my online app in the server I got an error like
Database Connection Failed
User coule_com#c17564 already has more than 'max_user_connections' active connections.
But this is working well in my local system. And this error occurs ANY TIME when I navigate in the server. If i refresh the browser i can able to move further. But I in need to solve this issue.
will anybody help me to solve this issue ?
connection code :
function makeConnection() {
global $config;
$this->ConLink = mysql_pconnect($config['DBHostName'],$config['DBUserName'],$config['DBPassword']) or die("Database Connection Failed". mysql_error());
mysql_select_db($config['DBName'], $this->ConLink);
return true;
}
Are you closing the connections when you're done with them?
If not, then I would assume there's lots of database connection objects lying around just waiting for GC to pick them up, and until it does, you risk running out of available connections.
you need to contact your host and get them to up the connection limit. if they won't, you need to find a better host. this is simply a fact of life in web-based applications.
Related
I'm building a web app that uses lots of requests to my database. Every thing was working perfectly smooth until a half an hour ago when the requests weren't returning... I checked the PHP file directly and it displays the following:
<br />
<b>Warning</b>: mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Too many connections in <b>/home/sanity/public_html/dev/forest/js/database.php</b> on line <b>7</b><br />
Unable to connect to MySQL
So I figured let's check phpMyAdmin, but it's not showing me ANYTHING except for a big red box that says:
SQL query: Edit Edit
SET CHARACTER SET 'utf8';
MySQL said: Documentation
#1045 - Access denied for user 'root'#'localhost' (using password: NO)
Between the last time it worked and now I haven't changed any configurations or code.. How do I begin to fix this?
Could this be caused by the fact my PHP files don't close the connection after using it? If so should I be closing the connection after every query? I figured the connection would close automatically when the user leaves the web site.
EDIT: The requests are sending through now and phpMyAdmin is back up, but how do I prepare this site for heavier traffic?
When I started my job, one of my first tasks was to continue working on what one of the directors had started coding. In his code, I saw this monstrosity:
function getTicket($id) {
mysql_connect("localhost","username","password");
mysql_select_db("database");
$sql = mysql_query("select * from tickets where id = ".intval($id));
return mysql_fetch_assoc($sql);
}
In other words, he was creating a whole new database connection every single time he wanted something from the database, and never closing any of them (instead letting them be closed at the end of the script automatically)
This was fine for basic testing, but as soon as I started writing more advanced stuff (before I'd discovered this piece of code) things majorly screwed up with the same "too many connections" error as you.
The solution was simple: restart the server to clear all pending connections, and fix the code to only connnect once per script execution.
This is what you should do. There should only ever be one call to mysql_connect (or indeed any database library's connect function) in your script (and I don't mean in a function that gets called several times!)
You should also check the database's configuration, just in case someone accidentally set the maximum connections too low, but generally this shouldn't be a problem if you manage your connections properly.
Though the mysql_* functions are deprecated (use a modern driver like PDO for instance), you should take a look at the mysql_close($con) function.
Here is the doc.
EDIT
If you are not using mysql_pconnect function, then your connection should be closed at the end of the execution of your script.
Apparently, one reason for such error is shared hostings. If you are using a shared hosting, generally speaking, the maximum connections to the server allowed by the hosting is not the greatest.
If you can change the max_connections system variable then try to change it to a greater number:
max_connections = 200
I started getting this error when trying to query the database on a connection that had timed out.
Fatal error: Uncaught exception 'ErrorException' with message 'mysql_query(): MySQL server has gone away'
So I did a bunch of research and 99% of the users on the forum say you can use the mysql_ping command to check for a connection, so I put this in place:
if(!mysql_ping($this->sDBLink))
Connect(true);
Now I get the same error, just in reference to the mysql_ping function instead of the mysql_query function:
Fatal error: Uncaught exception 'ErrorException' with message 'mysql_ping(): MySQL server has gone away'
How do I reliably check that a connection still exists? mysql_ping throws an exception.
Why does a connection time out? Because it has been unused for a period of time longer than a particular threshold. This kind of thing is necessary for all kinds of network applications to make them resilient. What if a user abruptly switches off a client machine while it is connected to a MySQL server? The server needs to be able to drop the client connection after a while.
This kind of thing is inherent in network programming. It's a feature, not a bug.
What can you do about this?
You can switch to a more modern mysql connection management library that handles this stuff.
Especially if your client software gets used infrequently, you can reorganize your software to connect to MySQL, use the connection, and disconnect. That way you won't need a persistent connection. But that's impractical if your client server gets used a lot; there's a lot of overhead to establishing a connection.
You can change the timeout value. See here. how to change timeout for mysql persistent connections
You can use the connection regularly. mysql_ping uses the connection without actually doing any server work. So would a query that said something like SELECT 1. If you ping the connection every minute or so, then a two minute timeout won't cause your MySQL server to conclude that your client has gone away and disconnect it.
You can handle the ErrorException you're getting correctly, by trying to re-establish the connection instead of just blowing out your program with an error message. Use PHP code something like this:
try {
some operation on the mysql connection.
}
catch (ErrorException $ex) {
disconnect mysql
connect mysql
}
So I know this is ugly and makes me sick it, but it will work until we can get enough income to warrant an upgrade.
All I did was close the connection then recreate it as I know the connection will have timed out almost 95% of the time.
if($this->sDBLink){
mysql_close($this->sDBLink);
}
if($bPersistant){
$this->sDBLink = mysql_pconnect($this->sHostname, $this->sUsername, $this->sPassword);
} else {
$this->sDBLink = mysql_connect($this->sHostname, $this->sUsername, $this->sPassword);
}
I'm developing an application, in which two tables of a database are connected but not the third. (This is on server. They are working fine on my local machine.)
1. Warning: mysql_connect() [function.mysql-connect]: [2002] No
connection could be made because the target machine actively (trying
to connect via tcp://localhost:3306) in D:\Hosting.. on line 5
2. Warning: mysql_connect() [function.mysql-connect]: No connection could be made because the target machine actively refused it. in D:\Hosting.. on line 5
At an earlier stage, I connected to the two tables from the same database successfully, so I've checked the physical location of each table of the database. No wonder all are physically resting at the same place in the database.
Please help me to understand the problem.
EDIT :
//Page1 has something like:
$conid=mysql_connect("dxxs.db.7xx7.hostedresource.com","dxxs","Kxx3") or die();
mysql_select_db("dxxs",$conid) or die();
mysql_query(“some basic 4-5 queries depending on user’s interaction with table1”);
mysql_close($conid);
//redirection to page2 from there to page3
$conid=mysql_connect("dxxs.db.7xx7.hostedresource.com","dxxs","Kxx3") or die();
mysql_select_db("dxxs",$conid) or die();
mysql_query(“some basic 4-5 queries depending on user’s interaction with table2”);
mysql_close($conid);
//some redirection to page4
$conid=mysql_connect("dxxs.db.7xx7.hostedresource.com","dxxs","Kxx3") or die();
mysql_select_db("dxxs",$conid) or die();
mysql_query(“some basic 4-5 queries depending on user’s interaction with table3”);
mysql_close($conid);
Now my problem is when connecting to table 3, the server is throwing an error as
stated above
Where in line 5, MySQL_connect is written. Please help me to understand why table 3 is not getting connected whereas table 1 & table 2 are able to connect. All are resting at the same database on the server.
It sounds like you are calling mysql_connect() once for each table you are working with. You do not need to do this - you only need to connect once for each server you are working with.
The server is probably limiting you to 2 concurrent connections, which is why it is refusing the third.
Try calling mysql_connect() once at the top of your script.
I have found the issue and solved it. Try to use my code using port after hostname:PORT:
$dbh=#mysql_connect('dbxxxxxx.db.1and1.com:3306','db-username','db-password');
if (!$dbh) {
$err_msg='Cannot connect to the database because: '.mysql_error();
}
I'm using Zend Freamwork for my website. And sometimes i get the following exception from my website:
Message: SQLSTATE[42000] [1203] User elibrary_books already has more than 'max_user_connections' active connections
As I know "Zend Freamwork" uses PDO to connect to the database.
How i can resolve this problem?
Always close your connection. If you are using Sql class it looks like:
$sql->getAdapter()->getDriver()->getConnection()->disconnect();
Sometimes MySQL connection thread is not thrown away even though you've torn down the socket cleanly; it still hangs around waiting for it to be reaped.
Check your settings for wait_timeout. Default value is unreasonably long. Optimal value might be around 20 seconds. You will probably also want it this low if you're using persistent connections.
Try setting the persistent flag in your database driver configuration to be false:
resources.db.params.persistent = 0
Very simple problem but one which I am unable to solve. Why would I get a ftp_connect() fail?
$conn = ftp_connect("www.server.com");
Have tried SSL connect. Intrestingly I had no problem with this ftp_connect() on this server until recently when I started getting error messages. Not too sure what triggered this perhaps an update as the code was not altered.
Thanks in advance
Edit: Found this in Plesk : Shell access to server with FTP user's credentials - ForbiddenHave no idea if its relevant
Ok, after lots of fiddling (asking someone else) this is the solution I found...
$conn = ftp_connect(localhost);
Well the simplest solution is the best. This works as the code is situated on the server to which it is trying to connect.