1、登錄mysql
[root@localhost ~]# mysql -uroot -p123456 [root@localhost ~]# mysql -hlocalhost -uroot -p123456
如果忘記密碼,則跳過MySQL的密碼認證過程。步驟如下:
- 修改Mysql配置文件:vi /etc/my.cnf(注:windows下修改的是my.ini)。在[mysqld]后面任意一行添加“skip-grant-tables”用來跳過密碼驗證的過程。
- 重啟Mysql:
- 進入Mysql:[root@localhost ~]# mysql -uroot -p
2、使用mysql數據庫,從user表中查看主機,用戶名,密碼
-- 使用mysql數據庫 mysql> use mysql; -- 查詢主機用戶名密碼:5.7版本之前的 mysql> select host,user,plugin,password from user; -- 查詢主機用戶名密碼:5.7版本之后的,包括5.7 mysql> select host,user,plugin,authentication_string from user; mysql> select host,user,plugin,authentication_string from user\G; mysql> select host,user,plugin,authentication_string from mysql.user;
3、修改密碼,刷新一下權限
mysql> update user set password=password("新密碼") where user="root"; mysql> flush privileges; mysql> quit
上面修改密碼是在5.7版本之前的。若是5.7版本之后的(包括5.7),沒有password這個字段了,則修改方法如下:
mysql> alter user "root"@"localhost" identified by "新密碼"; --方法1 mysql> update user set authentication_string=password("新密碼") where user="root"; -- 方法2 mysql> flush privileges; mysql> quit
4、如果以上不能解決密碼修改,則使用下面方法
mysql> use mysql; mysql> alter user "root"@"localhost" identified with mysql_native_password by "新密碼"; mysql> flush privileges;
修改加密規則:mysql> alter user "root"@"localhost" identified by 'password' PASSWORD EXPIRE NEVER;
如果執行以上的操作並沒有解決,請再把default_authentication_plugin=mysql_native_password添加到配置中。
5、再去編輯一下my.cnf配置文件,去掉skip-grant-tables。
6、重啟Mysql,用你修改后的密碼登錄Mysql。