我正在使用docker通过以下命令在单独的容器中启动Nginx,PHP和mariaDB:
I am using docker to start Nginx, PHP and mariaDB in seperate containers with these commands:
# DB docker run --name db -d -p 3306:3306 --restart=always -v /opt/db:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=<pass> -e MYSQL_USER=dbuser -e MYSQL_PASSWORD=<pass> mariadb # PHP docker run --name php -p :9000 -d --restart=always --link=db:db -v /www:/data php:fpm # WEB docker run --name web -d -p 80:80 --link php --link=db:db -v /www:/data -v /opt/nginx/default.conf:/etc/nginx/conf.d/default.conf nginx:latest(当然,我已经填好了我的密码)到目前为止,到目前为止,本地运行着一个旋转的Nginx,我可以在www文件夹中看到正确显示在 localhost
(of course has my password filled in) So far so good, have a spinning nginx running locally and I can see the index.htm and phpinfo.php in my www folder presented correctly at localhost
接下来,我创建了一个dbcheck.php页面,其中包含以下内容:
Next, I created a dbcheck.php page with the following contents:
<?php $dbh = mysqli_connect('localhost', 'dbuser', '<pass>'); if (!$dbh) { die('Could not connect: ' . mysqli_error()); } echo 'Connected successfully to MariaDB database'; mysqli_close($dbh); ?>结果是PHP没有安装MySQL扩展. 我在做什么错和/或如何安装MySQL扩展(以及在哪里安装)
The result is that PHP does not have the MySQL extension installed. What am I doing wrong and/or how can I install the MySQL extention (and where)
推荐答案请使用db作为主机名,而不是localhost.因此,代码将像这样
Please use db for hostname instead of localhost. So, the code will be like this
$dbh = mysqli_connect('db', 'dbuser', '<pass>');因为使用此选项--link=db:db将php容器与mariadb容器链接在一起.这意味着您链接到db容器并将其命名为db.因此,php容器只知道db作为mariadb容器的主机名
Because you link the php container with mariadb container using this options --link=db:db. It mean you link to db container and name it as db. So, php container just know db as the hostname for mariadb container
更多推荐
Docker PHP容器无法连接到MariaDB容器
发布评论