如何在Ubuntu 16.04中恢复MySQL密码

6

Mysql 版本 - mysql Ver 14.14 Distrib 5.7.18,适用于 Linux (x86_64) 平台,使用 EditLine 封装。

我忘记了我的密码,并尝试了许多在线命令。此外,问题在于授权表命令在我的终端中不起作用。


1
我投票关闭此问题,因为此问题应该在https://unix.stackexchange.com或https://askubuntu.com上发布。 - einpoklum
4个回答

15
~$ cat /etc/issue
Ubuntu 16.04.3 LTS \n \l

~$ aptitude show mysql-server |grep Version
Version: 5.7.20-0ubuntu0.16.04.1

在文件 /etc/mysql/debian.cnf 中有两行重要的内容:

user     = debian-sys-maint
password = <unique password>

使用该用户名和密码登录到mysql: 一旦以debian-sys-maint用户身份登录,您可以执行以下操作:

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

单独这样做是不行的,还需要另一步操作。ERROR 1698 (28000): Access denied for user 'root'@'localhost' - Stewart
这个有效了,由于某些原因,标准程序无法让我登录。 - pescamillam
1
太棒了,我刚刚在phpmyadmin中使用了这些凭据,并更改了root的密码。谢谢! - Harish Kamboj
时间节省者,你真是个天才 :) - Nuno Sarmento

2
以下调整对我的设置有效:
$ sudo mkdir /var/run/mysqld
$ sudo chown mysql: /var/run/mysqld
$ sudo kill -9 $(sudo cat /var/run/mysqld/mysqld.pid)

0

我尝试了所有方法,但都没有成功。以下的方法可行。我希望它在不同版本的Ubuntu上也能运行。

  1. 操作系统信息:
mysql --version
mysql  Ver 8.0.28-0ubuntu0.20.04.3 for Linux on x86_64 ((Ubuntu))
  1. 首先,使用命令停止MySQL服务
sudo systemctl stop mysql
  1. 其他命令无效,以下是我如何在没有任何权限检查的情况下启动MySQL服务器。要这样做,请运行:
// open the mysql systemd configuration file your default text editor. 
// In my case, it is nano editor.
sudo systemctl edit mysql

// Add the following 3 lines
[Service]
ExecStart=
ExecStart=/usr/sbin/mysqld --skip-grant-tables --skip-networking

4. 使用以下命令重新加载systemd配置:
sudo systemctl daemon-reload
  1. 启动MySQL服务
sudo systemctl start mysql

现在,以root用户身份连接到MySQL服务器而不需要密码:
sudo mysql -u root
mysql> FLUSH PRIVILEGES;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'add-newpassword-here';

7. 恢复修改过的systemd配置。
sudo systemctl revert mysql

你已经完成了。


0

1) 停止数据库服务器。 sudo systemctl stop mysql

2) 在不进行权限检查的情况下重新启动数据库服务器。使用以下命令: sudo mysqld_safe --skip-grant-tables --skip-networking &

3) 以 root 用户身份连接到数据库 mysql -u root

4) 更改 Root 密码

FLUSH PRIVILEGES;

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

5) 正常重启数据库服务器

sudo kill `cat /var/run/mysqld/mysqld.pid`

sudo systemctl start mysql

现在使用数据库中的新密码进行登录: mysql -u root -p

或者您可以点击以下链接:

https://www.digitalocean.com/community/tutorials/how-to-reset-your-mysql-or-mariadb-root-password


第二步)给我返回了“mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.” 这是我们应该创建该目录吗? - Stewart

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接