mysql忘记root密码的解决方法

来源:互联网 发布:手机网络信号不稳定 编辑:程序博客网 时间:2024/05/22 04:35
MySQL登录的命令是mysql, mysql 的使用语法如下: mysql [-u username] [-h host] [-p[password]] [dbname] username 与 password 分别是 MySQL用户名与密码,mysql的初始管理帐号是root,没有密码,注意:这个root用户不是Linux的系统用户。MySQL默认用户是 root,由于初始没有密码,第一次进时只需键入mysql即可。 
[root@test1 local]# mysql 
Welcome to the MySQL monitor. Commands end with ; or g. 
Your MySQL connection id is 1 to server version: 4.0.16-standard 
Type 'help;' or 'h' for help. Type 'c' to clear the buffer. 
mysql> 
出现了“mysql>”提示符。
增加了密码后的登录格式如下: 
mysql -u root -p 
Enter password: (输入密码) 

其中-u后跟的是用户名,-p要求输入密码,回车后在输入密码处输入密码。

如果root用户设置了密码,并且我们不知道root用户的密码,当我们在Linux中使用mysql命令登录时,会报错ERROR 1045 (28000): Access denied for user root@localhost (using password: NO)

解决办法;
1、停用mysql服务:# /etc/rc.d/init.d/mysqld stop 
2、输入命令:# mysqld_safe --user=mysql --skip-grant-tables --skip-networking & 

3、登入数据库:# mysql -u root mysql 4、mysql> use mysql;结果如下: Database changed 
5、mysql> UPDATE user SET Password=PASSWORD('newpassword')where USER='newuser'; 结果如下: 
Query OK, 3 rows affected (0.00 sec) 
Rows matched: 3  Changed: 3  Warnings: 0  
mysql>FLUSH PRIVILEGES; 结果如下: 
Query OK, 0 rows affected (0.00 sec)  
mysql> quit  

# /etc/init.d/mysql restart  
# mysql -u newuser–p 
Enter password:newpassword  
mysql><登录成功>  

如果我们需要修改数据库用户的密码,我们可以这样做

方法1:

在mysql系统外,使用mysqladmin
mysqladmin -u root -p password "test123"
Enter password: 【输入原来的密码】

方法2:

通过登录mysql系统,
mysql -uroot -p
Enter password: 【输入原来的密码】
mysql>use mysql;
mysql> update user set password=passworD("test") where user='root';
mysql> flush privileges;
mysql> exit; 

0 0