具体步骤:
- 1)先关闭MySQL服务;
- 执行
sudo /usr/local/mysql/support-files/mysql.server stop
或者在系统偏好设置中关闭MySQL服务(如果电脑有设置密码的,此处会要求输入计算机密码) - 2)去mysql文件夹里设置跳过验证
先进入mysql文件夹: cd /usr/local/mysql/bin/
设置权限,如果电脑有设置密码(开机和解锁计算机时要求输入的那个密码),在输入此句后会要求输入密码。输入密码后按回车确认
sudo su
跳过验证
./mysqld_safe --skip-grant-tables &
-
3)开始设置我们自己的新密码
打开一个新的终端,输入
/usr/local/mysql/bin/mysql -u root -p
然后会要求输入密码,因为此时根本没有密码,所以直接点确认,显示以下信息表示成功进入mysql(与windows系统一样)
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.7.11
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>
现在设置新密码,注意要打分号;
UPDATE mysql.user SET authentication_string=PASSWORD('cc77') where User='root';
回车确认,显示以下信息表示修改密码成功
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
然后刷新一下,让上述修改生效
flush privileges;
刷新成功会显示以下信息:
Query OK, 0 rows affected (0.01 sec)
- 4).重启mysql,用新密码登录,可以登录成功了。如果进行其他mysql操作,显示:
ERROR 1820 (HY000) :You must reset your password using AlTER USER statement before executing this statement.
意思是还要再重设一遍密码,这时候直接输入
SET PASSWORD = PASSWORD('cc77');
修改成功后,终端会显示:
Query OK, 0 rows affected, 1 warning (0.01 sec)
至此,修改密码彻底完成,可以做任何相关sql操作了