Docker PHP容器无法连接到MariaDB容器

编程入门 行业动态 更新时间:2024-10-26 00:24:00
本文介绍了Docker PHP容器无法连接到MariaDB容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用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容器

本文发布于:2023-10-18 12:13:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504198.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   连接到   Docker   PHP   MariaDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!