Nextcloud 21.04 Can't login from web. Synchronization via desktop and android client okay

Nextcloud version (eg, 20.0.5): 21.0.4
Operating system and version (eg, Ubuntu 20.04): Ubuntu 20.04
Apache or nginx version (eg, Apache 2.4.25): Apache 2.4.41
PHP version (eg, 7.4): 7.4

The issue you are facing:

Is this the first time you’ve seen this error? (Y/N): Y

Steps to replicate it:

  1. Try and log in using chrome browser
  2. After entering credentials in web interface - you never get logged in.
  3. Sometimes I will get 504 or internal Server error after many minutes
  4. Access with the same account continues to work through desktop and android clients with no problems.

The output of your Nextcloud log in Admin > Logging:

php7:error] [pid 16940] [client] PHP Fatal error:  Uncaught Doctrine\\DBAL\\Exception: Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [1040] Too many connections in /var/www/owncloud/lib/private/DB/Connection.php:85\nStack trace:\n#0 /var/www/owncloud/3rdparty/doctrine/dbal/src/Connection.php(1486): OC\\DB\\Connection->connect()\n#1 /var/www/owncloud/3rdparty/doctrine/dbal/src/Connection.php(1014): Doctrine\\DBAL\\Connection->getWrappedConnection()\n#2 /var/www/owncloud/lib/private/DB/Connection.php(226): Doctrine\\DBAL\\Connection->executeQuery()\n#3 /var/www/owncloud/3rdparty/doctrine/dbal/src/Query/QueryBuilder.php(210): OC\\DB\\Connection->executeQuery()\n#4 /var/www/owncloud/lib/private/DB/QueryBuilder/QueryBuilder.php(287): Doctrine\\DBAL\\Query\\QueryBuilder->execute()\n#5 /var/www/owncloud/lib/private/AppConfig.php(345): OC\\DB\\QueryBuilder\\QueryBuilder->execute()\n#6 /var/www/owncloud/lib/private/AppConfig.php(110): OC\\AppConfig->loadConfigValues()\n#7 /var/www/owncloud/lib/private/AppConfig.php(301): OC\\AppConfig->getApps()\n#8 /var in /var/www/owncloud/lib/private/DB/Connection.php on line 85

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

$CONFIG = array (
  'updatechecker' => false,
  'instanceid' => '#####',
  'passwordsalt' => '#####',
  'secret' => '####',
  'overwriteprotocol' => 'https',
  'trusted_domains' => 
  array (
    0 => '',
    1 => '',
    2 => '',
  'datadirectory' => '/var/www/owncloud/data',
  'overwrite.cli.url' => '',
  'dbtype' => 'mysql',
  'version' => '',
  'dbname' => 'owncloud',
  'dbhost' => 'localhost',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'oc',
  'dbpassword' => '####',
  'default_phone_region' => 'US',
  'logtimezone' => 'UTC',
  'installed' => true,
  'asset-pipeline.enabled' => false,
  'memcache.local' => '\\OC\\Memcache\\APCu',
  'filelocking.enabled' => 'true',
  'memcache.distributed' => '\\OC\\Memcache\\Redis',
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'redis' => 
  array (
    'host' => 'localhost',
    'port' => 6379,
    'timeout' => 0,
    'dbindex' => 0,
    'password' => '####',
  'maintenance' => true,
  'mail_from_address' => 'david',
  'mail_smtpmode' => 'smtp',
  'mail_domain' => '',
  'loglevel' => 2,
  'log_type' => 'file',
  'logfile' => 'nextcloud.log',
  'logdateformat' => 'F d, Y H:i:s',
  'theme' => '',
  'singleuser' => false,
  'mail_smtphost' => '',
  'mail_sendmailmode' => 'smtp',
  'mail_smtpport' => '25',
  'app_install_overwrite' => 
  array (
    0 => 'files_external_gdrive',
    1 => 'gallery',

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

[Sun Oct 10 13:21:50.684944 2021] [mpm_prefork:notice] [pid 4257] AH00169: caught SIGTERM, shutting down
[Sun Oct 10 13:21:51.189454 2021] [mpm_prefork:notice] [pid 4811] AH00163: Apache/2.4.41 (Ubuntu) configured -- resuming normal operations
[Sun Oct 10 13:21:51.190030 2021] [core:notice] [pid 4811] AH00094: Command line: '/usr/sbin/apache2'
[Sun Oct 10 14:12:14.917091 2021] [access_compat:error] [pid 4814] [client] AH01797: client denied by server configuration: /var/www/owncloud/config
[Sun Oct 10 14:12:15.739128 2021] [access_compat:error] [pid 4818] [client] AH01797: client denied by server configuration: /var/www/owncloud/config

You have already installed redis that takes a lot of charge from your database. But you still use too many connections, you might either increase the limit (MySQL Error “Too many connections” and how to resolve it – The Geek Diary) or figure out what app/function is creating most of these connections and disable it.

The external storage could be quite database intensive since it can scan the storage over and over again for changes. For me, it would depend a bit if it is a normal and expected behavior (e.g. due to increased usage, then an increase of resources/connections is the right way) or if there is perhaps a bug (where it would be helpful to identify the related function to report this bug).

I did try the solution recommended here Several issues after upgrading to Nextcloud 21 and the same account could login in a few seconds.

  1. Login to MySQL execute query on owncloud DB
  2. oc_authtoken MariaDB [owncloud]> delete from oc_authtoken where login_name = ‘user’;
  3. Query OK, 1701 rows affected (2.434 sec)
  4. After I could login with the same user through the web in under 10 seconds

I did have to reauthenticate on some clients, but I could login through the web. No significant effect on mobile clients, other than having to reauthenticate. Unfortunately 24 hours later I cannot log in through the web again.

A quick MariaDB [owncloud]> select login_name, count(*) from oc_authtoken group by login_name; shows this user now has 3,265 tokens.

My issue is sounding closer to this one: Hundreds of odd oc_authtoken entries for a user slow down performance · Issue #27603 · nextcloud/server · GitHub