Question

Setup MySQL on Ubuntu Droplet getting error ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Hi guys, I am very new to using a service like digitalocean (opposed to a one click setup of mediatemple or godaddy) and I am trying to setup and configure a new DB using MySQL but I keep getting the error

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

I have googled this, and searched the community for answers but cannot seem to solve this issue. Am I missing something? I have tried running it as root, and running it with sudo through another user and cannot get around it. If I offer no password it gives the same error but instead of using password: YES, it’s NO.

Thanks.

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Which one-click-setup did you use? You can check the configuration files probably and find the password. If you can’t find it, neither root without pass is working you can reset it:

Commands

sudo /etc/init.d/mysql stop

Now start up MySQL in safe mode, so you’ll skip the privileges table:

sudo mysqld_safe --skip-grant-tables &

Login with root:

mysql -uroot

And assign the DB that needs to be used:

use mysql;

Now all you have to do is reset your root password of the MySQL user and restart the MySQL service:

update user set password=PASSWORD("YOURPASSWORDHERE") where User='root';
flush privileges;

quit and restart MySQL:

quit
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start

Now your root password should be working with the one you just set, check it with:

mysql -u root -p

Hope this will help you figure it out.

On MySql 5.7 version you must replace query to:

 update user set authentication_string=PASSWORD("YOURPASSWORDHERE") where user="root"; 

everything else it’s like CrypticDesigns described…

I am also new in DigitalOcean and was following this article to setup a LEMP server: https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-20-04

I was having trouble on the last step. I was unable to connect to the database using the php script. It was showing the following error:

“Error!: SQLSTATE[HY000] [1045] Access denied for user”

I was able to log-in from the terminal using the same credential and was able to see the records on the table, but the php script was showing the connection error for the same credential, which was weird!

After searching all the posts on stackoverflow, found no solution for this. Finally don’t know why, I ran the php file from the terminal like this instead of the browser:

‘php /var/www/yourdomain/todolist.php’

which was showing me an error on line 3, which was the line with the password. It was telling me something like an empty value and indicating the last 2 characters of the password. I then noticed that in my password, before these last two characters, I have a “$” sign, which is the reserved symbol in php to indicate a variable!

So I changed my mysql user password with the following command:

“ALTER USER 'userName’@'localhost’ IDENTIFIED BY 'New-Password-Here’;”

And updated the php script accordingly. Voila! All worked perfectly afterwards!

Lesson learned: Do not have “$” sign on your password.

Hope it will save others an hour of tension on my same situation!