Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.0k views
in Technique[技术] by (71.8m points)

mysql - SQLState[HY000] Connection refused Laravel with docker

I'm setting up an existing Laravel project on my local machine using Docker. I realize similar questions have been asked many different times and the common refrain is "change DB_HOST to 127.0.0.1, however, my project already has that set.

Whenever I try to run: docker-compose exec app php artisan migrate:install to set the DB up I get this error:

In Connection.php line 664:

  SQLSTATE[HY000] [2002] Connection refused (SQL: create table `migrations` (`id` int unsigned not null auto_increment prima
  ry key, `migration` varchar(255) not null, `batch` int not null) default character set utf8mb4 collate utf8mb4_unicode_ci)


In Connector.php line 67:

  SQLSTATE[HY000] [2002] Connection refused

.env:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3385*
DB_DATABASE=project
DB_USERNAME=user
DB_PASSWORD=password

docker-compose.yml:

services: 
...
  mysql:
    image: mysql:5.5
    ports:
      - 3385:3306
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=project
      - MYSQL_USER=user
      - MYSQL_PASSWORD=password
    volumes:
      - data:/var/lib/mysql

Using the above credentials I am able to successfully connect to the database through a tool like SQLPro or docker-compose exec mysql bash. I know this is likely a configuration issue but I'm not sure what the problem is, Laravel, and really the PHP world, are not my forte.

*This is not the default port, the default port dosen't work either, I changed the port number so it wouldn't conflict with an instance of MySQL I have running locally.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your app container service would need to connect to mysql:3306, so

DB_HOST=mysql
DB_PORT=3306

If you don't need to connect to MySQL from the host, then you don't need the ports section at all.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...