Back in 2004, I wrote Foundation Mac OS X Web Development for friends of ED. The book covered all aspects of web development on the Mac OS X platform and sold well around the world. At the time of writing, Mac OS X was at version 10.3 (Panther). The majority of the book still holds up today, but for one wildly out of date section: installing and configuring the MAMP environment.
I figured it was time to address this on my blog. As well as updating these instructions, I’ll be covering the much requested setting up of virtual hosts.
Firstly, I’ll tackle the problem of installing MySQL, as some of you will be happy enough to use the pre-installed Apache and PHP. I’ll cover installing those from source those in a forthcoming article.
Installation Requirements
This install was performed on an old iBook G4 which I’ve crowbarred Leopard (10.5.6) and Xcode onto. It was a vanilla install on a clean disk, so there were no remnants from earlier versions of MAMP activity, other than the pre–installed MySQL in /usr/bin.
You’ll be using the command line on this job, for which you’ll need administrator access to do all this tinkering under the hood. As such, I’m going to assume you know what you’re doing and what not to do. If it’s not in your Dock already, you can find Terminal in the Utilities folder in your Applications directory: /Applications/Utilities/Terminal.app (or press cmd + U in Finder).
Set Your Path
To enable you to keep in control of things and because you keep a tight ship, you’ll be installing into /usr/local rather than /usr/bin. For reasons why you should do this, Dan Benjamin has written up a most excellent explanation: Using /usr/local. With that in mind, you’ll need to have your path set correctly, or this stuff ain’t gonna get you anywhere.
-
You need to add the following line into a file called
.bash_profile, so open up a new Terminal window and type the following:vim ~/.bash_profile
If the file already exists, it will open it. If no such file exists, you’ve just created it.
-
Press
ito enter insert mode and type:export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql:$PATH"
-
Press
escto exit insert mode and save your work by typing::wq
That’s your path set, so let’s get on with the main feature: installing MySQL.
Install MySQL 5.1.30
Basically, the steps are as follows: you’ll be creating a directory, downloading some files to it, building and then installing the files before finally telling OS X what to do with them. Right, let’s have at it.
-
Create a directory for the source files and then download & unpack them, entering your password when prompted:
sudo mkdir /usr/local/srccd /usr/local/srcsudo curl -O http://mysql.he.net/Downloads/MySQL-5.1/mysql-5.1.30.tar.gzsudo tar xzvf mysql-5.1.30.tar.gzcd mysql-5.1.30
-
Now that you’ve got the files, it’s time to configure them with this chunk of code [source for the curious]:
CC=gcc CFLAGS="-O3 -fno-omit-frame-pointer" CXX=gcc \CXXFLAGS="-O3 -fno-omit-frame-pointer -felide-constructors \-fno-exceptions -fno-rtti" \sudo ./configure --prefix=/usr/local/mysql \--with-extra-charsets=complex --enable-thread-safe-client \--enable-local-infile --disable-shared
-
With the configuration done, it’s time to build (make) and install the files. This is the part where you might want to nip off and put the kettle on, because building MySQL takes ages. Type:
sudo makesudo make install
-
MySQL installation requires a Mac OS X user account named mysql, but a user account with this name exists by default since Mac OS X 10.2. So, start MySQL off with some default databases:
cd /usr/local/mysqlsudo ./bin/mysql_install_db --user=mysqlsudo /usr/local/mysql/bin/mysqld_safe --socket=/private/tmp/mysql.sock --user=mysql &
-
Now that’s done, you can close that window. Open a new one and type the following to check
mysqldis running:ps aux | grep mysql
What you’ve done there is search through the processes that are running and asked for information about any
mysqlprocesses. If all has gone to plan, you should see a screen like this:_mysql 89722 0.0 0.1 613740 4380 ?? S 11:53am 0:00.03 /usr/local/mysql/libexec/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --log-error=/usr/local/mysql/var/macbook.local.err --pid-file=/usr/local/mysql/var/macbook.local.pidSuccess!
-
Now you need to set the admin password. In this code snippet, I’ve set the password to
new_password, but you should add your own password in place ofnew_password:mysqladmin -u root password new_password
If you get an error here telling you that
mysqladmin: command not found, then you didn’t set your path properly earlier./usr/local/mysqlis the part of the path that comes into play here. -
Next, you want to login to the mysql shell:
mysql -h localhost -u root -p
Type in the password you specified in the previous step, when prompted. You should see a screen like this:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.30 Source distributionType 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql> -
You’re almost done, but first you have to give the
mysqluser the right permissionsGRANT ALL PRIVILEGES ON *.* TO mysql@localhost IDENTIFIED BY 'new_password';
Obviously, replace
new_passwordwith the password you specified in the previous step. -
Now you’re good to go, so check to see what databases are present
SHOW DATABASES;
That should give you a screen that looks something like this:
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.05 sec)
Run MySQL on Boot
You’re almost there, don’t worry. To get MySQL to startup when you boot your Mac, you need a launchd file.
-
Open a new Terminal window and type:
vim ~/Desktop/com.mysql.mysqld.plist
-
Press
ito enter insert mode and type:<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict><key>KeepAlive</key><true/><key>Label</key><string>com.mysql.mysqld</string><key>Program</key><string>/usr/local/mysql/bin/mysqld_safe</string><key>RunAtLoad</key><true/><key>UserName</key><string>mysql</string><key>WorkingDirectory</key><string>/usr/local/mysql</string></dict></plist>
-
Press
escto exit insert mode and save your work by typing::wq
-
Now you need to move the file into the correct place and set the permissions on it. Type the following and type in your password when asked to do so:
sudo mv ~/Desktop/com.mysql.mysqld.plist /Library/LaunchDaemonssudo chown root /Library/LaunchDaemons/com.mysql.mysqld.plist
-
All that’s left to do now is tell
launchdto load and startup MySQL when your Mac boots, so type:sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysqld.plist
Hopefully, everything went as planned and you now have a fully working installation of MySQL 5.1.30 on your Mac.
I will try to answer any questions raised in the comments, time permitting, but feel free to help each other out in the meantime. =)
♼ Tweet this
This was a great walkthrough! Worked exactly as described. I feel all warm and mushy inside having compiled MySQL from source for the first time. Thanks so much!
If anyone’s worried about the line numbers, don’t. They won’t get pasted when you highlight and copy.
Success on MacBook 2.16GHz, 3GB mem, 320GB 7200, 10.5.6, XCode Tools from 10.5.0.
Great walk through except it didn’t work with the last build 5.1.31. when I ran the configuration script I received a “./configure” file not found error. Also, ‘make’ didn’t work, no target in the file. Any way it was a great tutorial and helped me better understand how mySQL is laid out. I did find this tutorial on how to configure the .pkg installed version http://wolfpaulus.com/journal/mac-osx/mysqlosx.html
while I was pulling my fairly sparse hair on why I was getting errors.
Thank you for your efforts.
Phil