方法1: 用SET PASSWORD命令
mysql -u root
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');
方法2:用mysqladmin
mysqladmin -u root password "newpass"
如果root已经设置过密码,采用如下方法
mysqladmin -u root password oldpass "newpass"
方法3: 用UPDATE直接编辑user表
mysql -u root
mysql> use mysql;
mysql> UPDATE user SET password = PASSWORD('newpass') WHERE user = 'root';
mysql> FLUSH PRIVILEGES;
在丢失root密码的时候,可以这样
mysqld_safe --skip-grant-tables &
mysql -u root mysql
mysql> UPDATE user SET `password`=password("root") where `user`='root';
mysql> FLUSH PRIVILEGES;
注:如果出现Unknown column 'password' in 'field list',采用如下语句来更新,因为新版本mysql采用authentication_string替代了password字段:
update mysql.user set authentication_string=password('root') where user='root' ;
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('root');
flush privileges;
quit;
即可
方法三除了可以更改密码,还可以更改host,如:
update user set host='%' where user='root';
参考
1、macOS安装MySQL 以及报错解决 - 掘金
https://juejin.im/post/5bcc0f89e51d450e64766670