MySQL 5.7忘記root密碼及修改root密碼的方法
【引自豆花飯燒土豆的博客】關(guān)閉正在運行的 MySQL :
- [root@www.woai.it ~]# service mysql stop
運行
- [root@www.woai.it ~]# mysqld_safe --skip-grant-tables &
為了安全可以這樣禁止遠程連接:
- [root@www.woai.it ~]# mysqld_safe --skip-grant-tables --skip-networking &
使用mysql連接server:
- [root@www.woai.it ~]# mysql -p
更改密碼:
- mysql> update mysql.user set authentication_string=password('123qwe') where user='root' and Host = 'localhost';
*特別提醒注意的一點是,新版的mysql數(shù)據(jù)庫下的user表中已經(jīng)沒有Password字段了
而是將加密后的用戶密碼存儲于authentication_string字段
- mysql> flush privileges;
- mysql> quit;
修改完畢。重啟
- [root@localhost ~]# service mysql restart
然后mysql就可以連接了
但此時操作似乎功能不完全,還要alter user…
- mysql> alter user 'root'@'localhost' identified by '123';
這樣也可以:
- mysql> set password for 'root'@'localhost'=password('123');
重點給大家介紹下mysql 5.7 root密碼修改
MySQL管理者密碼設(shè)置或修改:
依據(jù)官方說明5.6以后版本,***次啟動時會在root目錄下生產(chǎn)一個隨機密碼,文件名.mysql_secret。
- [root@bright ~]# cat /root/.mysql_secret
- # Password set for user 'root@localhost' at 2015-03-27 23:12:10
- :Jj+FTiqvyrF
- [root@bright ~]# cd /usr/local/mysql/bin/
- [root@bright bin]# ./mysqladmin -u root -h localhost password '123456' -p
Enter password: #此行輸入.mysql_secret里第二行內(nèi)容
- mysqladmin: [Warning] Using a password on the command line interface can be insecure.
- Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
官方的方式,筆者無論是否使用--skip-grant-tables啟動mysql都測試失敗,親們可以測試:
- shell>mysql -uroot -p'password' #password即.mysql_secret里的密碼
- mysql>SET PASSWORD = PASSWORD('newpasswd');
舊版本,安裝后ROOT無密碼,按如下操作:
方法一:
- shell>service mysqld stop #停止mysql服務(wù)
- shell>mysqld_safe --skip-grant-tables & #以不啟用grant-tables模式啟動mysql
- shell>mysql -uroot -p #輸入命令回車進入,出現(xiàn)輸入密碼提示直接回車。
- mysql>use mysql;
- mysql>update user set password=PASSWORD("123456")where user="root"; #更改密碼為 newpassord
- mysql>flush privileges; #更新權(quán)限
- mysql>quit #退出
方法二:
- shell>service mysqld stop #停止mysql服務(wù)
- shell>mysqld_safe --skip-grant-tables & #以不啟用grant-tables模式啟動mysql
- shell>mysql -uroot -p #輸入命令回車進入,出現(xiàn)輸入密碼提示直接回車。
- mysql > set password for root@localhost = password('mysqlroot');
方法三:
- shell>/path/mysqladmin -u UserName -h Host password 'new_password' -p
參考:http://www.jb51.net/article/77858.htm
忘記mysql root用戶密碼的解決辦法(skip-grant-tables)
skip-grant-tables
顧名思義,數(shù)據(jù)庫啟動的時候 跳躍權(quán)限表的限制,不用驗證密碼,直接登錄。
注意:
這種情況只有在忘記root密碼 不得已重啟數(shù)據(jù)庫的情況下使用的?,F(xiàn)網(wǎng)環(huán)境慎用,需要重啟數(shù)據(jù)庫,并且安全性也比較難以保證。
1.修改配置參數(shù)
/etc/my.cnf
在
[mysqld] 下面加上:
skip-grant-tables
配置項。
2.重啟MySQL
使得參數(shù)生效:
service mysqld restart
3.注意事項
此時所有用戶登錄當前數(shù)據(jù)庫都是免密碼的,所以此時數(shù)據(jù)庫的安全性是非常低的。
4.修改密碼
具體的辦法:
參考以上介紹
5.去掉參數(shù)
a.密碼修改好了之后再將配置文件中 skip-grant-tables去掉
b.再次重啟數(shù)據(jù)庫。