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
Related
I'm currently querying a huge Firebird (v2.5) table (with millions of rows) in order to perform some row-level operations. To achieve that, the code is using chunking from Laravel 5.1, somewhat like this:
DB::connection('USER_DB')
->table($table->name)
->chunk(min(5000, floor(65500/count($table->fields))), function($data) {
// running code and saving
});
For some reason, I keep receiving the following error:
SQLSTATE[HY000]: General error: -902 Error reading data from the connection.
I've already tried changing chunk size, and different codes, but the error still appears. Sometime it happens at the beginning of the table, and sometimes after parsing several hundred-thousands or even millions rows. The thing is that I need to parse only the rows in this transaction (so I can't stop and reopen the script).
Tested for memory on the server (running on different place than the database), and it is not using nearly anything of it.
While writing this, I rechecked the Firebird log and found the following entry:
INET/inet_error: read errno = 10054
As far as I could find, this isn't actually a Firebird problem, but a winsock reset error, is that correct? If so, how could I prevent this from happening during the chunk query? And how can I check if that is a problem with windows or the firewall?
Update I
Digging on the firebird2.5.log on the PHP server, found this errors:
INET/inet_error: send errno = 104
REMOTE INTERFACE/gds__detach: Unsuccesful detach from database.
I have found the root of my problem. The thing is that the server was resetting the connection. In order to avoid that, I added a "heartbeat" query to run every few minutes. With this strategy I was able to prevent the connection from being reset.
Very simple-to-explain problem here (at least after struggling with it and simplifying it almost up to the absurd).
I do a connection via the OCI driver inside a file called whatever.Test.php. Just that, make a simple query, and exit. I then call the file from the browser, and the data taken from the database is properly displayed. Next step: I run PHPUnit over the same file, with the following result:
PHP Warning: oci_connect(): ORA-12154: TNS:could not resolve the connect identifier specified in ...
Any ideas?
The final solution was a little bit obvious once I came up with it. Somehow the server could figure out what the server I was trying to connect with was, via the tnsnames.ora file somewhere, I guess. However, since the script execution doesn't trigger all the server processes this information was not reachable any more from there. So what I needed to do is to provide all the data in the oci_connect() $connection_string parameter, using the format [//]host_name[:port][/service_name][:server_type][/instance_name] (check documentation). Previously I was only giving the host_name part.
(Thanks anyway for your reply, user*).
I am currently running an amazon EC2 instance to host my website. I am trying to connect to the MySQL database.
I have code ...
<h1>Test page</h1>
<?php
$connection = mysql_connect("xxx.xxxx.us-west-2.rds.amazonaws.com","username","password");
?>
An attempt to load this web page will cause an infinite loading symbol. It is as if an infinite loop is running. Here are some details ...
The page will load if i remove the mysql_connect() function
The page will properly display all php data including all other functions.
The "host","username" and "password" arguments are correct and the MySQL database server is running. I know this because i can connect and manipulate it through Sequel Pro.
The page will load if i modify the "host" argument to something incorrect.
Feel free to ask questions. Thanks.
EDIT: After waiting about 90 seconds, the page will eventually load and display error 2002.
Try using:
mysqli_connect()
instead of
mysql_connect()
So maybe this server only supports mysqli.
I'm not sure what else to try if everything else is right.
This means that the server is not responding to the request you sent and so mysql connection is not being established. Try using another server.
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
I am using SQL Server as my backend for program.when i am try to connect with database which not shows any type of errors,but it is not working.Which is not getting connection.
My code is like
mssql_connect('servername', 'db_user', 'db_password') or die('Error');
The funny thing is which is not connecting and also which is not show message Error.
How can we enable SQL Server configuration using php program (what is the script for that.not manually). How could we know the actual status of SQL Server in server?
My application is working properly in local;the problem is about public hosting.
turn on error_reporting in your php.ini. it might be some other section of code that is causing the issue, since you say that it doesn't go to die part. try checking first if it even goes to the mysql_connect() part. if possible post a part of your code around the mysql_connect.
I would bet it is localhost, I have, nor do I ever wish to, see the database sitting on the same server as the website. Check in your hosting control panel for the proper connection string.