修改MySql密碼

關閉正在運行的 MySQL :

?
1
[[email protected] ~]# service mysql stop

運行

?
1
[[email protected] ~]# mysqld_safe --skip-grant-tables &

爲了安全可以這樣禁止遠程連接:

?
1
[[email protected] ~]# mysqld_safe --skip-grant-tables --skip-networking &

使用mysql連接server:

?
1
[[email protected] ~]# mysql -p

更改密碼:

?
1
mysql>updatemysql.userset authentication_string=password('123qwe')whereuser='root'and Host = 'localhost';

*特別提醒注意的一點是,新版的mysql數據庫下的user表中已經沒有Password字段了

而是將加密後的用戶密碼存儲於authentication_string字段

update mysql.user set authentication_string=password('root') where user='root' ; 

?
1
2
mysql> flush privileges;
mysql> quit;

修改完畢。重啓

?
1
[root@localhost ~]# service mysql restart

然後mysql就可以連接了

但此時操作似乎功能不完全,還要alter user…

?
1
mysql>alteruser 'root'@'localhost'identified by'123';

這樣也可以:

?
1
mysql>setpassword for 'root'@'localhost'=password('123');

重點給大家介紹下mysql 5.7 root密碼修改

MySQL管理者密碼設置或修改:

依據官方說明5.6以後版本,第一次啓動時會在root目錄下生產一個隨機密碼,文件名.mysql_secret。

?
1
2
3
4
5
[root@bright ~]# cat /root/.mysql_secret
#Passwordset 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裏第二行內容

?
1
2
mysqladmin: [Warning] Using a passwordon the command line interface can be insecure.
Warning: Since passwordwill be sent toserver inplain text, use ssl connectionto ensure passwordsafety.

官方的方式,筆者無論是否使用--skip-grant-tables啓動mysql都測試失敗,親們可以測試:

?
1
2
shell>mysql -uroot -p'password'#password即.mysql_secret裏的密碼
mysql>SETPASSWORD = PASSWORD('newpasswd');

舊版本,安裝後ROOT無密碼,按如下操作:

方法一:

?
1
2
3
4
5
6
7
shell>service mysqld stop #停止mysql服務
shell>mysqld_safe--skip-grant-tables & #以不啓用grant-tables模式啓動mysql
shell>mysql -uroot -p #輸入命令回車進入,出現輸入密碼提示直接回車。
mysql>use mysql;
mysql>updateuser set password=PASSWORD("123456")whereuser="root"; #更改密碼爲 newpassord
mysql>flushprivileges; #更新權限
mysql>quit #退出

方法二:

?
1
2
3
4
shell>service mysqld stop #停止mysql服務
shell>mysqld_safe--skip-grant-tables & #以不啓用grant-tables模式啓動mysql
shell>mysql -uroot -p #輸入命令回車進入,出現輸入密碼提示直接回車。
mysql > setpassword for root@localhost = password('mysqlroot');

方法三:

?
1
shell>/path/mysqladmin -u UserName -h Host password'new_password' -p
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章