[mysql]MySQL修改root密码的多种方法

时间:2022-09-09 16:06:37


方法0:使用GRANT语句 

       mysql>grant all on *.* to 'root'@'localhost' IDENTIFIED BY 'newpassword' with grant option ;

       mysql>grant all on *.* to 'root'@'%' IDENTIFIED BY 'newpassword' with grant option ;

       mysql>grant all on *.* to 'root'@'192.168.1.%' IDENTIFIED BY 'newpassword' with grant option ;

       mysql>flush privileges;



方法1 SET PASSWORD命令

  mysql -u root

  mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');



方法2:用mysqladmin

  mysqladmin -u root password "newpassword"

  如果root已经设置过密码,采用如下方法

  mysqladmin -u root password oldpass "newpassword"



方法3 UPDATE直接编辑user

  mysql -u root

  mysql> use mysql;

  mysql> UPDATE user SET Password = PASSWORD('newpassword') WHERE user = 'root';

  mysql> FLUSH PRIVILEGES;



方法4:在丢失root密码的时候,可以这样

  mysqld_safe --skip-grant-tables&

  mysql -u root mysql

  mysql> UPDATE user SET password=PASSWORD("newpassword") WHERE user='root';

  mysql> FLUSH PRIVILEGES;