mysql 修改root密码 (忘记密码)

MySQL - Resetting a lost MySQL root password


The MySQL root password allows full access to the MySQL database and allows for all actions to be undertaken including creating new users, new databases, setting access rules and so on.

Losing one can be a difficult issue to encounter. Luckily, resetting the root password is easy as long as you have sudo access to the Server.

Contents

Not the Server root user

A common issue is confusing the Server root user with the MySQL root user.

The Server root user is the server's main user. The MySQL root user has complete control over MySQL only. The two 'root' users are not connected in any way.

Stop MySQL

The first thing to do is stop MySQL. If you are using Ubuntu or Debian the command is as follows:

        
        
sudo /etc/init.d/mysql stop

For CentOS, Fedora, and RHEL the command is:

        
        
sudo /etc/init.d/mysqld stop

Safe mode

Next we need to start MySQL in safe mode - that is to say, we will start MySQL but skip the user privileges table. Again, note that you will need to have sudo access for these commands so you don't need to worry about any user being able to reset the MySQL root password:

        
        
sudo mysqld_safe --skip-grant-tables &

Note: The ampersand (&) at the end of the command is required.

备注:在执行了上面的语句后,需要另外开一个窗口执行下面的语句。重启mysql的时候,这个窗口的执行会自己关闭

Login

All we need to do now is to log into MySQL and set the password.

        
        
mysql -u root

Note: No password is required at this stage as when we started MySQL we skipped the user privileges table.

Next, instruct MySQL which database to use:

        
        
use mysql;

Reset Password

Enter the new password for the root user as follows:

        
        
update user set password=PASSWORD("mynewpassword") where User='root';

and finally, flush the privileges:

        
        
flush privileges;

Restart

Now the password has been reset, we need to restart MySQL by logging out:

        
        
quit

and simply stopping and starting MySQL.

On Ubuntu and Debian:
        
        
sudo /etc/init.d/mysql stop ... sudo /etc/init.d/mysql start
On CentOS and Fedora and RHEL:
        
        
sudo /etc/init.d/mysqld stop ... sudo /etc/init.d/mysql start

Login

Test the new password by logging in:

        
        
mysql -u root -p
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值