可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I believe that I've successfully deployed my (very basic) site to fortrabbit, but as soon as I connect to SSH to run some commands (such as php artisan migrate
or php artisan db:seed
) I get an error message:
[PDOException] SQLSTATE[HY000] [2002] No such file or directory
At some point the migration must have worked, because my tables are there - but this doesn't explain why it isn't working for me now.
回答1:
The error message indicates that a MySQL connection via socket is tried (which is not supported).
In the context of Laravel (artisan), you probably want to use a different / the correct environment. Eg: php artisan migrate --env=production
(or whatever environment). See here.
回答2:
Laravel 4: Change "host" in the app/config/database.php
file from "localhost" to "127.0.0.1"
Laravel 5: Change "DB_HOST" in the .env
file from "localhost" to "127.0.0.1"
I had the exact same problem. None of the above solutions worked for me. I solved the problem by changing the "host" in the /app/config/database.php file from "localhost" to "127.0.0.1".
Not sure why "localhost" doesn't work by default but I found this answer in a similar question solved in a symfony2 post. https://stackoverflow.com/a/9251924/1231563
Update: Some people have asked as to why this fix works so I have done a little bit of research into the topic. It seems as though they use different connection types as explained in this post https://stackoverflow.com/a/9715164/1231563
The issue that arose here is that "localhost" uses a UNIX socket and can not find the database in the standard directory. However "127.0.0.1" uses TCP (Transmission Control Protocol), which essentially means it runs through the "local internet" on your computer being much more reliable than the UNIX socket in this case.
回答3:
I got the same problem and I'm running Mac OS X 10.10 Yosemite. I have enabled the Apache Server and PHP that already comes with the OS. Then I just configured the mCrypt library to get started. After that when I was working with models and DB I got the error:
[PDOException] SQLSTATE[HY000] [2002] No such file or directory
The reason I found is just because PHP and MySQL can't get connected themselves. To get this problem fixed, I follow the next steps:
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', ),
Now just save changes, and reload the page and it must work! I hope it could be useful!
回答4:
I encountered the [PDOException] SQLSTATE[HY000] [2002] No such file or directory
error for a different reason. I had just finished building a brand new LAMP stack on Ubuntu 12.04 with Apache 2.4.7, PHP v5.5.10 and MySQL 5.6.16. I moved my sites back over and fired them up. But, I couldn't load my Laravel 4.2.x based site because of the [PDOException]
above. So, I checked php -i | grep pdo
and noticed this line:
pdo_mysql.default_socket => /tmp/mysql.sock => /tmp/mysql.sock
But, in my /etc/my.cnf the sock file is actually in /var/run/mysqld/mysqld.sock
.
So, I opened up my php.ini and set the value for pdo_mysql.default_socket
:
pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock
Then, I restarted apache and checked php -i | grep pdo
:
pdo_mysql.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock
That fixed it for me.
回答5:
The answer from @stuyam solved the "No such file or directory" issue for me
Short answer: Change "host" in the /app/config/database.php file from "localhost" to "127.0.0.1"
But then I had a "Connection refused" error. If anyone had the same issue, my solution for this was to update the app/config/local/database.php file so the port is 8889:
'mysql' => array( 'driver' => 'mysql', 'host' => '127.0.0.1', 'port' => '8889', 'database' => 'databaseName', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ),
回答6:
If you are using Laravel Homestead, make sure you're calling the commands on the server.
homestead ssh
Then simply cd to the right directory and fire your command there.
回答7:
Mamp user enable option Allow network access to MYSQL

回答8:
Add mysql.sock path in database.php file like below example
'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'),
回答9:
In my case i had no problem at all, just forgot to start the mysql service...
sudo service mysqld start
回答10:
Step 1
Find the path to your unix_socket, to do that just run netstat -ln | grep mysql
You should get something like this
unix 2 [ ACC ] STREAM LISTENING 17397 /var/run/mysqld/mysqld.sock
Step 2
Take that and add it in your unix_socket param
'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' <----- ), ),
Hope it helps !!
回答11:
Building on the answer from @dcarrith ...
Instead of editing the config files, I created an alias in the location that PHP is looking that connects to the real mysql.sock. (source)
Just run these two commands (no restart needed):
mkdir /var/mysql ln -s /tmp/mysql.sock /var/mysql/mysql.sock
回答12:
I'm running on MAMP Pro and had this similar problem when trying to migrate (create db tables). Tried a few of these mentioned suggestions as well but didn't do it for me.
So, simply (after an hour googling), I added two things to the /config/database.php.
'port' => '1234', 'unix_socket' => '/path/to/my/socket/mysqld.sock'
Works fine now!
回答13:
As of Laravel 5 the database username and password goes in the .env file that exists in the project directory, e.g.
DB_HOST=127.0.0.1 DB_DATABASE=db1 DB_USERNAME=user1 DB_PASSWORD=pass1
As you can see these environment variables are overriding the 'forge' strings here so changing them has no effect:
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ],
More information is here https://mattstauffer.co/blog/laravel-5.0-environment-detection-and-environment-variables
回答14:
I ran into this problem when running PHPUnit in Elixir/Gulp, and Homestead as my Vagrant enviroment.
In my case I edited the .env file from DB_HOST=localhost
to DB_HOST=192.168.10.10
where 192.168.10.10
is the IP of my Vagrant/Homestead host.
回答15:
This is because PDO treats "localhost" host specially:
Note: Unix only: When the host name is set to "localhost", then the connection to the server is made thru a domain socket. If PDO_MYSQL is compiled against libmysqlclient then the location of the socket file is at libmysqlclient's compiled in location. If PDO_MYSQL is compiled against mysqlnd a default socket can be set thru the pdo_mysql.default_socket setting.
(from http://php.net/manual/en/ref.pdo-mysql.connection.php)
Changing localhost to 127.0.0.1 will "force" the use of TCP.
Note: mysqli_connect is working fine with localhost.
回答16:
If you are using Laravel Homestead, here is settings
(include Vagrant-Virtual Machine)
.bash-profile
alias vm="ssh vagrant@127.0.0.1 -p 2222"
database.php
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'database' => env('DB_DATABASE', 'homestead'), 'username' => env('DB_USERNAME', 'homestead'), 'password' => env('DB_PASSWORD', 'secret'), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ],
Terminal
vm vagrant@homestead:~/Code/projectFolder php artisan migrate:install
回答17:
Attempt to connect to localhost:
SQLSTATE[HY000] [2002] No such file or directory
Attempt to connect to 127.0.0.1:
SQLSTATE[HY000] [2002] Connection refused
OK, just comment / remove the following setting from my.cnf (on OS X 10.5: /opt/local/etc/mysqlxx/my.cnf
) to obtain:
[mysqld] # skip-networking
Of course, stop and start MySQL Server.
回答18:
If anyone are still looking for the answer, just check your .env file. For some reason laravel create a .env.example file, so all this answers didn't work for me. I fixed my issue renamming .env.example to .env
回答19:
it's the correct response for me. I disable the option pdo_mysql.default_socket in my php.ini and I design the host at 1.27.0.0.1
回答20:
When using a VirtualMachine make sure you ssh into that machine and navigate to your App folder and call the php artisan migrate command from there.
回答21:
I had problems accessing my drupal website and I had a problem exactly like that I fixed it within less than 2 hours. Try on your command line and restart your mysql server or service. Type this when logged in as root or super user, - service mysqld restart
It should work. If it doesn't restart your local webserver, - service httpd restart
That should be enough. Hope it works for other environments too. Cheers.
回答22:
In may case, I'd simply used
vagrant up
instead of
homestead up
for my forge larval setup using homestead. I'm assuming this meant the site was getting served, but the MySQL server wasn't ever booted. When I used the latter command to launch my vagrant box, the error went away.
回答23:
All these answers seem like heavy lifting...
I just created a .env
file; edited my bootstrap/app.php
file, and uncommented the following line...
Dotenv::load(__DIR__.'/../');
Hope this helps someone
回答24:
[This is outdated, database info is stored in .env now] More easier way to solve the issue by is editing the
app/config/local/database.php
:)
Change the database configuration here and it should work
回答25:
This happened to me because MySQL wasn't running. MySQL was failing to start because I had a missing /usr/local/etc/my.cnf.d/
directory.
This was being required by my /usr/local/etc/my.cnf
config file as a glob include (include /usr/local/etc/my.cnf.d/*.cnf
).
Running mkdir /usr/local/etc/my.cnf.d
, and then starting MySQL, fixed the issue.
回答26:
Check your port carefully . In my case it was 8889 and i am using 8888. change "DB_HOST" from "localhost" to "127.0.0.1" and vice versa