SQLSTATE[08004] [1040] Too many connections Error

Dear Team,

We had the fatal error below when we upgraded our system to 3.3.0.17 PHP 8.0
This error is repeating it self which make log file so large. I looked the forum to see if someone faced the same issue but got nothing
your help is highly appreciated.
-Salam

[27-May-2024 09:25:40 UTC] PHP Fatal error:  Uncaught PDOException: SQLSTATE[08004] [1040] Too many connections in /***/***public_html/lib/pkp/lib/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:38
Stack trace:
#0 /***/***public_html/lib/pkp/lib/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php(38): PDO->__construct('mysql:host=loca...', '*********", '*********", Array)
#1 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(67): Doctrine\DBAL\Driver\PDOConnection->__construct('mysql:host=loca...', '*********", '*********", Array)
#2 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(46): Illuminate\Database\Connectors\Connector->createPdoConnection('mysql:host=loca...', '*********", '*********", Array)
#3 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php(24): Illuminate\Database\Connectors\Connector->createConnection('mysql:host=loca...', Array, Array)
#4 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php(184): Illuminate\Database\Connectors\MySqlConnector->connect(Array)
#5 [internal function]: Illuminate\Database\Connectors\ConnectionFactory->Illuminate\Database\Connectors\{closure}()
#6 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php(926): call_user_func(Object(Closure))
#7 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php(961): Illuminate\Database\Connection->getPdo()
#8 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php(405): Illuminate\Database\Connection->getReadPdo()
#9 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php(360): Illuminate\Database\Connection->getPdoForSelect(true)
#10 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php(664): Illuminate\Database\Connection->Illuminate\Database\{closure}(Object(Illuminate\Database\Query\Expression), Array)
#11 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php(631): Illuminate\Database\Connection->runQueryCallback(Object(Illuminate\Database\Query\Expression), Array, Object(Closure))
#12 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php(373): Illuminate\Database\Connection->run(Object(Illuminate\Database\Query\Expression), Array, Object(Closure))
#13 [internal function]: Illuminate\Database\Connection->cursor(Object(Illuminate\Database\Query\Expression), Array)
#14 /***/***public_html/lib/pkp/classes/session/SessionDAO.inc.php(37): Generator->current()
#15 /***/***public_html/lib/pkp/classes/session/SessionManager.inc.php(170): SessionDAO->getSession('268f07f13ea1f8d...')
#16 [internal function]: SessionManager->read('268f07f13ea1f8d...')
#17 /***/***public_html/lib/pkp/classes/session/SessionManager.inc.php(60): session_start()
#18 /***/***public_html/lib/pkp/classes/session/SessionManager.inc.php(131): SessionManager->__construct(Object(SessionDAO), Object(Request))
#19 /***/***public_html/lib/pkp/classes/i18n/PKPLocale.inc.php(131): SessionManager::getManager()
#20 /***/***public_html/lib/pkp/classes/i18n/PKPLocale.inc.php(290): PKPLocale::getLocale()
#21 /***/***public_html/lib/pkp/classes/core/Dispatcher.inc.php(116): PKPLocale::initialize(Object(Request))
#22 /***/***public_html/lib/pkp/classes/core/PKPApplication.inc.php(360): Dispatcher->dispatch(Object(Request))
#23 /***/***public_html/index.php(68): PKPApplication->execute()
#24 {main}

Next Doctrine\DBAL\Driver\PDO\Exception: SQLSTATE[08004] [1040] Too many connections in /***/***public_html/lib/pkp/lib/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDO/Exception.php:18
Stack trace:
#0 /***/***public_html/lib/pkp/lib/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php(42): Doctrine\DBAL\Driver\PDO\Exception::new(Object(PDOException))
#1 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(67): Doctrine\DBAL\Driver\PDOConnection->__construct('mysql:host=loca...', '*********", '*********", Array)
#2 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(46): Illuminate\Database\Connectors\Connector->createPdoConnection('mysql:host=loca...', '*********", '*********", Array)
#3 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php(24): Illuminate\Database\Connectors\Connector->createConnection('mysql:host=loca...', Array, Array)
#4 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php(184): Illuminate\Database\Connectors\MySqlConnector->connect(Array)
#5 [internal function]: Illuminate\Database\Connectors\ConnectionFactory->Illuminate\Database\Connectors\{closure}()
#6 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php(926): call_user_func(Object(Closure))
#7 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php(961): Illuminate\Database\Connection->getPdo()
#8 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php(405): Illuminate\Database\Connection->getReadPdo()
#9 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php(360): Illuminate\Database\Connection->getPdoForSelect(true)
#10 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php(664): Illuminate\Database\Connection->Illuminate\Database\{closure}(Object(Illuminate\Database\Query\Expression), Array)
#11 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php(631): Illuminate\Database\Connection->runQueryCallback(Object(Illuminate\Database\Query\Expression), Array, Object(Closure))
#12 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php(373): Illuminate\Database\Connection->run(Object(Illuminate\Database\Query\Expression), Array, Object(Closure))
#13 [internal function]: Illuminate\Database\Connection->cursor(Object(Illuminate\Database\Query\Expression), Array)
#14 /***/***public_html/lib/pkp/classes/session/SessionDAO.inc.php(37): Generator->current()
#15 /***/***public_html/lib/pkp/classes/session/SessionManager.inc.php(170): SessionDAO->getSession('268f07f13ea1f8d...')
#16 [internal function]: SessionManager->read('268f07f13ea1f8d...')
#17 /***/***public_html/lib/pkp/classes/session/SessionManager.inc.php(60): session_start()
#18 /***/***public_html/lib/pkp/classes/session/SessionManager.inc.php(131): SessionManager->__construct(Object(SessionDAO), Object(Request))
#19 /***/***public_html/lib/pkp/classes/i18n/PKPLocale.inc.php(131): SessionManager::getManager()
#20 /***/***public_html/lib/pkp/classes/i18n/PKPLocale.inc.php(290): PKPLocale::getLocale()
#21 /***/***public_html/lib/pkp/classes/core/Dispatcher.inc.php(116): PKPLocale::initialize(Object(Request))
#22 /***/***public_html/lib/pkp/classes/core/PKPApplication.inc.php(360): Dispatcher->dispatch(Object(Request))
#23 /***/***public_html/index.php(68): PKPApplication->execute()
#24 {main}

Next Illuminate\Database\QueryException: SQLSTATE[08004] [1040] Too many connections (SQL: SELECT * FROM sessions WHERE session_id = 268f07f13ea1f8d27fbb6949bad5a082) in /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
Stack trace:
#0 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php(631): Illuminate\Database\Connection->runQueryCallback(Object(Illuminate\Database\Query\Expression), Array, Object(Closure))
#1 /***/***public_html/lib/pkp/lib/vendor/laravel/framework/src/Illuminate/Database/Connection.php(373): Illuminate\Database\Connection->run(Object(Illuminate\Database\Query\Expression), Array, Object(Closure))
#2 [internal function]: Illuminate\Database\Connection->cursor(Object(Illuminate\Database\Query\Expression), Array)
#3 /***/***public_html/lib/pkp/classes/session/SessionDAO.inc.php(37): Generator->current()
#4 /***/***public_html/lib/pkp/classes/session/SessionManager.inc.php(170): SessionDAO->getSession('268f07f13ea1f8d...')
#5 [internal function]: SessionManager->read('268f07f13ea1f8d...')
#6 /***/***public_html/lib/pkp/classes/session/SessionManager.inc.php(60): session_start()
#7 /***/***public_html/lib/pkp/classes/session/SessionManager.inc.php(131): SessionManager->__construct(Object(SessionDAO), Object(Request))
#8 /***/***public_html/lib/pkp/classes/i18n/PKPLocale.inc.php(131): SessionManager::getManager()
#9 /***/***public_html/lib/pkp/classes/i18n/PKPLocale.inc.php(290): PKPLocale::getLocale()
#10 /***/***public_html/lib/pkp/classes/core/Dispatcher.inc.php(116): PKPLocale::initialize(Object(Request))
#11 /***/***public_html/lib/pkp/classes/core/PKPApplication.inc.php(360): Dispatcher->dispatch(Object(Request))
#12 /***/***public_html/index.php(68): PKPApplication->execute()
#13 {main}

I have the same issue…

Upgraded from 3.1.2.4 to 3.2.1.4 and 3.3.0.17, running on a 8.2 php version.

System seems to be ok, working fine, but have several erros like that.

Something to worry @asmecher @Vitaliy ?

Hi all,

The listed query (working with the sessions table) is a quick and lightweight operation, but it’s probably happening at a time of high server load. You’ll need to find out what’s happening on the server at this time in order to understand what’s causing the problem. For example, is a search engine performing heavy indexing activity? Is a single URL being hammered e.g. as a DDOS attack? If you’re able to find out more information about that, I can possibly suggest next steps; just this log isn’t enough to determine.

Regards,
Alec Smecher
Public Knowledge Project Team

Hi @asmecher
Thank you so much for your reply. After we looked through our server, we figured out Google search engine and some other engines performing heavy indexing activity. Could you help us to overcome this issue. We also activated another layer of protection by adding Cloudflare service to protect our journals from attacks such as DDOS attacks.
Many Thanks
Salam

Hi @Salam_Al-Khammasi,

I would suggest looking up robots.txt, which is a standard mechanism you can use to instruct indexers to behave better. The reputable search engines support it. See for example:

Regards,
Alec Smecher
Public Knowledge Project Team