Trusted answers to developer questions

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

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

When you install MySQL and try to access it from your local machine as root, you may encounter this error.

Run the command

mysql -u root -p

When prompted for a password, you press enter. The process suggests that a password for root has not yet been set. However, you should set the password because running as root without a password has many security concerns.

svg viewer

If the error persists, follow these commands:

  1. Stop the mysql daemon by typing:
mysql stop
  1. Start mysql without any privileges using:
mysqld_safe --skip-grant-tables &
  1. The terminal will stop responding. Open a new terminal and type:
mysql -u root
  1. Enter the permission setting of the root user by typing:
mysql> use mysql;

mysql> select * from user;

mysql> flush privileges;

mysql> grant all privileges on *.* to root@localhost identified by 'Password' with grant option;

Enter your password in place of Password.

  1. Exit the shell:
mysql> quit
  1. Restart mysql by typing:
kill -KILL [PID of mysqld_safe]
kill -KILL [PID of mysqld]
service mysql start
  1. Retry logging in by typing:
mysql -u root -p password

RELATED TAGS

mysql
error

CONTRIBUTOR

Fahad Farid
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?