Reputation: 1346
When running queries in my Docker I get this error, for example
php artisan clear-compiled
In Connection.php line 664:
SQLSTATE[HY000] [2002] Connection timed out (SQL: select `id`, `name` from
`users` where `profile_is_public` = 1 and `status` = 1 order by `created_at
` desc limit 6)
What are common cases for the connection timed out error? What could be wrong with my config? Maybe it could be something with mysql socket? How to find out?
I think I entered correct user and password.
docker-compose.yml
version: '3'
services:
web:
build: ./webserver
ports:
- "80:80"
- "443:443"
volumes:
- //docker/dockertest/webserver/app:/var/www/vhosts/app
links:
- db:db
command:
- /usr/local/bin/apache2_install_composer_dependencies.sh
db:
image: mysql:8.0
container_name: db
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_DATABASE: myDb
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
- //docker/dockertest/install/db_dump:/docker-entrypoint-initdb.d
- persistent:/var/lib/mysql
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8000:80
environment:
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
persistent:
Laravel .env
DB_CONNECTION=mysql
#host points to Docker container
DB_HOST=db
DB_PORT=3306
DB_DATABASE=myDb
DB_USERNAME=root
DB_PASSWORD=test
Update
Simple PHP Script like this works, but Laravel does not (still same error)
$servername = "db"; $username = "root"; $password = "test"; $dbname = "myDb";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM Person";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
}
$conn->close();
Upvotes: 3
Views: 6887
Reputation: 1346
Turned out I had wrong env variable name in config/database.php
for DB_HOST (it was like 'host'=> env('DB_HOST_1', 'some ip')
)... Did not notice at first. Now it works
Upvotes: 1