22

I'm trying to reinstall mysql on my MAC OS X Yosemite. for this I followed the instruction as mentioned below

sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/MySQL*
vim /etc/hostconfig and removed the line MYSQLCOM=-YES-
rm -rf ~/Library/PreferencePanes/MySQL*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm -rf /var/db/receipts/com.mysql.*

I also tried

brew uninstall mysql

after this I installed mysql using homebrew using command brew install mysql. After installation when I tried to run mysql -u root It throws the following error

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

I didn't set password for mysql.I don't know what's going wrong.Any suggestion will be appreciated. Thank you

Bibek Sharma
  • 3,020
  • 5
  • 36
  • 60

7 Answers7

117

I installed mysql with homebrew and got the same problem as you because mysql has had an existing database with an existing password there. See this article for more details.

$ brew services stop mysql
$ sudo pkill mysqld
$ sudo rm -rf /usr/local/var/mysql/ # NOTE: this will delete your existing database!!!
$ brew postinstall mysql
$ brew services restart mysql
$ mysql -uroot
nass-bhtn
  • 13
  • 3
Ghrua
  • 5,126
  • 4
  • 16
  • 25
  • 4
    after spending more than an hour scouring the many answers to this question, yes this is the only solution that worked for me – stackPusher Apr 22 '18 at 20:35
  • THANK YOU. THIS ACTUALLY WORKED. So many answers on SO didn't work – David Corbin Jul 05 '19 at 21:00
  • 3
    Does anyone have any update on this? I tried this and still having the same error `ERROR 1045 (28000): Access denied for user 'user'@localhost` – Adam Nov 23 '19 at 21:52
  • 1
    found this the best solution. Had to run `sudo pkill mysqld` – Victor Jan 23 '20 at 16:04
  • the best solution ive seen – hugo411 Mar 17 '20 at 15:35
  • 1
    Perfect solution! After a lot of struggle, ended up here and saved! Thanks. After that if you want to reset the password, execute `ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';` credits: https://stackoverflow.com/questions/7864276/cannot-connect-to-database-server-mysql-workbench – Arjun Kalidas May 19 '20 at 06:29
  • you are lifesaver – nathan Jun 14 '20 at 19:35
  • 1
    I come from the future where a mysterious pandemic has taken over the world , And THIS is the only solution that works as of NOV-01-2020. Thanks! – ultrasounder Nov 01 '20 at 22:37
  • 1
    this gives a new error. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) – Md Sifatul Islam May 18 '21 at 08:36
7

Now sql generates an aliatory password that appears in the last screen.

sudo mysql -u root -h 127.0.0.1 -p
Enter password: (aliatory password)

we can change it

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-password';
Inês Gomes
  • 3,293
  • 1
  • 19
  • 30
3

I have resolved this issue for myself.

Please check my github with this link: https://github.com/LeVanTuan/access_sql

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

Fix bug access denied on macos 10.12 (Sierra)

install mysql (https://dev.mysql.com/downloads/mysql/) if already installed, skip this step

Update password of 'root' to access.

Open Terminal (Launchpad -> Other -> Terminal or (Command + space) -> Type 'terminal' )

Then type follow below:

export PATH=$PATH:/usr/local/mysql/bin/

sudo mysqld_safe --skip-grant-tables

then, restart Terminal (quit then open again)

then, keep type:

export PATH=$PATH:/usr/local/mysql/bin/

mysql -u root mysql

FLUSH PRIVILEGES;

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

\q

sudo /usr/local/mysql/support-files/mysql.server start

extra: start mysql: sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables stop mysql: sudo /usr/local/mysql/support-files/mysql.server stop

I hope it will help you

Chuck Le Butt
  • 45,923
  • 58
  • 187
  • 280
Leo LE
  • 41
  • 5
0

I tried all the commands mentioned in the stackoverflow, but nothing worked. Finally, I deleted every mysql folder in mac and reinstalled mysql. Finally the command

mysql -u root

worked for me. Then atlast, I set password for the mysql root by editing the configuration file.

R Nanthak
  • 335
  • 2
  • 15
0

**on your mac terminal type mysql -u root -p

0

You forgot to reset your temporary password. Re-install mysql, use the temporary password to connect to mysql and run:

mysql> SET PASSWORD = PASSWORD('your_new_password');
calinf
  • 1
0

Uninstalling mysql completely and installing with an installer solved the problem for me.

Solution:

  • Remove mysql complete from your computer

  • Download and Install mysql without brew. Specify your desire password here or based on the version the installer might mention you a password

  • set the path for for mysql

    export PATH=$PATH:/usr/local/mysql/bin

  • Check whether its installed properly mysql --version

  • mysql -uroot p to login

  • change the password if required mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('root')

Md Sifatul Islam
  • 776
  • 8
  • 28