Compiling and Installing MySQL 5 on Mac OS X Leopard

May 20, 2008

Contents

Introduction

I have found compiling MySQL Database Server Community Edition from source to be advantageous. Building MySQL from source allows complete control over the installation configuration as well as giving you a better sense of how to administer the server. If you are installing MySQL on a PowerPC it is best to compile MySQL since there is no packaged installation for Leopard and PowerPC.

This document assumes that the OS X development kit is installed. It also assumes that you are installing MySQL from scratch for the first time. If you have installed MySQL in the past, you may not have to perform some of the steps such as creating the MySQL user and group.

Download the source code and unpack into source directory

Download the MySQL source code

Download the Compressed GNU TAR archive (tar.gz) source download from http://dev.mysql.com/downloads/mysql/5.0.html The source downloads are at the bottom of the list. At the time of this writing version 5.0.51 is the latest release.

Create the source code directory and unpack

Launch Terminal and execute the following commands where <adminID> is your administrator ID.

# Create a /usr/local/src directory if it doesn't already exist
cd /usr/local
sudo mkdir src
sudo chown <adminID> src
sudo chgrp admin src
chmod 750 src

# Change to the source directory and unpack
cd src
tar -xvzf ~/Downloads/mysql-5.0.51.tar.gz

Create MySQL Group and User

It is recommended that you run the mysql server as its own user such as _mysql. On Leopard, the netinfo GUI utility for creating users and groups does not exist, so you must use the dscl command line utility instead. The dscl utility can be used either in command line or interactive mode, but I find the interactive mode to be a little faster to use. Be extremely careful when inside the dscl utility as root. If you make a mistake with the create command, you can generally reissue the command to overwrite it. A unique id must be created for both the mysql user and group. You can find out what id's are already in use by issuing the list command. The id number 74 is recommended for both the _mysql group and user.

# When launching dscl make sure you use the period (localhost)
sudo dscl .

# Change directories to Groups
cd Groups

# Find out what id's are already in use
list . PrimaryGroupID

# Create the _mysql group
create _mysql

# Create mysql alias for _mysql group
append _mysql RecordName mysql

# Create the group id number, where <GroupIDNUM> is an id number not already in use
create _mysql PrimaryGroupID <GroupIDNUM>

# Create a long name for the group id
create _mysql RealName "MySQL Group"

# Change directories to users
cd ../Users

# List user id's already in use
list . UniqueID

# Create the _mysql user
create _mysql

# Create mysql alias for the _mysql user
append _mysql RecordName mysql

# Create the long name
create _mysql RealName "MySQL User"

# Create the user id number, where <UserIDNUM> is an id number not already in use
create _mysql UniqueID <UserIDNUM>

# Create the primary group id number, where <GroupIDNUM> is the id number used when creating the group
create _mysql PrimaryGroupID <GroupIDNUM>

# Create the default shell
create _mysql UserShell /user/bin/false

# Exit dscl
quit

Create the MySQL Socket Directory

# Create and set permissions for /var/mysql directory
cd /var
sudo mkdir mysql
sudo chgrp mysql mysql
sudo chmod 775 mysql

Build, Test and Configure MySQL

You may want to build mysql with different options such as a another default charset and collation.
Use the ./configure --help command to get a complete list of build time options
When building the make file (configure) select either a 32-bit single architecture or 32/64-bit (4-way Universal) build.

Execute the following commands in Terminal:

The OS X developer tools must be installed.

# Change to the source directory
cd /usr/local/src/mysql-5.0.51

# Configure the make file for the architecture you are compiling on (1-Way 32-bit Binary)
CFLAGS="-O3 -fno-omit-frame-pointer" CXXFLAGS="-O3 -fno-omit-frame-pointer -felide-constructors -fno-exceptions -fno-rtti" ./configure --prefix=/usr/local/mysql --with-extra-charsets=complex --enable-thread-safe-client --enable-local-infile --enable-shared --with-unix-socket-path=/var/mysql/mysql.sock --with-charset=latin1 --with-collation=latin1_general_ci --with-msqld-user=_mysql

# Configure the make file to compile a 32/64-bit binary (4-Way Universal Binary)
CFLAGS="-arch ppc -arch i386 -arch ppc64 -arch x86_64 -O3 -fno-omit-frame-pointer" CXXFLAGS="-arch ppc -arch i386 -arch ppc64 -arch x86_64 -O3 -fno-omit-frame-pointer -felide-constructors -fno-exceptions -fno-rtti" ./configure --prefix=/usr/local/mysql --disable-dependency-tracking --with-extra-charsets=complex --enable-thread-safe-client --enable-local-infile --enable-shared --with-unix-socket-path=/var/mysql/mysql.sock --with-charset=latin1 --with-collation=latin1_general_ci --with-msqld-user=_mysql

# Build the executables
make

# Copy the installation to the default /usr/local/mysql directory
sudo make install

# Test the build
cd mysql-test
perl mysql-test-run.pl
or
perl mysql-test-run.pl --force /* Run without stopping for errors
When testing the 5.0.51 release you will get an error on the test "mysqlcheck." This error is not the fault of the mysql engine, but the test itself and can be ignored.

# Copy the configuration file to etc
cd ..
sudo cp support-files/my-medium.cnf /etc/my.cnf
cd /etc
sudo chown <adminID> my.cnf
sudo chgrp mysql my.cnf
chmod 640 my.cnf

# Configure MySQL installation directory permissions
cd /usr/local
sudo chown -R <adminID> mysql
sudo chgrp -R admin mysql
sudo chgrp mysql mysql
sudo chmod 775 mysql

# Create the database directory and initialize the grant tables
cd mysql
sudo bin/mysql_install_db --user=mysql

Start the MySQL database server and check for errors and warnings

Use the MySQL utility mysqld_safe to start the server

Execute the following commands in Terminal:

# Start the server
sudo -b bin/mysqld_safe

# Check for startup errors and warnings
# Change to user root
sudo -s

# Change to the database directory
cd var

# List the directory
ls

# Dump the error log
cat <ComputerName>.err

# Exit from user root
exit

# To stop the server ...
cd ..
bin/mysqladmin -u root -p shutdown

Secure the MySQL Installation

Use the MySQL utility to set the database root password

Execute the following commands in Terminal:

# If you stopped the server in the last step, restart it
sudo -b bin/mysqld_safe

# Set the database root password
bin/mysql_secure_installation

Add MySQL Utilities Directory to the shell path

Use your favorite editor to edit the bash shell .profile startup script in your home directory.

Execute the following commands in Terminal:

# Change to the home directory
cd

# Create .profile if it doesn't exist
touch .profile

# Edit the shell startup script with your favorite editor
nano .profile

# Add the MySQL directory to the path and exit the editor
export PATH=$PATH:/usr/local/mysql/bin

Starting MySQL at Boot Time

The easiest way to start MySQL at boot time is to download one of the package format (.dmg) versions of MySQL for OS X and run the MySQLStartupItem.pkg, which copies the startup scripts to /Library/StartupItems as part of its installation. If you are running on a PowerPC box you can download the 10.4 packaged version. Also, if you are running on a PowerPC box, you will also need to download an updated version of the preference pane from MySQL. Copy the preference pane to either /Library/PreferencePanes or ~/Library/PreferencePanes.

Once you have installed the startup scripts you must create a symbolic link from /usr/local/mysql/support-files/ to /usr/local/mysql/share/mysql/. The preference pane and boot time startup script expect the mysql startup script mysql.server to be located in /usr/local/mysql/suport-files.

# Change directories into the mysql directory
cd /usr/local/mysql

# Create the symbolic link
ln -s share/mysql support-files

Useful Links

Feedback

Please let me know if you found this page useful or have any other comments.

Comments:

Your Email (Optional):