I wanted my web system to automatically reconnect to the database if it reaches to maximum user connection. Or is there anyway i could reload the page automatically till it is connected to the database
$conn = new mysqli(DB_HOST,DB_USER,DB_PSWD,DB_NAME);
if($conn->connect_error)
die("Failed to connect database ".$conn->connect_error );
You can setting the retry variable($retry) as a flag to mark the DB connection status with default value you want. While connect DB , if it's ok then update retry flag = 0($retry=0) , else reduce the retry one unit ($retry--). Also you don't die process when have error exception.
You can enclose the connection in a function and call it many times you want to retry the connection:
function connectDB(DB_HOST,DB_USER,DB_PSWD,DB_NAME)
{
return $conn = new mysqli(DB_HOST,DB_USER,DB_PSWD,DB_NAME);
}
if(connectDB()->connect_error)
{
sleep(1);
if(connectDB()->connect_error)
{
die("Failed to connect database ".$conn->connect_error );
}
}
With sleep(1) you delay the script to retry the connection after 1 second.
Anyway, you should find the cause of the connection error and solve it; this solution may help you in the case the server is sometimes slow to respond.
Related
duplicate of unanswered: How to reconnect in php adodb after exceptions: Mysql server gone away or Lost connection to MySQL server during query
mysql_connect works the first time, but never works after that...
$connectDb = mysql_connect(secret, secret, secret);
mysql_select_db("secret", $connectDb);
$sleepPeriod = 1800;
sleep($sleepPeriod);
while (true) {
$result = mysql_query("good query", $connectDb);
if (!$result) {
if (mysql_error()=='MySQL server has gone away') {
echo "MySql connection was disconnected... reconecting...\n";
$connectDb = mysql_connect(secret, secret, secret);
mysql_select_db("secret", $connectDb);
continue;
} else {
die("Invalid Query: ".__FILE__.':'.__LINE__.' '.mysql_error()."\n");
}
}
//DO STUFF
sleep($sleepPeriod);
}
if a timeout or disconnect happens mysql_connect seems to fail and mysql_error continually returns "MySQL server has gone away", which results in an infinite loop that can go for days. is there some other way to clear the error response of mysql_error or to make mysql_connect run a second time without having to restart this program manually or resorting to cron.
I just noticed that mysql_connect has a strange(stupid?) parameter called new_link, however it would be an outrageous design if php's mysql code purposefully disables reconnects on timeout by default... I'll test regardless and get back.
mysql_connect doesn't care if the connection has disconnected or timed-out it will never connect a second time unless you call it with the parameter new_link with the value true.
mysql_connect($server,$username,$password,true);
I have the following code
function openDBConn($params){
$conn_mode = $params['conn_mode'];
$db_conn = $params['db_conn'];
//create connections
if(empty($db_conn->info)) {
$db_conn = new mysqli("localhost", $user, $password, "database");
$db_conn->set_charset('UTF8');
$mysqli_error = $db_conn->connect_error;
}
if($mysqli_error !== NULL){
die('Could not connect <br/>'. $mysqli_error);
}else{
return $db_conn;
}
}
//close db connection
function closeDBConn( $params ){
$db_conn = $params['db_conn'];
$db_conn->close;
}
//used as below
$db_conn = openDBConn();
save_new_post( $post_txt, $db_conn );
closeDBConn( array('db_conn'=>$db_conn));
From time to time, I get the "Could not connect. Too many connections" error.
This tends to happen when I have Google bot scanning my website.
This problem seems to have started ever since upgrading to MySQLi from MySQL.
Is there any advice on how ensure all connections are closed?
Thanks
You need to increase the number of connections to your MySQL server (the default is only 100 and typically each page load consumes one connection)
Edit /etc/my.cnf
max_connections = 250
Then restart MySQL
service mysqld restart
http://major.io/2007/01/24/increase-mysql-connection-limit/
Some hosters have a hard limit how many open database connections your are allowed to have. Maybe you want to contact your hoster to know how many you are allowed to open. For websites with hight traffic load more connections can be helpful.
Do you have access to the server directly or is it a hosted solution?
If you have direct access you can check the mySQL config files to see how many connections are allowed and increase it.
If you don't you might want to contact your webhost about increasing the limit and see if they will comply.
Here's an example of php code to make a connection to mysql and perform a select query using adodb :
include('adodb.inc.php'); # load code common to ADOdb
$db = &ADONewConnection('mysql');
$db->PConnect("localhost", "root", "password", "database");
$recordSet = &$conn->Execute('select * from products');
if (!$recordSet)
print $conn->ErrorMsg();
else
while (!$recordSet->EOF) {
print $recordSet->fields[0].' '.$recordSet->fields[1].'<BR>';
$recordSet->MoveNext();
}
$recordSet->Close(); # optional
$conn->Close(); # optional
?>
Do i have to use
$db = &ADONewConnection('mysql');
$db->PConnect("localhost", "root", "password", "database");
and
$recordSet->Close(); # optional
$conn->Close(); # optional
each time i want to make a query to unsure the error of max_connection reached ?
How can i manage when 1000 users or more are connected to my website with MySQL's max_connection = 100 ?
When the maximum number of connections has been reached, your $db->PConnect should throw an exception or return an error code (I don't know this driver too much, please check the man pages). You must watch this error and act accordingly in case of error. Typically, wait a few seconds, and try again a couple of times before returning an error to the user.
Now, the max_connection is the limit of concurrent connections. 1000 users connected to your application are (hopefully) not all running a query at the same time, so you should be safe for a while. At the end of a script execution, all connections are closed (or returned to the pool in your case), and become available to other users. So you will not reach your limit of 100 unless 100 users are actually clicking at the same time on some link in your application.
But you should write your scripts so that they open (or acquire) a connection as late as possible during the course of their execution, and close (or release) the connection as early as possible. This way, the connection is held for a span of time as short as possible, making it less likely to hit the limit.
Now, if you do reach the limit, then there is nothing else you can do but increasing the limit. The only workaround is to put exceeding connection requests on hold (as I suggest in the first paragraph).
I'm currently writing a PHP application and i noticed that my page loads kinda slow. I takes about 2 seconds (2.0515811443329 to be exact).
I've tracked down what the bottleneck was and it's the part where i'm creating a PDO connection to my MySQL database.
My 'connect()' method doesn't do any exoctic stuff. It simply looks like this:
public function connect ( $database, $host, $username, $password )
{
try
{
$this->db = new \PDO("mysql:dbname=".$database.";host=".$host, $username, $password);
if ( !$this->db )
{
throw new \Exception('Failed to connect to the database!');
}
$this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
catch ( \Exception $e )
{
echo '<strong>Exception: </strong>'.$e->getMessage();
return false;
}
return true;
}
So when i comment out the call to the 'connect()' method, then my page loads in: 0.035506010055542
This is a huge difference. I can imagine that creating a connection to a database does take up some time, but it takes more than 1,5 seconds... I'm not sure if this is normal?
If it is normal, that it takes up that amount of time then is there a way to store the database connection? Like putting it in a session? Actually, as far as i know storing it in a session isn't possible. But it would be the ideal solution. Storing the connection somewhere until the user closes his browser.
In anyway, is there a problem with my PDO / MySQL? And can i simply store the connection resource somehow? So that i don't have to reconnect to my database everytime for every new page?
PS. I'm doing this all on a localhost (Windows).
You're probably making a connection with 'localhost' as address. Try to change that to '127.0.0.1'. That should fix the problem.
You can create a persistent connection to database using PDO. From the manual
Many web applications will benefit from making persistent connections to database servers. Persistent connections are not closed at the end of the script, but are cached and re-used when another script requests a connection using the same credentials. The persistent connection cache allows you to avoid the overhead of establishing a new connection every time a script needs to talk to a database, resulting in a faster web application.
And example:
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
PDO::ATTR_PERSISTENT => true
));
?>
I have a Master - Slave setup for a web application written in PHP. I have a pool of slaves I use for reading, and a Master that is used for writes (and reads if a write has been sent this request). I would like to incorporate an automated system for removed crashed servers from the read pool. Currently I am using:
foreach($readers as $reader)
{
$fp = #fsockopen($reader['host'],3306,$errno,$errstr,1);
if(!$fp)
{
//Remove from pool
}
unset($fp);
}
My primary question is there a more reliable method. I have had quite a few false positives, and vice versa because it is not actually checking for a MySQL server, but rather just a connection on port 3306. Is there a way to check for a MySQL server without raising an exception, which is the behaviour of the PDO and MySQLi extensions in PHP.
You could just use mysql_connect() and check the result for false, and close the connection right away on success. You can make a dummy account with no privileges for that if you like.
That's really the only reliable way, especially if you want to distinguish a running MySQL server from any other random process listening on port 3306.
You could use mysql_ping() to check if a current DB Connection you have open is still alive
Here is the example posted at http://www.php.net/manual/en/function.mysql-ping.php
<?php
set_time_limit(0);
$conn = mysql_connect('localhost', 'mysqluser', 'mypass');
$db = mysql_select_db('mydb');
/* Assuming this query will take a long time */
$result = mysql_query($sql);
if (!$result) {
echo 'Query #1 failed, exiting.';
exit;
}
/* Make sure the connection is still alive, if not, try to reconnect */
if (!mysql_ping($conn)) {
echo 'Lost connection, exiting after query #1';
exit;
}
mysql_free_result($result);
/* So the connection is still alive, let's run another query */
$result2 = mysql_query($sql2);
?>
The best way to check if any service is alive is to actually use it. So for MySQL try to connect and execute some fast query, for web server try to fetch some file, for PHP try to fetch some simple script...
For MySQL master/slave setup, one of the solutions is to actually check the state of replication. You can check how many transactions is the slave behind master and decide to stop using that slave when/while it has old data. (I don't do the replication myself, but I think you need to compare the variables Read_Master_Log_Pos and Relay_Log_Pos)