2

edit 2 : Fixed it myself, see comments.

I am trying to access my Zf2 app via the terminal.

The idea is that a cron script will call the app on a regular basis, and a corresponding action will look for things in the database and update when needed.

When accessing the controller/action via the browser, everything's fine.

When accessing it via the terminal, I'm getting a mysql-related error and I'm a bit lost with it.

Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)

The command from the terminal is :

php index.php stories update

Here is my route :

'console' => array(
    'router' => array(
        'routes' => array(
              'updatestories' => array(
                    'options' => array(
                        'route'    => 'stories update',
                        'defaults' => array(
                            'controller' => 'Application\Controller\Index',
                            'action'     => 'updatestories'
                        )
                    )
                )
        )
    )
),

The action 'updatestories' is :

public function updatestoriesAction(){

    $request = $this->getRequest();

    // Make sure that we are running in a console and the user has not tricked our
    // application into running this action from a public web server.
    if (!$request instanceof ConsoleRequest){
        throw new \RuntimeException('You can only use this action from a console!');
    }

    $talents = $this->getStoriesTable()->fetchAll();

    var_dump($talents);

    return "end of script. !\n";
}

And here's the error I get only when reaching it via the terminal:

Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Driver/Pdo/Connection.php on line 281 ====================================================================== The application has thrown an exception! ====================================================================== Zend\Db\Adapter\Exception\RuntimeException Connect Error: SQLSTATE[HY000] [2002] No such file or directory ---------------------------------------------------------------------- /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Driver/Pdo/Connection.php:289 0 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Driver/Pdo/Pdo.php(244): Zend\Db\Adapter\Driver\Pdo\Connection->connect() 1 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/Db/Sql/Sql.php(120): Zend\Db\Adapter\Driver\Pdo\Pdo->createStatement() 2 /Users/gregorychiche/Sites/repos/module/Talents/src/Talents/Model/StoriesTable.php(35): Zend\Db\Sql\Sql->prepareStatementForSqlObject(Object(Zend\Db\Sql\Select)) 3 /Users/gregorychiche/Sites/repos/module/Application/src/Application/Controller/IndexController.php(123): Talents\Model\StoriesTable->fetchAll() 4 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractActionController.php(83): Application\Controller\IndexController->updatestoriesAction() 5 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent)) 6 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent)) 7 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) 8 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) 9 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Console\Request), Object(Zend\Console\Response)) 10 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent)) 11 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent)) 12 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) 13 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(309): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) 14 /Users/gregorychiche/sites/repos/public/index.php(24): Zend\Mvc\Application->run() 15 {main} ====================================================================== Previous Exception(s): ====================================================================== exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Driver/Pdo/Connection.php:281 Stack trace: 0 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Driver/Pdo/Connection.php(281): PDO->__construct('mysql:dbname=ta...', 'root', 'root', Array) 1 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Driver/Pdo/Pdo.php(244): Zend\Db\Adapter\Driver\Pdo\Connection->connect() 2 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/Db/Sql/Sql.php(120): Zend\Db\Adapter\Driver\Pdo\Pdo->createStatement() 3 /Users/gregorychiche/Sites/repos/module/Talents/src/Talents/Model/StoriesTable.php(35): Zend\Db\Sql\Sql->prepareStatementForSqlObject(Object(Zend\Db\Sql\Select)) 4 /Users/gregorychiche/Sites/repos/module/Application/src/Application/Controller/IndexController.php(123): Talents\Model\StoriesTable->fetchAll() 5 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractActionController.php(83): Application\Controller\IndexController->updatestoriesAction() 6 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent)) 7 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent)) 8 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) 9 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) 10 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Console\Request), Object(Zend\Console\Response)) 11 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent)) 12 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent)) 13 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) 14 /Users/gregorychiche/Sites/repos/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(309): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) 15 /Users/gregorychiche/sites/repos/public/index.php(24): Zend\Mvc\Application->run()

What am I missing ? I don't understand why the code is working when accessing via browser (if I remove that but of code that throws exception obviously) but not when using terminal. I thought zf2 was taking care of all depedencies.

Thanks in advance

edit 1 : getStoriesTable() is as follow. it works mighty fine when used in browser-context

public function getStoriesTable()
{
    if (!$this->storiesTable) {
        $sm = $this->getServiceLocator();
        $this->storiesTable = $sm->get('Talents\Model\StoriesTable');
    }
    return $this->storiesTable;
}

edit 2 : Fixed it myself, see comments.

Greg

5
  • The obvious thing that's likely to be different between running via the web or command line is the user the process is running under. Can you connect to mysql via the command-line? Commented Aug 1, 2013 at 19:57
  • Can you provide further details on php $this->getStoriesTable()? Commented Aug 1, 2013 at 23:12
  • added code for getStoriesTable, just standard code that I think I nicked from zf2 skeleton app ... timdev: I thought that since calling the zf2 app, the connection would be handled by zf2, using the credentials hard coded in my code ? Commented Aug 1, 2013 at 23:30
  • Fixed it myself. replaced 'localhost' with '127.0.0.1' in my config file, that did the trick. Commented Aug 6, 2013 at 22:23
  • if its fixed mark my ans as the right answer Commented Mar 29, 2016 at 16:06

1 Answer 1

2

Issue is mysql is not working on localhost.

In most cases change in global.php hostname to 127.0.0.1

Sign up to request clarification or add additional context in comments.

1 Comment

Saved my day. This was the reason console app didn't connect to MySQL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.