1. 程式人生 > >mysql5.7 修改root 密碼

mysql5.7 修改root 密碼

假如忘記mysql的root 密碼是什麼之後如何修改密碼

我的mysql是自己原始碼安裝的,mysql.server存放在/usr/local/mysql/support-files檔案當中

1、先關閉mysql

$ /usr/local/mysql/support-files/mysql.server stop

2、修改my.cnf配置檔案,檔案路徑是/etc/my.cnf

$ echo skip-grant-tables >> /etc/my.cnf
$ cat /etc/my.cnf

[mysqld]

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
skip-grant-tables

3、啟動mysql

$ /usr/local/mysql/support-files/mysql.server start

4、進入mysql

$ mysql

5、重新整理許可權

mysql> flush privileges; 

6、修改密碼

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD("123");

7、登陸測試

$ mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

密碼設定完畢

8、註釋掉my.cnf的修改,避免重啟時無密碼就可登陸

[mysqld]
$ vim /etc/my.cnf

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# skip-grant-tables