安装 MySQL 时出现问题 - mysqld:无法读取“/etc/mysql/conf.d/”目录(OS errno 13 - 权限被拒绝)

安装 MySQL 时出现问题 - mysqld:无法读取“/etc/mysql/conf.d/”目录(OS errno 13 - 权限被拒绝)

我正在尝试使用以下命令安装 MySQL:

sudo apt install mysql-server

并且我在这个过程结束时收到以下消息:

E: Sub-process /usr/bin/dpkg returned an error code (1)

整个输入/输出:

ubuntu@ubuntu:~$ sudo apt install mysql-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libfprint-2-tod1 libllvm10 libllvm11 linux-headers-5.4.0-40
  linux-headers-5.4.0-40-generic linux-headers-5.4.0-42
  linux-headers-5.4.0-42-generic linux-headers-5.4.0-45
  linux-headers-5.4.0-45-generic linux-headers-5.4.0-47
  linux-headers-5.4.0-47-generic linux-headers-5.4.0-56
  linux-headers-5.4.0-56-generic linux-headers-5.4.0-60
  linux-headers-5.4.0-60-generic linux-headers-5.4.0-65
  linux-headers-5.4.0-65-generic linux-headers-5.4.0-66
  linux-headers-5.4.0-66-generic linux-headers-5.4.0-67
  linux-headers-5.4.0-67-generic linux-headers-5.4.0-70
  linux-headers-5.4.0-70-generic linux-headers-5.4.0-71
  linux-headers-5.4.0-71-generic linux-headers-5.4.0-72
  linux-headers-5.4.0-72-generic linux-headers-5.4.0-73
  linux-headers-5.4.0-73-generic linux-headers-5.4.0-74
  linux-headers-5.4.0-74-generic linux-headers-5.4.0-77
  linux-headers-5.4.0-77-generic linux-headers-5.4.0-80
  linux-headers-5.4.0-80-generic linux-image-5.4.0-40-generic
  linux-image-5.4.0-42-generic linux-image-5.4.0-45-generic
  linux-image-5.4.0-47-generic linux-image-5.4.0-56-generic
  linux-image-5.4.0-60-generic linux-image-5.4.0-65-generic
  linux-image-5.4.0-66-generic linux-image-5.4.0-67-generic
  linux-image-5.4.0-70-generic linux-image-5.4.0-71-generic
  linux-image-5.4.0-72-generic linux-image-5.4.0-73-generic
  linux-image-5.4.0-74-generic linux-image-5.4.0-77-generic
  linux-image-5.4.0-80-generic linux-modules-5.4.0-40-generic
  linux-modules-5.4.0-42-generic linux-modules-5.4.0-45-generic
  linux-modules-5.4.0-47-generic linux-modules-5.4.0-56-generic
  linux-modules-5.4.0-60-generic linux-modules-5.4.0-65-generic
  linux-modules-5.4.0-66-generic linux-modules-5.4.0-67-generic
  linux-modules-5.4.0-70-generic linux-modules-5.4.0-71-generic
  linux-modules-5.4.0-72-generic linux-modules-5.4.0-73-generic
  linux-modules-5.4.0-74-generic linux-modules-5.4.0-77-generic
  linux-modules-5.4.0-80-generic linux-modules-extra-5.4.0-40-generic
  linux-modules-extra-5.4.0-42-generic linux-modules-extra-5.4.0-45-generic
  linux-modules-extra-5.4.0-47-generic linux-modules-extra-5.4.0-56-generic
  linux-modules-extra-5.4.0-60-generic linux-modules-extra-5.4.0-65-generic
  linux-modules-extra-5.4.0-66-generic linux-modules-extra-5.4.0-67-generic
  linux-modules-extra-5.4.0-70-generic linux-modules-extra-5.4.0-71-generic
  linux-modules-extra-5.4.0-72-generic linux-modules-extra-5.4.0-73-generic
  linux-modules-extra-5.4.0-74-generic linux-modules-extra-5.4.0-77-generic
  linux-modules-extra-5.4.0-80-generic
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libevent-pthreads-2.1-7 mysql-client-8.0 mysql-client-core-8.0
  mysql-server-8.0 mysql-server-core-8.0
Suggested packages:
  mailx tinyca
The following NEW packages will be installed:
  libevent-pthreads-2.1-7 mysql-client-8.0 mysql-client-core-8.0 mysql-server
  mysql-server-8.0 mysql-server-core-8.0
0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 24.1 MB of archives.
After this operation, 204 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://security.ubuntu.com/ubuntu focal-security/main amd64 mysql-client-core-8.0 amd64 8.0.26-0ubuntu0.20.04.2 [4222 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal/main amd64 libevent-pthreads-2.1-7 amd64 2.1.11-stable-1 [7372 B]
Get:3 http://security.ubuntu.com/ubuntu focal-security/main amd64 mysql-client-8.0 amd64 8.0.26-0ubuntu0.20.04.2 [22.0 kB]
Get:4 http://security.ubuntu.com/ubuntu focal-security/main amd64 mysql-server-core-8.0 amd64 8.0.26-0ubuntu0.20.04.2 [18.5 MB]
Get:5 http://security.ubuntu.com/ubuntu focal-security/main amd64 mysql-server-8.0 amd64 8.0.26-0ubuntu0.20.04.2 [1329 kB]
Get:6 http://security.ubuntu.com/ubuntu focal-security/main amd64 mysql-server all 8.0.26-0ubuntu0.20.04.2 [9540 B]
Fetched 24.1 MB in 5s (4936 kB/s) 
Preconfiguring packages ...
Selecting previously unselected package mysql-client-core-8.0.
(Reading database ... 881128 files and directories currently installed.)
Preparing to unpack .../0-mysql-client-core-8.0_8.0.26-0ubuntu0.20.04.2_amd64.de
b ...
Unpacking mysql-client-core-8.0 (8.0.26-0ubuntu0.20.04.2) ...
Selecting previously unselected package mysql-client-8.0.
Preparing to unpack .../1-mysql-client-8.0_8.0.26-0ubuntu0.20.04.2_amd64.deb ...
Unpacking mysql-client-8.0 (8.0.26-0ubuntu0.20.04.2) ...
Selecting previously unselected package libevent-pthreads-2.1-7:amd64.
Preparing to unpack .../2-libevent-pthreads-2.1-7_2.1.11-stable-1_amd64.deb ...
Unpacking libevent-pthreads-2.1-7:amd64 (2.1.11-stable-1) ...
Selecting previously unselected package mysql-server-core-8.0.
Preparing to unpack .../3-mysql-server-core-8.0_8.0.26-0ubuntu0.20.04.2_amd64.de
b ...
Unpacking mysql-server-core-8.0 (8.0.26-0ubuntu0.20.04.2) ...
Selecting previously unselected package mysql-server-8.0.
Preparing to unpack .../4-mysql-server-8.0_8.0.26-0ubuntu0.20.04.2_amd64.deb ...
grep: /etc/mysql/: No such file or directory
Unpacking mysql-server-8.0 (8.0.26-0ubuntu0.20.04.2) ...
Selecting previously unselected package mysql-server.
Preparing to unpack .../5-mysql-server_8.0.26-0ubuntu0.20.04.2_all.deb ...
Unpacking mysql-server (8.0.26-0ubuntu0.20.04.2) ...
Setting up mysql-client-core-8.0 (8.0.26-0ubuntu0.20.04.2) ...
Setting up mysql-client-8.0 (8.0.26-0ubuntu0.20.04.2) ...
Setting up libevent-pthreads-2.1-7:amd64 (2.1.11-stable-1) ...
Setting up mysql-server-core-8.0 (8.0.26-0ubuntu0.20.04.2) ...
Setting up mysql-server-8.0 (8.0.26-0ubuntu0.20.04.2) ...

Configuration file '/etc/mysql/mysql.conf.d/mysqld.cnf'
 ==> Deleted (by you or by a script) since installation.
 ==> Package distributor has shipped an updated version.
   What would you like to do about it ?  Your options are:
    Y or I  : install the package maintainer's version
    N or O  : keep your currently-installed version
      D     : show the differences between the versions
      Z     : start a shell to examine the situation
 The default action is to keep your current version.
*** mysqld.cnf (Y/I/N/O/D/Z) [default=N] ? 
update-alternatives: error: alternative path /etc/mysql/mysql.cnf doesn't exist
dpkg: error processing package mysql-server-8.0 (--configure):
 installed mysql-server-8.0 package post-installation script subprocess returned
 error exit status 2
No apport report written because the error message indicates its a followup erro
r from a previous failure.
                          dpkg: dependency problems prevent configuration of mys
ql-server:
 mysql-server depends on mysql-server-8.0; however:
  Package mysql-server-8.0 is not configured yet.

dpkg: error processing package mysql-server (--configure):
 dependency problems - leaving unconfigured
Processing triggers for systemd (245.4-4ubuntu3.11) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for ureadahead (0.100.0-21) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...
Errors were encountered while processing:
 mysql-server-8.0
 mysql-server
E: Sub-process /usr/bin/dpkg returned an error code (1)

当我运行时sudo mysql,出现此错误:

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

当我运行时systemctl status mysql,我得到这个输出:

mysql.service - MySQL Community Server

Loaded: loaded (/lib/systemd/system/mysql.service; disabled; ve>

Active: inactive (dead)

任何意见,将不胜感激。

答案1

由于 MySQL 是在过去某个时间点安装的,因此可能存在来自之前安装的空目录或配置文件片段,从而干扰新安装。安装 MySQL 时,它会查找某些目录是否存在。如果不存在,则会创建并填充这些目录。如果存在,则保留这些目录,并期望启动脚本能够找到以前的文件、执行任何升级并从那里继续。

话虽如此,解决这个问题最简单的方法还是——再次——删除 MySQL。以下是完整的步骤列表:

  1. 打开终端或通过 SSH 进入机器
  2. 确保 MySQL 服务没有运行(可能没有运行):
    sudo service mysql stop 
    
  3. 删除与 MySQL 相关的所有内容:
    sudo apt purge mysql-server mysql-common mysql-server-core-* mysql-client-core-*
    
  4. 确保没有数据库文件并且默认数据目录不存在:
    sudo rm -Rf /var/lib/mysql/
    
  5. 确保默认配置目录不存在:
    sudo rm -Rf /etc/mysql/
    
  6. 确保默认日志文件目录已消失:
    sudo rm -Rf /var/log/mysql
    
  7. 重新安装 MySQL:
    sudo apt install mysql-server
    

这应该会给你一个完全可操作的 MySQL 安装

答案2

我在一台旧的 ideapad 上尝试安装 mysql-server_5.7.35-1ubuntu18.04_i386.deb 时遇到了这个错误(“无法读取 '/etc/mysql/conf.d/' 的目录”),之前在旧 ideapad 上安装并删除了 mysql。结果发现 '/etc/mysql' 下没有 '/conf.d/' 目录,但有一个 '/mysql.conf.d/' 目录(其中包含一个 'mysqld.cnf' 文件)。我只需将 '/mysql.conf.d/' 目录重命名为 '/conf.d/',安装便会顺利进行,没有任何错误。不知道为什么。希望这能有所帮助。

相关内容