Fixing not being able to log in to fresh MySQL installation on Ubuntu

If you did a fresh installation on a new Ubuntu (~18.04) version, you may find that it is not possible to log in to MySQL with its root user, unless you do so as Ubuntu’s root user. Here’s how to fix it:

  1. Log in to MySQL using Ubuntu’s root user.: sudo mysql.
  2. Delete current root@localhost account.: DROP USER 'root'@'localhost';.
  3. Recreate user.: CREATE USER 'root'@'%' IDENTIFIED BY 'root';.
  4. Grant full permissions.: GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;.
  5. Flush privileges.: FLUSH PRIVILEGES;.
  6. Exit MySQL.: exit;.
  7. Log in to MySQL without using Ubuntu’s root user. When prompted, enter the password you assigned above.: mysql -u root -p.

Reference: https://askubuntu.com/a/784347/100470.

Leave a Reply