Best Practice for Handling Too Many mysql Connections

<?php
// Connect to mysql server
$link = mysql_connect(HOST, USER, PASSWORD);
if(!$link) {
    die('Could not connect to server: ' . mysql_error());
}

// Select database
$db = mysql_select_db(DATABASE);
if(!$db) {
    die('Cannot use the database');
}
mysql_set_charset('charset=utf8', $link); 

//code

?>

      

I am starting php. I wrote a simple php HTTP API that read some data from the database and returned in the body. This API is available asynchronously to many applications . When we increase the number of users to more than 200 per second, I get the following warning

PHP Warning:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Too many connections in /home/ws/public_html/include/get_details.php on line 3

      

I want to know the best solution for it.

thank

+3


source to share


1 answer


first you can use mysqltuner to check your db status. Other solutions, use redis or memcache to reduce connections. If you have your sessions stored in the DB try changing and using redis. phpredis



+1


source







All Articles