'Could not find driver' error when running occ upgrade

The Basics

  • Nextcloud Server version (e.g., 29.x.x):
    31.0.1

  • Operating system and version (e.g., Ubuntu 24.04):
    Arch Linux 6.16

  • Web server and version (e.g, Apache 2.4.25):
    4.2.0

  • PHP version (e.g, 8.3):
    8.2.29

  • Is this the first time you’ve seen this error? (Yes / No):
    Yes

  • When did this problem seem to first start?
    After system update, which included MariaDB and NC Contacts app.

  • Installation method (e.g. AlO, NCP, Bare Metal/Archive, etc.)
    Bare metal

  • Are you using CloudfIare, mod_security, or similar? (Yes / No)
    No

Summary of the issue you are facing:

When running occ upgrade, I receive a fatal error saying, ‘Could not find driver…’ and NC stays in maintenance mode.

Steps to replicate it (hint: details matter!):

  1. Run: sudo -u nextcloud php-legacy /usr/share/webapps/nextcloud/occ upgrade (this is on Arch bare metal)

  2. Get the error message: An unhandled exception has been thrown: Doctrine\DBAL\Exception: Failed to connect to the database: An exception occurred in the driver: could not find driver in /usr/share/webapps/nextcloud/lib/private/DB/Connection.php:237

I found the solution myself. Turns out running occ from a hook is difficult because it doesn’t read the environment variable NEXTCLOUD_PHP_CONFIG. Therefore, enabling pdo_mysql in the global php.ini is necessary in order to run occ from the CLI.

Solution: uncomment pdo_mysql in the global php.ini for the version of PHP that you’re running for Nextcloud.