Monday, August 17, 2009

Install MySQL on Mac OS X

Using MySQL and Java? Check out an easier way: Yank

Update: I just verified that the 10.6 version works on Mountain Lion 10.8.

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. Under the section "MySQL Community Server" click on the "Download" link. Find 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-bit)" version because I have Mac OS X 10.5 running on a MacBook with a 64-bit Intel processor. You want to download the DMG Archive, not the compressed TAR Archive.

Note: Use the 10.6 version for Snow Leopard, Lion, and Mountain Lion.





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 as well as the MySQL.prefPane.



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 and MySQL.prefPane.



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. You can also probably start MySQL without rebooting by starting it through the MySQL preference Pane under System Preferences although I haven't tried that. If you prefer the command line, the start and stop commands are listed at the bottom of this article.

Step 6: Use 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 using MySQL, and you can tell it what to do at the MySQL command prompt:
mysql>



Note about error 2002 people are reporting!


If after installing MySQL and trying to use MySQL as root user as in step 6, and you see this following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

, it's probably because MySQL is installed but not yet running. Make sure you've started MySQL as outlined in Step 5. To verify that it's running, open up Activity Monitor and under "All Processes", verify you see the process "mydqld".


If you installed the preference pane, you should also see there that MySQL is running.


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
And also: Set MySQL Server System Variables on Mac OS X
And also: My Mac OS X MySQL Cheat Sheet

46 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!

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

Ilango said...

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

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

Unknown 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!

Anonymous 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!??!

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

Lolman said...

Hi, I have the same problem of Laurent:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

And I've tried to solve this changing socket to tcp but didnt work as well... I use OS 10.6 (Snow leopard)...
Do u know how to solve this?

ohh and thks... your topic was very helpfull

Lolman said...

Hi I`ve solved my problem... The error was just because the MySQL wasnt started... but now i cant access de DB:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

This is the error i got... I want to know how to change my MySQl user and PW....

Tim Molter said...

Lolman,

Sorry I don't really know what to tell you, having never run into that problem myself. :(

Lolman said...

thks for your answer... but I solved this one too... Do you use phpmyadmin?

I cant connect my MySQL to the Phpmyadmin... but thanks for your article.... this was a life saver....

Chris said...

Hi!

Thanks for the post. It's really useful!

Besides, is there any short way to activate mySql instead of typing /usr/local/mysql/bin/mysql

Tim Molter said...

Hi Chris,

No, that's the fastest way, but you can use the up arrow on your keyboard within the terminal to cycle through previous commands that you typed in.

Anonymous said...

I am no expert here, but as far as I can see there is a security issue with your approach. When installing MySQL as you described, the database will end up running as the Administrator user and having its privileges - which is not desired.

You should use the chown and chgroup commands to reassign the files to another user account (e.g. create a "mysqluser" account with standard privileges only). You can find other manuals on the web on this and it seems to be recommended.

But thanks nonetheless for your article (and the other articles too).

Tim Molter said...

Hi Chris,

I don't see a security issue in the way that I install MySQL. All I show here is how to install it and verify that it's working logging in with the default root user. Of course, configuring MySQL to be secure is a wise thing to do if you're going to be using it in the public domain. But that's outside the scope of this particular article.

Steve Swinsburg said...

Just a note that this doesn't work on 10.6 if running on an older macbook pro, eg from 2007. See http://bugs.mysql.com/bug.php?id=54718

I have no idea why because these instructions are correct for newer models. I have been battling with this for ages :(

Tim Molter said...

Steve, Thanks for the heads up!

tbear said...

I hope you're still watching this blog. My problem is mysql used to work on my Mac SnowLeopard, until I got back from vacation. Now I only have the mysql-5.1.47-osx10.6-x86 version, not the "mysql" alias, so I can't get the server to start. I don't want to lose all the databases I've set up (with several tables each) using Workbench. I'd uninstall it but for that. Is there a way I can make an alias of mysql-5.1.47-osx10.6-x86? I've tried but no luck.
Thanks,
Teddi

Tim Molter said...

Teddi, sorry, I don't have any advice for your problem.

musicman432 said...

Hey, is there a place I can download a package for a G4 PPC Mac?

Thanks,
musicman432

Tim Molter said...

musicman432, sorry, I don't know.

Anonymous said...

This worked like a charm. I have been trying to follow the Mysql documentation and for some reason it never worked for me.

Thanks a ton.

Anonymous said...

Hi Tim,
thank you for the tutorial.
I`m trying again to install mysql-5.5.8-osx10.6-x86_64 on 10.6.6 but when I restart a message appears: "startupitems/MySQLCOM doesn`t have the proper security settings to run" (maybe bed translation from italian).
Do you have any idea about that problem ?

Thank you

mat (from Tokyo)

Tim Molter said...

mat, I don't know exactly what's up with that. I think I just clicked on "restore" or "fix" or something to that order, and then restarted and everything worked fine.

Radu kabalah said...

Amazing.. just it! everything here seems easy

Unknown said...

Hi Tim

I installed and uninstalled MySQL following the above instruction million times but it never works for me, not i gave up and really need to install that on my mac, ned your advice and help.

when i am trying to run mysql from terminal i keep getting error:
Command i am firing:
/usr/local/mysql/bin/mysql -u root -p
error:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR! Manager of pid-file quit without updating file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


I downloaded the mysql from mysql.com, my mac version is Mac OS X 10.6, do you think is that the problem ?

Tim Molter said...

@Ankit Sorry, I never ran into that problem so I can't give you any advice. Good luck!

Anonymous said...

Thank you so much!

saf said...

OMG. Thank you so much!
And it really IS a piece o' cake!

After following your steps, I had a problem (in Step 6):
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

Google then helped me:
https://discussions.apple.com/thread/1053234?start=0&tstart=0

Just citing to help anyone with this problem, as I think this is simpler than what you had posted.
And once again, thanks a lot Tim!

Harris said...

Thanks a lot! Very helpful post :)

H_A_ said...

Worked Fine! Very good tutorial, thanks man!

maks said...

Great help man !! Thanks a lot!!!

Unknown said...

Helpful even almost 3 years later. The one thing I think you are missing is the installation of MySQLStartupItem.pkg. This caused the following error to occur when I tried to connect as the root user : Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2). MySQLStartupItem.pkg can be found in the MySQL package installer, which is provided on a disk image (.dmg). Open the package/image with the appropriate tool ( e.g. right click on the image to use the DiskImageMounter default utility on Mac or Alcohol120 on Windows) and then double click on MySQLStartupItem.pkg. After a successful installation of MySQLStartupItem.pkg, run sudo /Library/StartupItems/MySQLCOM/MySQLCOM start on Mac. You should be able to connect as a root user after that.

Rohit said...

Thank you for the very simple instructions! I making my first ever server app, am on a tight deadline, and your post really helped. Thanks again!

Anonymous said...

I am getting error a step 6-
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

can you pl suggestsolution for it.

thanks,
katie

Tim Molter said...

@ everyone getting and error 2002, See my update to this article under the section titled: "Note about error 2002 people are reporting!". The problem is most likely that MySQL is not running.

Alessandro Marlos said...

Great, friend. thank you very much. It works properly...

Dean said...

Awesome post, thank you!