Skip to content
Advertisement

Codeigniter mysql can’t connect in docker

Codeigniter 3.0 can not connect to local mysql, so please let me know the reason.

Error message is SQLSTATE [HY000] [2002] Connection refused. My Codeigniter is working on Docker.

Specifically on Docker in mac

  • php-fpm 7.1
  • MySQL 5.7
  • Nginx 2.4

Containers are working.

And that MySQL container can be connected from sequel pro or Mac terminal, only from Codeigniter can connect.

Successful connection code from my Mac terminal

JavaScript

db_test.php

JavaScript

result — SQLSTATE [HY000] [2002] Connection refused.

/config/database.php

JavaScript

docker-compose.yml

JavaScript

etc/mysql/mysql.conf.d/mysqld.cnf in MySQL container

JavaScript

Advertisement

Answer

Your database connection inside the php container is:

JavaScript

Since you in your compioser file, “link” the phpcontainer to the databaser container, and a link is essentliation addition of a line in your hosts-file, so mysql will point to the ip in the internal docker-network for you.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement