Php – Native PHP / Mysql installation on Mac, activating the PDO driver

MySQLmysqlipdoPHP

I have a custom installation on the MacOSX Lion consisting of the following:

  1. Native PHP upgraded to 5.4, works like a charm.
  2. Installed MySQL, being run from /usr/local/mysql-5.5.25-osx10.6-x86_64/bin/mysql, works like a charm, Workbench connects and can do ops.
  3. Edited php.ini in /etc/ to activate the following:

    extension=php_mysqli.so
    extension=php_pdo_pgsql.so
    extension=php_pdo_mysql.so
    
  4. phpinfo() says the following:

    PDO

    PDO support enabled
    PDO drivers mysql, pgsql, sqlite

    pdo_mysql

    PDO Driver for MySQL enabled
    Client API version mysqlnd 5.0.10 – 20111026 – $Id: b0b3b15c693b7f6aeb3aa66b646fee339f175e39 $

    Directive Local Value Master Value
    pdo_mysql.default_socket /var/mysql/mysql.sock /var/mysql/mysql.sock

That means it works, yes?

Two problems though:

  • No such file as /var/mysql/mysql.sock, it's in /tmp/mysql.sock. But I configured and built php with the PDO options on: –with-mysql=mysqlnd –with-mysqli=mysqlnd –without-pear –with-pdo-mysql=mysqlnd –with-mysql-sock=/var/mysql/mysql.sock. No effect if I change the ini setting to /tmp/mysql.sock, it's reflected in php.ini, but the error stays exactly the same.

  • When I try to instantiate the PDO class inside PHP, this happens:
    Uncaught exception 'PDOException' with message 'could not find driver'

When I try accessing MySQL from PHP in an old school mysqli kind of way, I get this:

Failed to connect to MySQL: (2002) No such file or directory Warning: main(): Couldn't fetch mysqli in…

What else can I try to make my PHP and my MySQL play nice?

Best Answer

The solution was to do the following:

  1. Change .sock path in php.ini to reflect the actual path
  2. Restart your computer, not just apache

It appears the php.ini changes did not load while doing

$ sudo apachectl restart

and needed the Macbook to reboot before it took them into account.