问题描述
我有2个用于php应用程序和mysql的docker容器.两者都可以单独正常工作.我可以在 localhost:8000
上访问我的php应用程序,并可以使用MySQL Workbench在 localhost:3306
上连接mysql.
I am having 2 docker containers for php app and mysql. Both are working perfectly individually. I can access my php app at localhost:8000
and can connect mysql at localhost:3306
using MySQL Workbench.
但是,我在容器内的php应用程序无法连接到另一个容器内的mysql数据库.
But, my php app which is inside the container is not able to connect to the mysql db which is inside another container.
我的docker-compose.yml文件如下:
My docker-compose.yml file is as follows:
version: '3'
services:
website:
container_name: php-app
image: php-app
build:
context: ./
volumes:
- php-app:/var/www/html/
ports:
- 8000:80
depends_on:
- mysql
mysql:
image: mysql:8.0
container_name: mysql-server-80
command: --default-authentication-plugin=mysql_native_password
volumes:
- .:/application
restart: always
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=testdb
- MYSQL_USER=root
- MYSQL_PASSWORD=123456
ports:
- "3306:3306"
volumes:
jayde-ci:
mysql:
基于codeigniter的php应用程序中的数据库配置文件如下:
My database configuration file inside the codeigniter based php app is as follows:
$db['default'] = array(
'dsn' => '',
'hostname' => 'mysql',
'username' => 'root',
'password' => '123456',
'database' => 'test',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
推荐答案
您应将密码字段在localhost上设置为空白.试试这个
You should make password filed blank on localhost. try this
$db['default'] = array(
'dsn' => '',
'hostname' => 'mysql',
'username' => 'root',
'password' => '',
'database' => 'test',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
这篇关于无法从php容器连接到mysql容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!