67

I download from https://github.com/luciddreamz/laravel Laravel for openshift then upload over my repository over github. This code for connect to database not work. The problem is that load variable from .env file that locate in root of project

for solve this problem change.env

# local environment only
#   for production, see .openshift/.env

APP_ENV=APPLICATION_ENV
APP_DEBUG=true
APP_URL=OPENSHIFT_APP_DNS
APP_KEY=OPENSHIFT_SECRET_TOKEN

DB_DRIVER=mysql
DB_HOST=OPENSHIFT_MYSQL_DB_HOST
DB_PORT=OPENSHIFT_MYSQL_DB_PORT
DB_DATABASE=OPENSHIFT_APP_NAME
DB_USERNAME=OPENSHIFT_MYSQL_DB_USERNAME 
DB_PASSWORD=OPENSHIFT_MYSQL_DB_PASSWORD

CACHE_DRIVER=apc
SESSION_DRIVER=file

my error :

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

createConnection('mysql:host=OPENSHIFT_MYSQL_DB_HOST;port=OPENSHIFT_MYSQL_DB_PORT;dbname=OPENSHIFT_APP_NAME', 
array('driver' => 'mysql', 
      'host' => 'OPENSHIFT_MYSQL_DB_HOST', 
      'port' => 'OPENSHIFT_MYSQL_DB_PORT', 
      'database' => 'OPENSHIFT_APP_NAME', 
      'username' => 'OPENSHIFT_MYSQL_DB_USERNAME', 
      'password' => 'OPENSHIFT_MYSQL_DB_PASSWORD', 
      'charset' => 'utf8',  
      'collation' => 'utf8_unicode_ci', 
      'prefix' => '', 
      'strict' => false, 
      'name' => 'mysql'), 
array('0', '2', '0', false, '0')) in MySqlConnector.php line 20
2

22 Answers 22

74

First line of the error message describes the error type: "PDOException". The next line displays PDO::errorInfo, i.e:

  1. SQLSTATE error code (a five characters alphanumeric identifier defined in the ANSI SQL standard).
    2. Driver-specific error code.
    3. Driver-specific error message.
  • "HY000" is a general server error (see Server Error Codes and Messages in MySQL docs).
  • "2002" is MySQL Client Error Code meaning "Can't connect to local MySQL server through socket" (see (Client Error Codes and Messages in MySQL docs).
  • The driver specific error code and message ("php_network_getaddresses: getaddrinfo failed: Name or service not known") tell you that PDO is not able to resolve the host name.

The stack trace you attached, line 3, reveals that you did not specify the database connection parameters in the configuration file. The error shows up when you test on local, right? You need to update /.env with the actual database connection parameters.

4
  • what parameter is false ?in order that i change this to actual parameter? May 12, 2015 at 12:53
  • @javadhajian The ones in .env. I guess that in local the APP_* and DB_* constants that refer to OPENSHIFT_* env vars should be replaced with your db parameters.
    – TazGPL
    May 12, 2015 at 15:46
  • 'APP_' 'DB_' is not constant but this is variable that have parameter May 12, 2015 at 17:44
  • I am getting this error randomly. Not every time, what will be the cause? Oct 12, 2023 at 16:39
44

Check your DB_HOST on your .env file

DB_HOST=http://localhost/ --> DB_HOST=localhost

php artisan config:clear it will help you, clear cached config

21

If someone facing this problem when using Docker, be sure if you are using your version of SQL.

In my case: MYSQL_VERSION=latest changing to MYSQL_VERSION=5.7.

Then you need to remove your unused Docker images with docker system prune -a (docs).

Also, in your .env you need to change:

DB_HOST=mysql

And run:

php artisan config:clear

I think it will help someone.

1
  • 1
    I had this problem only when running seeds, tried this without success. but running the seed inside docker image docker-compose exec workspace bash worked fine.
    – moghwan
    Jan 16, 2021 at 17:38
11

I faced the same problem, resolved by replace DB_HOST value by '127.0.0.1' with adding additional line for DB_SOCKET, for my case it was /var/run/mysqld/mysqld.sock

#DB_HOST=mysql
DB_HOST=127.0.0.1
DB_SOCKET=<mysql exicutable path>
0
10

I'm using Laravel with Docker. I solved this problem by changing the value of DB_HOST from localhost to mysql.

DB_HOST=mysql
2
  • 2
    my DB_HOST is mysql but not working!
    – Azade
    Oct 10, 2021 at 8:36
  • 1
    If you run with Docker then DB_HOST should equal to docker service name for mysql
    – JanuszO
    Dec 17, 2023 at 17:59
6

I was having issues getting my Laravel app to connect to my Laravel Sail MySQL 8.0 container. I resolved this issue by changing the DB_HOST from mysql to 127.0.0.1 (contrary to what the Sail documentation says).

5

If you are in a network with multiple docker containers, make sure they are all up and running! One of my containers was down, pretty obvious if you know where to check.

4

If you are using docker just check to make sure that your DB_HOST value corresponds to the correct ip address got by running docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name or id>

3

It looks like you did not add a MySQL database to your application, OR you added it after you installed laravel. In that case, you need to stop & start (not restart) your application so that it will pick up your environment variables. (rhc app stop , rhc app start ). If you did not add a database yet, you will need to add one of the mysql cartridges, and then stop & start your application using the previously shown commands.

1
  • i exam this solution not work for me and show to me same error May 12, 2015 at 17:42
3

I was having the same problem and everyone was talking about this is related to DNS configuration, which make sense, since your container maybe isn't knowing how to resolve the name of the domain where your database is.

I guess your can configure that at the moment you start your container, but I think it's better to config this once and for all.

I'm using Windows 10 and in this case docker's gui give us some facilities.

Just right click on docker's icon in the tray bar and select "Settings" item.

Docker's options

Then, on the Docker's window, select the "Network" section and change the DNS option from "Automatic" to "Fixed" and hit "Apply". Docker will restart itself after that. I putted the Google's DNS (8.8.8.8) and it worked fine to me.

docker's network section with dns config

Hope it helps.

3

In my case, when laravel generated the .env configuration file, laravel also generated two uncommented "DB_HOST" lines at line 11 and 12, delete the one that says "mysql" and uncomment (if yours it's commented) the other one (the one with the localhost ip 127.0.0.1) and it worked. (In my case).

Have a great day

2

If you are on production server and the .env file doesn't work at all, go to /bootstrap/cache/config.php and on the line 230 more or less you will find the database data that is beeing cached from the .env file.

'mysql' => 
  array (
    'driver' => 'mysql',
    'host' => '127.0.0.1',
    'port' => '3306',
    'database' => 'yorDBname',
    'username' => 'YOURUSERNAME',
    'password' => 'yourpass',
    'unix_socket' => '',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
  ),
2

Another reason this can sometimes come up is due to a misconfiguration in your docker-compose (if you're using it) whereby the mysql container is not on the same network that your app servers are on.

If the network flag is forgotten or incorrect docker won't add the mysql to the DNS for the app server and therefore throw an error like this.

2

For anyone facing same issue using Docker, make sure you added correct ENV var in your docker-compose file:

services:
    app:
       ...
        environment:
            - 'DB_PORT=3306'
            - 'DB_HOST=mysql' // this value must match `container_name` at `db` service, which is `mysql` (or match the service key name itself, which in this case is `DB_HOST=db`)
        depends_on:
            - mysql
    db:
        container_name: mysql
        ...  

Note: after adding ENV var in docker-compose file, it will overwrite any other ENV var that has same name in your container filesystems, which means if you define DB_HOST=localhost at your laravel .env file at your container, it will not take effect.

Link to the doc that explains this: https://docs.docker.com.xy2401.com/compose/environment-variables/

And here is a relevant excerpt from that page:

When you set the same environment variable in multiple files, here’s the priority used by Compose to choose which value to use:

1/ Compose file

2/ Shell environment variables

3/ Environment file

4/ Dockerfile

5/ Variable is not defined

1

We're using symfony with doctrine and we are in the process of automating deployment. I got this error when I simply hadn't provided the correct db creds in parameters.yml (I was running doctrine:migrations:migrate)

This thread has sent me on a bit of a wild goose chase, so I'm leaving this here so others might not have to.

0

If someone is facing this issue in the hosting services like hostgator or any thing else then you should have to change the value of "localhost" to the relative values which is provided by the hosting provider. In my case it is available in the "phpmyadmin" tab of "cpanel" at the top right corner as "localhost:3302"

0

In my case, it had absolute nothing to do with network issues.

I had been using my local laravel testing environment using laradock. Because laradock runs the mysql server in a different container, you have to set your mysql host in the .env file to the name of the docker mysql container, which by default is mysql.

When I switched back to using homestead, it was trying to connect to a host named mysql, when in fact is should be looking on localhost.

What a DUH moment!

0

For people who may have had the same issue as me when running Docker, I had to increase my disk image size.

Docker Desktop -> preferences -> resources -> Disk image size.

Check that and if its almost full that could be the reason for this error as the docker SQL image will stop running if this is almost full.

0

We had the same issue, and it was caused by our server's inability to reach the database server, this was due to a problem in the DNS serveur of our serveur which were the default ones that came with it.

We fixed this by changing the DNS serveurs to those of Google 8.8.8.8 and 8.8.4.4

-1

Just as on FYI to others suddenly experiencing this on an otherwise (previously) working Laravel installation:

My Laravel environment is on a Ubuntu VirtualBox hosted on a Win-10 laptop.

I used my phone to share Internet via USB and suddenly started experiencing this issue. I changed to using the phone as wireless hotspot and the issue went away. This is a nameserver/networking issue and is not specific to Laravel or MySQL.

-1

If you are using Shopware with docker then you need to first run ./psh.phar ssh then any bin/console command will work

-2

In my case, everything was set up correctly, but my Docker infrastructure needed more RAM. I'm using Docker for Mac, where default RAM was around 1 GB, and as MySQL uses around 1.5Gb of RAM ( and probably was crashing ??? ), changing the Docker RAM utilization level to 3-4 Gb solved the issue.

Not the answer you're looking for? Browse other questions tagged or ask your own question.