mysql忘记root密码该如何处理?

当我们用mysql -uroot -pxxxxxx登录mysql时,如果root密码错误,则会有如下提示:

myhost:~ # mysql -uroot -pxxxxxx
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
myhost:~ # 

那如果mysql的root密码忘记了,该如何处理呢?采用如下的方法可进行密码复位。步骤:

1、修改/etc/my.cnf

在文档内搜索mysqld定位到[mysqld]文本段:
/mysqld(在vi编辑状态下直接输入该命令可搜索文本内容)

在[mysqld]后面添加“skip-grant-tables”用来跳过密码验证的过程,如下图所示:

[mysqld]

skip-grant-tables
# Remove leading # and set to the amount of RAM for the most important data
 

保存并退出。

2、重启mysql

myhost:/etc # service mysqld restart
myhost:/etc # 
myhost:/etc # service mysqld status
mysqld.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/init.d/mysqld)
   Active: active (running) since Wed 2019-10-02 08:38:05 CST; 13s ago
  Process: 15467 ExecStop=/etc/init.d/mysqld stop (code=exited, status=0/SUCCESS)
  Process: 15503 ExecStart=/etc/init.d/mysqld start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/mysqld.service
           ├─15525 /bin/sh /opt/mysql/bin/mysqld_safe --datadir=/opt/mysql/mysqldat
           └─15853 /usr/local/mysql/bin/mysqld --basedir=/opt/mysql --datadir=/opt/er=mysql --log-error=/opt/mysql/mysqldata/myhost.err --pid-file=/opt/mysql/mysqldat-port=3306

Oct 02 08:38:04 myhost systemd[1]: Starting LSB: start and stop MySQL...
Oct 02 08:38:05 myhost mysqld[15503]: Starting MySQL...done
Oct 02 08:38:05 myhost systemd[1]: Started LSB: start and stop MySQL.
myhost:/etc #

3、 重启后登录mysql并重置密码

update mysql.user set authentication_string=password('xxxxxxx!')  where user='root';

4、编辑my.cnf,去掉刚才添加的内容,然后重启mysql,这时候使用mysql -uroot -pxxxxxxx就能登录root用户了。

 

 

 

你可能感兴趣的:(mysql,数据库,Using,a,password,on,the,comman)