Compiling and Installing MySQL 5 on Mac OS X Leopard
May 26, 2009
Contents
- Introduction
- Download and Unpack the MySQL Source Code
- Create MySQL User and Group
- Create MySQL Socket Directory
- Build, Test and Configure MySQL
- Start MySQL Database Server and Check for Errors
- Secure the MySQL Installation
- Add MySQL Utilities to the Path
- Starting MySQL at Boot Time
- Troubleshooting
- Links
- Feedback
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. These instructions will work with MySQL 5.1, as well as MySQL 5.0.
This document assumes that the OS X developer tools are installed (XCode 3.0). 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.
I have tried to make this document as concise and direct to the point as possible. It is helpful to have some knowledge of the basic UNIX commands, permissions and the GNU build system.
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, under the heading Source downloads. At the time of this writing version 5.0.81 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.81.tar.gz
# Consider changing the ownership of the /usr/local directory to your admin id. It makes administration easier as you don't have to do so many sudo's or run as root.
Create MySQL Group and User
It is recommended that you run the mysql server as its own user such as _mysql. On Leopard, this id should already be created for you. However, if _mysql does not exist, you must use the dscl command line utility, as the netinfo GUI utility for creating users and groups no longer exists. 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 /usr/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.81
# 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-mysqld-user=_mysql
# Configure the make file to compile a 32/64-bit binary (4-Way Universal Binary)
# Note: The 4-Way Universal Binary build takes a long time
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-mysqld-user=_mysql
# Build the executables
make
# Test the build
cd mysql-test
perl mysql-test-run.pl
or
perl mysql-test-run.pl --force /* Run without stopping for errors
# You will receive one error when testing. There is a patch located at http://lists.mysql.com/commits/67644
# Create mysql binaries and database directory
sudo mkdir /usr/local/mysql-5.0.81
# Create a symbolic link to the installation directory
sudo ln -sfhv /usr/local/mysql-5.0.81 /usr/local/mysql
# Copy the installation to the default /usr/local/mysql directory
cd ..
sudo make install
# Copy the configuration file to /etc
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
# If you overwrote your existing installation, don't perform this step,
# as the recursive chown's will change the permissions on your database directory.
cd /usr/local
sudo chown -RH <adminID> mysql
sudo chgrp -RH admin mysql
sudo chgrp _mysql mysql
sudo chmod 775 mysql
sudo chmod -h 775 mysql
sudo chgrp -h _mysql mysql
sudo chown -h <adminID> mysql
# Create the database directory and initialize the grant tables
cd mysql
sudo bin/mysql_install_db --user=_mysql
# Note: If you are running MySQL 5.1 and receive the error - [ERROR] /usr/local/mysql/libexec/mysqld: unknown option '--skip-federated', you may have an old /etc/my.cnf configuration file. Edit my.cnf and delete or comment out the skip-federated option.
# You may also want to add the lower_case_table_names = 2 option to /etc/my.cnf to keep the server from generating warnings in the log file about lower case table names. Add this option under [mysqld].
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 - Make sure to stop any MySQL servers already running
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
# Login to your mysql server with mysql command line tool
bin/mysql -u root -p
# Add the -h hostname option if you are operating remotely.
# Use the quit command to exit the mysql command line tool.
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.
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
ln -s share/mysql support-files
Troubleshooting
If things are not running as expected first check the logs for errors in /usr/local/mysql/var
Keep in mind that the packaged version of mysql and the source version of mysql keep the database files in different directories. The source version in /usr/local/mysql/var and the packaged version in /usr/local/mysql/data. Also, the packaged version of mysql creates the socket in /tmp where as these instructions create the socket at the prescribed Apple location of /var/mysql. If you are running phpMyAdmin you may have to change the socket location in the config.inc.php configuration file to look something like this: $cfg['Servers'][$i]['socket'] = '/var/mysql/mysql.sock';.
Useful Links
Feedback
Please let me know if you found this page useful or have any other comments.