我是docker的初学者。 我可以登录到我的mysql容器并访问数据库。 但是我无法从我的PHP容器以及尝试从mysql客户端(sequelPro)连接时访问它。
PHP 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
COPY ./php.ini /usr/local/etc/php/
RUN a2enmod rewrite
RUN service apache2 restart
docker-compose.yml
version: '2'
services:
mysql:
image: mysql:5.7
ports:
- "4407:3306"
volumes:
- ./db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=shutterfly
shutterfly:
build: ./shutterfly
volumes:
- ./shutterfly:/var/www/html/
depends_on:
- mysql
restart: always
links:
- mysql
ports:
- "8000:80"
CodeIgnitor给我这个错误:
Severity: Warning
Message: mysqli::real_connect(): (HY000/2002): Connection refused
Filename: mysqli/mysqli_driver.php
Line Number: 201
Backtrace:
File: /var/www/html/index.php
Line: 315
Function: require_once
Unable to connect to your database server using the provided settings.
Filename: core/CodeIgniter.php
Line Number: 518
在我的 Database.php 中,
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => "0.0.0.0",
'database_user' => 'root',
'database_password' => 'root',
'databasename' => 'shutterfly',
'port' => '4407',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
你能让我知道我在想什么吗?
答案 0 :(得分:0)
您在Database.php中的端口应为3306,主机名应为mysql
。当docker使用网络在内部进行连接时,您应该指定内部端口,而不是裸露的端口。
如果要使用公开的端口4407,则应在主机名中使用计算机的实际IP地址。不是localhost / 0.0.0.0。当您从Docker容器中使用它们时,它是容器的本地主机。不属于主机。