ITPub博客

首页 > 数据库 > MySQL > mysql root 修改密码的多种方法

mysql root 修改密码的多种方法

原创 MySQL 作者:starive 时间:2014-11-24 10:58:06 0 删除 编辑

mysql root 修改密码的多种方法




如果root密码没有丢失


方法1: 用SET PASSWORD命令


  mysql -u root -p登陆
输入密码:

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

方法2:用mysqladmin

如果root没有设置密码
shell> mysqladmin -u root password "newpass"


如果root已经设置密码:
shell> mysqladmin -u root password oldpass "newpass"   ????有问题


方法3: 用UPDATE直接编辑user表

  mysql -u root -p
输入密码:

  mysql> use mysql;

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

  mysql> FLUSH PRIVILEGES;


如果root密码丢失


在丢失root密码的时候,可以这样

.修改MySQL的登录设置: 
# vi /etc/my.cnf 
在[mysqld]的段中加上一句:skip-grant-tables 
例如: 
[mysqld] 
datadir=/var/lib/mysql 
socket=/var/lib/mysql/mysql.sock 
skip-grant-tables 
保存并且退出vi。 
3.重新启动mysqld 
# /etc/init.d/mysqld restart 
Stopping MySQL: [ OK ] 
Starting MySQL: [ OK ] 
4.登录并修改MySQL的root密码 
# /usr/bin/mysql 
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 3 to server version: 3.23.56 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 
mysql> USE mysql ; 
Reading table information for completion of table and column names 
You can turn off this feature to get a quicker startup with -A 
Database changed 
mysql> UPDATE user SET Password = password ( 'new-password' ) WHERE User = 'root' ; 
Query OK, 0 rows affected (0.00 sec) 
Rows matched: 2 Changed: 0 Warnings: 0 
mysql> flush privileges ; 
Query OK, 0 rows affected (0.01 sec) 
mysql> quit 
Bye 

5.将MySQL的登录设置修改回来 
# vi /etc/my.cnf 
将刚才在[mysqld]的段中加上的skip-grant-tables删除 
保存并且退出vi。 

6.重新启动mysqld 
# /etc/init.d/mysqld restart 
Stopping MySQL: [ OK ] 
Starting MySQL: [ OK ]


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26435490/viewspace-1344207/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-01-18

  • 博文量
    143
  • 访问量
    530075