I’m on bedside watch this evening with my pops, who is dealing with fluid drain and a tracheotomy tube, so I decided to find something light to occupy my downtime: Installing MySQL and PHP on my laptop and setting up a development environment. Because what could be more easy and fun than wading through pages and pages of forums to try to install and talk to an open-source database?

Bubble

Disclaimer: I’m not a grey-haired wizard, so these instructions are what worked for me, but not the One True Way. I’m including them here to maybe help someone else save some time. I’m running 10.4.11 on an Intel MacBook Pro, and I’ve never installed or started MySQL, Apache, or PHP on it. So here goes.

First, I checked to see if MySQL was installed on my machine with this terminal command (which should return a tidy list of applications):

ps -axc

If it’s installed and running, there should be something in the list called “mysqld”. It wasn’t there for me. I also searched for any files called MySQL, and didn’t find anything that looked like it was a running module. So I downloaded an Intel installer package from the MySQL site, ran the installer, and installed the Preference Pane.

After wondering stupidly for a few minutes why I couldn’t get anything to come up at http://localhost/ in Safari, I remembered to turn on Personal Web Sharing in the Preferences, which starts Apache. This is why they call me the Idiot.

Next, I looked to see if PHP was running on this machine by writing the following file and saving it as php_test.php inside /Library/WebServer/Documents:

<html>

<p>This is an html line</p>

<?php

echo “this is a php line”;

phpinfo();

?>

</html></span>

Opening up Safari, I pointed to it here: http://localhost/php_test.php

Safari dutifully printed the code without parsing the PHP, so I knew it wasn’t working. I do know that PHP is preinstalled on OS X but not configured to run out of the box, so I did some looking.

Following directions here, I opened the http.conf file in Pico (I’m a dork, yes)

sudo pico /etc/httpd/httpd.conf

and uncommented (remove the #) the following lines:

# LoadModule php4_module libexec/httpd/libphp4.so

# AddModule mod_php4.c

I went back to the Preferences, turned off Personal Web Sharing, and turned it back on (thereby restarting Apache without having to resort to 5 minutes of Google searching and arcane Terminal commands). If all was done correctly, Apache should now be loaded with the PHP modules on.

Reloading the php_test.php file in Safari, I got a happy PHP config page (you’ll know it when you see it).

OK, so next I have to see if I can talk to MySQL with PHP. I’m coming clean here and admitting I’m using a copy of PHP & MySQL for Dummies, which doesn’t really get into the issue of passwords and root users until Chapter 5, and then it sort of glosses over things. A simple fact it took a while to find is that MySQL starts up with a superuser of “root” and a blank password. (I think I already knew this, but then forgot it). Anyway, I used a script found here, and changed the following lines from

$host=”hostname”;

$user=”mysqlaccount”;

$password=”mysqlpassword”;

to

$host=”localhost”;

$user=”root”;

$password=””;

All this page will do is talk to MySQL and return a list of variables, thereby confirming that it’s running and available to connect.

When I loaded the page in Safari, I got the dreaded “Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock'” error. As much as the Idiot here can figure out, this is some issue with the OS X install of MySQL and the pathways it uses.

Looking around the web, I found this page in Apple’s forums which seems to fix the issue. In a nutshell, I followed the instructions at the bottom of the page to create a symbolic link to the lock file, like so:

cd /var

ls

If there’s no directory in there named “mysql”, then do this:

sudo mkdir mysql

cd mysql

sudo ln -s /private/tmp/mysql.sock mysql.sock

Then, I reloaded the mysql_up.php file I’d made earlier, and I got the result I was looking for: A table with a bunch of numbers that made no sense (variables). but the important thing was that there were no errors, which meant that PHP was talking to MySQL.

Now, I just need to learn how to program so I can use this shit.

Date posted: May 9, 2008 | Filed under geek | Comments Off on More Distractions, or: MySQL and PHP on OS X for morons.

Comments are closed.