PHP Unit With Mysql and MAMP

by John H
2 minutes

I had an enormous difficulty getting Mysql to work with PHP Unit.  I kept getting errors with it not finding Mysql.  The issue had to do with phpunit not being able to locate the mysql socket.  I finally was able to solve the issue two different ways.

Sym Link Solution

The first solution I was able to get working was by creating a sym link for the socket in the location that phpunit was looking. In short phpunit was looking for my mysql sock file in '/var/mysql/mysql.sock' and my mysql sock file is in the applications folder under mamp.

The solution is to create a symbolic link to the sock file in the mamp director

sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock

PHP Mysql connection solution

The other solution - which I believe is the better solution is to define a persistent connection that my code uses. My PHP db connection is mysqli_connect(). Normally I just have the minimum parameters , mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_NAME), but to fix this issue you'll need to include two other parameters like so.

mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_NAME, NULL, '/Applications/MAMP/tmp/mysql/mysql.sock');

As you can see the parameters now include the socket location for mysql sock in the MAMP directory. Now my phpunit tests work in conjunction with my local setup.

Related Articles

PHP Sessions and SSL

I may want to make it so that I my php sessions last longer on a project that I'm working on right...

John H John H
~1 minute

Setup Google Analytics with Subdomain

By default all of the traffic that goes to any subdomain shows up as a root traffic.  What if you...

John H John H
~1 minute

Tracking button clicks with Google Analytics and JQuery

Using jquery it is  very easy to push clicks and events to Google analytics To make things...

John H John H
2 minutes