1.修改user表 更新密码
#查询密码
#使用mysql数据库 mysql> use mysql; #查询主机用户名密码:5.7版本之前的 mysql> select host,user,plugin,password from user; #查询主机用户名密码:5.7版本之后的,包括5.7 mysql> select host,user,plugin,authentication_string from user; mysql> select host,user,plugin,authentication_string from userG; mysql> select host,user,plugin,authentication_string from mysql.user;
#修改密码
mysql> update user set password=password("新密码") where user="root"; mysql> flush privileges; mysql> exit
我的mysql版本比较高,所以上面的方法并不适用,下面演示>5.7版本的修改密码
use mysql; alter user "root"@"localhost" identified with mysql_native_password by "新密码"; flush privileges; exit
如果以上方法不成功请参考下面的:
mysql> alter user "root"@"localhost" identified by "新密码"; --方法1 mysql> update user set authentication_string=password("新密码") where user="root"; -- 方法2 mysql> flush privileges; mysql> quit
2.使用mysqladmin修改密码
mysqladmin -uroot -p旧密码 password 新密码 #示例 mysqladmin -uroot -pnu0l password nu1l
会提示权限过高,忽略即可,在登录mysql就可以使用新密码了
忘记密码时
vi /etc/my.cnf
添加:
[mysqld]