我相信我已经成功地部署了我的(非常基本的)网站到fortrabbit,但只要我连接到SSH运行一些命令(如php artisan migrate或php artisan db:seed),我得到一个错误消息:

[PDOException]
SQLSTATE[HY000] [2002] No such file or directory

在某种程度上,迁移一定是有效的,因为我的表在那里——但这并不能解释为什么它现在不适合我。


当前回答

我在访问Drupal网站时也遇到了类似的问题。我通过打开命令行,重新启动我的MySQL服务器或服务来修复它:

service mysqld restart

这应该有用。如果没有,重启你的本地web服务器:

service httpd restart

这应该足够了。希望它也适用于其他环境。注意,这些命令通常需要超级用户权限。

其他回答

我的答案是针对Laravel的。

在database.php配置文件中创建到本地Docker MySQL服务的新连接并将其设置为默认连接后,我收到了这条消息。我忘记了我通过在模型中覆盖它来设置一个不同的连接:

class Model extends \Illuminate\Database\Eloquent\Model
{
    protected $connection;

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->connection = 'some_other_connection';
    }
...

因此,即使我在database.php文件中的默认连接指向正确的凭据,模型仍然使用我从本地环境文件中删除的远程数据库连接配置。

我遇到了同样的问题,我运行的是Mac OS X 10.10 Yosemite。我已经启用了操作系统自带的Apache服务器和PHP。然后,我刚刚配置了mCrypt库开始。在那之后,当我使用模型和DB时,我得到了一个错误:

[PDOException]
SQLSTATE[HY000] [2002] No such file or directory

我发现的原因只是因为PHP和MySQL不能自己连接。 为了解决这个问题,我遵循以下步骤:

Open a terminal and connect to the mysql with: mysql -u root -p It will ask you for the related password. Then once you get the mysql promt type the next command: mysql> show variables like '%sock%' You will get something like this: +-----------------------------------------+-----------------+ | Variable_name | Value | +-----------------------------------------+-----------------+ | performance_schema_max_socket_classes | 10 | | performance_schema_max_socket_instances | 322 | | socket | /tmp/mysql.sock | +-----------------------------------------+-----------------+ Keep the value of the last row: /tmp/mysql.sock In your laravel project folder, look for the database.php file there is where you configure the DB connection parameters. In the mysql section add the next line at the end: 'unix_socket' => '/tmp/mysql.sock' You must have something like this: 'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'SchoolBoard', 'username' => 'root', 'password' => 'venturaa', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'unix_socket' => '/tmp/mysql.sock', ),

现在只需保存更改,并重新加载页面,它必须工作!

步骤1

找到你的unix_socket的路径,只需运行netstat -ln | grep mysql

你应该得到这样的东西

unix  2      [ ACC ]     STREAM     LISTENING     17397    /var/run/mysqld/mysqld.sock

步骤2

将其添加到unix_socket参数中

'mysql' => array(
            'driver'    => 'mysql',
            'host'      => '67.25.71.187',
            'database'  => 'dbname',
            'username'  => 'username',
            'password'  => '***',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'unix_socket'    => '/var/run/mysqld/mysqld.sock' <-----
            ),
        ),

希望能有所帮助!!

错误消息表明尝试通过套接字进行MySQL连接(不支持)。

在Laravel (artisan)环境中,您可能需要使用不同/正确的环境。例如:php artisan migrate——env=生产(或任何环境)。在这里看到的。

要添加到@alexventuraio的解决方案中,如果你在Laravel 5.8上遇到了这个问题,你可能想要替换数据库配置文件中'unix_socket'的值,如下:

 'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        // 'unix_socket' =>'/var/run/mysqld/mysqld.sock',
        'unix_socket' => env('DB_SOCKET', ''), // replace with this line, so you can always add the mysqld.sock from the dotenv file
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => false,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],