linux下mysql忘記root密碼解決方法


如果使用 MySQL 數據庫忘記了root賬號密碼,可以通過調節配置文件,跳過密碼的方式登數據庫,

在數據庫里面修改賬號密碼,一般默認的賬號是 root

1、編輯 MySQL 配置文件 my.cnf

注意: 以實際 my.cnf 配置文件路徑為准

vim /etc/my.cnf

[mysqld]
spik-grant-tables  #增加

2、重啟 MySQL 服務

注意:以實際 MySQL 啟動腳本路徑為准

/etc/init.d/mysqld restart

若報錯,注意觀察my.cnf配置文件中的內容

3、登陸數據庫

/usr/bin/mysql 輸入如下命令:

注意:以實際 MySQL 執行文件路徑為准

mysql> USE mysql;
mysql> UPDATE user SET Password = password ('新密碼') WHERE User = 'root' ;
mysql> flush privileges ;
mysql> quit

4、刪除或者注釋第一步驟中添加的 spik-grant-tables

5、重啟 MySQL 服務

/etc/init.d/mysqld restart

6、使用新密碼測試

 

特殊情況:

我這是使用源碼安裝的mysql,默認配置文件my.cnf(/usr/local/mysql/my.cnf)中的內容都沒有配置,都是在啟動文件mysqld(/etc/rc.d/init.d/mysqld)中進行配置的

datadir=/data/mysql

basedir=/usr/local/mysql

 

若按照上述方法進操作,在my.cnf中添加"spik-grant-tables",則重啟mysql時報錯

 

正確的解決方法:

根據啟動文件mysqld中的datadirbasedir參數相應的啟用my.cnf中的選項,然后再添加"spik-grant-tables",mysql重啟就不會報錯,也能正常按照下面的步驟進行

未修改前的my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# spik-grant-tables
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port =  .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

修改后的my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# spik-grant-tables
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
# server_id = .....
# socket = .....
skip-grant-tables

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

然后再重啟mysql,進行下面的步驟,重置密碼后,再把my.cnf中的修改的那四項全部注釋掉,再次重啟mysql即可。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM