---
title: "MySQL修改root密码的各种方法整理"
author: "conkeyn"
date: "2009-10-22T23:55:48Z"
tags: ["Google", "JavaScript", "MySQL"]
slug: "mysql-root"
draft: false
lastmod: "2009-10-22T23:55:48Z"
id: 501238
url: "https://conkeyn.iteye.com/blog/501238"
---

整理了以下四种在MySQL中修改root密码的方法:

方法1： 用SET PASSWORD命令

　　mysql -u root

　　mysql&gt; SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');

方法2：用mysqladmin

&lt;!-- IWMS\_AD\_BEGIN--&gt;

&lt;script src="http://www.linuxidc.com/system/system60.js" type="text/javascript"&gt;&lt;/script&gt;&lt;script type="text/javascript"&gt;&lt;!-- google\_ad\_client = &quot;pub-5195587195407606&quot;; /* Linuxidc 468x60, 创建于 08-8-31 \*/ google\_ad\_slot = &quot;9963827011&quot;; google\_ad\_width = 468; google\_ad\_height = 60; //--&gt; &lt;/script&gt;&lt;script src="http://pagead2.googlesyndication.com/pagead/show\_ads.js" type="text/javascript"&gt; &lt;/script&gt;

&lt;!-- IWMS\_AD\_END--&gt;

　　mysqladmin -u root password "newpass"

　　如果root已经设置过密码，采用如下方法

　　mysqladmin -u root password oldpass "newpass"

方法3： 用UPDATE直接编辑user表

　　mysql -u root

　　mysql&gt; use mysql;

　　mysql&gt; UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';

　　mysql&gt; FLUSH PRIVILEGES;

在丢失root密码的时候，可以这样

　　mysqld\_safe --skip-grant-tables&

　　mysql -u root mysql

　　mysql&gt; UPDATE user SET password=PASSWORD("new password") WHERE user='root';

　　mysql&gt; FLUSH PRIVILEGES;