Question

I can't create a database with mysql

ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES) Using Debian: I have uninstalled and reinstalled mysql using apt-get remove --purge mysql*, then reinstalled. Running mysql_secure_installation does not work because it will not accept my password. I hoped clearing all things mysql related would reset the password used for this. How can I have a clean start with mysql? How can I prevent this from happening in the future?

Thank you


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.

Bobby Iliev
Site Moderator
Site Moderator badge
December 29, 2023
Accepted Answer

Hello,

The error you’re encountering, “ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)”, is a common MySQL authentication issue. It seems like there’s a problem with the root password or the way MySQL is interpreting your login credentials. Here’s a step-by-step guide to resolve this and ensure a clean start with MySQL on Debian:

Please be aware that removing the MySQL directory, specifically /var/lib/mysql, will permanently delete all existing MySQL databases and data stored on your server.

  1. Uninstall MySQL Completely:

    • You’ve already done a basic uninstall, but let’s make sure everything is completely removed.
      sudo apt-get remove --purge mysql-server mysql-client mysql-common
      sudo apt-get autoremove
      sudo apt-get autoclean
      sudo rm -rf /etc/mysql /var/lib/mysql
      sudo apt update
      
    • This will remove all MySQL packages and configurations.
  2. Reinstall MySQL:

    • Install MySQL again with:
      sudo apt install mysql-server
      
    • During the installation, you should be prompted to set a new root password.
  3. Secure MySQL Installation:

    • Run the mysql_secure_installation script:
      sudo mysql_secure_installation
      
    • This script will guide you through several security settings, including setting the root password, removing anonymous users, disallowing root login remotely, and more.
  4. Troubleshooting the Root Password:

    • If you still can’t log in, you might need to reset the root password manually. Stop the MySQL service:
      sudo systemctl stop mysql
      
    • Start MySQL with skip-grant-tables option:
      sudo mysqld_safe --skip-grant-tables &
      
    • Log in to MySQL without a password:
      mysql -u root
      
    • Once logged in, reset the root password:
      FLUSH PRIVILEGES;
      ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
      EXIT;
      
    • Replace new_password with your new root password. Then restart MySQL:
      sudo systemctl restart mysql
      

Besides that, check for any error logs in /var/log/mysql/ for more specific error messages and ensure that there are no other conflicting services or incorrect configurations.

Best,

Bobby

apt-get install mysql

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel