Reputation: 6681
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.
Upvotes: 474
Views: 840252
Reputation: 352
If you're using the sail
you should set DB_HOST=mysql
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=<your_database>
DB_USERNAME=sail
DB_PASSWORD=password
Upvotes: 1
Reputation: 161
For me I hit this error when testing a remote mysql server using ssl and had forgotten to setup the database connections with the ssl certificate.
Upvotes: 1
Reputation: 261
In my case I had to remove the bootstrap/cache folder and try it again.
My cenario was after a server migration.
UPDATED AND WORKING
That's all.
Upvotes: 1
Reputation: 10049
One of simplest reasons for this error is that a MySQL server is not running. So verify that first. In case it's up, proceed to other recommendations:
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
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
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.
Upvotes: 904
Reputation: 21
Check your MySQL server is running, if database connection is correct configured.
Upvotes: 0
Reputation: 598
Just i do one change in .env file
I have following line of code.
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=root
DB_PASSWORD=
Change host name localhost to 127.0.0.1
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=root
DB_PASSWORD=
That is work in my case because that can't find any hostname like localhost
And after changing hostname write following command
php artisan config:clear
php artisan migrate:install
php artisan migrate
Upvotes: 10
Reputation: 1626
All the existing answers are correct and might work for you.
My case was a bit different. I encountered this error when I started a docker project locally using docker-compose up
.
The problem for my specific case was that I ran the docker-compose up
from a sub-folder instead of the root folder in which the docker-compose file lies.
Make sure you run docker-compose up
from the folder where the docker-compose.yml
is saved to!
Upvotes: 0
Reputation: 984
To add to @alexventuraio's solution, if you are experiencing this issue on Laravel 5.8, you might want to replace the value of 'unix_socket' in your database config file as thus:
'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'),
]) : [],
],
Upvotes: 0
Reputation: 1123
solved
in my case it was a logic issue in code, the connection values are in a if statement:
if($_SERVER['HTTP_HOST'] == "localhost")
so the solution was to add a pipe and add 127.0.0.1, that solved the problem for me
if($_SERVER['HTTP_HOST'] == "localhost" || $_SERVER['HTTP_HOST'] == "127.0.0.1")
Upvotes: 2
Reputation: 194
I got the same problem in ubuntu 18.04 with nginx. By following the below steps my issue has been fixd:
First open terminal and enter into mysql CLI. To check mysql socket location I write the following command.
mysql> show variables like '%sock%'
I got something like the below :
+-----------------------------------------+-----------------------------+
| Variable_name | Value |
+-----------------------------------------+-----------------------------+
| mysqlx_socket | /var/run/mysqld/mysqlx.sock |
| performance_schema_max_socket_classes | 10 |
| performance_schema_max_socket_instances | -1 |
| socket | /var/run/mysqld/mysqld.sock |
+-----------------------------------------+-----------------------------+
4 rows in set (0.00 sec)
In laravel project folder, look for the database.php file in the config folder. In the mysql section I modified unix_socket according to the above table.
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database_name',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'unix_socket' => '/var/run/mysqld/mysqld.sock',
),
Now just save changes, and reload the page and it worked.
Upvotes: 3
Reputation: 2728
I had the same problem using Docker and MySQL service name db
in docker_compose.yml file:
I added the following in the .env
file:
DB_HOST=db
you should also assure that your host is discoverable from the php app.
It was because PHP didn't figure out which host to use to connect.
Upvotes: 4
Reputation: 4564
My answer is specific to Laravel.
I had this message after creating a new connection in the database.php
configuration file to a local Docker MySQL service and setting it as the default connection. I forgot that I was setting a different connection by overwriting it in the Model:
class Model extends \Illuminate\Database\Eloquent\Model
{
protected $connection;
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->connection = 'some_other_connection';
}
...
So even if my default connection in the database.php
file was pointing to the right credentials, the model was still using the remote database connection configuration which I had removed from the local environment file.
Upvotes: 0
Reputation: 99
I had similar problems accessing my Drupal website. I fixed it by opening the command line, and restarting my MySQL server or service:
service mysqld restart
This should work. If it doesn't, restart your local webserver:
service httpd restart
That should be enough. Hope it works for other environments, too. Note that these commands generally require superuser privileges.
Upvotes: 4
Reputation: 723
In my case i had no problem at all, just forgot to start the mysql service...
sudo service mysqld start
Upvotes: 30
Reputation: 794
For anyone trying to create a fresh db connection not on laravel but bumped here seeking for answers to run PDO from the Terminal. This would be of help to you. And you can refactor it to work best for you.
<?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
}
hope it helps!
Upvotes: 0
Reputation: 10174
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!
Upvotes: 116
Reputation: 653
I had this problems when I was running my application using docker containers.
The solution was put the name of the MySQL service container I was using in docker_compose.yml
on DB_HOST. In my case, it was db
:
DB_HOST=db
Hope it helps.
Upvotes: 7
Reputation: 6568
It worked after I change from DB_HOST=localhost
to DB_HOST=127.0.0.1
at .env file
Upvotes: 24
Reputation: 12272
In my case, I was running php artisan migrate on my mac terminal, when I needed to ssh into vagrant and run it from there. Hope that helps someone the headache.
Upvotes: 1
Reputation: 22711
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.
Upvotes: 24
Reputation: 377
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
Upvotes: 4
Reputation: 58810
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
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 !!
Upvotes: 15
Reputation: 1439
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.
Upvotes: 1
Reputation: 511
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' => '',
),
Upvotes: 51
Reputation: 1039
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'),
Upvotes: 21
Reputation: 1623
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
Upvotes: 0
Reputation: 1080
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
Upvotes: 1
Reputation: 39
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.
Upvotes: 2
Reputation:
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.
Upvotes: 4