compose中连接到mysql

compose中连接到mysql

本文介绍了php无法在docker-compose中连接到mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的docker-compose

Here is my 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

Here is how I build my own php7-fpm:lastest

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

我无法连接到mysql容器

I cannot connect to mysql container

$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中没有这样的文件或目录连接失败:没有这样的文件或目录"

I always get error: " Warning: mysqli_connect(): (HY000/2002): No such file or directory in /usr/share/nginx/html/db.php on line 8Connect failed: No such file or directory"

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

Where I run command sudo netstat -tulpn | grep :3306

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

I get this tcp6 0 0 :::3306 :::* LISTEN 15362/docker-proxy

请帮助!

推荐答案

更改PHP脚本

$serverName = 'db_mysql';

它将起作用.

这篇关于php无法在docker-compose中连接到mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 15:45