当前位置: 首页>>技术教程>>正文


如何在Linux服务器上重置MySQL根密码

, ,
以下指南将为您提供有关如何在Linux上重设管理根密码的简单步骤。

停止MySQL

首先,停止MySQL服务器:


# service mysql stop
 * Stopping MySQL database server mysqld              [ OK ]

启动MySQL服务器>

启动您的MySQL服务器,但是跳过所有主要特权和联网:


# mkdir -p /var/run/mysqld
# chown mysql:mysql /var/run/mysqld
# /usr/sbin/mysqld --skip-grant-tables --skip-networking &
[1] 8142

登录到MySQL

接下来,以root用户身份登录MySQL,无需输入密码:


# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.12-0ubuntu1 (Ubuntu)

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>

同花顺特权


mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected, 6 warnings (0.02 sec)

mysql>

设定新密码

接下来,将新的root密码设置为eg。linuxconfig然后退出:


mysql> SET PASSWORD FOR root@'localhost' = PASSWORD('linuxconfig');
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> quit
Bye

重启MySQL数据库

优雅地停止当前的mysql进程:


# kill %1
[1]+  Done                    /usr/sbin/mysqld --skip-grant-tables --skip-networking

接下来,我们需要启动MySQL数据库:


# service mysql start

使用新密码登录


# mysql -u root --password=linuxconfig
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.12-0ubuntu1 (Ubuntu)

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>

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/11344.html,未经允许,请勿转载。