Console Application - Cdbconnection Failed

Hi everyone,

I come from italian forum, where I’ve already asked about this issue, but I have not received answers. I hope someone of you can help me with it.

I’m trying to set up a console application. I’ve followed this, and a simple example like this


class TestCommand extends CConsoleCommand


    public function run($args) {

        echo "hello";




is working fine. But when i try to load a model, like this


class TestCommand extends CConsoleCommand


    public function run($args) {

    	$users = User::model()->findAll();

    	foreach ($users as $user) {

    	     echo $user->first_name;





I get

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

this is the config file


return array(









			'class' => 'CDbConnection',

			'connectionString' => 'mysql:host=localhost;dbname=myapp',

			'emulatePrepare' => true,

			'username' => 'root',

			'password' => 'root',

			'charset' => 'utf8',





The config db array is the same as the main.php inside config folder, so can’t be wrong.

What’s missing?

Thank you very much.


Are you on Debian or Ubuntu? I suspect the mysql PDO driver isn’t registered for your PHP cli.

Edit - for reference: (second note)

Hi, thanks for your quick answer.

Right now I’m working on local machine with windows, just running the command from the prompt.

Btw, the PDO is installed, the webApplication use the same configuration and works fine. Do I’ve to move to real server to test it?


Edit: I’ve moved the proj on server, and it’s working fine :) . Thanks for your help

Hi there,

I seem to be lost in the same territory of CDbConnection failing in a console application. And just like the situation of Giacomo, the same configuration and code works correctly in the web application.

But unlike Giacomo, I’m getting a different error

[error] [exception.CDbException] exception 'CDbException' with message 'CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] No such file or directory' in /Users/pdatar/yii/framework/db/CDbConnection.php:382

I’ve explained my situation in another forum thread here

Thank again in advance for helping.

I want to be able to test the configuration on my local machine first before moving it to a live production server. My set up is:

MySQL and PHP from MAMP

Running on Mac OS X 10.9

And it’s using version 5 of php