**Note**: even though I'm reporting this bug, I can't reproduce it locally as I use SQLite and a different setup. @cmelo can reproduce this and provide more information. It might also be related to CentralAuth or the wiki farm config.
**Steps to replicate the issue** (include links if applicable):
* Make sure you have a local wiki running on MySQL
* Set `$wgMainCacheType` to `CACHE_ANYTHING` **[to be confirmed]**
* Open localhost:8080/w/index.php (or equivalent) in your browser. Note, this bug can only reproduced by hitting index.php directly.
**What happens?**:
You get the following error:
```
Sorry! This site is experiencing technical difficulties.
Try waiting a few minutes and reloading.
(Cannot access the database: Cannot access the database: Too many connections (wikifarm-db))
Backtrace:
#0 /var/www/html/metawiki/includes/libs/rdbms/loadbalancer/LoadBalancer.php(779): Wikimedia\Rdbms\LoadBalancer->reportConnectionError()
#1 /var/www/html/metawiki/includes/libs/rdbms/loadbalancer/LoadBalancer.php(767): Wikimedia\Rdbms\LoadBalancer->getServerConnection()
#2 /var/www/html/metawiki/includes/libs/rdbms/database/DBConnRef.php(103): Wikimedia\Rdbms\LoadBalancer->getConnectionInternal()
#3 /var/www/html/metawiki/includes/libs/rdbms/database/DBConnRef.php(117): Wikimedia\Rdbms\DBConnRef->ensureConnection()
#4 /var/www/html/metawiki/includes/libs/rdbms/database/DBConnRef.php(369): Wikimedia\Rdbms\DBConnRef->__call()
#5 /var/www/html/metawiki/includes/libs/rdbms/querybuilder/SelectQueryBuilder.php(772): Wikimedia\Rdbms\DBConnRef->selectRow()
#6 /var/www/html/metawiki/includes/page/PageStore.php(201): Wikimedia\Rdbms\SelectQueryBuilder->fetchRow()
#7 /var/www/html/metawiki/includes/cache/LinkCache.php(451): MediaWiki\Page\PageStore->MediaWiki\Page\{closure}()
#8 /var/www/html/metawiki/includes/cache/LinkCache.php(485): MediaWiki\Cache\LinkCache->getGoodLinkRowInternal()
#9 /var/www/html/metawiki/includes/page/PageStore.php(190): MediaWiki\Cache\LinkCache->getGoodLinkRow()
#10 /var/www/html/metawiki/includes/page/PageStore.php(156): MediaWiki\Page\PageStore->getPageByNameViaLinkCache()
#11 /var/www/html/metawiki/includes/page/PageStore.php(328): MediaWiki\Page\PageStore->getPageByName()
#12 /var/www/html/metawiki/includes/title/Title.php(3804): MediaWiki\Page\PageStore->getPageByReference()
#13 /var/www/html/metawiki/includes/title/Title.php(1076): MediaWiki\Title\Title->getFieldFromPageStore()
#14 /var/www/html/metawiki/includes/title/Title.php(3659): MediaWiki\Title\Title->getContentModel()
#15 /var/www/html/metawiki/includes/Output/OutputPage.php(2717): MediaWiki\Title\Title->getPageLanguage()
#16 /var/www/html/metawiki/includes/Output/OutputPage.php(2811): MediaWiki\Output\OutputPage->addAcceptLanguage()
#17 /var/www/html/metawiki/includes/Output/OutputPage.php(2920): MediaWiki\Output\OutputPage->sendCacheControl()
#18 /var/www/html/metawiki/includes/actions/ActionEntryPoint.php(162): MediaWiki\Output\OutputPage->output()
#19 /var/www/html/metawiki/includes/MediaWikiEntryPoint.php(199): MediaWiki\Actions\ActionEntryPoint->execute()
#20 /var/www/html/metawiki/index.php(58): MediaWiki\MediaWikiEntryPoint->run()
#21 {main}
```
**What should have happened instead?**:
It should load normally (and redirect you to the main page).
**Software version** (on `Special:Version` page; skip for WMF-hosted wikis like Wikipedia): master
**Other information** (browser name/version, screenshots, etc.):
A 2500-lines stacktrace gets logged in the debug log for the attempted queries. It looks like it's trying to load the User object from cache (`User::loadFromCache`), which eventually reaches SqlBagOStuff, which tries to obtain a DB connection. It then calls `LoadBalancer::isPrimaryRunningReadOnly()` to see if we're read-only. But `isPrimaryRunningReadOnly` also uses WANCache to store this value, so it gets to SqlBagOStuff again, it opens a new connection, it checks whether it's read-only, which opens a new connection, and so on until we run out of available connections. Here are the last 100 lines from that stack trace:
```
[TBD]
```
We ran a git bisect, and it pointed to [[https://gerrit.wikimedia.org/r/c/mediawiki/core/+/955771 | r955771]].
Might be somewhat related to T163492, although that's a different problem.