Docker-Compose 使用 PHP (MySQLi) 连接 MySQL 数据库 [英] Docker-Compose Using PHP (MySQLi) to Connect to MySQL database

查看:151
本文介绍了Docker-Compose 使用 PHP (MySQLi) 连接 MySQL 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,交易如下:我使用 Docker-Compose 创建 3 个服务:一个用于平台,一个用于 db (mysql),第三个用于 PHPMyAdmin

Ok, so here's the deal: I'm using Docker-Compose to create 3 services: one for the platform, one for db (mysql) and the third for PHPMyAdmin

我正在尝试使用 mysqli 连接到端口 5001 上的数据库.

I'm trying to use mysqli to connect to the database on port 5001.

有趣的是,我可以使用 SQL Workbench 和 PHPMyAdmin 以相同的参数连接到数据库,但是在使用 PHP MySQLi 连接时出现错误(如下所示)

What’s interesting is that I can connect to the database using SQL Workbench and PHPMyAdmin with the same parameters, but I get an error (seen below) when connecting using PHP MySQLi

平台:

  $db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);

主机是 127.0.0.1,DB_USER 是 root 及其各自的密码,我提供了一个 DB_Name,端口是 5001.

The host is 127.0.0.1, the DB_USER is root with its respective password, I've provided a DB_Name and the port is 5001.

我的 Docker-Compose.yml 如下所示:

My Docker-Compose.yml looks like this:

version: '3.3'

services:
  platform:
    build: .
    ports:
      - "5000:80"
    links:
      - db:mysql

  db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     ports:
       #- "5001:80"
       - "5001:3306"
     environment:
       MYSQL_ROOT_PASSWORD: some_great_password_here
       MYSQL_DATABASE: DB_NAME_HERE
       MYSQL_USER: USERNAME
       MYSQL_PASSWORD: PASSWORD

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links:
      - db:mysql
    ports:
      - 8181:80
    environment:
      MYSQL_USERNAME: USERNAME
      MYSQL_ROOT_PASSWORD: PASSWORD

volumes:
    db_data:

出于某种原因,我不断收到错误消息:

For some reason, I keep getting the error:

mysqli::mysqli(): (HY000/2003): Can't connect to MySQL server on '127.0.0.1' (111) in ...

任何提示或解决方案将不胜感激!

Any tips or a solution would be much appreciated!

推荐答案

在翻阅文档和其他 Stackoverflow 帖子后,我找到了解决问题的方法.

I found a solution to my problem after churning through documentation and other Stackoverflow posts.

尝试从容器内部连接到数据库时,我们需要使用容器/服务名称,而不是localhost"或 127.0.0.1.这是因为当我们调用localhost"时,它会在每个容器内寻找 localhost.我们需要通过网桥连接到不同容器内的 MySQL 数据库.

When attempting to connect to the database from inside a container, we need to use the container / service name as opposed to 'localhost' or 127.0.0.1. This is because when we call "localhost," it's looking for localhost inside each container. We need to connect to a MySQL database inside a different container, via a network bridge.

我们这样做的方法是简单地将容器名称别名为主机,这样:

The way we do this is to simply alias the container name as the host, such that:

$db = new mysqli("db", DB_USER, DB_PASS, DB_NAME, DB_PORT);

请注意,主机名是 docker-compose.yml 文件中定义的容器名称db".这是因为在容器之间创建了 docker 网络桥接器.端口为 MySQL 默认端口 3306.

Notice that the Hostname is the container name "db," as defined in the docker-compose.yml file. This works because of the docker network bridge that is created between containers. The port is the default MySQL port of 3306.

但是需要注意的是,如果我们想使用 Sequel Pro 或 Workbench 等外部"应用程序连接到数据库,我们可以使用 localhost 或 127.0.0.1 连接到数据库,因为我们不直接处理容器.但是需要注意的是,默认端口 3306 在这种情况下不起作用.我们需要用 3306 映射一个外部端口.在我的例子中,这将是 5001,如我的 docker-compose.yml 文件中所示.

It's important to note however, if we want to use an "external" app like Sequel Pro or Workbench to connect to the database, we can use localhost or 127.0.0.1 to connect to the database since we are not dealing directly with containers. It should be noted however, that the default port of 3306 won't work in this situation. We need to map an external port with 3306. In my case, that would be 5001, as shown in my docker-compose.yml file.

这篇关于Docker-Compose 使用 PHP (MySQLi) 连接 MySQL 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆