我相信我已经成功地部署了我的(非常基本的)网站到fortrabbit,但只要我连接到SSH运行一些命令(如php artisan migrate或php artisan db:seed),我得到一个错误消息:
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
在某种程度上,迁移一定是有效的,因为我的表在那里——但这并不能解释为什么它现在不适合我。
我相信我已经成功地部署了我的(非常基本的)网站到fortrabbit,但只要我连接到SSH运行一些命令(如php artisan migrate或php artisan db:seed),我得到一个错误消息:
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
在某种程度上,迁移一定是有效的,因为我的表在那里——但这并不能解释为什么它现在不适合我。
当前回答
在我的情况下,我没有任何问题,只是忘记启动mysql服务…
sudo service mysqld start
其他回答
添加mysql。袜子路径在database.php文件如下示例
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
Eample
'mysql' => [
'driver' => 'mysql',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '8889'),
我遇到了同样的问题,我运行的是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', ),
现在只需保存更改,并重新加载页面,它必须工作!
对于任何试图创建一个新的db连接而不是在laravel上,而是在这里寻找从终端运行PDO的答案的人。这对你有帮助。你可以重构它,使其最适合你。
<?php
class db
{
private $DBHOST = 'localhost'; // you don't need 127.0.0.1
private $DRIVER = 'mysql';
private $PORT = '8888'; // database port. 8888 is mine
private $DB = 'example-db';
private $PASS = 'example-pass';
private $USER = 'root';
private $SOCKS = ''; // can fill this or leave blank.
// - connect (dummy connection)
private function con()
{
if ($this->SOCKS == '')
{
// run shell command to get
$socks = shell_exec('netstat -ln | grep mysql');
$socks = trim(substr($socks, strpos($socks, '/')));
$this->SOCKS = strlen($socks) > 0 ? ';unix_socket='.$socks : '';
}
else
{
$this->SOCKS = ';unix_socket='.$this->SOCKS;
}
$dsn = $this->DRIVER.':host='.$this->DBHOST.';dbname='.$this->DB;
// add socks
$dsn .= $this->SOCKS;
// add port
$dsn .= (strlen($this->PORT) > 0) ? ';port='.$this->PORT : '';
// extablish connection
$con = new PDO($dsn, $user, $pass);
// return PDO instance.
return $con;
}
// - ends here
// now you can call $this->con() within class to use connection
// would run fine on any terminal
}
希望能有所帮助!
基于@dcarrith的回答…
我没有编辑配置文件,而是在PHP正在查找的位置中创建了一个别名,该别名连接到真正的mysql.sock。(源)
只需运行这两个命令(不需要重新启动):
mkdir /var/mysql
ln -s /tmp/mysql.sock /var/mysql/mysql.sock
尝试连接到本地主机:
SQLSTATE[HY000] [2002] No such file or directory
尝试连接127.0.0.1:
SQLSTATE[HY000] [2002] Connection refused
好的,只需在my.cnf (OS X 10.5: /opt/local/etc/mysqlxx/my.cnf)中注释/删除以下设置即可获得:
[mysqld]
# skip-networking
当然,停止和启动MySQL服务器。