PDO object:could not find driver

Viewed 1368

Normally I can find the answer to my problem without posting but I have looked at a few dozen posts regarding the same error message- most of them suggest installing php-mysql or php7.3-mysql or uncommenting the pdo extension in php.ini. I have tried all of these without any success. I suspect this must be some type of configuration error. The older methods of connecting to MySQL in php work just fine. The PDO driver is just not working.

There are no errors in the respectable nginx log, syslog, nor php7.3-fpm's log.

Frustrated and out of ideas, earlier today I tried upgrading to Debian bullseye assuming there may be some package incompatibility but that did not seem to help either. This took my php version to 7.4 (which seems to be working just fine everywhere else on the server)

Is there some way to find out more information as to why? Or a more detailed error log (cannot find any other than what I see in browser). I'm at a complete dead end right now.

Some info:

php -v
PHP 7.4.15 (cli) (built: Feb 20 2021 09:45:56) ( NTS )

mysql -V
mysql  Ver 8.0.23 for Linux on x86_64 (MySQL Community Server - GPL)

nginx -v
nginx version: nginx/1.18.0

uname -a
Linux *****xx 5.10.0-5-cloud-amd64 #1 SMP Debian 5.10.24-1 (2021-03-19) x86_64 GNU/Linux

php -i | grep pdo
/etc/php/7.4/cli/conf.d/10-pdo.ini,
/etc/php/7.4/cli/conf.d/20-pdo_mysql.ini,
API Extensions => mysqli,pdo_mysql
pdo_mysql
pdo_mysql.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock

apt install php-mysql
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
php-mysql is already the newest version (2:7.4+76).

apt list --installed | grep php
php-common/testing,now 2:76 all [installed,automatic]
php-fpm/testing,now 2:7.4+76 all [installed]
php-mysql/testing,now 2:7.4+76 all [installed]
php-xml/testing,now 2:7.4+76 all [installed]
php7.4-cli/testing,now 7.4.15-5+deb11u1 amd64 [installed,automatic]
php7.4-common/testing,now 7.4.15-5+deb11u1 amd64 [installed,automatic]
php7.4-fpm/testing,now 7.4.15-5+deb11u1 amd64 [installed,automatic]
php7.4-json/testing,now 7.4.15-5+deb11u1 amd64 [installed,automatic]
php7.4-mysql/testing,now 7.4.15-5+deb11u1 amd64 [installed,automatic]
php7.4-opcache/testing,now 7.4.15-5+deb11u1 amd64 [installed,automatic]
php7.4-readline/testing,now 7.4.15-5+deb11u1 amd64 [installed,automatic]
php7.4-xml/testing,now 7.4.15-5+deb11u1 amd64 [installed,automatic]


apt list --installed | grep mysql
dovecot-mysql/testing,now 1:2.3.13+dfsg1-1 amd64 [installed]
mysql-apt-config/now 0.8.16-1 all [installed,local]
mysql-client/now 8.0.23-1debian10 amd64 [installed,local]
mysql-common/now 8.0.23-1debian10 amd64 [installed,local]
mysql-community-client-core/now 8.0.23-1debian10 amd64 [installed,local]
mysql-community-client-plugins/now 8.0.23-1debian10 amd64 [installed,local]
mysql-community-client/now 8.0.23-1debian10 amd64 [installed,local]
mysql-community-server-core/now 8.0.23-1debian10 amd64 [installed,local]
mysql-community-server/now 8.0.23-1debian10 amd64 [installed,local]
mysql-server/now 8.0.23-1debian10 amd64 [installed,local]
mysqltuner/testing,now 1.7.17-1 all [installed]
php-mysql/testing,now 2:7.4+76 all [installed]
php7.4-mysql/testing,now 7.4.15-5+deb11u1 amd64 [installed,automatic]
postfix-mysql/testing,now 3.5.6-1 amd64 [installed]

EDIT: Relevant function code (from tiny tiny RSS's git)

```
function __construct() {
            ORM::configure(self::get_dsn());
            ORM::configure('username', Config::get(Config::DB_USER));
            ORM::configure('password', Config::get(Config::DB_PASS));
            ORM::configure('return_result_sets', true);
            if (Config::get(Config::DB_TYPE) == "mysql" && Config::get(Config::MYSQL_CHARSET)) {
                    ORM::configure('driver_options', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . Config::get(Config::MYSQL_CHARSET)));
            }
    }
 public static function get_dsn() {
            $db_port = Config::get(Config::DB_PORT) ? ';port=' . Config::get(Config::DB_PORT) : '';
            $db_host = Config::get(Config::DB_HOST) ? ';host=' . Config::get(Config::DB_HOST) : '';
            if (Config::get(Config::DB_TYPE) == "mysql" && Config::get(Config::MYSQL_CHARSET)) {
                    $db_charset = ';charset=' . Config::get(Config::MYSQL_CHARSET);
            } else {
                    $db_charset = '';
            }

            return Config::get(Config::DB_TYPE) . ':dbname=' . Config::get(Config::DB_NAME) . $db_host . $db_port . $db_charset;
    }

   public function pdo_connect() : PDO {

            try {
                    $pdo = new PDO(self::get_dsn(),
                            Config::get(Config::DB_USER),
                            Config::get(Config::DB_PASS));
            } catch (Exception $e) {
                    print "<pre>Exception while creating PDO object:" . $e->getMessage() . "</pre>";
                    exit(101);
            }

            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            if (Config::get(Config::DB_TYPE) == "pgsql") {

                    $pdo->query("set client_encoding = 'UTF-8'");
                    $pdo->query("set datestyle = 'ISO, european'");
                    $pdo->query("set TIME ZONE 0");
                    $pdo->query("set cpu_tuple_cost = 0.5");

            } else if (Config::get(Config::DB_TYPE) == "mysql") {
                    $pdo->query("SET time_zone = '+0:0'");

                    if (Config::get(Config::MYSQL_CHARSET)) {
                            $pdo->query("SET NAMES " . Config::get(Config::MYSQL_CHARSET));
                    }
            }

            return $pdo;
    }


```
1 Answers

If you just grabbed a copy of ttrss recently, it looks like Fox changed how the variables were being accessed in the configuration. Instead of using defined variables, it's now using environmental variables. For example

putenv('TTRSS_DB_TYPE=mysql');
putenv('TTRSS_DB_HOST=mydbhost');
putenv('TTRSS_DB_USER=trss');
putenv('TTRSS_DB_NAME=trss');
putenv('TTRSS_DB_PASS=supersecretpassword');

See https://git.tt-rss.org/fox/tt-rss/wiki/GlobalConfig and https://community.tt-rss.org/t/exception-while-creating-pdo-object-could-not-find-driver/4540

Related