25

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,210
  • 6
  • 42
  • 64

10 Answers10

128

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
  • 23
  • 3
Ghrua
  • 6,746
  • 5
  • 17
  • 25
  • 6
    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` – chiri Jan 23 '20 at 16:04
  • 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
  • 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
8

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
  • 4,313
  • 1
  • 24
  • 32
4

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
  • 47,570
  • 62
  • 203
  • 289
Leo LE
  • 51
  • 5
3

In my case with a Mac M1 and Mysql 8.0.29-arm64 this answer worked for me:

https://www.codegrepper.com/code-examples/sql/how+to+reset+root+password+in+mysql+m1+mac;

Run the server in safe mode with privilege bypass:

> sudo mysqld_safe --skip-grant-tables

In a new Terminal window

> mysql -u root
UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;
exit;

Then

> mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
Amr Aly
  • 3,871
  • 2
  • 16
  • 33
  • When go to the new Terminal window, using the `mysql -u root`; it raises an error. The same password error – htlbydgod Jul 28 '22 at 04:15
  • 1
    Thank you, the --skip-grant-tables fixed it for me. I'm using `mysql.server start --skip-grant-tables` in this case – fileyfood500 Oct 28 '22 at 18:34
1

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
  • 846
  • 10
  • 28
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
  • 344
  • 3
  • 17
  • I have tried to install mariadb on a Mac M1 but on trying to access with "mariadb -u root -p" always get the error: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) No solution presented here works and there is no explanation why this happens in the mac yet easily installs on other OSes I would really love to get Mariadb or Mysql running on my Mac and not have to look for Windows to test my apps ;( – githeko Jun 24 '23 at 10:00
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

Update for anyone still not finding a resolution with a new M1 Mac. This completely clears your database FYI!!

First run brew services stop mysql

I found it easier to just open finder and use the got to folder and open /opt/homebrew, under the var folder I deleted the MySQL folder and under locks I deleted the MySQL lock files.

brew install MySQL and followed the install again.

Celtic89
  • 13
  • 3
0

If none of the above methods work, try the following method, which is applicable for macOS 13, MySQL 8, and installation via Homebrew.

1: unistall mysql: brew uninstall mysql

2 run cmd : ps -ax | grep mysql | grep -v grep

It will return a result:

3964 ?? 0:00.03 /bin/sh /opt/homebrew/opt/mysql/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql

4065 ?? 0:02.26 /opt/homebrew/opt/mysql/bin/mysqld --basedir=/opt/homebrew/opt/mysql --datadir=/opt/homebrew/var/mysql --

Remember the path /opt/homebrew/opt/mysql/

3 : Kill all PIDs that you receive , Example: sudo kill 3964

4 : Delete the mysql directory in the path obtained above /opt/homebrew/opt/.

5 : Finally, reinstall MySQL.