How to install MySQL on Mac OS X Snow Leopard

In the last post of this series ([intlink id=”how-to-create-a-wordpress-development-environment-on-mac-os-x-snow-leopard” type=”post”]How to create a WordPress development environment on Mac OS X Snow Leopard[/intlink]) we covered [intlink id=”how-to-setup-apache-and-php-on-mac-os-x-snow-leopard” type=”post”]how to install Apache and PHP on a Mac OS X Snow Leopard[/intlink]. Now it’s time to go on and also install MySQL so we could continue with setting up WordPress.

Download and install MacPorts

Getting MacPorts is relatively easy – just go to MacPorts website and download MacPorts for Snow Leopard. After the download is finished just go ahead and install it (pretty regular OS X app installation procedure).

Once you have MacPorts installed we should see if it needs any updates. For that please make sure that you have administrator rights, open Terminal and type in following command:

sudo port selfupdate

Hopefully everything went fine and we can continue with installing MySQL.

Lets install MySQL on Snow Leopard

First off we’ll need to install MySQL from Macports. Lets use following command in Terminal to do that:

sudo port install mysql5 mysql5-server

The installation process can take a while and you’ll see something like that on your Terminal:

---> Computing dependencies for mysql5
---> Fetching mysql5
---> Attempting to fetch mysql-5.0.85.tar.gz from http://ftp.plusline.de/mysql/Downloads/MySQL-5.0/
---> Verifying checksum(s) for mysql5
---> Extracting mysql5
---> Applying patches to mysql5
---> Configuring mysql5
---> Building mysql5
---> Staging mysql5 into destroot
---> Installing mysql5 @5.0.85_0+server
---> Activating mysql5 @5.0.85_0+server
---> Cleaning mysql5
---> Computing dependencies for mysql5-server
---> Fetching mysql5-server
---> Verifying checksum(s) for mysql5-server
---> Extracting mysql5-server
---> Configuring mysql5-server
---> Building mysql5-server
---> Staging mysql5-server into destroot
---> Creating launchd control script
###########################################################
# A startup item has been generated that will aid in
# starting mysql5-server with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
###########################################################
---> Installing mysql5-server @5.0.85_0
******************************************************
* In order to setup the database, you might want to run
* sudo -u mysql mysql_install_db5
* if this is a new install
******************************************************
---> Activating mysql5-server @5.0.85_0
---> Cleaning mysql5-server

Looks like we were lucky and didn’t catch any errors on this one.

Add MySQL as a startup item

If you are like me then you want that MySQL would be always running. For me it’s just easier that way since I’ll use it on daily basis.

sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist

Done. MySQL now starts every time you start your Mac.

Create initial MySQL databases

MySQL requires that you create the initial databases. On our case the MySQL databases are kept under /opt/local/var/db/mysql5/. If you take a look you’ll notice that this folder is empty.

Lets create the default databases by executing this command:

sudo -u mysql mysql_install_db5

You should see something like that:

Installing MySQL system tables...
091023 22:11:11 [Warning] Setting lower_case_table_names=2 because file system for /opt/local/var/db/mysql5/ is case insensitive
OK
Filling help tables...
091023 22:11:11 [Warning] Setting lower_case_table_names=2 because file system for /opt/local/var/db/mysql5/ is case insensitive
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
/opt/local/lib/mysql5/bin/mysqladmin -u root -h MacBook-Pro.local password 'new-password'

Alternatively you can run:
/opt/local/lib/mysql5/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /opt/local ; /opt/local/lib/mysql5/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl

Please report any problems with the /opt/local/lib/mysql5/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com

Start MySQL for the first time

We have the default databases created and it’s time to start MySQL server for the first time.

sudo /opt/local/lib/mysql5/bin/mysqld_safe &

You’ll get a response like this

Starting mysqld daemon with databases from /opt/local/var/db/mysql5

Once the server has started just press ctrl + c on your keyboard to exit.

Run MySQL from command line easily

I always tend to forget that I need to use mysql5 on command line instead of just typing in mysql.

Fortunately there’s an easy fix for that, just create symlinks for MySQL and MySQLDump

 sudo ln -s /opt/local/bin/mysql5 /opt/local/bin/mysql
 sudo ln -s /opt/local/bin/mysqldump5 /opt/local/bin/mysqldump

Done. Now you can run MySQL with just typing mysql instead of mysql5.

Secure your MySQL server

It’s always a good idea to have password for your system services. To do that on MySQL just type in the following command.

/opt/local/lib/mysql5/bin/mysqladmin -u root password [your password goes here]

That’s it. MySQL should be up and running for you now with no problems.

Tips and tricks

Some tips and tricks that I used to forget myself quite often.

Shut down MySQL server manually

mysqladmin5 -u root -p shutdown

Remove MySQL from system start up.

sudo launchctl unload -w /Library/LaunchDaemons/com.mysql.mysqld.plist
Martin Kivi
Martin is the Founder and CEO of PerfectLine. He is passionate about front end development, user interface design and building awesome products. He will be your first point of contact here at PerfectLine.

7 Comments

  • Tim

    Martin, thanks for the writeup. very helpful, i can never seem to remember the launchctl stuff.

    you might also want to have a look at homebrew — “The missing package manager for OS X” — which is way cooler than macports IMHO.

    http://github.com/mxcl/homebrew

    cheers
    tim

    • Martin Kivi

      I’m really glad that you found it helpful.

      I’ve looked into Homebrew and it was pretty cool but the old habits are hard to die. I’m sure that I’ll check out Homebrew more in depth next time I’ll do a re-install on my machine.

  • JurLan

    Hi.
    I’m running into some problems.
    First I had XAMMP installed. I removed it with AppCleaner and installed mysql using your method.
    I didn’t get any errors, but when I try to start mysql, it fails.
    In /opt/local/var/db/mysql5/JurLans-MacBook-Pro.local.err
    I see the line:
    [ERROR] Can’t find messagefile ‘/Applications/XAMPP/xamppfiles/var/mysql/share/mysql5/mysql/english/errmsg.sys’
    Could this be related?
    Thanks in advance.

  • Dan

    This worked perfectly for me. Thank you very much for taking the time to write this out step by step. I couldn’t have gotten MySQL working without this!

    Cheers,

    D.

  • Michele

    Thanks a bunch for putting this up – the instructions are easy to follow, and more importantly – they work!

  • Nick

    After running
    sudo port install mysql5 mysql5-server

    Getting error–

    Warning: xcodebuild exists but failed to execute
    Warning: Xcode does not appear to be installed; most ports will likely fail to build.
    —> Computing dependencies for mysql5
    —> Dependencies to be installed: openssl readline ncurses
    —> Building openssl
    Error: Target org.macports.build returned: can’t read “build.cmd”: Failed to locate ‘make’ in path: ‘/opt/local/bin:/opt/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin’ or at its MacPorts configuration time location, did you move it?
    Error: Failed to install openssl
    Log for openssl is at: /opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_devel_openssl/openssl/main.log
    Error: The following dependencies were not installed: openssl readline ncurses
    Error: Status 1 encountered during processing.
    To report a bug, see

    • Nick

      never mind I was able to fix those errors and able to follow steps you mentioned. Thanks for help.

Liked this post?

There’s more where that came from. Follow us on Facebook, Twitter or subscribe to our RSS feed to get all the latest posts immediately.