MySQL修改密碼和忘記ROOT密碼

來源:互聯網
上載者:User

MySQL修改密碼和忘記ROOT密碼

前言:本文檔介紹MYSQL修改使用者密碼的方法和忘記root密碼後的處理方法。

一、三種修改密碼的方式

1、修改目前使用者的登入密碼

指令碼:SET PASSWORD=PASSWORD('netpassword');

例: 

mysql> set password=password('root');

Query OK, 0 rows affected (0.00 sec) 

2、使用SET修改其他使用者的密碼,通過ROOT修改其他帳號的密碼

指令碼:SET PASSWORD FOR

'user' @'host'=PASSWORD('newpassword') 

例: 

mysql> select host,user from mysql.user;

+----------------+--------+

| host | user |

+----------------+--------+

| | test |

| % | john1 |

| % | mytest |

| 127.0.0.1 | root |

| 192.168.189.71 | john |

| 192.168.189.71 | john1 |

| ::1 | john |

| ::1 | root |

| localhost | john |

| localhost | root |

| mysql | root |

+----------------+--------+

11 rows in set (0.00 sec) 

mysql> SET PASSWORD FOR

-> 'john1' @'%'=PASSWORD('john');

Query OK, 0 rows affected (0.00 sec) 

3、使用UPDATE語句修改其他使用者的密碼,需要有修改MYSQL許可權的使用者,一般用ROOT使用者

指令碼:

UPDATE mysql.user SET

PASSWORD=PASSWORD('newpassword')

WHERE

User='username'

AND

Host='host';

例: 

mysql> UPDATE mysql.user SET

-> PASSWORD=PASSWORD('root')

-> where

-> User='root' AND Host='127.0.0.1';

Query OK, 0 rows affected (0.02 sec)

Rows matched: 0 Changed: 0 Warnings: 0

二、忘記ROOT密碼的處理方法

1、關閉資料庫

指令碼:[root@mysql etc]# service mysql stop 

2、使用指令碼: mysqld_safe --skip-grant-tables 啟動資料庫 

使用/usr/bin/mysqld_safe --skip-grant-tables&啟動資料庫 

3、使用空密碼進入資料庫(mysql命令後直接斷行符號) 

[root@mysql ~]# mysql

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.6.20-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql> 

4、使用UPDATE語句修改ROOT密碼 

mysql> UPDATE mysql.user SET

-> PASSWORD=PASSWORD('root')

-> where

-> User='root' AND Host='localhost'; 

5、關閉資料庫並重新以正常方式啟動資料庫 

[root@mysql ~]# service mysql restart;

Shutting down MySQL.. SUCCESS!

Starting MySQL.. SUCCESS!

[root@mysql ~]# mysql

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@mysql ~]# mysql -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.6.20-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

顯示可以成功登入,整個過程很簡單。大功告成!!!

--------------------------------------分割線 --------------------------------------

Ubuntu 14.04下安裝MySQL

《MySQL權威指南(原書第2版)》清晰中文掃描版 PDF

Ubuntu 14.04 LTS 安裝 LNMP Nginx\PHP5 (PHP-FPM)\MySQL

Ubuntu 14.04下搭建MySQL主從伺服器

Ubuntu 12.04 LTS 構建高可用分布式 MySQL 叢集

Ubuntu 12.04下原始碼安裝MySQL5.6以及Python-MySQLdb

MySQL-5.5.38通用二進位安裝

--------------------------------------分割線 --------------------------------------

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.