【问题标题】:Connection error: No such file or directory连接错误:没有这样的文件或目录
【发布时间】:2020-09-12 03:22:42
【问题描述】:

我在尝试加载与我的数据库有连接的 PHP 文件时收到错误“连接错误:没有这样的文件或目录”。我没有连接到数据库的 PHP 文件加载没有问题。我正在运行 Apache

我在网上找到了一些文章,建议我可能需要编辑我的 php.ini 文件,并尝试将我的 db_Connect 文件中的“localhost”切换到我的公共 IP 地址 - 但是到目前为止还没有运气。

这是我的数据库连接:

<?php 

    // connect to the database
    $conn = mysqli_connect('localhost', 'username', 'password', 'database');

    // check connection
    if(!$conn){
        echo 'Connection error: '. mysqli_connect_error();
    }

?>

以下是今天的一些日志:

[Mon May 25 12:55:06.855800 2020] [php7:warn] [pid 12713] [client 108.24.134.163:59698] PHP Warning:  mysqli_connect(): (HY000/2002): No such file or directory in /home/ubuntu/wescreen/config/db_connect.php on line 4, referer: http://wescreen.tv/index2.php
[Mon May 25 12:55:36.367547 2020] [php7:warn] [pid 12712] [client 108.24.134.163:59712] PHP Warning:  mysqli_connect(): (HY000/2002): No such file or directory in /home/ubuntu/wescreen/config/db_connect.php on line 4, referer: http://wescreen.tv/index2.php

非常感谢任何帮助!

【问题讨论】:

  • 您是否已将文件放入 htdocs 中?
  • 您的数据库凭据是否正确?我不这么认为
  • @FelippeDuarte 我浏览了列出的两个解决方案 1) 在我的 db_connect 上更改 localhost --> 127.0.0.1 2) 我尝试在我的 php.ini 文件中更新以下两行= /tmp/mysql.sock mysqli.default_socket pdo_mysql.default_socket= 有什么想法吗?还是没有运气
  • @RohitSahu 我的数据库凭据是正确的。使用 XAMPP 在我的本地运行一切正常

标签: php apache ubuntu


【解决方案1】:

首先检查您的 /etc/hosts 文件并检查您的站点是否有指向本地主机的 url。应该是这样的

  127.0.0.1 localhost
  127.0.0.1 test.com

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters``

然后,前往/etc/apache2/sites-available 中的文件夹和000-default.conf 文件中,您会发现以下几行:

     <VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

通常,DocumentRoot 是项目目录所在的位置。尝试并验证这是真的。 检查/etc/apache2/ 目录中的文件ports.conf。 你会发现这些行:

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 80

<IfModule ssl_module>
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

通过执行以下命令检查您正在使用的端口是否成功运行:

  netstat -anp | grep apache

完成后,在终端中使用以下命令重新启动 apache 服务:

sudo service apache2 restart

希望你一切顺利。

【讨论】:

  • 感谢@orissin 的回复。我按照您指定的所有指示进行操作,但没有运气。我注意到我的主机文件中唯一不同的是我还有一行:ff02::3 ip6-allhosts 在文件的底部
  • 检查我编辑的答案,它可能会给你一个更好的提示,为什么你的本地主机不工作。
  • @orinsin 再次感谢您的回复。我的 ports.conf 文件与您上面的内容相匹配。当我运行命令时,我看到以下内容:tcp6 0 0 :::80 :::* LISTEN 14027/apache2
猜你喜欢
  • 2021-04-16
  • 2021-10-31
  • 1970-01-01
  • 2021-12-27
  • 2013-08-08
  • 2013-11-04
  • 2012-02-18
  • 2012-10-21
  • 2011-03-22
相关资源
最近更新 更多