Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PDOException] SQLSTATE[HY000] [2002] Connection refused #1204

Closed
ultrablue opened this issue Oct 22, 2017 · 7 comments
Closed

[PDOException] SQLSTATE[HY000] [2002] Connection refused #1204

ultrablue opened this issue Oct 22, 2017 · 7 comments

Comments

@ultrablue
Copy link

Info:

  • Docker version ($ docker --version):
    Docker version 17.09.0-ce, build afdb6d4

  • Laradock commit ($ git rev-parse HEAD):
    903c774

  • System info (Mac, PC, Linux):
    Mac

  • System info disto/version:
    OS X 10.12.6

Issue:

When running artisan migrate, I'm getting the following errors:

php laravel/artisan migrate

[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations)

[PDOException]
SQLSTATE[HY000] [2002] Connection refused


Expected behavior:

Not sure. This is my first time trying to get going with Laravel. I assume not an error.


Reproduce:

See above.


What I've tried so far

I've tried the following in conf/database.php:

'host' => env('DB_HOST', 'localhost')
'host' => env('DB_HOST', 'mysql')
'host' => env('DB_HOST', '172.20.0.2') (The mysql image's IP number as reported by docker inspect)

'username' => env('DB_USERNAME', 'root),
'password' => env('DB_PASSWORD', 'root'),

The root and default users are working as expected (because I can connect using them using SQL Pro).

I can connect from the host using SQL Pro.
I can access the database using phpMyAdmin (http://localhost:8080).
The 'default' database exists.


Relevant Code:

.env:

### MYSQL ##############################################################################################################

MYSQL_VERSION=8.0
MYSQL_DATABASE=default
MYSQL_USER=default
MYSQL_PASSWORD=secret
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=root
MYSQL_ENTRYPOINT_INITDB=./mysql/docker-entrypoint-initdb.d

conf/database.php

       'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'default'),
            'username' => env('DB_USERNAME', 'default'),
            'password' => env('DB_PASSWORD', 'secret'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],
@cocoastorm
Copy link
Contributor

Hey,

I think values inside your Laravel project .env are overriding the config/database.php values (as they should be). I would replace the values inside .env instead like so:

.env

APP_ENV=local
APP_DEBUG=true
APP_KEY=
APP_TIMEZONE=UTC

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=default
DB_USERNAME=root
DB_PASSWORD=root

CACHE_DRIVER=file
QUEUE_DRIVER=sync

@ultrablue
Copy link
Author

Thanks for the response.

My env-example doesn't have many of those keys in it. Therefore, neither does my .env. Here are the ones from your list that I'm not seeing:
APP_ENV
APP_DEBUG
APP_KEY
APP_TIMEZONE
CACHE_DRIVER
QUEUE_DRIVER

Having said that, if the DB_* keys mean (for example) MYSQL_*, then I don't see these ones in my env-example:

MYSQL_CONNECTION
MYSQL_HOST
MYSQL_USERNAME (but it does have MYSQL_USER).

Thanks!

@cocoastorm
Copy link
Contributor

Hey,

I meant the .env inside of your Laravel project, not the .env from this Laradock repository.

Or you can do this, inside conf/database.php:

'host' => 'mysql'
'database' => 'default',
'username' => 'root',
'password' => 'root',

@ultrablue
Copy link
Author

Ah. I guess I'm pretty wet behind the ears, I didn't even know such a thing existed. Thanks for the insight!

What's the relationship between laradock/.env and project/laravel/.env?

Thanks again!
Greg

@AlbinoDrought
Copy link

#1204 (comment)
@ultrablue

Aside from the file name, laradock/.env and project/laravel/.env are almost entirely decoupled. The .env files are used for storing "secret" or frequently-changing configuration options, but both are mostly independent.

The entire laradock repo (this repo), I think, was originally meant for hosting a Laravel stack. This can make stuff a little more confusing. It can, however, be used for hosting pretty much any stack that needs PHP, a webserver (apache, nginx+phpfpm), a database, and even more (queues, cache, mail). I think this is a result of feature creep.

Laravel is a framework that works great with Laradock. Laravel can be hosted with many things - a manually-installed LAMP stack, a raspberry pi, some Digital Ocean one-click droplet, or with an easily-usable (hopefully) and easily-repeatable container (Laradock). Applications made in Laravel usually require, at least, a proper database config - this will have to be setup in the Laravel .env file and have to match the same settings for your database. For many shared-hosting situations, this is often a username and password given to you by your host. For Laradock, this is the username and password configured in the laradock/.env file, or root:root, or a user that was manually created afterwards.

This seems to be the only (existing) relation between Laravel and Laradock - the services (DB, Queue, maybe mailer, maybe reverse proxy) need to be compatible. Besides this, Laradock or Laravel can be swapped out for most other hosting stacks or web frameworks.

luv u pls respond ❤️

Thanks,
Sean

@ultrablue
Copy link
Author

@khoaNguyen96 Sorry for the delay. Your suggestion worked. Updating laravel-project/.env with the correct database settings is working for me. Thanks!

@ultrablue
Copy link
Author

@AlbinoDrought thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants