Ubuntu – Cannot log into MySQL

16.04moodle;MySQLWordpress

I have a very common problem with my Ubuntu 16.04 LTS/MySQL 5.7.18/WordPress running on a Digital Ocean droplet with 2 CPU, 40GB SSD and 16GB RAM. The droplet is only running one site and has hardly any traffic because it is still under development.

  1. I cannot connect via 'mysql -u root -p'.
  2. I cannot use the 'sudo mysqlsecureinstallation' script.

Every time after entering the password I get the dreaded "Access denied for user 'root'@'localhost' (using password: YES)" error message.

This appears to be a very common problem. I wasted a whole day following all the usual recommendations, such as reinstalling MySQL, with and without prior uninstall, crossgrading to MariaDB and back to MySQL, all to none avail. What am I doing wrong?

Btw, the WordPress site is working fine. The problem only became apparent when I added Moodle to the installation and got stuck at the point where you need to create the Moodle tables. Since I cannot log into the MySQL server, I'm now stuck.

Best Answer

Make sure you'v started mysql.

mysql -h 127.0.0.1 -u root

if you'v not set mysql password you should not include -p, notice you have to specify the host