CDbConnection failed to open the DB connection: could not find driver

While trying to configure your Yii with a MySQL database (or another database) you might get the following error:

CDbConnection failed to open the DB connection: could not find driver

My configuration (components class property in protected/main.php) looks like this:

'db' => array(
            'class' => 'CDbConnection',
            'connectionString' => 'mysql:host=localhost;dbname=yourdatabasename',
            'username' => 'youruser',
            'password' => 'yourpassword',
            'emulatePrepare' => true, // needed by some MySQL installations
        ),
From first look everything is fine and I have made sure that the MySQL credentials are correct. Though as the error suggests the database driver is missing. 

In the application log(protected/runtime/application.log) you will see something similar to:

2011/03/21 16:10:00 [error] [exception.CDbException] exception 'CDbException' with message 'CDbConnection failed to open the DB connection: could not find driver' in /path_to_yii_framework/db/CDbConnection.php:287

This shows that I have forgotten to add MySQL PDO while compiling PHP in LiteSpeed. This can be easily ammended adding the following option:

--with-pdo-mysql

Once you recompile with MySQL PDO this error is gone. Note that you might have MySQL / MySQLi support in PHP but it will not be enough for Yii and this would confuse you. 

blog comments powered by Disqus