Database connections

Nextcloud version (eg, 10.0.2): 11.0.2
Operating system and version (eg, Ubuntu 16.04): CentOS 7
Apache or nginx version (eg, Apache 2.4.25): Apache 2.4.6
PHP version (eg, 5.6): 7.0.21
Is this the first time you’ve seen this error?: No

Can you reliably replicate it? (If so, please outline steps): No

The issue you are facing:

We seem to have a problem whereby the maximum number of MariaDB (5.5.52) connections (max_connections)
originally changed from 100 to 200 (to overcome this problem in the past) has now had to be increased again
(the behaviour our users see is a 500 in their browser window when trying to connect to the Nextcloud instance).
The relevant log entry (in /var/log/httpd/ssl_error_log is shown below. We have
transaction-isolation set to READ-COMMITTED.

Any suggestions welcome …

The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):

   <?php
      $CONFIG = array (  
       'instanceid' => 'aaaaaaa',  
       'passwordsalt' => 'yyyyyyy',  
       'secret' => 'xxxxxx',  
       'trusted_domains' =>   
           array (  
               0 => 'xxx.yyy.zzz.aaa',  
           ),  
       'datadirectory' => '/mnt/data',  
       'overwrite.cli.url' => 'https://xxx.yyy.zzz.aaa',  
        'dbtype' => 'mysql',  
        'version' => '11.0.2.7',  
        'dbname' => 'ncdata',  
        'dbhost' => 'localhost',  
        'dbtableprefix' => 'oc_',  
        'dbuser' => 'oc_admin',  
        'dbpassword' => 'xxxxxxxx',  
        'installed' => true,  
        'ldapIgnoreNamingRules' => false,  
        'mail_from_address' => 'support',  
         'mail_smtpmode' => 'smtp',  
         'mail_domain' => 'bbb.ccc.ddd,    
         'memcache.local' => '\\OC\\Memcache\\APCu',  
         'memcache.locking' => '\\OC\\Memcache\\Redis',  
         'redis' =>   
              array (  
                   'host' => 'localhost',  
                   'port' => 6379,  
              ),  
        'maintenance' => false,  
        'quota_include_external_storage' => false,  
        'filesystem_check_changes' => 1,  
        'loglevel' => 1,  
         'log_rotate_size' => 104855760,  
        'ldapProviderFactory' => '\\OCA\\User_LDAP\\LDAPProviderFactory',  
         'mail_smtphost' => 'aaa.bbb.ccc.ddd',  
        'mail_smtpport' => '25',  
      );  

The output of your Apache/nginx/system log in /var/log/httpd/ssl_error_log:

        [Thu Jul 27 09:38:23.608145 2017] [:error] [pid 10689] [client a.b.c.d:50475] PHP Fatal error:  Uncaught  Doctrine\\DBAL\\DBALException: Failed to connect to the database: An exception occured in driver: SQLSTATE[HY000] [1040] Too many connections in /var/www/nextcloud/lib/private/DB/Connection.php:60\nStack trace:\n#0 /var/www/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(429): OC\\DB\\Connection->connect()\n#1 /var/www/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(389): Doctrine\\DBAL\\Connection->getDatabasePlatformVersion()\n#2 /var/www/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(328): Doctrine\\DBAL\\Connection->detectDatabasePlatform()\n#3 /var/www/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(621): Doctrine\\DBAL\\Connection->getDatabasePlatform()\n#4 /var/www/nextcloud/lib/private/DB/Connection.php(147): Doctrine\\DBAL\\Connection->setTransactionIsolation(2)\n#5 /var/www/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/DriverManager.php(172): OC\\DB\\Connection->__construct(Array, Object(Doctrine\\DBAL\\Driver\\PDOMySql\\Driver), Object(D in /var/www/nextcloud/lib/private/DB/Connection.php on line 60 

and in /var/log/httpd/ssl_access_log lots of GET /status.php with a 500 return code.

How many users do you have and how many accesses at the same time?

Also note 11.0.2 is not the latest version of NC 11: https://nextcloud.com/changelog/#latest11

It turns out the increase in database connections (past 200) was due to the
fact lots of httpd processes
were starting up due to some problem with the filestore for /mnt/data where
Nextcloud
instance data resides. And the default maximum number of httpd processes
in
mpm prefork mode is 256. Any ideas on why so many Apache processes start
up over time?
Is it something in the retry logic on the sync client side (it looks like
there is about ~40 clients
connected)?

The client supports parallel upload, I think it was up to 4 connections at a time. Unfortunately, I can’t tell you a number how many workers are needed in average per client and how many sql connections, so you have to carefully adjust it by yourself. It is worth monitoring your system a bit, if there are sql queries that are very slow or if database creates a lot of i/o-wait (check related innodb_* parameters in your database config).