brew install mysql on macOS
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:
- installed mysql with
brew install mysql
-
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
Start mysql with
mysql.server start
command, to be able to log on it-
Used the alternate security script:
/usr/local/Cellar/mysql/5.5.10/bin/mysql_secure_installation
-
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 lineMYSQLCOM=-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.
nikola
Updated on October 07, 2020Comments
-
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 over 13 yearsthe 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 over 13 yearsThis 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 over 13 yearsDid you have a previous install of MySQL on that machine?
-
cointilt about 13 yearsThis worked for me! I started guessing the password before I tried this too and the password ended up being 'password'. So try that.
-
Rupert over 12 yearsLaunching 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 over 12 yearsThank you for your detailed instruction! Installing mysql in mac all night long had almost killed me.
-
tronbabylove about 12 yearsGreat 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 almost 12 yearsgo 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 almost 12 yearsYa, 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. over 11 yearsIf 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 over 11 yearsBecause 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 over 10 yearsI have` [~/Library/LaunchAgents]$ ls com.adobe.ARM.202f4087f2bbde52e3ac2df389f53a4f123223c9cc56a8fd83a6f7ae.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 over 10 yearsI had to add
--explicit_defaults_for_timestamp
But then I still get48617 [ERROR] /usr/local/opt/mysql/bin/mysqld: unknown option '--skip-bdb'
-
reneruiz about 10 yearsI was using the stop service/script, and it kept restarting. Was that because I never unloaded the LaunchAgent? (doh)
-
fbtb about 10 years@Ava ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
-
TKH about 10 yearsI also got bitten by doing all of the above without removing /tmp/mysql.sock first.
-
rfreytag almost 10 yearsmysql_install_db can now be omitted, brew creates the server automatically in the right directoy.
-
Madbreaks over 9 yearsThanks, +1. Couple comments: you say to
sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
, then a few steps later you attempt tolaunchctl unload
that file...but it won't be there, because you deleted it. Also,brew install mysql
ran themysql_install_db
step for me, no need to do it twice. -
YPCrumble about 9 years@CorySimmons does this mean I should use my own name, or
root
in thewhoami
part of your commands? -
YPCrumble about 9 yearsConfirmed - whoami is my name (I used
root
). This worked great. -
iPad Guy almost 9 yearsThis works for mariadb, too (with appropriate changes). Thanks!
-
Rarylson Freitas almost 9 yearsTry running
sudo chown -R mysql /usr/local/var/mysql/
before runningmysql.server start
if you're runningmysql.server
as root (when starting using the root user, the MySQL server downgrades to the_mysql
user). -
KcC0 over 8 yearsHey, did you happen to find a solutions for this problem?
-
ecbrodie over 8 yearsNote that
/etc/hostconfig
does not exist on Yosemite and beyond (superuser.com/questions/850974/…) -
ruhanbidart over 8 yearsWhat 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 over 8 yearsYes, 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 over 8 yearsI 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 over 8 yearsI 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 over 8 yearsif 'brew cleanup' doesn't do the trick, try 'brew cleanup -s'
-
Matt Rowles over 8 yearsThis 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 wasmysql_install_db --verbose --user=whoami--basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
due to unrecognised options. -
Jacob Raccuia almost 8 yearsstep 2 was definitely required for me
-
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 over 7 yearsBrew now supports a
services
command, so these can be replaced withbrew services stop mysql
andbrew services start mysql
respectively. -
Matheus Abreu over 7 yearsI had an issue with
mysql -uroot
returning a ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) Solved bymysql -u root -p
-
Ameer Sheikh over 7 yearsupon using mysql_install_db:
[WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
. -
pilat about 7 yearsIt 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 about 7 yearsI have a question: is there any chance to hook up my old databases to this new install?? :/
-
corysimmons about 7 yearsMake a dump of your db then import it into the fresh install.
-
Rohman HM almost 7 yearsYou saved my time. Thanks a lot dude
-
oshaiken over 6 years@forloop 1. ps -ef | grep mysql 2. kill -9 pid 3. mysql.server start
-
Danyal Aytekin almost 6 yearsomg, the amount of questions and 1,000 line answers to this problem... think i'll just
brew install redis
tbh -
Haril Satra almost 6 yearsIt 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 over 5 yearsfor 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 over 5 yearsmysql_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 over 5 yearsBoom! Yesterday I simply ran
brew install mysql
but was having trouble getting it to run. All I had to do wasmysql.server start
. Thanks!! -
Nic Scozzaro almost 5 yearsWhat do I do about getting the error "mysql_install_db: command not found"?
-
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 almost 4 yearswhen copy all database from "mysql.bak" it's not working :(