问题
I'm fairly new to docker, and I've just been going through the CMS's to see how easy they are to configure. So far, Wordpress and Joomla check out.
When I run the drupal container linked to mysql, I get to the drupal installation screen and where it says to connect the DB, and I use my root credentials and db host being 'localhost', and receive errors trying to connect. I've attached an image to show you the output.drupal-config-db-output-error
The error I get :
Failed to connect to your database server. The server reports the following message: SQLSTATE[HY000] [2002] No such file or directory.
Any help on this would be great. I tried to see if I could access the physical the volume with the config files, but I couldn't find them using Kitematic.
Thank you!
回答1:
The following docker compose file will startup Drupal connected to another container running Mysql
db:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=letmein
- MYSQL_DATABASE=drupal
- MYSQL_USER=drupal
- MYSQL_PASSWORD=drupal
volumes:
- /var/lib/mysql
web:
image: drupal
links:
- db:mysql
ports:
- "8080:80"
volumes:
- /var/www/html/sites
- /var/www/private
Note that the drupal container uses docker links. This will create a /etc/hosts entry called "mysql". Use this instead of "localhost" when running the drupal install screens.
Note
The docker compose file syntax has changed since this answer was originally drafted.
Here is the updated syntax
version: '2'
services:
mysql:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=letmein
- MYSQL_DATABASE=drupal
- MYSQL_USER=drupal
- MYSQL_PASSWORD=drupal
volumes:
- /var/lib/mysql
web:
image: drupal
depends_on:
- mysql
ports:
- "8080:80"
volumes:
- /var/www/html/sites
- /var/www/private
回答2:
The trick for me was to add the service name of the database container in the Drupal connection settings (settings.local.php) instead of 'localhost' or '127.0.0.1', like:
$databases['default']['default'] = array(
'database' => 'database-name',
'username' => 'root',
'password' => 'root',
'host' => 'database-container-name',
'port' => '3306',
'driver' => 'mysql',
'prefix' => '',
);
来源:https://stackoverflow.com/questions/33422546/docker-drupal-container-linked-to-mysql-container-cant-connect-to-mysql-durin