方法1: 用SET PASSWORD命令
mysql -u root
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');
方法2:用mysqladmin(这个是在linu命令行界面)
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密码的时候
在/etc/my.conf中,[mysql]的段中添加一句:
skip-grant-tables
保存退出,重启mysql
/etc/init.d/mysqld restart
使用命令进入mysql:
mysql-uroot-p
按两下回车进入mysql
mysql>use mysql;
mysql>updateusersetpassword=password('123456')whereuser='root';
注意:CentOS7的password的字段修改了
update user set authentication_string = password('root'), password_expired = 'N', password_last_changed = now() where user = 'root';
在之前的版本中,密码字段的字段名是 password,5.7版本改为了 authentication_string
重新编辑/etc/f
删除或者注释掉skip-grant-tables
重启服务ok