Laradock is a complete PHP local development environment provided by Docker
Error when connecting to MySQL in the framework
SQLSTATE[HY000] [2002] Connection refused
The main reason is that there is not enough understanding of the isolation mechanism of Docker containers. Each container is isolated. If there are interdependent services, it is necessary to perform display associations, such as using options
--link
.
In the same way,docker-compose
when using , the association between containers is similar to the following method:
# docker-compose.xml basic
version: '2'
services:
...
php:
build: ./php
...
links:
- "mysql"
mysql:
build: ./mysql
ports:
- "3306:3306"
environment:
MYSQL_PASSWORD: root
Note that the key point is here: the code to test the connection to MySQL is actually running in the container corresponding to PHP, and the MySQL service is in its own container. When our host fills in 127.0.0.1, it actually corresponds to the PHP container. Inside, so it is impossible to find the corresponding service, which causes the above connection refused error.
So, how do you connect?
In fact, after the containers are associated, they can be connected by the container name.
In the above docker-compose.xml
document, the container corresponds to the service name MySQL mysql
, PHP container name is associated with it mysql
, so the 127.0.0.1
change mysql
and then connect to.
# thinkphp project modify database.php file
'hostname' => 'mysql',
# Laravel project modify .env file
DB_HOST=mysql
Read More:
- [Solved] docker mysql SQLSTATE[HY000] [2002] Connection refused
- [Solved] Error response from daemon: Conflict. The container name “/mysql is already in use by container
- Docker: How to Solve MYSQL8 & Navicat remote connection error
- Remote connection to MySQL database error: is not allowed to connect to this MYSQL server solution
- (Fixed) workbench MySQL Error Code: 2013. Lost connection to MySQL server during query
- [Solved] Navicat connection error 1251 compatibility with docker MySQL
- Docker mysql8 modify password
- MySQL Build table error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL ser
- IDEA maven Config MYSQL Connection Error: Could not create connection to database server.
- How to Fix MySQL ERROR 1130 (HY000): Host ‘XXXX’ is not allowed to connect to this MySQL server
- [Solved] There is a problem with mysql8.0 without password in Navicat connection
- [Solved] ERROR 2002 (HY000): Can’t connect to local MySQL server through socket’/var/lib/mysql/mysql.sock’ (2)
- [Solved] Centons7 docker:mysql:5.7 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
- The setobject() method reports an error. The parameter index out of range and MySQL syntax error exception report an error
- [Solved] MYSQL Command Execute Error: Can ‘t connect to local MySQL server through socket ‘/tmp/mysql.sock ‘(2) “
- [Solved] Error Code: 2013. Lost connection to MySQL server during query
- How to Solve can‘t connect to local mysql server through socket ‘/var/lib/mysql/mysql.sock‘
- [Solved] C# connecting to MySQL database reports an error
- Android connection to cloud MySQL error: java.lang.NoClassDefFoundError Failed resolution of LjavasqlSQLType
- MySQL Install Error: MySQL error 1042: Unable to connect to any of the specified MySQL hosts