> 数据库 > MySQL >

MySQL修改root密码

先登录mysql

mysql -uroot -p
 

然后用SET PASSWORD命令修改密码

set password for root@localhost = password('123456');  #本地登录密码

set password for root@'%' = password('123456');           #远程登录密码

 

注:如果出现Your password does not satisfy之类的密码太简单导致密码安全问题,需要设置一下密码长度和密码安全等级

set global validate_password_length=1;

set global validate_password_policy=0;

 

先登录mysql

mysql -uroot -p
 

用GRANT权限命令修改权限和密码

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '123456' WITH GRANT OPTION;
 

先关闭MySQL服务器

systemctl stop mysqld
 

修改配置文件,在[mysqld]的段加入skip-grant-tables,使用无密码登录

vim /etc/my.cnf
 

启动MySQL

systemctl start mysqld

用mysql命令直接登录root
 

其它用命令修改密码

use mysql;

update user set authentication_string=password('root') where user='root'; 

flush privileges ;

 

注:MySQL 5.7中密码是authentication_string字段

修改vim /etc/my.cnf,将skip-grant-tables删除,改回原来的样子

 

重起MySQL

systemctl restart mysqld


 

(责任编辑:IT)