1. 程式人生 > >linux下mysql5.7.12忘記密碼

linux下mysql5.7.12忘記密碼

一. 碰到問題

問題:mac上安裝的mysql5.7.12,長時間不用,忘記密碼,navicat連線時提示:
Access denied for user ‘root’@’localhost’ (using password: YES)

解決:重置密碼

二. 重置密碼詳情:

2.1 關閉mysql服務

mac下可在系統偏好設定中關閉或在終端中使用kill強制關閉,進入root使用者

xiaoxiangdeMacBook-Pro:bin root# ps -ef|grep mysql
   74   838     1   0 10:17AM ??         0:00.53 /usr/local/mysql/bin/mysqld --user=_mysql --basedir=/usr/local
/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/mysqld.local.err --pid-file=/usr/local/mysql/data/mysqld.local.pid 0 1124 814 0 10:36AM ttys000 0:00.00 grep mysql xiaoxiangdeMacBook-Pro:bin root# kill -9 838 xiaoxiangdeMacBook-Pro:bin root# ps -ef|grep mysql
0 1133 814 0 10:37AM ttys000 0:00.00 grep mysql

2.2 啟動mysql的安全模式

命令:./mysqld_safe –user=root –skip-grant-tables –skip-networking &

xiaoxiangdeMacBook-Pro:bin root# pwd
/usr/local/mysql/bin
xiaoxiangdeMacBook-Pro:bin root# ./mysqld_safe --user=root --skip-grant-tables --skip-networking & 
[1] 1184 xiaoxiangdeMacBook-Pro:bin root# 2016-08-09T02:40:37.6NZ mysqld_safe Logging to '/usr/local/mysql/data/xiaoxiangdeMacBook-Pro.local.err'. 2016-08-09T02:40:37.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data xiaoxiangdeMacBook-Pro:bin root# ps -ef|grep mysql 0 1184 814 0 10:40AM ttys000 0:00.02 /bin/sh ./mysqld_safe --user=root --skip-grant-tables --skip-networking 0 1296 1184 0 10:40AM ttys000 0:00.25 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=root --skip-grant-tables --skip-networking --log-error=/usr/local/mysql/data/xiaoxiangdeMacBook-Pro.local.err --pid-file=/usr/local/mysql/data/xiaoxiangdeMacBook-Pro.local.pid 0 1303 814 0 10:41AM ttys000 0:00.00 grep mysql

2.3 使用root使用者免密碼登陸mysql

命令:mysql -u root -p

xiaoxiangdeMacBook-Pro:bin root# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 56
Server version: 5.7.12 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

提示:如果在mysql正常執行的情況下,輸入mysql提示:
mysql command not found
遇上-bash: mysql: command not found,是因為/usr/local/bin目錄下沒有mysql,需要建立軟連結:把mysql安裝目錄,比如/usr/local/mysql/bin/mysql,對映到/usr/local/bin目錄下:

# cd /usr/local/bin
# ln -fs /usr/local/mysql/bin/mysql mysql

還有其它常用命令mysqladmin、mysqldump等不可用時候都可按用此方法解決。建立後可檢視:

xiaoxiangdeMacBook-Pro:~ root# ls /usr/local/bin
bbdiff      idea        prl_convert prlcore2dmp prlsrvctl
bbedit      mysql       prl_disk_tool   prlctl
bbfind      mysqladmin  prl_perf_ctl    prlexec

2.4 選擇使用mysql資料庫

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 

2.5 重置密碼

檢視當前密碼,已加密的。注意mysql的版本不同,密碼儲存的欄位不同,首先檢視mysql版本是5.7.12,密碼儲存欄位為authentication_string;

xiaoxiangdeMacBook-Pro:bin root# mysql --version
mysql  Ver 14.14 Distrib 5.7.12, for osx10.11 (x86_64) using  EditLine wrapper
mysql> select user,host,authentication_string from user where user="root";
+------+-----------+-------------------------------------------+
| user | host      | authentication_string                     |
+------+-----------+-------------------------------------------+
| root | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+------+-----------+-------------------------------------------+
1 row in set (0.00 sec)

mysql> 

重置密碼,並檢視,重新整理許可權,並退出mysql

mysql> update user set authentication_string=password('123456') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> select user,host,authentication_string from user where user="root";
+------+-----------+-------------------------------------------+
| user | host      | authentication_string                     |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+-----------+-------------------------------------------+
1 row in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;

2.6 退出安全模式

xiaoxiangdeMacBook-Pro:bin root# ps -ef|grep mysql
    0  1184   814   0 10:40AM ttys000    0:00.02 /bin/sh ./mysqld_safe --user=root --skip-grant-tables --skip-networking
    0  1296  1184   0 10:40AM ttys000    0:00.54 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=root --skip-grant-tables --skip-networking --log-error=/usr/local/mysql/data/xiaoxiangdeMacBook-Pro.local.err --pid-file=/usr/local/mysql/data/xiaoxiangdeMacBook-Pro.local.pid
    0  1755   814   0 10:57AM ttys000    0:00.00 grep mysql
xiaoxiangdeMacBook-Pro:bin root# kill -9 1184
xiaoxiangdeMacBook-Pro:bin root# kill -9 1296
xiaoxiangdeMacBook-Pro:bin root# ps -ef|grep mysql
    0  1781   814   0 10:58AM ttys000    0:00.00 grep mysql

2.7 重新啟動mysql,重啟不了可以先重啟一下機器,用navicat連線成功


以上,有問題請留言