Ubuntu – thesql -u root doesnt work but sudo thesql -u root does, why

17.10MySQL

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?

Best Answer

  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