Fork me on GitHub


MacPorts, php5-mysql & mysqlnd

August 26, 2009 | PHP Scripting

If you don’t know what that title says, don’t bother reading on 😉

A little useless history: MacPorts decided to use the recent release of PHP 5.3 to change the way they handle PHP5 Extensions. They used to be applied by the MacPorts variants system but have been changed (and appear to still be in transition) to each be their own package. This doesn’t bother me as it brings MacPorts to be more in line with how other package managers manage PHP extensions.

What I didn’t notice in my most recent upgrade was that the php5-mysql extension defaults to using a new extension from MySQL called MySQL Native Driver. Its benefits sure look nice and I can understand why they’d want to unify everything.

It would be nice if it worked in MacPorts… ’cause it doesn’t. And the error messages are stupidly cryptic. For example, I got these error messages when trying to load a project:

PHP Warning: mysqli::real_escape_string() [mysqli.real-escape-string]: \
Couldn't fetch mysqli in /path/to/file.php on line 72, \
referer: http://localhost/dir/test.php

Pretty darned informative, eh? And on top of that PHPMyAdmin continued to function normally and without incident. WordPress wouldn’t connect either.

I still have no idea what this means or why it was happening, I just know that I found a workaround. Re-install php5-mysql with the mysql5 variant. ie:

$ sudo port deactivate php5-mysql
$ sudo port install php5-mysql +mysql5

This tells PHP to use the old-style MySQL install specific libraries. In my case (and I suspect in just about every case I’ll ever run in to) this will be fine. WordPress is now back up and all my other projects are still functional as well. All is right with the world again… well, sort of. 😉

The only drawback is that I’ll probably never notice when the MySQL Native Driver actually starts working as I’ll forget this in a few months and never think to check it again…

Update: It turns out that I’m a doofus on this one. When upgrading PHP I didn’t notice one of the install messages left during the build process. It reads:

To use mysqlnd with a local MySQL server, edit /opt/local/etc/php5/php.ini and set mysql.default_socket, mysqli.default_socket and pdo_mysql.default_socket to /opt/local/var/run/mysql5/mysqld.sock

So, with that change, I’m up and running using the default mysqlnd variant of php5-mysql.

4 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  • Very helpful post. I just hit the same problem and you saved me at least an hour of mucking around. Thankyou!

    Ryan, August 27, 2009 11:45 pm | permalink

  • Did you have to do anything else? I am working on setting this up. I see mysqlnd in the phpinfo but I cannot connect to the database. I set the various sockets to the above but no go. Any ideas or help? I’m using OSX and installed php using macports

    Jason, February 28, 2012 6:43 pm | permalink


  1. Installed MacPorts PHP, now cannot interface with MySQL – Mac OS X | All about Apple Products

    […] is a great blog post which only states the same phrase that the php5-mysql post-install script […]

    April 14, 2011 | 8:49 am

  2. Installed MacPorts PHP, now cannot interface with MySQL - Mac OS X - PHP Solutions - Developers Q & A

    […] is a great blog post which only states the same phrase that the php5-mysql post-install script […]

    January 13, 2014 | 7:22 pm

Comments are closed