« | »

Install MySQL on Mac OSX using Homebrew

UPDATE: This post is probably very out of date. Please use at your own risk.

mysqlRecently, while doing some development on my Mac, I realized I didn’t have MySQL installed. I could have loaded up an instance of Ubuntu 12.04 LTS on VirtualBox and used that. However, I thought it would be much more convenient to have it available directly instead in a virtualized environment. Here are the instructions for installing it on a Mac using Homebrew.

This guide assumes Homebrew is installed and properly functioning.

First, ensure that Homebrew is update to date and ready to brew:

Next, install MySQL:

Start MySQL:

Secure your MySQL installation. The main purpose of doing this is to ensure that the configuration of the local environment is set up as close as possible to the production environment.

Run the following two commands to allow MySQL to run under your user account:

Add the following my.cnf file to /etc:

Restart the MySQL server:

13 Responses to Install MySQL on Mac OSX using Homebrew

  1. angela says:

    hi, I am following your directions, but ran into a glitch on step “Run the following two commands to allow MySQL to run under your user account:” on mac mavericks, in terminal, installed via homebrew. Can you be more explicit on the break out of the 2 commands? Everything seems okay up until this. Am I doing this correctly? Any help much appreciated, this is my first time installing without mamp, I’m trying to get better at using terminal…

    here is what I am getting as error in terminal after copy&paste your code:

    Macintosh:~ angie$ unset TMPDIR
    Macintosh:~ angie$ mysql_install_db –verbose –user=whoami \\
    FATAL ERROR: Could not find ./bin/my_print_defaults

    If you compiled from source, you need to run ‘make install’ to
    copy the software into the correct location ready for operation.

    If you are using a binary release, you must either be at the top
    level of the extracted archive, or pass the –basedir option
    pointing to that location.
    Macintosh:~ angie$ –datadir=/usr/local/var/mysql –tmpdir=/tmp
    -bash: –datadir=/usr/local/var/mysql: No such file or directory
    Macintosh:~ angie$

  2. Chris Beck says:

    Thanks so much.

    FWIW, I had to make sure that mysqld wasn’t running in order to run mysql_install_db

    Also, I chose to use launchd to run mysql at startup per the homebrew instructions

    To have launchd start mysql at login:
    ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

  3. Steve S says:

    I’m a noob trying to teach myself some things. I just tried to follow these instructions; here’s the result, copied directly from my Terminal window:

    Steves-MacBook-Pro:/ steves93$ brew install mysql
    ==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/mysql-5.6.16.mavericks.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Pouring mysql-5.6.16.mavericks.bottle.tar.gz
    ==> Caveats
    A “/etc/my.cnf” from another install may interfere with a Homebrew-built
    server starting up correctly.

    To connect:
    mysql -uroot

    To have launchd start mysql at login:
    ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
    Then to load mysql now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
    Or, if you don’t want/need launchctl, you can just run:
    mysql.server start
    ==> /usr/local/Cellar/mysql/5.6.16/bin/mysql_install_db –verbose –user=steves93 –basedir=/usr/local/Cellar/mysql/5.6.16 –datad
    ==> Summary

  4. Dal Price says:

    I used your tutorial to install mySQL on my machine. I wrote a follow up post on my blog on how to connect to a mySQL database using the command line once you have setup mySQL.

    Connect to a mySQL Database

    I thought other readers may be interested in reading it as a follow up to your tutorial / instructions.

  5. Jacob says:

    I am having the same problem as Angela.

  6. Jacob says:

    I found this link useful for resolving my issue: https://coderwall.com/p/os6woq

  7. Anonymous says:

    Getting “Can’t connect…through socket ‘/tmp/mysql'” when installing MySQL on Mac OS X 10.6 with…

    http://sevennet.org/2014/12/01/how-to-getting-cant-connect-through-socket-tmpmysql-when-installing-mysql-on-mac-os-x-10-6-with-homebrew/

  8. Erin says:

    I’m installing on Yosemite. Most of the process worked but when I get to the mysql_install_db step I get this error:

    WARNING: The host ‘MacBook-Pro.local’ could not be looked up with /usr/local/opt/mysql/bin/resolveip.
    This probably means that your libc libraries are not 100 % compatible
    with this binary MySQL version. The MySQL daemon, mysqld, should work
    normally with the exception that host name resolving will not work.
    This means that you should use IP addresses instead of hostnames
    when specifying MySQL privileges !

    And then if I continue on, when I get to the restart at the end, I get the error:
    . ERROR! The server quit without updating PID file (/usr/local/var/mysql/MacBook-Pro.local.pid).

    I don’t understand this — any thoughts?

  9. admin says:

    No, I don’t have a solution. I’ve been use VirtualBox mainly. Have you tried Googling the error?

  10. KaliDebian says:

    When running the two commands to allow to run under user:

    –basedir=”$(brew –prefix mysql)”

    after running above command, I get error:

    -bash: –basedir=/usr/local/opt/mysql: No such file or directory

    A bit confused since there is mysql directory and I am in the current version, is it because those need to be run outside of the ../mysql/5.6.24 directory?

    Please advise.

  11. KaliDebian says:

    @Erin:

    Are you in the mysql directory?

    /usr/local/Cellar/mysql/

    in order to run those two commands that is where you need to be I think, however my issue is that it is asking for usr/local/opt/mysql which is a bit confusing since that is not where mysql installs with homebrew…

    @admin, should we make new dir /opt just to put the mysql install in it and why does –basedir=”$(brew –prefix mysql)” not look for mysql in the proper dir? or if it is then why does homebrew install mysql in /Cellar?

    Sorry, please exclude my previous question.

  12. slowjack2k says:

    @Erin, my issue was that the folder /var/log/mysql was missing. mysqld did start up after I created this folder.

  13. slowjack2k says:

    @admin mysql_secure_installation should happen after the mysql_install_db step, because the test db will be still aviable in the other way around.

    Further more I had to stop mysql bevor the mysql_install_db step.

Leave a Reply

Your email address will not be published. Required fields are marked *