4

i know this might be a bit out of bounds for this stackexchange site, but i am going a bit batty and any help would be amazing

i am trying to get a console plugin up and running. i started with just the shell of one. got all the basics in place. then when i run craft/app/etc/console/yiic.php (with no params) to get a list of available commands, i get db connection errors.

things to note:

  • i can run yiic.php on my staging server (a centos LAMP cloud server instance at Rackspace) and it's fine, it just has problems locally
  • i am using MAMP Pro locally
  • i can load the site fine through my browser, so credentials in config\db.php are AOK
  • i can fire up mysql from the cmdline using the same db credentials, and it's fine

any help would be so appreciated!

the dumped errors i get are as follows:

CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] No such file or directory
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php (829)
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/console/ConsoleApp.php (203)
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php (80)
CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] No such file or directory
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php (842)
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/console/ConsoleApp.php (203)
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php (80)
CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] No such file or directory
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php (829)
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/console/ConsoleApp.php (203)
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php (80)
CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] No such file or directory
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php (842)
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/console/ConsoleApp.php (203)
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php (80)
CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] No such file or directory
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php (829)
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/console/ConsoleApp.php (203)
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php (80)
CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] No such file or directory
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php (842)
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/console/ConsoleApp.php (203)
in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php (80)
exception 'CException' with message 'Craft\ConsoleApp and its behaviors do not have a method or closure named "getTranslatedBrowserLanguage".' in /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/framework/base/CComponent.php:266
Stack trace:
#0 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php(910): CComponent->__call('getTranslatedBr...', Array)
#1 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php(910): Craft\ConsoleApp->getTranslatedBrowserLanguage()
#2 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php(800): Craft\AppBehavior->_getFallbackLanguage()
#3 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php(570): Craft\AppBehavior->getTargetLanguage(false)
#4 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/console/ConsoleApp.php(103): Craft\AppBehavior->getLanguage()
#5 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/framework/i18n/CMessageSource.php(83): Craft\ConsoleApp->getLanguage()
#6 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/framework/YiiBase.php(590): CMessageSource->translate('craft', 'Craft can\xE2\x80\x99t c...', NULL)
#7 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/Craft.php(382): YiiBase::t('craft', 'Craft can\xE2\x80\x99t c...', Array, NULL, NULL)
#8 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php(843): Craft\Craft::t('Craft can\xE2\x80\x99t c...')
#9 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/console/ConsoleApp.php(203): Craft\AppBehavior->createDbConnection()
#10 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php(80): Craft\ConsoleApp->getComponent('db')
#11 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php(487): Craft\AppBehavior->isInstalled()
#12 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/behaviors/AppBehavior.php(187): Craft\AppBehavior->getInfo('edition')
#13 [internal function]: Craft\AppBehavior->getEdition()
#14 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/framework/base/CComponent.php(261): call_user_func_array(Array, Array)
#15 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/console/ConsoleApp.php(307): CComponent->__call('getEdition', Array)
#16 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/console/ConsoleApp.php(307): Craft\ConsoleApp->getEdition()
#17 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/console/ConsoleApp.php(74): Craft\ConsoleApp->_setEditionComponents()
#18 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/framework/base/CApplication.php(169): Craft\ConsoleApp->init()
#19 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/framework/YiiBase.php(121): CApplication->__construct('/Users/bhu/drop...')
#20 /Users/bhu/dropbox-bhu/Dropbox/devel/binarybhu/websites/blackstarcoffee/blackstarcoffee.dev/craft/app/etc/console/yiic.php(78): YiiBase::createApplication('Craft\\ConsoleAp...', '/Users/bhu/drop...')
#21 {main}
4

finally worked it out! when connecting through localhost PDO does a socket-based connection to mysql, not tcp. under MAMP the socket file is "non-standard", and so i have to set it explicitly. when running a console command via yiic.php, CRAFT_ENVIRONMENT is set to console, and so i add the following to the end of craft/config/db.php:

'console' => [
    'unixSocket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
],

and all is well.

UPDATE: Better Solution

the above is not optimal as obviously one can run a plugin from the console on a live or staging server, as well as a dev machine! so i came up with a better approach: add support for a --mamp cmdline parameter. so my db.php became:

$db_config = [

    '*' => [
        'server' => 'localhost',
        'user' => '...',
        'password' => '...',
        'database' => '...',
        'tablePrefix' => 'craft',
    ],

];

// watch for MAMP (need to specify socket file explicitly)
if (php_sapi_name() == "cli") {
    $idx = 0;
    foreach($_SERVER['argv'] as $key => $arg) {
        if (strcasecmp($arg, '--mamp') === 0) {
            $db_config['*']['unixSocket'] = '/Applications/MAMP/tmp/mysql/mysql.sock';
            array_splice($_SERVER['argv'], $idx, 1);
            break;
        }
        $idx++;
    }
}


return $db_config;
| improve this answer | |
  • Very helpful. Thanks. For my own future reference as well as others, my full command line was then ./yiic migrate create MyDataBaseUpdate MyPlugin --mamp – John Morton Mar 7 '16 at 18:10
  • Feel free to mark your own answer as correct! Certainly worked for me :) – Josh Angell Sep 16 '16 at 10:45
  • Any luck with this in Craft 3? – Stian Karlsen Mar 11 '19 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.