MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

How to restore deleted ‘root’ user and password for MySQL

If the MySQL root account has been dropped or its password lost, restart the server with grant checks turned off, recreate the account with CREATE USER and GRANT ALL, then restart it normally. The INSERT INTO mysql.user statement above only works on old MySQL 5.6-era servers. This page explains why it fails on MySQL 5.7, 8.x and MariaDB 10.4+, and gives a procedure that works on current versions.


Why the INSERT statement above fails on modern MySQL

The example above writes a row straight into the mysql.user grant table. That was a common workaround years ago. On any currently supported server it fails or leaves you with an account that cannot log in:

  • The password column no longer exists. MySQL 5.7 removed it and stores the hash in authentication_string. The statement fails with ERROR 1054 (42S22): Unknown column 'password' in 'field list'.
  • PASSWORD() was removed in MySQL 8.0. Calling it returns a syntax error.
  • authentication_string = 'y' overwrites the password hash with the literal letter y. That is not a valid hash, so nobody can authenticate as this account.
  • x509_issuer, x509_subject and ssl_cipher should be empty strings, not 'y'. These columns hold certificate requirements, not yes/no flags.
  • The privilege list is incomplete for MySQL 8.0. It has no Create_role_priv or Drop_role_priv, and no dynamic privileges such as SYSTEM_USER. Those live in mysql.global_grants, not in mysql.user.
  • In MariaDB 10.4 and later, mysql.user is a view over mysql.global_priv, so you cannot insert into it.
  • The post leaves out how to get in. If root has been deleted, you need some way to connect before you can run any SQL, and the server has to reload its grant tables afterwards.

Account-management statements (CREATE USER, ALTER USER, GRANT) avoid all of these problems. The server fills in the correct columns, authentication plugin and hash format for its own version.

Before you start: check for another admin login

Restarting with grant checks off means downtime. If another account with global CREATE USER and GRANT OPTION privileges still works, use that account and skip straight to the SQL in step 2.

On Debian and Ubuntu, MySQL packages sometimes create a maintenance account. Its credentials are stored in a root-only file:

sudo cat /etc/mysql/debian.cnf
sudo mysql --defaults-file=/etc/mysql/debian.cnfCode language: JavaScript (javascript)

On MariaDB, root@localhost normally uses unix_socket authentication, so sudo mysql may still work even if you have forgotten the password. Test both options before you restart anything.

Step 1: start MySQL with grant tables disabled

Put the recovery options in a temporary config file. This works the same way for every systemd unit name and packaging style.

  1. Stop the service: sudo systemctl stop mysql on Debian/Ubuntu, or sudo systemctl stop mysqld on RHEL, Rocky and Alma (mariadb for MariaDB).
  2. Create the temporary file shown below. On Debian/Ubuntu use /etc/mysql/mysql.conf.d/zz-recovery.cnf (or /etc/mysql/mariadb.conf.d/ for MariaDB). On RHEL-family systems use /etc/my.cnf.d/zz-recovery.cnf.
  3. Start the service again with sudo systemctl start mysql (or mysqld / mariadb).
[mysqld]
skip-grant-tables
skip-networkingCode language: CSS (css)

Note: While skip-grant-tables is active, anyone who can reach the server has full access without a password. skip-networking restricts connections to the local socket. MySQL 8.0 turns it on automatically with skip-grant-tables, but set it explicitly anyway. Keep this window as short as you can.

Step 2: recreate the root account

Connect over the local socket with no password, then reload the grant tables. Without FLUSH PRIVILEGES, the account statements are rejected with ERROR 1290 ... running with the --skip-grant-tables option.

sudo mysql
FLUSH PRIVILEGES;

-- See what is left
SELECT user, host, plugin FROM mysql.user;

-- Case A: root was deleted
CREATE USER 'root'@'localhost' IDENTIFIED BY 'Use-A-Strong-Passw0rd!';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION;

-- Case B: root exists but the password is lost
-- ALTER USER 'root'@'localhost' IDENTIFIED BY 'Use-A-Strong-Passw0rd!';

FLUSH PRIVILEGES;
EXIT;Code language: PHP (php)

GRANT ALL PRIVILEGES ON *.* grants every static and dynamic global privilege the server supports, so you do not need to list privilege columns yourself. WITH GRANT OPTION lets root grant privileges to other accounts. GRANT PROXY restores the proxy privilege that a default root account has.

On MariaDB 10.4 and later, you can restore the default behaviour, where both socket login and a password work:

CREATE USER 'root'@'localhost'
  IDENTIFIED VIA unix_socket OR mysql_native_password USING PASSWORD('Use-A-Strong-Passw0rd!');
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;Code language: JavaScript (javascript)

Step 3: restart normally and verify

sudo rm /etc/mysql/mysql.conf.d/zz-recovery.cnf   # or /etc/my.cnf.d/zz-recovery.cnf
sudo systemctl restart mysql                       # or mysqld / mariadb

mysql -u root -p -e "SELECT CURRENT_USER(); SHOW GRANTS;"Code language: PHP (php)

Expected output starts like this:

+----------------+
| CURRENT_USER() |
+----------------+
| root@localhost |
+----------------+
GRANT ALL PRIVILEGES ON *.* TO `root`@`localhost` WITH GRANT OPTION ...Code language: JavaScript (javascript)

Depending on the version, MySQL 8.0 may list the static and dynamic privileges as separate GRANT lines instead of ALL PRIVILEGES. Either form is correct.

Common errors and fixes

ErrorCauseFix
ERROR 1290 (HY000): ... --skip-grant-tables option so it cannot execute this statementGrant tables were not reloadedRun FLUSH PRIVILEGES; first, then repeat the statement
ERROR 1396 (HY000): Operation CREATE USER failed for 'root'@'localhost'The account, or leftover rows from it, still existUse ALTER USER, or run DROP USER 'root'@'localhost'; and then CREATE USER
ERROR 1054: Unknown column 'password'Running the INSERT from the example above on MySQL 5.7+Use CREATE USER / GRANT instead
ERROR 1045: Access denied after restartWrong password, or you connected over TCP as root@127.0.0.1, which is a different account from root@localhostConnect through the socket (mysql -u root -p) or create root@127.0.0.1 as well
Server will not startTypo in the recovery file, or the file is in a directory that is not includedCheck journalctl -u mysql and confirm your config includes that directory

Best practices

  • Keep a second admin account with a different name so a single mistake cannot lock you out.
  • Never drop or edit the system accounts mysql.sys, mysql.session and mysql.infoschema on MySQL 8.
  • Do not modify grant tables with INSERT, UPDATE or DELETE. Account-management statements are replicated and validated in a way that direct table edits are not.
  • On a replica or cluster node, run recovery on each server that lost the account, or recreate it on the source so replication carries it to the others.
  • Store the new root password in a secrets manager and limit root to localhost.

Frequently asked questions

How do I recover MySQL if I deleted the root user?

Start mysqld with skip-grant-tables and skip-networking and connect locally without a password. Then run FLUSH PRIVILEGES, CREATE USER 'root'@'localhost' IDENTIFIED BY '...' and GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION, and finally restart the server normally.

Why does PASSWORD() not work in MySQL 8?

MySQL 8.0 removed the PASSWORD() function. Set passwords with CREATE USER ... IDENTIFIED BY or ALTER USER ... IDENTIFIED BY and the server hashes them with the account's authentication plugin.

Can I reset the root password without restarting MySQL?

Only if another account with CREATE USER and GRANT OPTION privileges still works, such as debian-sys-maint on some Debian/Ubuntu installs. If none does, you need a restart with skip-grant-tables or an --init-file.

Is it safe to run MySQL with skip-grant-tables?

Only for a few minutes and only with networking disabled, because every connection gets full privileges without authentication. Remove the option and restart as soon as the account is restored.

Do I need to recreate root@127.0.0.1 and root@::1 too?

Only if your applications or tools connect over TCP as root. root@localhost covers socket connections, and on most current installs it is the only root account.

Related tutorials

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

How to Configure Artifactrory with MySQL Database?

To run JFrog Artifactory 7 on MySQL instead of the default embedded Derby database, you create a dedicated database and user, put the MySQL Connector/J JAR in…

Read More

Python Tutorials: Database operations using python – mysql

This tutorial connects Python to MySQL with the official mysql-connector-python driver and runs the full set of CRUD operations: create a database and table, then insert, read,…

Read More

Rundeck Database Migration From H2 to MySql and postgresql

This guide moves Rundeck off its embedded H2 database and onto MySQL or PostgreSQL, which is what you should use in production. The steps above set up…

Read More

MySql Backup and Restore – A Complete Guide

MySQL logical backups are made with mysqldump, which writes a database out as a plain SQL file, and restored with the mysql client, which runs that file…

Read More

How to Reset or Recover Forgotten mySql root Password

To reset a forgotten MySQL or MariaDB root password, stop the server, restart it with grant checks turned off (skip-grant-tables), set a new password with ALTER USER,…

Read More

Import all MySQL databases at one time in seperate file for Backup

This page shows two ways to back up MySQL: with mysqldump –all-databases you get one large file, and with a shell loop you get one dump file…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x