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
passwordcolumn no longer exists. MySQL 5.7 removed it and stores the hash inauthentication_string. The statement fails withERROR 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 lettery. That is not a valid hash, so nobody can authenticate as this account.x509_issuer,x509_subjectandssl_ciphershould 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_privorDrop_role_priv, and no dynamic privileges such asSYSTEM_USER. Those live inmysql.global_grants, not inmysql.user. - In MariaDB 10.4 and later,
mysql.useris a view overmysql.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.
- Stop the service:
sudo systemctl stop mysqlon Debian/Ubuntu, orsudo systemctl stop mysqldon RHEL, Rocky and Alma (mariadbfor MariaDB). - 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. - Start the service again with
sudo systemctl start mysql(ormysqld/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
| Error | Cause | Fix |
|---|---|---|
ERROR 1290 (HY000): ... --skip-grant-tables option so it cannot execute this statement | Grant tables were not reloaded | Run 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 exist | Use 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 restart | Wrong password, or you connected over TCP as root@127.0.0.1, which is a different account from root@localhost | Connect through the socket (mysql -u root -p) or create root@127.0.0.1 as well |
| Server will not start | Typo in the recovery file, or the file is in a directory that is not included | Check 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.sessionandmysql.infoschemaon MySQL 8. - Do not modify grant tables with
INSERT,UPDATEorDELETE. 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
roottolocalhost.
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
- More SQL & MySQL tutorials
- Top 50 Interview question and answers of MySQL
- Datadog Tutorials: Datadog Agent integration with mysql databases?
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.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals