无法访问本地主机 mysql 服务器。错误 2002 (HY000):无法通过套接字“/var/run/mysqld/mysqld.sock”连接到本地 MySQL 服务器

无法访问本地主机 mysql 服务器。错误 2002 (HY000):无法通过套接字“/var/run/mysqld/mysqld.sock”连接到本地 MySQL 服务器

我继承了一台安装了 mysql 的 ubuntu 机器,但我不知道该怎么做。我的问题是我无法访问 mysql。

这是我的主人的一些信息:

cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.1 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.1 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

MySQl 版本mysql Ver 8.0.27-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))

这是错误信息:

$ mysql -u root -p
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

---

$ service mysqld start
Failed to start mysqld.service: Unit mysqld.service not found.

该文件/var/run/mysqld/mysqld.sock不存在。

编辑更多信息

$ netstat -tlpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      99/systemd-resolved
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      136/sshd: /usr/sbin
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      303/master
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      313/sshd: root@pts/
tcp6       0      0 :::80                   :::*                    LISTEN      138/apache2
tcp6       0      0 :::22                   :::*                    LISTEN      136/sshd: /usr/sbin
tcp6       0      0 ::1:25                  :::*                    LISTEN      303/master
tcp6       0      0 ::1:6010                :::*                    LISTEN      313/sshd: root@pts/
$ systemctl start mysql
Job for mysql.service failed because the control process exited with error code.
See "systemctl status mysql.service" and "journalctl -xe" for details.
$ systemctl status mysql.service
* mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; disabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Thu 2022-01-27 08:32:50 CET; 10s ago
    Process: 5687 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
    Process: 5696 ExecStart=/usr/sbin/mysqld (code=exited, status=1/FAILURE)
   Main PID: 5696 (code=exited, status=1/FAILURE)
     Status: "Server startup in progress"
      Error: 2 (No such file or directory)

Jan 27 08:32:50 osticket systemd[1]: mysql.service: Main process exited, code=exited, status=1/FAILURE
Jan 27 08:32:50 osticket systemd[1]: mysql.service: Failed with result 'exit-code'.
Jan 27 08:32:50 osticket systemd[1]: Failed to start MySQL Community Server.
Jan 27 08:32:50 osticket systemd[1]: mysql.service: Scheduled restart job, restart counter is at 5.
Jan 27 08:32:50 osticket systemd[1]: Stopped MySQL Community Server.
Jan 27 08:32:50 osticket systemd[1]: mysql.service: Start request repeated too quickly.
Jan 27 08:32:50 osticket systemd[1]: mysql.service: Failed with result 'exit-code'.
Jan 27 08:32:50 osticket systemd[1]: Failed to start MySQL Community Server.
$ pwd && ls
/var/log
alternatives.log       apache2        auth.log.4.gz  dpkg.log.1     lastlog        mysqld.err            php7.4-fpm.log.2.gz  php7.4-fpm.log.8.gz  syslog.3.gz           wtmp
alternatives.log.1     apt            btmp           dpkg.log.2.gz  mail.log       php7.4-fpm.log        php7.4-fpm.log.3.gz  php7.4-fpm.log.9.gz  syslog.4.gz           wtmp.1
alternatives.log.2.gz  auth.log       btmp.1         dpkg.log.3.gz  mail.log.1     php7.4-fpm.log.1      php7.4-fpm.log.4.gz  private              syslog.5.gz
alternatives.log.3.gz  auth.log.1     dist-upgrade   dpkg.log.4.gz  mail.log.2.gz  php7.4-fpm.log.10.gz  php7.4-fpm.log.5.gz  syslog               syslog.6.gz
alternatives.log.4.gz  auth.log.2.gz  dmesg          faillog        mail.log.3.gz  php7.4-fpm.log.11.gz  php7.4-fpm.log.6.gz  syslog.1             syslog.7.gz
alternatives.log.5.gz  auth.log.3.gz  dpkg.log       journal        mail.log.4.gz  php7.4-fpm.log.12.gz  php7.4-fpm.log.7.gz  syslog.2.gz          ubuntu-advantage.log
$ cat /var/log/mysqld.err
2022-01-26T12:23:07.952718Z 0 [ERROR] [MY-010083] [Server] --verbose is for use with --help; did you mean --log-error-verbosity?
2022-01-26T12:23:07.952814Z 0 [ERROR] [MY-010095] [Server] Failed to access directory for --secure-file-priv. Please make sure that directory exists and is accessible by MySQL Server. Supplied value : /var/lib/mysql-files
2022-01-26T12:23:07.953735Z 0 [ERROR] [MY-010119] [Server] Aborting
$ namei -l /var/lib/mysql-files
f: /var/lib/mysql-files
drwxr-xr-x root root /
drwxr-xr-x root root var
drwxr-xr-x root root lib
                     mysql-files - No such file or directory

我尝试了很多方法都无法解决。有人能帮我吗?

谢谢。

答案1

您的 mysqld 没有启动并在错误日志中显示以下错误:

无法访问 --secure-file-priv 的目录。请确保该目录存在且可供 MySQL 服务器访问。提供的值:/var/lib/mysql-files

/var/lib/mysql-files缺少,因此您需要创建它。

sudo install -o mysql -g mysql -m 0700 -d /var/lib/mysql-files

之后您应该能够使用 启动 mysqld systemctl start mysql

答案2

netstat -tlpn- 是mysql在端口 3306 上运行吗?还是在其他地方?默认情况下,与本地主机的连接是通过套接字尝试添加的--protocol 3306- 请参阅如何强制 MySQL 通过 TCP 而不是 Unix 套接字进行连接?- 然后,ps ax | grep mysql然后lsof -p <mysqld pid>查找 IPv4 或 IPv6 或套接字 - 套接字可能放置在与客户端期望的位置不同的位置。

答案3

该服务名称是mysqlDebian/Ubuntu 中的,因此您需要使用它systemctl start mysql来启动服务器。

相关内容