在Docker中运行查询时,出现此错误,例如
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)
常见的连接超时错误情况是什么?我的配置有什么问题?也许它与mysql套接字有关?如何找出?
我认为我输入了正确的用户名和密码。
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
更新
像这样的简单PHP脚本有效,但是Laravel不能(仍然是相同的错误)
$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();
答案 0 :(得分:0)
原来,我在config/database.php
中为DB_HOST输入了错误的env变量名(就像'host'=> env('DB_HOST_1', 'some ip')
)……起初没有注意到。现在可以了