Setting up MySQL on Mac OS X

Date: Fri Nov 09 2007
Mac OS X does not include MySQL or any other SQL server. Since a database is such a crucial part of web site construction, let's look at how to set this up.

Getting the server

Macintosh, Apache, Mysql and PHP (MAMP) offers a convenient package that you download as a DMG and it executes an Apache/MySQL/PHP instance. It is meant for development, not for running a production web server.

MySQL AB offers a precompiled version of MySQL that can be installed so it starts on every reboot. This is the server we will discuss on this page. The MAMP package is useful as a preconfigured bundle that you can click and run, but for the purpose of having the database always running one must install it this other way. One can also download the source and compile it yourself, if you want to customize it further.

MySQL offers three versions, Standard, Max (containing experimental additions) and Debug (including extra debug information).

Once on the MySQL site, click on the downloads button and choose the Community Server. This is the free version. They offer some questions to help you determine whether it's a good idea for you to run this version. For example if you already have database administration experience. If this list of requirements makes you nervous, you should consider the MAMP package instead.

Two important notes are available in the MySQL documentation:

Installation

The DMG you download contains two packages. The server, and a startup item.

The server is in a package named mysql-standard-version.pkg and this is what should be installed first. Double click on it, answer all the questions, and the server will be installed as /usr/local/mysql-VERSION and /usr/local/mysql.

Verify an account named mysql exists on your system:


% grep mysql /etc/passwd
mysql:*:74:74:MySQL Server:/var/empty:/usr/bin/false

The startup item package sets up a script that starts the database server when the system boots.

And the DMG includes a "prefPane" file which, if you double click, will install itself into the system preferences. From the preferences pane you can start or stop the MySQL server, and control whether it starts automatically when the system boots.

After installation go to /usr/local/mysql and run the configure script therein. This script sets up initial database information, initial tables, and starts the MySQL server running.

For convenience you should set /usr/local/mysql/bin to be in your PATH variable. One way is in your ~/.profile to add this line:


export PATH=/usr/local/mysql/bin:${PATH}

MySQL has its own set of user names, passwords and permissions that are separate from those used by Mac OS X. Out of the box the MySQL installation has open passwords, and the configure script suggests you should change them. This is a good idea, generally.

Section 2.3.15, “Post-Installation Setup and Testing” covers some steps to follow after installing the server.

2.3.15.3. Securing the Initial MySQL Accounts covers how to set protections on the initial accounts.

5.9. MySQL User Account Management covers information on setting up user accounts and controlling passwords.

Customization

You may, or may not, want to customize some aspects to how the MySQL instance runs on your system. For example, where do the data files go? By default the data files live in /usr/local/mysql/data but you may want them to be elsewhere.

This MySQL installation does not have a system configuration file. Instead the configuration is controlled by the /usr/local/mysql/bin/mysqld_safe script. This is the script to modify if you wish MySQL to run differently.