解決方法:
第一步:如果mysql服務(wù)正在進(jìn)行,將之停止炸茧。
第二步:在終端中以管理員權(quán)限啟動(dòng)mysqld_safe,命令如下:
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
執(zhí)行結(jié)果如下:
2016-06-12T08:29:17.6NZ mysqld_safe Logging to '/usr/local/mysql/data/lyqdeMacBook-Pro.local.err'.
2016-06-12T08:29:17.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
第三步:不要關(guān)閉當(dāng)前的終端窗口,新建一個(gè)終端窗口逻住,輸入如下命令,回車登錄mysql
/usr/local/mysql/bin/mysql
登錄后迎献,看到歡迎信息:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 5.7.13 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"這個(gè)數(shù)據(jù)庫鄙信,SQL如下:
mysql> use mysql;
看到結(jié)果:
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>
然后,更新root的密碼忿晕,SQL如下:
mysql> update user set authentication_string=password('root') where Host='localhost' and User='root';
注意:
①有的版本的mysql中装诡,密碼可能存儲在password字段中,可以使用"describe user;"命令來查看下表結(jié)構(gòu)再操作
②authentication_string的值一定通過password函數(shù)來計(jì)算(password('root'))
執(zhí)行結(jié)果如下:
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 1
退出mysql(執(zhí)行sql語句:exit)
最后一步:將mysqld_safe進(jìn)程殺死践盼,重啟mysqld鸦采。
可能會遇到的問題
登錄mysql
/usr/local/mysql/bin/mysql -uroot -proot
這個(gè)時(shí)候,如果執(zhí)行查詢之類的操作咕幻,比如執(zhí)行"show databases;"渔伯,可能會有如下提示:
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
根據(jù)提示進(jìn)行操作,輸入如下SQL語句肄程,這個(gè)語句的作用是修改root用戶的口令為root:
mysql> alter user 'root'@'localhost' identified by 'root';
結(jié)果:
Query OK, 0 rows affected