Getting A Database Error While Running Console Commands

Hello everyone,

I’m learning how to run Cron jobs and have created a console command class extending the CConsoleCommand class. It has an action inside which I’m loading a model and performing a find. I’ve made sure I’ve modified the console.php config file correctly. I’ve also created a cron.php file in the folder level of the main index.php which is exactly similar to index.php but it’s set to run the console.php file inside the command Yii::createConsoleApplication($config)->run(); where $config has the path to the console.php file.

So for very simple stuff upon running bash: php cron.php inside the application, it works. The simple script here was just writing something to the cron log. Now I’m trying to get it go retrieve some fields from the database and it’s now giving me the following error:

2014/03/04 07:42:13 [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 gone and had a look at that line in the framework file but couldn’t figure out where to go next.

What I also did was just quickly test the same action inside a regular controller in the application and it was working there. So it seems that there’s something wrong with my configuration of the console application but I can’t seem to figure out what it is.

If you want I can upload the files that I’ve made.

Thanks in advance for your help :)

Replying to my own post because I’ve found the answer to what was happening.

Now my environment was a Mac OS X 10.9 OS with MAMP delivering PHP and MYSQL.

Now in one of the related posts with this topic, I found that it’s necessary to create a symbolic link between /Applications/MAMP/tmp/mysql/mysql.sock to /tmp/mysql.sock as the later does not exist.

But after doing that it didn’t work either.

But finally what got it working was a change in the connection string from


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

to


'connectionString'=>'mysql:host=127.0.0.1:8889;dbname=********',

I had seem some people mentioning that changing from localhost to 127.0.0.1 should do the trick but it needs the extra port specification ( mine was set to 8889 by default by MAMP so I used that ) … and after that it was working as normal …

Another thing I found of a nuisance was that of timezones. I was using the date(‘Y-m-d H:i:s’) and I hadn’t defined a default timezone, which is not necessary in the web application. But here I had specifically define to get the script working.

Anyway, hopefully someone finds this useful.

Cheers

Prathamesh