php无法连接到docker-compose中的mysql

时间:2017-01-16 08:12:41

标签: mysql docker-compose

这是我的docker-compose

version: '2'

services:
  nginx:
    image: nginx:1.11.8-alpine
    ports:
      - "8081:80"
    volumes:
      - ./code:/usr/share/nginx/html
      - ./html:/myapp
      - ./site.conf:/etc/nginx/conf.d/site.conf
      - ./default.conf:/etc/nginx/conf.d/default.conf
      - ./error.log:/var/log/nginx/error.log
      - ./nginx.conf:/etc/nginx/nginx.conf
    links:
      - phpfpm
  phpfpm:
    image: php7-fpm:latest
    ports:
      - "9000:9000"
    volumes:
      -  ./code:/usr/share/nginx/html
    links:
      - db_mysql
  db_mysql:
    image: mysql:5.7.17
    volumes:
      - db_data:/var/lib/mysql
    # restart: no
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: wp2017
      MYSQL_USER: wp
      MYSQL_PASSWORD: wp2017
volumes:
  db_data:

以下是我如何构建自己的php7-fpm:lastest

FROM php:7.1-fpm-alpine
RUN docker-php-ext-install mysqli

我无法连接到mysql容器

$serverName = 'localhost';
$userName = 'wp';
$password = 'wp2017';
$dbName = 'wp2017';

$link = mysqli_connect($serverName, $userName, $password, $dbName);

if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}

我总是得到错误:“     警告:mysqli_connect():( HY000 / 2002):第8行的/usr/share/nginx/html/db.php中没有这样的文件或目录 连接失败:没有这样的文件或目录“

我在哪里运行命令     sudo netstat -tulpn | grep:3306

我明白了     tcp6 0 0 ::: 3306 ::: * LISTEN 15362 / docker-proxy

请帮忙!

1 个答案:

答案 0 :(得分:7)

更改PHP脚本

summarize_all

它会起作用。