如何将root密码设置为空

2021-11-20 00:00:00 mysql mysql-management

如何将 MySQL 的 root 用户的密码更改为 null -- 意味着没有密码或 '' -- 从 MySQL命令行客户端?

How can I change the password for root user of MySQL to null -- meaning no password or '' -- from the MySQL command line client?

推荐答案

您可以通过以下五个简单的步骤恢复 MySQL 数据库服务器密码.

You can recover MySQL database server password with following five easy steps.

第 1 步:停止 MySQL 服务器进程.

Step # 1: Stop the MySQL server process.

第 2 步:使用 --skip-grant-tables 选项启动 MySQL (mysqld) 服务器/守护进程,这样它就不会提示输入密码.

Step # 2: Start the MySQL (mysqld) server/daemon process with the --skip-grant-tables option so that it will not prompt for password.

第 3 步:以 root 用户身份连接到 mysql 服务器.

Step # 3: Connect to mysql server as the root user.

第 4 步:设置新的 mysql root 帐户密码,即重置 mysql 密码.

Step # 4: Setup new mysql root account password i.e. reset mysql password.

第 5 步:退出并重新启动 MySQL 服务器.

Step # 5: Exit and restart the MySQL server.

以下是您需要为每个步骤键入的命令(以 root 用户身份登录):

Here are commands you need to type for each step (login as the root user):

第一步:停止mysql服务

# /etc/init.d/mysql stop

输出:

Stopping MySQL database server: mysqld.

第 2 步:在没有密码的情况下启动 MySQL 服务器:

Step # 2: Start to MySQL server w/o password:

# mysqld_safe --skip-grant-tables &

输出:

[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started

第 3 步:使用 mysql 客户端连接到 mysql 服务器:

Step # 3: Connect to mysql server using mysql client:

# mysql -u root

输出:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.15-Debian_1-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>

第 4 步:设置新的 MySQL 根用户密码

Step # 4: Setup new MySQL root user password

mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit

第 5 步:停止 MySQL 服务器:

Step # 5: Stop MySQL Server:

# /etc/init.d/mysql stop

输出:

Stopping MySQL database server: mysqld
STOPPING server from pid file /var/run/mysqld/mysqld.pid
mysqld_safe[6186]: ended
[1]+  Done                    mysqld_safe --skip-grant-tables

第 6 步:启动 MySQL 服务器并对其进行测试

Step # 6: Start MySQL server and test it

# /etc/init.d/mysql start
# mysql      
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 
# mysql -u root -p

来源:http://www.cyberciti.biz/tips/恢复-mysql-root-password.html

相关文章