Zend_Db connects to wrong mysql socket

While working on a small project today. I was confronted with a Zend_Db exception that i have seen before. But it still had me searching for a solution. So this time i will write it done for future reference.

I’m working on a small ZF project which uses the MVC structure. And in the Initializer the database connection is setup like this:

public function initDb()
    {
    	$pdoParams = array(
            PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
        );

        $params = array(
            'host'     => 'localhost',
            'username' => '***********',
            'password' => '***********',
            'dbname'   => '***********',
            'driver_options' => $pdoParams,
        );

        $db = Zend_Db::factory('Pdo_Mysql', $params);
        Zend_Db_Table_Abstract::setDefaultAdapter($db);
        Zend_Registry::set('DB', $db);
    }

So when i first instantiated a connection to the database i was presented a nice error on screen. The stack trace is quiet long. But this is the most relevant part.

exception ‘Zend_Db_Adapter_Exception’ with message ‘SQLSTATE[HY000] [2002] Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)’

The php bug tracker revealed a nice solution. For some strange reason the PDO extension can’t determine the correct socket while the mysql, mysqli extensions can. This is easily solved in the bootstrap of the project by adding an extra parameter to the config array passed when calling Zend_Db::factory();

$params = array(
            'host'     => 'localhost',
            'username' => '***********',
            'password' => '***********',
            'dbname'   => '***********',
            'driver_options' => $pdoParams,
            'unix_socket' => '/var/run/mysqld/mysqld.sock'
        );

        $db = Zend_Db::factory('Pdo_Mysql', $params);
        Zend_Db_Table_Abstract::setDefaultAdapter($db);
        Zend_Registry::set('DB', $db);
del.icio.us Digg DZone reddit SlashDot StumbleUpon Technorati

2 Responses to 'Zend_Db connects to wrong mysql socket'

Subscribe to comments with RSS or TrackBack to 'Zend_Db connects to wrong mysql socket'.

  1. If you get an error re: mysqli when you try to access phpmyadmin from the ZS console, check what port you are accessing phpmyadmin.

    See this page: http://forums.zend.com/viewtopic.php?f=8&t=242
    and
    http://lenss.nl/2009/05/zend-server-ce-myphpadmin-mysqli-not-loaded/

    I still have not found out how to reinstall Zend phpMyAdmin so it runs on lighttpd, as I chose Apache the first time and a complete removal doesn’t reprompt for which web server to automatically configure it for. Let me know if you figure that one out.

    Hing Hoo Bird

    8 Jul 09 at 01:47

  2. [...] are a number links you can find by Googling that address this issue. One of possible interest is: http://lenss.nl/2009/06/zend_db-conn…-mysql-socket/ Finally, Zend's adapter has had known issues in the past, but I assume you're setup is up to [...]

Leave a Reply