Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best practice to handle 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 php beginer.I have written a simple php HTTP API which read some data from database and return in body. This API is accessed by many uses application asynchronously. When we increase the number of user more than 200 each second, i nam getting 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 best solution for it.

Thanks

like image 899
user1725661 Avatar asked Jan 18 '26 17:01

user1725661


1 Answers

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

like image 98
Bartolome Serapio Avatar answered Jan 20 '26 08:01

Bartolome Serapio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!