简体   繁体   English

连接到 docker-compose 中的 mysql

[英]Connect to mysql in docker-compose

I am using docker-compose version 1.21.0.我正在使用 docker-compose 版本 1.21.0。

I've made the docker-compose.yml:我制作了 docker-compose.yml:

  container_name: web
  image: nginx:latest
  ports:
    - "80:80"
  volumes:
    - ./code:/code
    - ./nginx.conf:/etc/nginx/conf.d/default.conf
  links:
    - php
    - mysql

php:
  container_name: php
  build: .
  volumes:
    - ./code:/code
  environment:
    XDEBUG_CONFIG: remote_host=host.docker.internal
  links:
    - mysql

mysql:
  container_name: mysql
  image: mysql:latest
  ports:
    - "3306:3306"
  volumes:
    - ./storage-mysql:/var/lib/mysql
    - ./conf-mysql:/etc/mysql/conf.d
  environment:
    - MYSQL_ROOT_PASSWORD=passwordz

All is fine, I can access my mysql db with:一切都很好,我可以通过以下方式访问我的 mysql 数据库:

docker exec -it mysql mysql -u root -p

Note, I have setup my mysql DB with:注意,我已经设置了我的 mysql DB:

bind-address            = 127.0.0.1

Because I do not want to expose mysql to internet.因为我不想将 mysql 暴露在互联网上。

And I have made a simple form with PHP:我用 PHP 做了一个简单的表格:

<?php

$link = mysqli_connect("mysql:3306", "root", "passwordz", "testdb");

if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}


$icao = mysqli_real_escape_string($link, $_REQUEST['icao']);
$airfield = mysqli_real_escape_string($link, $_REQUEST['airfield']);
$lati = mysqli_real_escape_string($link, $_REQUEST['lati']);


$sql = "INSERT INTO testdb (icao, airfield, lati) VALUES ('$icao', '$airfield', '$lati')";
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}


mysqli_close($link);
?>

But with many try with differents configuration, I have the http error message after the post:但是通过许多尝试不同的配置,我在发布后收到了 http 错误消息:

Warning: mysqli_connect(): (HY000/2002): Connection refused in /code/insert.php on line 4警告:mysqli_connect(): (HY000/2002): Connection denied in /code/insert.php on line 4

Did I miss something?我错过了什么? I am totaly lost and really need help.我完全迷失了,真的需要帮助。

Regards,问候,

In Docker you almost always need to set bind-address and similar options to 0.0.0.0 ("everywhere"), or else the server won't be reachable from outside its own container.在 Docker 中,您几乎总是需要将bind-address和类似选项设置为0.0.0.0 (“无处不在”),否则将无法从其自己的容器外部访问服务器。

You can add an IP address to ports: to attach the published port to a specific host interface:您可以将 IP 地址添加到ports:将发布的端口附加到特定主机接口:

services:
  mysql:
    ports:
      # This binds the published port to the _host's_ localhost
      # interface; it will be reachable from outside Docker on
      # the same host, but not from other hosts
      - '127.0.0.1:3306:3306'

If you don't need the database to be accessible from outside Docker, you can also remove the ports: section entirely.如果您不需要从 Docker 外部访问数据库,您也可以完全删除ports:部分。 It will still be reachable from other containers using its Compose service name and the standard MySQL port ( mysql:3306 ).它仍然可以使用其 Compose 服务名称和标准 MySQL 端口( mysql:3306 )从其他容器访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM