10

I recently followed some tutorials and installed mysql using sudo apt-get install mysql-server-5.7

I can connect to the database using password by running the following command:

sudo mysql -u root -p

I try to connect by running:

mysql -u root -p

but I get the error:

ERROR 1698 (28000): Access denied for user 'root'@'localhost'

Why? How can I fix this?

4
  • 2
    dear downvoter , can you also drop a reason for downvote , atleast an explation why this is an stupid question ? Jan 5, 2018 at 23:48
  • This might be related. Look at some of the answers: askubuntu.com/questions/766334/…
    – Terrance
    Jan 6, 2018 at 0:57
  • Did you set a root password for mysql during the installation? Jan 6, 2018 at 0:57
  • @steeldriver : yes I did Jan 6, 2018 at 19:12

2 Answers 2

28
  1. Access with sudo: sudo mysql -u root -p
  2. Delete the root user: drop user 'root'@'localhost';
  3. Create the root user again: create user 'root'@'%' identified by 'your_password';
  4. Give permissions: grant all privileges on *.* to 'root'@'%' with grant option;
  5. Update permission tables: flush privileges;
  6. Exit MYSQL and try to reconnect without sudo.

Font: can't login as mysql user root from normal user account in ubuntu 16.04

3
  • 1
    Thanks! These steps worked for me too and I don't need sudo now to use mysql! Looks like replacing the host localhost with % resolved the issue for me. Feb 27, 2019 at 22:43
  • but still cannot access remotely Nov 23, 2019 at 13:30
  • Didn't work for me, now I can't get in with or without sudo! Dec 23, 2019 at 2:49
0
  1. Access with sudo : sudo mysql -u root -p
  2. Remove the super user : drop user root@localhost;
  3. Create a user the same name : create user 'root'@'localhost' identified by 'password';
  4. Grant all privileges to the user : grant all privileges on *.* to 'root'@'localhost';
  5. Refresh permission table : flush privileges;
  6. Exit the table : exit Now try to access it directly without sudo command. works on Ubuntu 20.10 (Groovy Gorilla)
2
  • The OP is on 17.10 - so it's uncertain if your answer works. Dec 28, 2020 at 11:18
  • Just not 100% sure for 17.10. Dec 29, 2020 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.