搜尋
首頁資料庫mysql教程MySQL 修改用户密码及重置root密码

为数据库用户修改密码是DBA比较常见的工作之一。对于MySQL用户账户的密码修改,有几种不同的方式,推荐的方式使用加密函数来修改

为数据库用户修改密码是DBA比较常见的工作之一。对于MySQL用户账户的密码修改,,有几种不同的方式,推荐的方式使用加密函数来修改密码。本文主要描述了通过几种不同的方式来修改用户密码以及mysql root账户密码丢失(重置root密码)的处理方法。

1、密码修改的几种方法

a、可以在创建用户的时候指定密码,以及直接使用grant创建用户的时候指定密码。
  对于已经存在的用户直接使用grant方式也可以修改密码
如下:

--演示版本
root@localhost[(none)]> show variables like 'version%'; 
+-------------------------+------------------------------+ 
| Variable_name          | Value                        | 
+-------------------------+------------------------------+ 
| version                | 5.5.37                      | 
| version_comment        | MySQL Community Server (GPL) | 
| version_compile_machine | x86_64                      | 
| version_compile_os      | Linux                        | 
+-------------------------+------------------------------+ 

--下面我们使用grant方式创建一个新帐户fred,并设定密码
root@localhost[(none)]> grant usage on *.* to 'fred'@'localhost' identified by 'fred';
Query OK, 0 rows affected (0.00 sec)

--查看刚刚创建的账户
root@localhost[(none)]> select host,user,password from mysql.user where user='fred';
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | fred | *6C69D17939B2C1D04E17A96F9B29B284832979B7 |
+-----------+------+-------------------------------------------+

--下面可以成功登陆mysql
SZDB:~ # mysql -ufred -pfred

fred@localhost[(none)]>

b、使用set password方式来修改账户密码
--下面我们使用set password方式来设定密码
root@localhost[(none)]> set password for 'fred'@'localhost'=password('passwd');
Query OK, 0 rows affected (0.00 sec)

root@localhost[(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

--再次登陆时,之前的密码已经失效,无法登陆
SZDB:~ # mysql -ufred -pfred
ERROR 1045 (28000): Access denied for user 'fred'@'localhost' (using password: YES)

--下面使用新密码登陆成功
SZDB:~ # mysql -ufred -ppasswd

fred@localhost[(none)]>

--检索数据库是否存在linuxidc用户,如下密码为null
root@localhost[(none)]> select host,user,password from mysql.user where user='linuxidc';
+-----------+------+----------+
| host      | user | password |
+-----------+------+----------+
| localhost | linuxidc |          |
+-----------+------+----------+

c、加密方式更新系统表user的password列
--我们尝试直接更新密码列(不使用加密函数方式)
root@localhost[(none)]> update mysql.user set password='linuxidc' where user='linuxidc';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

--由于直接使用明文,因此系统表user列password显示为明文
root@localhost[(none)]> select host,user,password from mysql.user where user='linuxidc';
+-----------+------+----------+
| host      | user | password |
+-----------+------+----------+
| localhost | linuxidc | jack    |
+-----------+------+----------+

--Author : Leshami
--Blog  :

root@localhost[(none)]> flush privileges;
Query OK, 0 rows affected (0.02 sec)

--此时无法登陆
SZDB:~ # mysql -ulinuxidc -pjack -h localhost     
ERROR 1045 (28000): Access denied for user 'linuxidc'@'localhost' (using password: YES)

--下面我们通过set方式来修改linuxidc的密码,提示找不到linuxidc用户
root@localhost[(none)]> set password for 'linuxidc'@'localhost'=password('linuxidc');
ERROR 1133 (42000): Can't find any matching row in the user table

--我们切换到mysql数据库下尝试,
root@localhost[(none)]> use mysql 

root@localhost[mysql]> set password for 'linuxidc'@'localhost'=password('passwd');  --在mysql数据库下依旧无法更新用户linuxidc的密码
ERROR 1133 (42000): Can't find any matching row in the user table

--下面我们尝试用password函数方式来更新password列
root@localhost[mysql]> update user set password=password('passwd') where user='linuxidc'; --此方式更新成功
Query OK, 1 row affected (0.04 sec)
Rows matched: 1  Changed: 1  Warnings: 0

root@localhost[mysql]> select host,user,password from user where user='linuxidc';    --可以看到密码已经变成了密文
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | linuxidc | *59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0 |
+-----------+------+-------------------------------------------+

root@localhost[mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

--此时登陆成功
robin@SZDB:~> mysql -ulinuxidc -ppasswd         

linuxidc@localhost[(none)]>

--------------------------------------分割线 --------------------------------------

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通用二进制安装

--------------------------------------分割线 --------------------------------------

更多详情见请继续阅读下一页的精彩内容:

linux

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
mysql:blob和其他無-SQL存儲,有什麼區別?mysql:blob和其他無-SQL存儲,有什麼區別?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而ilenosqloptionslikemongodb,redis和calablesolutionsolutionsolutionsoluntionsoluntionsolundortionsolunsonstructureddata.blobobobissimplobisslowdeperformberbutslowderformandperformancewithlararengedata;

mySQL添加用戶:語法,選項和安全性最佳實踐mySQL添加用戶:語法,選項和安全性最佳實踐May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串數據類型常見錯誤?MySQL:如何避免字符串數據類型常見錯誤?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingSefectery.1)usecharforfixed lengengtrings,varchar forvariable-varchar forbariaible length,andtext/blobforlargerdataa.2 seterters seterters seterters

mySQL:字符串數據類型和枚舉?mySQL:字符串數據類型和枚舉?May 13, 2025 am 12:05 AM

mysqloffersechar,varchar,text,and denumforstringdata.usecharforfixed Lengttrings,varcharerforvariable長度,文本forlarger文本,andenumforenforcingDataAntegrityWithaEtofValues。

mysql blob:如何優化斑點請求mysql blob:如何優化斑點請求May 13, 2025 am 12:03 AM

優化MySQLBLOB請求可以通過以下策略:1.減少BLOB查詢頻率,使用獨立請求或延遲加載;2.選擇合適的BLOB類型(如TINYBLOB);3.將BLOB數據分離到單獨表中;4.在應用層壓縮BLOB數據;5.對BLOB元數據建立索引。這些方法結合實際應用中的監控、緩存和數據分片,可以有效提升性能。

將用戶添加到MySQL:完整的教程將用戶添加到MySQL:完整的教程May 12, 2025 am 12:14 AM

掌握添加MySQL用戶的方法對於數據庫管理員和開發者至關重要,因為它確保數據庫的安全性和訪問控制。 1)使用CREATEUSER命令創建新用戶,2)通過GRANT命令分配權限,3)使用FLUSHPRIVILEGES確保權限生效,4)定期審計和清理用戶賬戶以維護性能和安全。

掌握mySQL字符串數據類型:varchar vs.文本與char掌握mySQL字符串數據類型:varchar vs.文本與charMay 12, 2025 am 12:12 AM

chosecharforfixed-lengthdata,varcharforvariable-lengthdata,andtextforlargetextfield.1)chariseffity forconsistent-lengthdatalikecodes.2)varcharsuitsvariable-lengthdatalikenames,ballancingflexibilitibility andperformance.3)

MySQL:字符串數據類型和索引:最佳實踐MySQL:字符串數據類型和索引:最佳實踐May 12, 2025 am 12:11 AM

在MySQL中處理字符串數據類型和索引的最佳實踐包括:1)選擇合適的字符串類型,如CHAR用於固定長度,VARCHAR用於可變長度,TEXT用於大文本;2)謹慎索引,避免過度索引,針對常用查詢創建索引;3)使用前綴索引和全文索引優化長字符串搜索;4)定期監控和優化索引,保持索引小巧高效。通過這些方法,可以在讀取和寫入性能之間取得平衡,提升數據庫效率。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器