Installing MySQL on Mac OS X 10.5

Since the release of Mac OS X Leopard, installing MySQL has caused headaches for nearly all its early adopters, including myself. After much research, I had to bite the bullet and just figure it out myself. These instructions are what I came up with, and I hope they prove helpful to others. I’ve tried this on several machines and the method works every time.

In this article I am going to assume you are familiar with the UNIX command line. If you are not familiar with UNIX then I recommend you purchase and read UNIX Visual QuickStart Guide. Most web developers are exposed to UNIX every day and its important to know the technology. This way you can configure and install your own software.

(Note: If you don’t already have Mac Ports then I’d suggest you download and install it. It’s an excellent UNIX package manager for Mac OS X. Make sure you read the instructions for installing Mac Ports. Mac Ports requires Xcode tools to be installed first for example. )

  • First run this command in your terminal to install the MySQL 5 package:

    sudo port install mysql5 +server
  • Second load the MySQL server by running the following command: (Note: Mac OS X Tiger and above use launchd for starting programs, and this command is worth getting familiar with. In this case the startup configuration file was created for us when we installed MySQL 5.)

    sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
  • Third figure out the path for your server socket. It should be /opt/local/var/run/mysql5/mysqld.sock but verify this by running this command:

    mysql_config5 --socket
    /opt/local/var/run/mysql5/mysqld.sock
     
  • Fourth you want to create a shortcut to the MySQL socket so that PHP, Ruby on Rails, Python, and your other languages can access it. These commands should make MySQL work for PHP and Ruby on Rails:

    sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock
    sudo mkdir /var/mysql
    sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /var/mysql/mysql.sock
  • Fifth you will need to shut down the MySQL server for now. Run this command:

    sudo launchctl unload -w /Library/LaunchDaemons/org.macports.mysql5.plist
  • Sixth you need to setup the root user and the default MySQL database. Sometimes called the grant tables. To do this run this command:

    sudo /opt/local/lib/mysql5/bin/mysql_install_db --user=mysql
  • And last you’ll need to start up the MySQL server and then login and change the root password.

    sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
    mysql5 -u root
    UPDATE mysql.user SET Password = PASSWORD('password') WHERE User = 'root';
    FLUSH PRIVILEGES;
    quit
  • Now you should be able to login using your new password.

    mysql5 -u root -p

That’s it you’re all setup with MySQL on Mac OS X Leopard.

49 thoughts on “Installing MySQL on Mac OS X 10.5”

  1. What is wrong when I do this command:

    computer-name:~ user$ mysql_config5 –socket
    -bash: mysql_config5: command not found

    I installed macports as well

  2. sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock
    sudo mkdir /var/mysql
    sudo ln -s /var/mysql/mysql.sock

    The 3rd ln -s command seems to be missing an argument.

  3. John

    Thank you for your feedback.

    I edited my post to include the correction for this command.

    sudo ln -s /var/mysql/mysql.sock
    to
    sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock

    All the command mysql_config5 --socket does is tell you the path to the server socket. It should be /opt/local/var/run/mysql5/mysqld.sock as shown in the ln commands.

    I edited this portion of my post too. It wasn’t clear enough.

  4. John

    You need to edit your “.profile” file in your home directory. After you install Mac Ports you need to setup your paths in bash.

    You need to setup your paths for Mac Ports. This explains why your mysql_config5 command doesn’t work. You will have trouble with the rest of the commands unless you setup a .profile in your home directory.

    Make a new text file using your favorite text editor and save it in your home directory as “.profile”. Any file with a . in front of it is a hidden file. It must be saved as “.profile”. You can find it and edit it using your terminal at any time. Run ls -a to see hidden files in a directory for example.

    Put the following in this text file.

    export PATH=/opt/local/bin:/opt/local/lib:/opt/local/man:$PATH

    Close your terminal window and open a new one. Your problem should be fixed.

  5. I struggled to get this working for a while, but here is the command that did the trick for me in the end:

    (key being the underscore for the _mysql user)

    sudo -u _mysql mysql_install_db5

    I also had to run a chgrp -R _mysql on /opt/var/local/db

    Hope that helps someone.

  6. Thanks for the step by step instructions – I struggled all afternoon yesterday before finding your steps. One correction though – in the launchctl command, ‘darwinports’ should be ‘macports’ instead. Perhaps this changed since your first posting.

  7. Dan is correct. Can this post be corrected to change the launchctl command from “darwinports” to “macports”. That change worked for me. Great steps otherwise.

  8. Thanks guys, It looks like an update to the MacPorts MySQL packaged changed the name of that file. So I edited the post to reflect this change. I am glad that this has been helpful to people. I plan on writing more posts just like this one.

  9. Fourth you want to create a shortcut to the MySQL socket so that PHP, Ruby on Rails, Python, and your other languages can access it. These commands should make MySQL work for PHP and Ruby on Rails:

    sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock
    sudo mkdir /var/mysql
    sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /var/mysql/mysql.sock

    ——————-

    This I do not understand. I have to run
    sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock

    every time to keep my rails app going when I start up my computer every day. Is there a way to set this once?

    Thanks

  10. Jeff,

    What you need to do is add the port command to your path.


    # Setting the path for MacPorts.
    export PATH=/opt/local/bin:/usr/local/bin:/usr/local/sbin:$PATH

    Use a text editor to edit the hidden file .profile. If this file doesn’t exist then create one. Add the above line to it. You should be all set.

    Then you can run this command below or close your terminal window and open a new one.


    . .profile

    Let me know if this helps.

  11. Wes,

    You shouldn’t have to run those commands everytime you start up your computer. Your tmp/ folder is being cleared out when you restart your computer. Are you running any maintenance programs on startup that could be clearing it out?

    You can also configure it so your socket is in a different location if you want.

  12. Thanks for putting this together, Mike.

    This all works for me up until this point:

    mysql5 -u root
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/opt/local/var/run/mysql5/mysqld.sock’ (2)

    Any suggestions?

    Thanks,

    Toby

  13. Guys,

    I am going to look into this tonight. I will hopefully have an answer for you tomorrow. Got a tight deadline today and won’t be able to look into it until tonight.

    My guess is the package has been updated since the last time I installed it. So I might just have to tweak my instructions a little. Its odd the instructions work up until the end though. If you see any strange output during the installation process let me know. I will post back here about my progress tomorrow. Will update this post as necessary.

    Mike

  14. Same “Error 2002″ when trying to login. I don’t think the launchctl line is working on this latest build.

  15. Yeah, I am going to install this tomorrow night and come up with a solution. Apologize for the delay been extremely busy this last month.

  16. Thank you very much for the instructions.. it worked .. I have installed navicat lite and using mysql from it…

  17. Greg,

    Does this error go away after you put in the new plist file. I will round up a plist file and put the example on this blog post.

    Mike

  18. Apologies for being vague. No it doesn’t.
    It does give me another problem to solve though. Trying to “sudo launchctl load -w” the new .plist file I get:
    launchctl: Couldn’t stat(“/Library/LaunchDaemons/org.macports.mysql5.plis”): No such file or directory
    nothing found to load

    I’m still trying…

    -Greg

  19. Apologies again… I misspelled the command. The error I’m getting with the new .plist file is:
    launchctl: Dubious ownership on file (skipping): /Library/LaunchDaemons/org.macports.mysql5.plist
    nothing found to load

    Getting this even though owner of the file is “wheel”.

  20. Greg,

    The plist file your using doesn’t seem to be correct… At least I don’t think it is after looking at it. Could you email it to me.

    Email located here:
    http://www.martinoflynn.com/about/team/digitalmediaservices/mikeruschak

    Your plist file should look something like what is on this page.

    http://www.macosxhints.com/article.php?story=20080128103022907

    Unfortunately my MacBook Pro I use for work doesn’t have a copy of this file. Fortunately my personal one does, but its just not with me at this time. I can get to this file tonight and email it to you and update this blog post if its successful. Something must have been changed on MacPorts end to not install this file.

  21. Mike,
    macports changed the packages.
    sudo port install mysql5 +server is now obsolete.
    The port mysql5-server (load mysql 5.0) is buggy because of a script directive
    that uses a mysql 5.1 var changed from 5.0 .

    I menaged to complete the trip using

    port mysql5-devel (that loads mysql 5.1).

    Hope this helps.

  22. Hi,

    To install mysql5 i used mysql_server_devel port as follow:

    $sudo port install mysql5-server-devel

    This is the output when installed:

    ###########################################################
    # A startup item has been generated that will aid in
    # starting mysql5-server-devel 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-devel @5.1.37_0
    ******************************************************
    * In order to setup the database, you might want to run
    * sudo -u mysql mysql_install_db5
    * if this is a new install
    ******************************************************

    Then to start the server I use:

    $ sudo mysqld_safe5

    And to stop the server:

    $ sudo mysqladmin5 shutdown

    Not sure if this is the best way for you… at least it worked for me.

    Thanks :-)

  23. Thanks for this post,
    I am having trouble running mysql in 10.6 (snow leopard) (it seems to be correctly installed, but… I don’t know.)
    I found this post might be useful but I only want to use mysql casually, so what do I have to do for manual start? (I think the daemons in Library are those right?)
    Can I skip some steps? or do I need to do a final setting?

    I really don’t want mysql running in the background all the time.

    Thank you in advance

  24. Ehm… Don’t you find mysql.sock in /tmp/mysql.sock in Leopard?

    This didn’t work for me:

    sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /var/mysql/mysql.sock

    Actually you have to create a softlink to /tmp/mysql.sock

    sudo ln -s /var/mysql/mysql.sock /opt/local/var/run/mysql5/mysqld.sock

    This did work ;)

  25. macports changed the packages.
    sudo port install mysql5 +server is now obsolete.

    $ sudo port install mysql5 mysql5-server

    If you run port installed you will see version of mysql5 & mysql5-server installed
    $ port installed
    mysql5 @5.1.40_0 (active)
    mysql5-server @5.1.40_0 (active)

    I was able to follow all the steps until the last step where you setup mysql5 password for user root. I can’t connect to local MySQL.

    $ sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
    $ mysql5 -u root
    Enter password:
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/opt/local/var/run/mysql5/mysqld.sock’ (62)

    I check many times that I have the symbolic links on both path /var/mysql & /opt/local/var/run/mysql5

    $ ls /var/mysql
    mysql.sock
    $ ls /opt/local/var/run/mysql5
    mysqld.sock

    Since macport update their package many tutorial blogs have been outdated. I have spend 3 days going into circles. Your blog post is the most updated and accurate even more complete than official mysql.com.

    Would you please give me more insight on how to resolve the last part? I have been spending few days just to install the mysql on local machine

  26. I find this is very useful.
    http://obscuredclarity.blogspot.com/2009/08/install-mysql-on-mac-os-x.html

    After I follow your instruction until the last step, I use the Step 4: Verify Install. Fire up the Terminal and type in: cd /usr/local , hit enter, type in: ls , and verify that there are two items. The Mac OS X .pkg of MySQL installs itself into /usr/local/mysql-VERSION and /usr/local/mysql.

    Step 5: Restart your computer. When the computer reboots, it will startup the database server allowing you to connect to it.

    Step 6: Start MySQL. Type in: /usr/local/mysql/bin/mysql -u root -p . The last part of the command tells it you want to start it up as the root user and that you’d like to be prompted for a password. By default the root’s password is blank, so when you are prompted for it, just hit enter. You are now running MySQL, and you can tell it what to do at the MySQL command prompt: mysql>.

    It seems like the miracle step is to restart the computer. After starting up the computer and open up a new terminal, I am able to start mysql using user root without password.

    /usr/local/mysql/bin/mysql -u root -p

    mysql5 -u root won’t work at all. It gives me the same ERROR 2002 (HY000) even after I restart computer or restart the terminal.

    $ mysql5 -u root
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/opt/local/var/run/mysql5/mysqld.sock’ (2)

  27. Hi,

    I have the same problem than Bess :

    $ mysql5 -u root
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/opt/local/var/run/mysql5/mysqld.sock’ (2)

    Does anybody else have an idea to fix this problem? I tried for hours and it drives me crazy now. Thanks a lot in advance I appreciate.

  28. Simple&Best :)
    Use pkg mac installer from here:
    Pick Installer
    and after Installation follow instructions contained in Readme…

    If you have to reinstall MySql… before remove old install
    following:
    Remove old
    Best Wishes from Dave ;)

  29. I would suggest you check out the articles on Snow Leopard. Dan has a great article on compiling MySQL for Snow Leopard. Compiling your own allows for more control. I recommend this route. Its also a good opportunity to learn how the underpinnings of UNIX work.

    http://hivelogic.com/articles/compiling-mysql-on-snow-leopard/

    I have been pretty busy as of late and haven’t had time to update this post. Rest assured I will update this post as soon as possible. This way everyone will have instructions for the MacPorts solution as well. The benefit of Mac Ports is obviously that it will keep the MySQL software up to date for you. Stay tuned I hope to have this article updated, or a follow up article posted the week of December 7th. At the very least I will follow up with a comment on this post regarding my progress. Thanks all for the comments as they will help me update this post.

  30. Mike, can’t thank you enough for this article. It absolutely nailed my problems. I’m just starting Rails development and was attempting to set up my Rails environment. The MySQL frustration had me at wits end. I did a fresh search on Google today and found this article. Awesome! The perfect way to end the year and start a new one. Thanks again!

  31. Any idea why PHP5 (also macports) doesn’t connect to MySQL5 via socket connection only via TCP/IP? I’ve been trying for hours to find a fix and no solution.

  32. Thanks for this one, it seemed to be the missing link after I had updated my ports (MySQL no longer autolaunched).

    I’ve repeated all the steps here, but after every restart I have to reconnect the socket:
    sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock

    Why is that? Can you give me a hint how to fix this?

    Thanks, Babs

  33. Before arrived on this site i was stuck with this error : ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/opt/local/var/run/mysql5/mysqld.sock’ (2) so I had followed all your steps and that work (I am on OSX Lion 10.7.3) !
    Thank you so mutch !

  34. Holy moly, it’s nearly a 4 year old post – but it helped me configure mysql5 for use with rails 3.2.7 on os 10.7.4!!!

    The only difference was that the mysql.sock actually existed in /tmp/mysql/, so instead of messing with it out of fear of braking it even more I just made links to the other two locations as you suggested.

    THANK YOU, YOU ROCK, YOU ARE AWESOME!

  35. ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/opt/local/var/run/mysql5/mysqld.sock’ (2) is a beast.
    This error is often thrown by mixed installation. This can happen, if you’ve installed a macports version and are running a XAMPP.

    First check if your database is running, if not start it.

    ps -wf |grep mysql

    You should get something like that.

    0 2621 1 0 2:00pm ?? 0:00.02 /bin/sh /Applications/XAMPP/xamppfiles/bin/mysqld_safe –datadir=/Applications/XAMPP/xamppfiles/var/mysql –pid-file=/Applications/XAMPP/xamppfiles/var/mysql/pirateair.fritz.box.pid

    In this case my db runs with xampp and the socket is written to /Applications/XAMPP/xamppfiles/var/mysql/

    Now, if i want to use mysql (or mysql5) or phyton, perl, php in terminal/console i’ve to change the link in /opt/local/var/run/mysql5/
    e.g.
    sudo ln -s /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock /opt/local/var/run/mysql5/mysqld.sock

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>