重新安装 MySQL 或重置其 root 密码

重新安装 MySQL 或重置其 root 密码

我在两个 Ubuntu 16.04 上运行 LAMP 下的 MySQL 5.7.19。这是一个 AWS EC2 实例。

我安装 MySQL 已经有一段时间了,而且我还没有用过它,所以如果我重新安装它,就不会有问题了。我输入的 root 密码都不起作用。

我试过

 sudo /etc/init.d/mysql stop
 sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
sudo  mysql -u root

导致

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

然后我尝试了概述的程序这里.当我进入

 sudo apt-get install mysql-server

我有

Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 linux-headers-generic : Depends: linux-headers-4.4.0-119-generic but it is     not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

当我进入

ps -aux | grep mysql

结果是

ubuntu   27152  0.0  0.0  12948   984 pts/1    S+   12:30   0:00 grep --color=auto mysql

当我进入

 mysql -u root -p

我明白了

Enter password:

因此 mysql 尚未卸载,我无法重新配置它。

更新 1:

当我跑步的时候

sudo dpkg-reconfigure mysql-server-5.7

我有

update-alternatives: warning: forcing reinstallation of alternative 
/etc/mysql/mysql.cnf because link group my.cnf is broken
update-alternatives: warning: not replacing /etc/mysql/my.cnf with a link
mysql_upgrade: [ERROR] unknown option '--~'
Upgrade process encountered error and will not continue.

 mysql -u root -p

结果是

Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

编辑2

我尝试了以下操作并得到了相关结果。

ubuntu@ip-10-0-1-73:~$ sudo apt autoremove --purge mysql-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 linux-headers-generic : Depends: linux-headers-4.4.0-119-generic but it is     not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify     a solution).
ubuntu@ip-10-0-1-73:~$ sudo apt install mysql-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 linux-headers-generic : Depends: linux-headers-4.4.0-119-generic but it is     not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify     a solution).

跑步

apt-get -f install

没有改变任何东西。

编辑3:

如果我使用

sudo mysql -u root

它会带我进入 mysql。如果我使用 -p 开关,它会接受任何密码。但是,当我输入

mysql> UPDATE mysql.user SET authentication_string=PASSWORD('newPassword') WHERE User='root';

我明白了

Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

Warning (Code 1681): 'PASSWORD' is deprecated and will be removed in a future release.

然后我进入

FLUSH PRIVILEGES;

并收到以下消息

Query OK, 0 rows affected (0.00 sec)

然后我退出,如果我不使用 sudo,仍然会得到相同的结果

 mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

编辑4:

我尝试了以下操作并得到了以下结果。

$ sudo apt install linux-headers-4.4.0-119-generic
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 linux-headers-4.4.0-119-generic : Depends: linux-headers-4.4.0-119 but it     is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
ubuntu@ip-10-0-1-73:~$ sudo apt-get -f install linux-headers-4.4.0-119-    generic
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 linux-headers-4.4.0-119-generic : Depends: linux-headers-4.4.0-119 but it     is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
$ sudo apt-get -f install
Reading package lists... Done
Building dependency tree
Reading state information... Done
Correcting dependencies... Done
The following packages were automatically installed and are no longer     required:
  linux-headers-4.4.0-64 linux-headers-4.4.0-64-generic linux-headers-4.4.0-    66 linux-headers-4.4.0-66-generic linux-headers-4.4.0-70 linux-headers-4.4.0-70-generic
  linux-headers-4.4.0-71 linux-headers-4.4.0-71-generic linux-headers-4.4.0-    72 linux-headers-4.4.0-72-generic linux-headers-4.4.0-75 linux-headers-4.4.0-75-    generic
  linux-headers-4.4.0-78 linux-headers-4.4.0-78-generic linux-headers-4.4.0-    79 linux-headers-4.4.0-79-generic linux-headers-4.4.0-81 linux-headers-4.4.0-81-generic
  linux-headers-4.4.0-83 linux-headers-4.4.0-83-generic linux-headers-4.4.0-87 linux-headers-4.4.0-87-generic linux-headers-4.4.0-89 linux-headers-4.4.0-89-generic
  linux-headers-4.4.0-91 linux-headers-4.4.0-91-generic linux-headers-4.4.0-92 linux-headers-4.4.0-92-generic linux-image-4.4.0-112-generic
  linux-image-4.4.0-64-generic linux-image-4.4.0-66-generic linux-image-    4.4.0-70-generic linux-image-4.4.0-71-generic linux-image-4.4.0-72-generic
  linux-image-4.4.0-75-generic linux-image-4.4.0-78-generic linux-image-4.4.0-79-generic linux-image-4.4.0-81-generic linux-image-4.4.0-83-generic
  linux-image-4.4.0-87-generic linux-image-4.4.0-89-generic linux-image-4.4.0-91-generic linux-image-4.4.0-92-generic linux-image-4.4.0-97-generic
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  linux-headers-4.4.0-119 linux-headers-4.4.0-119-generic
The following NEW packages will be installed:
  linux-headers-4.4.0-119 linux-headers-4.4.0-119-generic
0 upgraded, 2 newly installed, 0 to remove and 224 not upgraded.
10 not fully installed or removed.
Need to get 0 B/10.8 MB of archives.
After this operation, 78.3 MB of additional disk space will be used.
Do you want to continue? [Y/n]
(Reading database ... 512472 files and directories currently installed.)
Preparing to unpack .../linux-headers-4.4.0-119_4.4.0-119.143_all.deb ...
Unpacking linux-headers-4.4.0-119 (4.4.0-119.143) ...
dpkg: error processing archive /var/cache/apt/archives/linux-headers-4.4.0-    119_4.4.0-119.143_all.deb (--unpack):
 unable to create '/usr/src/linux-headers-4.4.0-    119/include/linux/cpufeature.h.dpkg-new' (while processing './usr/src/linux-        headers-4.4.0-119/include/linux/cpufeature.h'): No space left on device
No apport report written because the error message indicates a disk full     error
                                                                                      dpkg-deb: error: subprocess paste was killed by signal (Broken pipe)
Preparing to unpack .../linux-headers-4.4.0-119-generic_4.4.0-    119.143_amd64.deb ...
Unpacking linux-headers-4.4.0-119-generic (4.4.0-119.143) ...
dpkg: error processing archive /var/cache/apt/archives/linux-headers-4.4.0-    119-generic_4.4.0-119.143_amd64.deb (--unpack):
 unable to create '/usr/src/linux-headers-4.4.0-119-    generic/include/config/rtc/drv/88pm860x.h.dpkg-new' (while processing     './usr/src/linux-headers-4.4.0-119-generic/include/config/rtc/drv/88pm860x.h'):     No space left on device
No apport report written because the error message indicates a disk full     error
                                                                                  dpkg-deb: error: subprocess paste was killed by signal (Broken pipe)
Errors were encountered while processing:
 /var/cache/apt/archives/linux-headers-4.4.0-119_4.4.0-119.143_all.deb
 /var/cache/apt/archives/linux-headers-4.4.0-119-generic_4.4.0-    119.143_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

$df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev              498080       0    498080   0% /dev
tmpfs             101440   12068     89372  12% /run
/dev/xvda1       8117828 5515716   2166836  72% /
tmpfs             507184       0    507184   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs             507184       0    507184   0% /sys/fs/cgroup
tmpfs             101444       0    101444   0% /run/user/1000

答案1

要重置 MySQL 密码,请按照“B.5.3.2.2 重置 Root 密码:Unix 和类 Unix 系统“:

通过向进程发送正常kill(非kill -9)来 停止 MySQL 服务器mysqld。在以下命令中使用 .pid 文件的实际路径名:

kill `cat /mysql-data-directory/host_name.pid`

在 cat 命令中使用反引号(而不是正引号)。这会导致 cat 的输出被替换到 kill 命令中。

创建一个文本文件,其中包含一行密码分配语句。将密码替换为您要使用的密码。

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

保存文件。本示例假设您将文件命名为 /home/me/mysql-init。该文件包含密码,因此不要将其保存在其他用户可以读取的地方。如果您不是以 l(服务器运行时的用户)身份登录,请确保该文件具有允许读取它mysq的权限。mysql

使用特殊选项启动 MySQL 服务器--init-file

mysqld --init-file=/home/me/mysql-init &

服务器--init-file 在启动时执行该选项指定的文件内容,改变'root'@'localhost'账户密码。

可能还需要其他选项,具体取决于您通常如何启动服务器。例如,--defaults-fil在 之前可能需要 e --init-file

服务器启动成功后,删除/home/me/mysql-init

答案2

首先,让我们修复你的包管理器问题。使用以下命令安装缺少的依赖项:

sudo apt install -f

此后,如果您已经卸载mysql-server,请重新安装:

sudo apt install mysql-server

如果您尚未卸载它,或者重新安装它没有提示您输入新的 MySQL 根密码,现在运行(如有必要,调整版本号):

sudo dpkg-reconfigure mysql-server-5.7

现在应该已经重置了你的密码。尝试使用交互式密码提示以 root 身份再次登录:

mysql -u root -p

如果仍然出现错误

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

这很可能意味着 MySQL 服务器没有运行。尝试使用以下命令启动它:

sudo systemctl restart mysql

此后,systemctl status mysql应报告为“活动(运行)”。然后尝试再次登录。

答案3

终于整理好了。

首先,找到并删除 my.cnf.bak 的所有实例。还删除了 /etc/alternatives/my.cnf、/var/lib/dpkg/alternatives/my.cnf 和 /etc/mysql/conf.d(但备份了它们)。然后

sudo apt-get remove --purge mysql-\*
sudo apt-get install mysql-server mysql-client

如果你仍然得到类似的

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

这通常可以通过以下方式解决

sudo service mysql start

相关内容