Monday, August 17, 2009

Install MySQL on Mac OS X


There are already several instructions out there on how to install MySQL on Mac OS X, but most of them are either outdated or more complicated than they need to be. Here you will find an easy tutorial showing how to install MySQL, start it up, access it as the default root user, create a database, delete the database and exit.

Step 1: Download MySQL. Go to the downloads page at mysql.com and download the particular version of MySQL that is appropriate for your OS and machine architecture. In my case, I downloaded the "Mac OS X 10.5 (x86_64)" version because I have Mac OS X 10.5 running on a MacBook with a 64-bit Intel processor.



Step 2: Access the installers. Go the the directory where the .dmg file was downloaded to and mount the .dmg file by double clicking it. The .dmg contains the MySQL and MySQL Startup installer packages.

Step 3: Install MySQL and the MySQL Startup Item. Double-clicking on mysql*.pkg icon starts an installer wizard that takes you through a few intuitive installation steps. Once you click through the wizard and accept a license agreement you should see the "install succeeded" page. Repeat for MySQLStartupItem.pkg.

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>.

Step 7: Create a database, delete it and exit MySQL. At the mysql> prompt, type in: create database mynewdatabase; . Verify that it was created by typing in: show databases; . You should see the database along with the default databases. Delete the database by typing in: drop database mynewdatabase; . Verify that it was deleted by typing in: show databases; . Exit MySQL by typing in: exit; .


Piece of Cake!!

In addition, here are the commands you need to start and stop MySQL:
sudo /Library/StartupItems/MySQLCOM/MySQLCOM stop
sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
sudo /Library/StartupItems/MySQLCOM/MySQLCOM restart


See also: Uninstall MySQL on Mac OS X
And also: Installing and Running Tomcat on Mac OS X


If you enjoyed this post, make sure you subscribe to the Obscured Clarity RSS feed to receive new posts in a reader or via email!

15 comments:

scoobynz said...

Great article, really simple and actually works unlike most of the other tutorials out there. Nice work.

Tim Molter said...

@scoobynz I'm glad you found the instructions useful. Thanks for stopping by!

wadada said...

Hi there, i followed your instructions but i'm getting an error when i do:
/usr/local/mysql/bin/mysql -u root -p
-bash: /usr/local/mysql/bin/mysql: Bad CPU type in executable

I'm running 10.6 Intel Core Duo.
Have you had the same experience?

Tim Molter said...

@wadada, I'm guessing you downloaded and installed the wrong version of MySQL. I see that MySQL.com doesn't have a version for Mac OS X 10.6 yet, but I would guess the version: "Mac OS X 10.5 (x86_64)" would work for your Mac. If that is what you installed, I don't have any other ideas for you. No, I didn't experience that.

Samantha & Ilango said...

Thanks very much for your instructions. It worked for me very well.

gtinla said...

Hi Tim,

awesome instructions - I love simplicity. I installed the 5.1.4_x86_64 version on a MBP running OSX 10.6.1 and it runs flawless.

Fura said...

Good grief! You cannot imagine how much time I spent trying to get MYSQL downloaded and installed properly. Then with article, I did everything correctly after only about 5 minutes.

Do you have any simple instructions dealing with MYSQL passwords?

Tim Molter said...

@samamtha @gtinla @fura, glad the instructions helped! Thanks for leaving a comment. I haven't posted any additional MySQL instructions, but I can point you to a reference that I always use, including how to deal with MySQL passwords: http://www.pantz.org/software/mysql/mysqlcommands.html

Max Maxsen said...

Hi Tim, great tutorial! I wish there would be more such easy instructions on the mysql issue. Anyway, now I'am trying to get phpmyadmin running on my mac pro, without success! I searched all over the net but could'nt find a solution. Is there an easy instruction somewhere for setting up phpmyadmin?

Tim Molter said...

Max, you should check out XAMPP, that's what I use. Well at least the Apache server part of it. I would like to install a plain Apache server and phpmyadmin in the near future, but at this point I haven't looked into how to do it. Good luck!!

Andrew Boyce said...

You're a life saver! I accidentally deleted my root user (don't ask), and in the process I lost all of my privileges. I ran through your uninstall, then your install, and everything has been restored. I'm just glad that I didn't really get any databases set up to loose since it was a new install. Infinite thanks to you good sir!

Laurent said...

Hi there, I still have the same problem

[@renzo /usr/local/mysql/bin ] mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/local/var/run/mysql5/mysqld.sock' (2)

I tried for hours to fix it by using different ways but I can't find an issue

Does anybody else have an idea what's wrong here?

Thanks a lot in advance.

Tim Molter said...

Laurent, I have a few ideas what you can try. 1) Type exactly /usr/local/mysql/bin/mysql -u root -p to start mysql, don't cd to the bin directory first. 2) Uninstall all installations of Mysql and start over completely following the instructions exactly. 3)try connecting to mysql via tcp/ip rather than the socket. You have to change some stuff in certain config files to do that. Google around and see if you can find how to do that. I don't remember off hand, sorry.

Tim Molter said...

Laurent, regarding point 3) that I made above, you might want to check out this: https://www.alternc.org/browser/install/scripts/etc/phpmyadmin/config.inc.php?rev=587

In order to change MySQL, so that it connects to the MySQL DB via TCP rather than a socket. Find the line "$cfg['Servers'][$i]['connect_type'] = 'socket'; // How to connect to MySQL server ('tcp' or 'socket')" in config.inc.php and change 'socket' to 'tcp'.

Maybe that will help!??!

Laurent said...

Hi Tim, thanks a lot for your answers, I really appreciate. Ok I'll check this out and try to fix it.
I'll let you know if it works. Thanks again.