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即可。

來源:http://www.cnblogs.com/sanduzxcvbnm/p/6080380.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章