MAC上安装数据库后,重启数据库后,发现需要输入密码,但是由于密码没保存,一直连接不上,总是报错。
不输入密码时会报错:
ERROR 1045: Access denied for user: 'root@localhost' (Using password: NO)
输入密码会报错:
ERROR 1045: Access denied for user: 'root@localhost' (Using password: YES)
ERROR 1045: Access denied for user: 'root@localhost' (Using
password: NO)
或
ERROR 1045: Access denied for user: 'root@localhost' (Using
password: YES)
网上查了很多方法,总算走通了,重设了root密码。分享一下,帮助遇到同样问题的小伙伴。
1、终端里输入命令关闭正在运行的mysql
/usr/local/mysql/bin/mysqladmin -u root -p shutdown
关闭后,可以看到系统设置里面的mysql已经关闭了,左边变成红色。
2、 查看mysql的目录
3、进入mysql的bin目录并执行管理员命令
$ cd /usr/local/mysql/bin
$ sudo su
输入管理员密码后会看到
sh-3.2#
在sh-3.2#后面输入下面命令以安全模式运行mysql
sh-3.2# ./mysqld_safe --skip-grant-tables &
运行结束,会发现mac的系统偏好设置中,mysql已经重新运行了,左侧的状态变成绿色
4、Command + N 重新打开一个终端
输入mysql -u -root,发现已经不需要输入密码就能进入mysql
Your MySQL connection id is 57
Server version: 5.7.10 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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>
5、使用mysql密码设置的命令set password,重置密码
set password for root@localhost = password('alice');
发现报错。
mysql> set password for root@localhost = password('alice');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'password('alice')' at line 1
直接使用
SET PASSWORD=PASSWORD('alice');
发现也报错。
网上查了下原因,可能是我数据库的版本比较高,所以导致这个命令不生效了,于是更新使用其他命令alter user 'root'@'localhost' identified by 'alice';来设置密码。
mysql> SET PASSWORD=PASSWORD('alice');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'PASSWORD('alice')' at line 1
mysql> alter user 'root'@'localhost' identified by 'alice';
Query OK, 0 rows affected (0.02 sec)