mysql忘记root密码的解决方法

本文介绍了MySQL数据库的登录命令及语法,并详细说明了如何在忘记密码的情况下重置MySQL的root用户密码,同时还提供了两种更新数据库用户密码的方法。
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; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值