Linux下修改mysql的root密码方法



本人使用的mysql版本为5.6.29.


Mysql修改root密码方法:


第一种方法:mysqladmin修改root密码:


1)新数据库,从未设置过root密码,可以使用下面命令:

# mysqladmin -u root password "newpass"  #新密码为“newpass”

Warning: Using a password on the command line interface can be insecure.


2)已经设置过root密码,想使用mysqladmin修改,使用下面命令:

[root@localhost ~]# mysqladmin -u root -pnewpass  password "redhat"   #新密码为“redhat”

Warning: Using a password on the command line interface can be insecure.

截图如下:


错误使用方法:

纠正网上很多博客mysqladmin修改root密码的一个错误命令:

[root@localhost ~]# mysqladmin -u root password redhat "password" 

mysqladmin: connect to server at 'localhost' failed

error: 'Access denied for user 'root'@'localhost' (using password: NO)'


注释:看到报错很多人认为权限不够,去设置权限,我也设置了然并卵.网上博客一爬十,十爬百,90%的博客都在写使用上面这条

错误的命令 #mysqladmin -u root password oldpassword "newpassword"


第二种方法:使用set password命令:


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

Query OK, 0 rows affected (0.00 sec)

截图如下:


第三种方法:使用update user直接登录数据库修改密码:

mysql> update user set password = password('newpass') where user = 'root';

Query OK, 5 rows affected (0.01 sec)

Rows matched: 5  Changed: 5  Warnings: 0


mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)


mysql> \q

截图如下:


第四种方法:使用skip-grant-tables参数可以在my.cnf文件添加,也可以启动时添加.

1)修改my.cnf文件:

[root@localhost ~]# cat /etc/my.cnf | grep skip

skip_name_resolve=on  

skip-grant-tables

[root@localhost ~]# /etc/init.d/mysqld restart

Shutting down MySQL..                                      [  OK  ]

Starting MySQL.                                            [  OK  ]

[root@localhost ~]# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.6.29 MySQL Community Server (GPL)


Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> quit

Bye

截图如下:

2)启动时添加--skip-grant-tables参数:

[root@localhost ~]# /etc/init.d/mysqld restart --skip-grant-tables

Shutting down MySQL..                                      [  OK  ]

Starting MySQL.                                            [  OK  ]

[root@localhost ~]# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.6.29 MySQL Community Server (GPL)


Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> quit

Bye

截图如下:


注意:

下面这条命令试了十几遍也没成功,可能版本不一样吧:

#mysqld_safe --skip-grant-tables &


#没有过滤到任何参数,不确定.

[root@localhost ~]# /data/mysql/bin/mysqld_safe --help | grep skip-grant-table



你可能感兴趣的:(Linux下修改mysql的root密码方法)