简体   繁体   English

Docker MySQL mysqli::real_connect():(HY000/2002): 连接被拒绝

[英]Docker MySQL mysqli::real_connect():(HY000/2002): Connection refused

I'm new to docker/docker-compose.我是 docker/docker-compose 的新手。 I'm trying to connect 2 containers which are the CodeIgniter application and MySQL .我正在尝试连接 2 个容器,它们是CodeIgniter应用程序和MySQL I can't seems to solve the 'mysqli::real_connect(): (HY000/2002): Connection refused' issue on my web app.我似乎无法解决我的 web 应用程序上的“mysqli::real_connect(): (HY000/2002): Connection denied”问题。 I was able to ping from inside the container to the other and vice versa.我能够从容器内部 ping 到另一个容器,反之亦然。

This is my docker-compose.yml file:这是我的docker-compose.yml文件:

version: '3'

services:

  web:
    container_name: ciblog
    build: .
    links: 
      - db
    depends_on:
      - db
    ports:
      - '80:80'
    volumes: 
      - .:/var/www/html/

  db:
    container_name: mysql
    image: mysql:latest
    ports:
      - '3306:3306'
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
      MYSQL_USER: root
      MYSQL_DATABASE: ciblog
    volumes:
      - ./assets/sql/ciblog.sql:/docker-entrypoint-initdb.d/ciblog.sql

Dockerfile : Dockerfile

FROM php:7.0-apache
RUN apt-get update && \
    apt-get install -y libfreetype6-dev libjpeg62-turbo-dev && \
    docker-php-ext-install mysqli && \
    docker-php-ext-install mbstring && \
    docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/  &&  \
    docker-php-ext-install gd
RUN a2enmod rewrite
RUN service apache2 restart

Here's my database.php configuration:这是我的数据库。php 配置:

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'dbport' => '3306',
    'hostname' => 'db',
    'username' => 'root',
    'password' => '',
    'database' => 'ciblog',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => FALSE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

Could anyone please help me identify the issue?有人可以帮我确定问题吗? Thank you in advance.先感谢您。 Here's my GitHub repository in case you want to see the files: https://github.com/josephsim/docker-ciblog如果您想查看文件,这是我的 GitHub 存储库: https://github.com/josephsim/docker-ciblog

In Docker Compose-based setups, you can use the names of services as hostnames.在基于 Docker Compose 的设置中,您可以使用服务名称作为主机名。 In your example, set在您的示例中,设置

'hostname' => 'db'

Networking in Compose discusses this in more detail. Compose 中的网络更详细地讨论了这一点。

Docker containers don't have consistent IP addresses, and it's usually not useful to manually look them up. Docker 容器没有一致的 IP 地址,手动查找它们通常没有用。 You don't need links: in modern Docker either and removing that line won't be harmful.您不需要links:在现代 Docker 中,删除该行也不会有害。

I finally got it.我终于明白了。 Changing the MySQL version works.更改 MySQL 版本有效。

image: mysql:5.7

Source: https://stackoverflow.com/a/50169745/12555817来源: https : //stackoverflow.com/a/50169745/12555817

In Docker Compose-based setups, you can use the names of services as hostnames.在基于 Docker Compose 的设置中,您可以使用服务名称作为主机名。 In your example, set在您的示例中,设置

'hostname' => 'db' '主机名' => 'db'

if you are using docker with version 2.0.0 and below.如果您使用 docker 和 2.0.0 及以下版本。 please use image: mysql:5.7 I fixed my issue.请使用image: mysql:5.7我解决了我的问题。

暂无
暂无

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

相关问题 获取“mysqli::real_connect(): (HY000/2002): 连接被拒绝” - Getting "mysqli::real_connect(): (HY000/2002): Connection refused" 数据库错误 mysqli::real_connect(): (HY000/2002): Connection denied codeingiter - Datebase error mysqli::real_connect(): (HY000/2002): Connection refused codeingiter 实时服务器上的 Codeigniter 错误消息:mysqli::real_connect(): (HY000/2002): 连接被拒绝 - Codeigniter Error Message on Live server : mysqli::real_connect(): (HY000/2002): Connection refused mysqli::real_connect(): (HY000/2002): 由于目标机器主动拒绝,无法建立连接 - mysqli::real_connect(): (HY000/2002): No connection could be made because the target machine actively refused it mysqli::real_connect(): (HY000/2002): 实时连接被拒绝 - mysqli::real_connect(): (HY000/2002): Connection refused on Live WAMP和mysqli :: real_connect():(HY000 / 2002)? - WAMP and mysqli::real_connect(): (HY000/2002)? mysqli :: real_connect():(HY000 / 2002):在Cplesk中 - mysqli::real_connect(): (HY000/2002):in cplesk CodeIgniter 消息:mysqli::real_connect():(HY000/2002):连接超时,在服务器中出现错误 - CodeIgniter Message: mysqli::real_connect(): (HY000/2002): Connection timed out giving error in server 警告:mysqli_connect():( HY000 / 2002):拒绝连接 - Warning: mysqli_connect(): (HY000/2002): Connection refused in MySQL 错误 mysqli::__construct(): (HY000/2002): 连接被拒绝 - MySQL Error mysqli::__construct(): (HY000/2002): Connection refused
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM