brew install mysql on macOS

461,879

Solution 1

I think one can end up in this position with older versions of mysql already installed. I had the same problem and none of the above solutions worked for me. I fixed it thus:

Used brew's remove & cleanup commands, unloaded the launchctl script, then deleted the mysql directory in /usr/local/var, deleted my existing /etc/my.cnf (leave that one up to you, should it apply) and launchctl plist

Updated the string for the plist. Note also your alternate security script directory will be based on which version of MySQL you are installing.

Step-by-step:

brew remove mysql

brew cleanup

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

sudo rm -rf /usr/local/var/mysql

I then started from scratch:

  1. installed mysql with brew install mysql
  2. ran the commands brew suggested: (see note: below)

    unset TMPDIR
    
    mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
    
  3. Start mysql with mysql.server start command, to be able to log on it

  4. Used the alternate security script:

    /usr/local/Cellar/mysql/5.5.10/bin/mysql_secure_installation
    
  5. Followed the launchctl section from the brew package script output such as,

    #start
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
    
    #stop
    launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
    

Note: the --force bit on brew cleanup will also cleanup outdated kegs, think it's a new-ish homebrew feature.

Note the second: a commenter says step 2 is not required. I don't want to test it, so YMMV!

Solution 2

Here are detailed instructions combining getting rid of all MySQL from your Mac then installing it The Brew Way as Sedorner wrote above:

Remove MySQL completely per The Tech Lab

  • ps -ax | grep mysql
  • stop and kill any MySQL processes
  • sudo rm /usr/local/mysql
  • sudo rm -rf /usr/local/var/mysql
  • sudo rm -rf /usr/local/mysql*
  • sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
  • sudo rm -rf /Library/StartupItems/MySQLCOM
  • sudo rm -rf /Library/PreferencePanes/My*
  • launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
  • edit /etc/hostconfig and remove the line MYSQLCOM=-YES-
  • rm -rf ~/Library/PreferencePanes/My*
  • sudo rm -rf /Library/Receipts/mysql*
  • sudo rm -rf /Library/Receipts/MySQL*
  • sudo rm -rf /private/var/db/receipts/*mysql*
  • sudo rm -rf /tmp/mysql*
  • try to run mysql, it shouldn't work

Brew install MySQL per user Sedorner from this StackOverflow answer

  • brew doctor and fix any errors

  • brew remove mysql

  • brew cleanup

  • brew update

  • brew install mysql

  • unset TMPDIR

      mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp # whoami is executed inline
    
  • mysql.server start

  • run the commands Brew suggests, add MySQL to launchctl so it automatically launches at startup

mysql should now work and be running all the time as expected

Godspeed.

Solution 3

Had the same problem. Seems like there is something wrong with the set up instructions or the initial tables that are being created. This is how I got mysqld running on my machine.

If the mysqld server is already running on your Mac, stop it first with:

launchctl unload -w ~/Library/LaunchAgents/com.mysql.mysqld.plist

Start the mysqld server with the following command which lets anyone log in with full permissions.

mysqld_safe --skip-grant-tables

Then run mysql -u root which should now let you log in successfully without a password. The following command should reset all the root passwords.

UPDATE mysql.user SET Password=PASSWORD('NewPassword') WHERE User='root'; FLUSH PRIVILEGES;

Now if you kill the running copy of mysqld_safe and start it up again without the skip-grant-tables option, you should be able to log in with mysql -u root -p and the new password you just set.

Solution 4

If brew installed MySQL 5.7, the process is a bit different than for previous versions. In order to reset the root password, proceed as follows:

sudo rm -rf /usr/local/var/mysql
mysqld --initialize

A temporary password will be printed to the console and it can only be used for updating the root password:

mysql.server start
echo "ALTER USER 'root'@'localhost' IDENTIFIED BY 'my-new-password';" | mysql -uroot --password=TEMPORARY_PASSWORD

Solution 5

I had the same problem just now. If you brew info mysql and follow the steps it looks like the root password should be new-password if I remember correctly. I was seeing the same thing you are seeing. This article helped me the most.

It turned out I didn't have any accounts created for me. When I logged in after running mysqld_safe and did select * from user; no rows were returned. I opened the MySQLWorkbench with the mysqld_safe running and added a root account with all the privs I expected. This are working well for me now.

Share:
461,879
nikola
Author by

nikola

Updated on October 07, 2020

Comments

  • nikola
    nikola over 3 years

    I'm trying to setup up MySQL on mac os 10.6 using Homebrew by brew install mysql 5.1.52.

    Everything goes well and I am also successful with the mysql_install_db.
    However when I try to connect to the server using:

    /usr/local/Cellar/mysql/5.1.52/bin/mysqladmin -u root password 'mypass'
    

    I get:

    /usr/local/Cellar/mysql/5.1.52/bin/mysqladmin: connect to server at 'localhost' 
    failed error: 'Access denied for user 'root'@'localhost' (using password: NO)'

    I've tried to access mysqladmin or mysql using -u root -proot as well,
    but it doesn't work with or without password.

    This is a brand new installation on a brand new machine and as far as I know the new installation must be accessible without a root password. I also tried:

    /usr/local/Cellar/mysql/5.1.52/bin/mysql_secure_installation
    

    but I also get

    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
  • nikola
    nikola over 13 years
    the problem is that i can't access the mysql server at all after the installation by brew. I can start it, but it won't let me in as root with or without password. (therefore i can't create other users or manage permissions.. or have I not understood you right?
  • Nick Vanderbilt
    Nick Vanderbilt over 13 years
    This solution is not working for me because mysql -u root is not taking me to mysql prompt. It says Access denied for user 'root'@'localhost' (using password: NO)
  • Darren Newton
    Darren Newton over 13 years
    Did you have a previous install of MySQL on that machine?
  • cointilt
    cointilt about 13 years
    This worked for me! I started guessing the password before I tried this too and the password ended up being 'password'. So try that.
  • Rupert
    Rupert over 12 years
    Launching mysqld_safe without the grant tables worked for me. However, the UPDATE did not work since I do not have any records on select * from mysql.user; Since we are already logged in as root, we can simply insert one for root by running GRANT ALL PRIVILEGES ON . TO 'root'@'localhost' IDENTIFIED BY 'whatever'; FLUSH PRIVILEGES;
  • CMinus
    CMinus over 12 years
    Thank you for your detailed instruction! Installing mysql in mac all night long had almost killed me.
  • tronbabylove
    tronbabylove about 12 years
    Great answer, thanks! One thing to add: Though it is mentioned in the answer, I missed it and in so doing lost about 6 hours trying to get set up on Lion OSX. Make absolutely sure that you delete the old mysql directory at /usr/local/var/mysql if you have a previous version of MySql before you reinstall. Otherwise you will not be able to log in as root initially to set the password and you will spend a lot of time yelling at your computer.
  • Marco
    Marco almost 12 years
    go into ~/Library/LaunchAgents to see what the .plist file for mysql is actually called - in my case it was installed by homebrew so you need to modify the above uninstall procedure.
  • Rob Barreca
    Rob Barreca almost 12 years
    Ya, the unload and rm ~/Library/LaunchAgents/ steps really matter so make sure you delete the proper .plist file. I tried 7 times until I did that right.
  • Ben M.
    Ben M. over 11 years
    If you're transitioning from MAMP, like I was, and using the MySQL Ruby Gem, I needed to also remove the MySQL gem before I could get the login to work. I ran rvm implode, then started the brew install and I was able to run the mysql_secure_installation
  • Tommy
    Tommy over 11 years
    Because this thread is old, the "launchctl unload" line above is now wrong. The file homebrew installs is not longer called "com.mysql.mysqld.plist", it is called "homebrew.mxcl.mysql.plist". The line should now read "launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
  • Ava
    Ava over 10 years
    I have` [~/Library/LaunchAgents]$ ls com.adobe.ARM.202f4087f2bbde52e3ac2df389f53a4f123223c9cc56a8‌​fd83a6f7ae.plist com.mysql.mysqld.plist com.facebook.videochat.ava.plist homebrew.mxcl.postgresql.plist` under ` ~/Library/LaunchAgents` but unloading them gives launchctl: Couldn't stat("com.mysql.mysqld.plist"): No such file or directory
  • the
    the over 10 years
    I had to add --explicit_defaults_for_timestamp But then I still get 48617 [ERROR] /usr/local/opt/mysql/bin/mysqld: unknown option '--skip-bdb'
  • reneruiz
    reneruiz about 10 years
    I was using the stop service/script, and it kept restarting. Was that because I never unloaded the LaunchAgent? (doh)
  • fbtb
    fbtb about 10 years
    @Ava ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
  • TKH
    TKH about 10 years
    I also got bitten by doing all of the above without removing /tmp/mysql.sock first.
  • rfreytag
    rfreytag almost 10 years
    mysql_install_db can now be omitted, brew creates the server automatically in the right directoy.
  • Madbreaks
    Madbreaks over 9 years
    Thanks, +1. Couple comments: you say to sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist, then a few steps later you attempt to launchctl unload that file...but it won't be there, because you deleted it. Also, brew install mysql ran the mysql_install_db step for me, no need to do it twice.
  • YPCrumble
    YPCrumble about 9 years
    @CorySimmons does this mean I should use my own name, or root in the whoami part of your commands?
  • YPCrumble
    YPCrumble about 9 years
    Confirmed - whoami is my name (I used root). This worked great.
  • iPad Guy
    iPad Guy almost 9 years
    This works for mariadb, too (with appropriate changes). Thanks!
  • Rarylson Freitas
    Rarylson Freitas almost 9 years
    Try running sudo chown -R mysql /usr/local/var/mysql/ before running mysql.server start if you're running mysql.server as root (when starting using the root user, the MySQL server downgrades to the _mysql user).
  • KcC0
    KcC0 over 8 years
    Hey, did you happen to find a solutions for this problem?
  • ecbrodie
    ecbrodie over 8 years
    Note that /etc/hostconfig does not exist on Yosemite and beyond (superuser.com/questions/850974/…)
  • ruhanbidart
    ruhanbidart over 8 years
    What works for me was something similar to what you make. In the part that you updated the password of the user, I had to make: use mysql; update user set authentication_string=password('1111') where user='root'; As in: stackoverflow.com/a/31122246/1328261
  • mrucci
    mrucci over 8 years
    Yes, this will work, but if you wanted to install MySQL 5.7 (as currently done by brew install mysql), you could follow the directions in my answer stackoverflow.com/a/33924648/133106
  • markphd
    markphd over 8 years
    I can see the temp password generated but when I try to run mysql, I get: ERROR! The server quit without updating PID file (/usr/local/var/mysql/XXXXX.local.pid). and when I do mysql -u root, the password is not accepted.
  • markphd
    markphd over 8 years
    I successfully installed mysql 5.7.9 using the dmg installer which prompted me a window that it generated a temp password for me. And now mysql is working correctly. Too bad I had to spend huge amount of time just to get it working on a brand new Macbook. :|
  • Nick Sarafa
    Nick Sarafa over 8 years
    if 'brew cleanup' doesn't do the trick, try 'brew cleanup -s'
  • Matt Rowles
    Matt Rowles over 8 years
    This worked for me also, I was having 'Plugin is not installed' issues when I tried mysql -u root. The only step that seemed to not work for me was mysql_install_db --verbose --user=whoami--basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp due to unrecognised options.
  • Jacob Raccuia
    Jacob Raccuia almost 8 years
    step 2 was definitely required for me
  • Luke Murray
    Luke Murray over 7 years
    mysqld -initialize --verbose --user=whoami --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp use this for El Capitan then it should work
  • sina
    sina over 7 years
    Brew now supports a services command, so these can be replaced with brew services stop mysql and brew services start mysql respectively.
  • Matheus Abreu
    Matheus Abreu over 7 years
    I had an issue with mysql -uroot returning a ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) Solved by mysql -u root -p
  • Ameer Sheikh
    Ameer Sheikh over 7 years
    upon using mysql_install_db: [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize.
  • pilat
    pilat about 7 years
    It looks that brew install mysql runs 'mysqld --inititalize` automatically now. So, the only thing you would need to do is: brew services start mysql.
  • pilat
    pilat about 7 years
    I have a question: is there any chance to hook up my old databases to this new install?? :/
  • corysimmons
    corysimmons about 7 years
    Make a dump of your db then import it into the fresh install.
  • Rohman HM
    Rohman HM almost 7 years
    You saved my time. Thanks a lot dude
  • oshaiken
    oshaiken over 6 years
    @forloop 1. ps -ef | grep mysql 2. kill -9 pid 3. mysql.server start
  • Danyal Aytekin
    Danyal Aytekin almost 6 years
    omg, the amount of questions and 1,000 line answers to this problem... think i'll just brew install redis tbh
  • Haril Satra
    Haril Satra almost 6 years
    It gave me the error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) Doing mysql.server start solved the problem for me.
  • Test Test
    Test Test over 5 years
    for me, above reseting/altering of password did not work. I had to log into mysql using temporary password then run alter command 1. mysql -u root -p <temp-password> 2. ALTER USER 'root'@'localhost' IDENTIFIED BY 'New-Password';
  • slashdottir
    slashdottir over 5 years
    mysql_install_db --verbose --user=whoami --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp mysql_install_db: [ERROR] unknown variable 'tmpdir=/tmp' 2018-12-03 15:59:37 [ERROR] Unrecognized options
  • Steven Thate
    Steven Thate over 5 years
    Boom! Yesterday I simply ran brew install mysql but was having trouble getting it to run. All I had to do was mysql.server start. Thanks!!
  • Nic Scozzaro
    Nic Scozzaro almost 5 years
    What do I do about getting the error "mysql_install_db: command not found"?
  • Tim Strawbridge
    Tim Strawbridge over 4 years
    @ Nic Scozzaro: I had to link mysql manually with [brew link --force [email protected]] in order for bash to see any mysql commands.
  • Jahirul Islam Mamun
    Jahirul Islam Mamun almost 4 years
    when copy all database from "mysql.bak" it's not working :(