PHP在某些情况下无法解析mysql容器名称 [英] PHP can not resolve mysql container name under certain circumstances
问题描述
我有旧版PHP应用程序.它使用mysqli制作mysql东西.所以在配置中,我有DSN字符串连接到mysql服务.这样
I have legacy PHP application. It used mysqli to make mysql things. So in config I have DSN string to connect to mysql service. Like that
mysql://username:password@db_hostname/dbname
Mysqli返回错误
Mysqli returns error
警告:mysqli :: real_connect():php_network_getaddresses:getaddrinfo失败:名称或服务未知
但是,如果我尝试使用此方法手动连接
But if I will try connect by hand using this
mysqli::real_connect("db_hostname")
它显示警告:mysqli :: real_connect():(HY000/1045):用户``@'172.21.0.3'的访问被拒绝(使用密码:否)
real_connect("user@db_hostname")
,real_connect("mysql:db_hostname")
,real_connect("mysql:host=db_hostname")
无法解析主机地址.
real_connect("user@db_hostname")
, real_connect("mysql:db_hostname")
, real_connect("mysql:host=db_hostname")
can not resolve host address.
我做错了什么?
docker-compose.yml:
docker-compose.yml:
version: "3"
services:
nginx:
image: nginx:1.13-alpine
volumes:
- ./docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
- ./docker/nginx/errors.log:/var/logs/errors.log
- ./docker/nginx/access.log:/var/logs/access.log
- ./:/var/www/html
ports:
- "8081:80"
command: nginx -g "daemon off;"
php:
build:
context: ./docker/
dockerfile: php.Dockerfile
user: "1000"
volumes:
- ./docker/timezone.php.ini:/usr/local/etc/php/conf.d/timezone.php.ini:ro
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
- ./:/var/www/html
db_hostname:
image: mysql:5.5
user: "1000"
environment:
- MYSQL_DATABASE=db
- MYSQL_ROOT_PASSWORD=root
- MYSQL_USER=user
- MYSQL_PASSWORD=user
volumes:
- ./docker/mysql:/var/lib/mysql
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
推荐答案
当您缺少链接时,将db容器与php一起使用.当您将db与PHP链接时,可以从PHP的本地主机访问它.
As you missing linking you db container with php. When you link db with PHP it will be accessible from localhost of PHP.
在这里,我修改了您的docker-compose文件,因为我到处都喜欢阿尔卑斯山;).而且我使用管理员并与db链接以验证连接,而不是代码级检查.由于我没有php docker文件,因此我正在创建包含php7的php:alpine.
Here is I modified your docker-compose file as I like alpine everywhere ;). And I use adminer and link with db to verfiy connection instead of code level checking. As i dont have you php docker file so i am creating php:alpine which contain php7.
version: "3"
services:
nginx:
image: nginx:1.13-alpine
container_name: nginx
ports:
- "8081:80"
command: nginx -g "daemon off;"
db:
image: mysql:5.5
environment:
MYSQL_ROOT_PASSWORD: example
php:
image: php:alpine
container_name: php
tty: true
links:
- db
adminer:
image: adminer
container_name: adminer
ports:
- 8080:8080
links:
- db
然后我在php容器中安装了mysqli
Then i installed mysqli in php container
docker exec -it php ash
并在命令
docker-php-ext-install mysqli;
然后创建test.php
Then create test.php
此处服务器名称为 db .请注意,您的服务器名称将是链接到php的数据库容器名称.
如果您ping db
在php容器中,它将对db容器执行ping操作.
here server name is db. Note that your server name will be what is db container name which is link to php.
if you ping db
in php container it will ping db container.
<?php
$servername = "db";
$username = "root";
$password = "example";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
现在在php容器中运行此文件.您可以挂载并运行您的php代码,但这是我所做的测试.我尝试以root用户身份登录.
Now run this file in php container. You can mount and run your php code but this is what I did for testing. And I tried as a root user.
php -f test.php
这是也与db链接的管理员
And here is adminer which also link with db
这篇关于PHP在某些情况下无法解析mysql容器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!