{"id":32715,"date":"2026-11-29T22:05:37","date_gmt":"2026-11-29T22:05:37","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=32715"},"modified":"2026-11-29T22:05:37","modified_gmt":"2026-11-29T22:05:37","slug":"how-to-restore-deleted-root-user-and-password-for-mysql","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-restore-deleted-root-user-and-password-for-mysql\/","title":{"rendered":"How to restore deleted &#8216;root&#8217; user and password for MySQL"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">If the MySQL <code>root<\/code> account has been dropped or its password lost, restart the server with grant checks turned off, recreate the account with <code>CREATE USER<\/code> and <code>GRANT ALL<\/code>, then restart it normally. The <code>INSERT INTO mysql.user<\/code> 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.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/ce5b4d2181c4087e9c5c673acc763472.js\"><\/script>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Why the INSERT statement above fails on modern MySQL<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The example above writes a row straight into the <code>mysql.user<\/code> 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:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>The <code>password<\/code> column no longer exists.<\/strong> MySQL 5.7 removed it and stores the hash in <code>authentication_string<\/code>. The statement fails with <code>ERROR 1054 (42S22): Unknown column &#039;password&#039; in &#039;field list&#039;<\/code>.<\/li>\n\n\n<li><strong><code>PASSWORD()<\/code> was removed in MySQL 8.0.<\/strong> Calling it returns a syntax error.<\/li>\n\n\n<li><strong><code>authentication_string = &#039;y&#039;<\/code> overwrites the password hash<\/strong> with the literal letter <code>y<\/code>. That is not a valid hash, so nobody can authenticate as this account.<\/li>\n\n\n<li><strong><code>x509_issuer<\/code>, <code>x509_subject<\/code> and <code>ssl_cipher<\/code> should be empty strings<\/strong>, not <code>&#039;y&#039;<\/code>. These columns hold certificate requirements, not yes\/no flags.<\/li>\n\n\n<li><strong>The privilege list is incomplete for MySQL 8.0.<\/strong> It has no <code>Create_role_priv<\/code> or <code>Drop_role_priv<\/code>, and no dynamic privileges such as <code>SYSTEM_USER<\/code>. Those live in <code>mysql.global_grants<\/code>, not in <code>mysql.user<\/code>.<\/li>\n\n\n<li><strong>In MariaDB 10.4 and later, <code>mysql.user<\/code> is a view<\/strong> over <code>mysql.global_priv<\/code>, so you cannot insert into it.<\/li>\n\n\n<li><strong>The post leaves out how to get in.<\/strong> 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.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Account-management statements (<code>CREATE USER<\/code>, <code>ALTER USER<\/code>, <code>GRANT<\/code>) avoid all of these problems. The server fills in the correct columns, authentication plugin and hash format for its own version.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Before you start: check for another admin login<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Restarting with grant checks off means downtime. If another account with global <code>CREATE USER<\/code> and <code>GRANT OPTION<\/code> privileges still works, use that account and skip straight to the SQL in step 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On Debian and Ubuntu, MySQL packages sometimes create a maintenance account. Its credentials are stored in a root-only file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">sudo cat \/etc\/mysql\/debian.cnf\nsudo mysql --defaults-file=<span class=\"hljs-regexp\">\/etc\/my<\/span>sql\/debian.cnf<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">On MariaDB, <code>root@localhost<\/code> normally uses <code>unix_socket<\/code> authentication, so <code>sudo mysql<\/code> may still work even if you have forgotten the password. Test both options before you restart anything.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: start MySQL with grant tables disabled<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Put the recovery options in a temporary config file. This works the same way for every systemd unit name and packaging style.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Stop the service: <code>sudo systemctl stop mysql<\/code> on Debian\/Ubuntu, or <code>sudo systemctl stop mysqld<\/code> on RHEL, Rocky and Alma (<code>mariadb<\/code> for MariaDB).<\/li>\n\n\n<li>Create the temporary file shown below. On Debian\/Ubuntu use <code>\/etc\/mysql\/mysql.conf.d\/zz-recovery.cnf<\/code> (or <code>\/etc\/mysql\/mariadb.conf.d\/<\/code> for MariaDB). On RHEL-family systems use <code>\/etc\/my.cnf.d\/zz-recovery.cnf<\/code>.<\/li>\n\n\n<li>Start the service again with <code>sudo systemctl start mysql<\/code> (or <code>mysqld<\/code> \/ <code>mariadb<\/code>).<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-attr\">&#91;mysqld]<\/span>\n<span class=\"hljs-selector-tag\">skip-grant-tables<\/span>\n<span class=\"hljs-selector-tag\">skip-networking<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> While <code>skip-grant-tables<\/code> is active, anyone who can reach the server has full access without a password. <code>skip-networking<\/code> restricts connections to the local socket. MySQL 8.0 turns it on automatically with <code>skip-grant-tables<\/code>, but set it explicitly anyway. Keep this window as short as you can.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: recreate the root account<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Connect over the local socket with no password, then reload the grant tables. Without <code>FLUSH PRIVILEGES<\/code>, the account statements are rejected with <code>ERROR 1290 ... running with the --skip-grant-tables option<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">sudo mysql<\/code><\/span><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">FLUSH PRIVILEGES;\n\n-- See what is left\nSELECT user, host, plugin FROM mysql.user;\n\n-- <span class=\"hljs-keyword\">Case<\/span> A: root was deleted\nCREATE USER <span class=\"hljs-string\">'root'<\/span>@<span class=\"hljs-string\">'localhost'<\/span> IDENTIFIED BY <span class=\"hljs-string\">'Use-A-Strong-Passw0rd!'<\/span>;\nGRANT ALL PRIVILEGES ON *.* TO <span class=\"hljs-string\">'root'<\/span>@<span class=\"hljs-string\">'localhost'<\/span> WITH GRANT OPTION;\nGRANT PROXY ON <span class=\"hljs-string\">''<\/span>@<span class=\"hljs-string\">''<\/span> TO <span class=\"hljs-string\">'root'<\/span>@<span class=\"hljs-string\">'localhost'<\/span> WITH GRANT OPTION;\n\n-- <span class=\"hljs-keyword\">Case<\/span> B: root exists but the password is lost\n-- ALTER USER <span class=\"hljs-string\">'root'<\/span>@<span class=\"hljs-string\">'localhost'<\/span> IDENTIFIED BY <span class=\"hljs-string\">'Use-A-Strong-Passw0rd!'<\/span>;\n\nFLUSH PRIVILEGES;\n<span class=\"hljs-keyword\">EXIT<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><code>GRANT ALL PRIVILEGES ON *.*<\/code> grants every static and dynamic global privilege the server supports, so you do not need to list privilege columns yourself. <code>WITH GRANT OPTION<\/code> lets root grant privileges to other accounts. <code>GRANT PROXY<\/code> restores the proxy privilege that a default root account has.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On MariaDB 10.4 and later, you can restore the default behaviour, where both socket login and a password work:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">CREATE USER <span class=\"hljs-string\">'root'<\/span>@<span class=\"hljs-string\">'localhost'<\/span>\n  IDENTIFIED VIA unix_socket OR mysql_native_password USING PASSWORD(<span class=\"hljs-string\">'Use-A-Strong-Passw0rd!'<\/span>);\nGRANT ALL PRIVILEGES ON *.* TO <span class=\"hljs-string\">'root'<\/span>@<span class=\"hljs-string\">'localhost'<\/span> WITH GRANT OPTION;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Step 3: restart normally and verify<\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">sudo rm \/etc\/mysql\/mysql.conf.d\/zz-recovery.cnf   <span class=\"hljs-comment\"># or \/etc\/my.cnf.d\/zz-recovery.cnf<\/span>\nsudo systemctl restart mysql                       <span class=\"hljs-comment\"># or mysqld \/ mariadb<\/span>\n\nmysql -u root -p -e <span class=\"hljs-string\">\"SELECT CURRENT_USER(); SHOW GRANTS;\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Expected output starts like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+----------------+\n| CURRENT_USER() |\n+----------------+\n| root@localhost |\n+----------------+\nGRANT ALL PRIVILEGES ON *.* TO <span class=\"hljs-string\">`root`<\/span>@<span class=\"hljs-string\">`localhost`<\/span> WITH GRANT OPTION ...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Depending on the version, MySQL 8.0 may list the static and dynamic privileges as separate <code>GRANT<\/code> lines instead of <code>ALL PRIVILEGES<\/code>. Either form is correct.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td><code>ERROR 1290 (HY000): ... --skip-grant-tables option so it cannot execute this statement<\/code><\/td><td>Grant tables were not reloaded<\/td><td>Run <code>FLUSH PRIVILEGES;<\/code> first, then repeat the statement<\/td><\/tr><tr><td><code>ERROR 1396 (HY000): Operation CREATE USER failed for &#039;root&#039;@&#039;localhost&#039;<\/code><\/td><td>The account, or leftover rows from it, still exist<\/td><td>Use <code>ALTER USER<\/code>, or run <code>DROP USER &#039;root&#039;@&#039;localhost&#039;;<\/code> and then <code>CREATE USER<\/code><\/td><\/tr><tr><td><code>ERROR 1054: Unknown column &#039;password&#039;<\/code><\/td><td>Running the INSERT from the example above on MySQL 5.7+<\/td><td>Use <code>CREATE USER<\/code> \/ <code>GRANT<\/code> instead<\/td><\/tr><tr><td><code>ERROR 1045: Access denied<\/code> after restart<\/td><td>Wrong password, or you connected over TCP as <code>root@127.0.0.1<\/code>, which is a different account from <code>root@localhost<\/code><\/td><td>Connect through the socket (<code>mysql -u root -p<\/code>) or create <code>root@127.0.0.1<\/code> as well<\/td><\/tr><tr><td>Server will not start<\/td><td>Typo in the recovery file, or the file is in a directory that is not included<\/td><td>Check <code>journalctl -u mysql<\/code> and confirm your config includes that directory<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Best practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Keep a second admin account with a different name so a single mistake cannot lock you out.<\/li>\n\n\n<li>Never drop or edit the system accounts <code>mysql.sys<\/code>, <code>mysql.session<\/code> and <code>mysql.infoschema<\/code> on MySQL 8.<\/li>\n\n\n<li>Do not modify grant tables with <code>INSERT<\/code>, <code>UPDATE<\/code> or <code>DELETE<\/code>. Account-management statements are replicated and validated in a way that direct table edits are not.<\/li>\n\n\n<li>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.<\/li>\n\n\n<li>Store the new root password in a secrets manager and limit <code>root<\/code> to <code>localhost<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">How do I recover MySQL if I deleted the root user?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Start mysqld with <code>skip-grant-tables<\/code> and <code>skip-networking<\/code> and connect locally without a password. Then run <code>FLUSH PRIVILEGES<\/code>, <code>CREATE USER &#039;root&#039;@&#039;localhost&#039; IDENTIFIED BY &#039;...&#039;<\/code> and <code>GRANT ALL PRIVILEGES ON *.* TO &#039;root&#039;@&#039;localhost&#039; WITH GRANT OPTION<\/code>, and finally restart the server normally.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does PASSWORD() not work in MySQL 8?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">MySQL 8.0 removed the <code>PASSWORD()<\/code> function. Set passwords with <code>CREATE USER ... IDENTIFIED BY<\/code> or <code>ALTER USER ... IDENTIFIED BY<\/code> and the server hashes them with the account&#039;s authentication plugin.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I reset the root password without restarting MySQL?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Only if another account with <code>CREATE USER<\/code> and <code>GRANT OPTION<\/code> privileges still works, such as <code>debian-sys-maint<\/code> on some Debian\/Ubuntu installs. If none does, you need a restart with <code>skip-grant-tables<\/code> or an <code>--init-file<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is it safe to run MySQL with skip-grant-tables?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Do I need to recreate root@127.0.0.1 and root@::1 too?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Only if your applications or tools connect over TCP as root. <code>root@localhost<\/code> covers socket connections, and on most current installs it is the only root account.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/database\/sql\/\">More SQL &amp; MySQL tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/top-50-interview-question-and-answers-of-mysql\/\">Top 50 Interview question and answers of MySQL<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/how-to-setup-datadog-agent-integration-with-mysql-to-collect-many-metrics-from-mysql-databases\/\">Datadog Tutorials: Datadog Agent integration with mysql databases?<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[1],"tags":[],"class_list":["post-32715","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=32715"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32715\/revisions"}],"predecessor-version":[{"id":80418,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32715\/revisions\/80418"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=32715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=32715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=32715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}