標籤:lead data restart ted oca password upd option quit
如果使用 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中的datadir和basedir參數相應的啟用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即可。
linux下mysql忘記root密碼解決方案