Skip to content

Commit

Permalink
MySQL Docker Compose Example (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkacenja authored and rfay committed Aug 22, 2019
1 parent a9d31bc commit 6cd72be
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
39 changes: 39 additions & 0 deletions docker-compose-services/mysql/README.md
@@ -0,0 +1,39 @@
For the vast majority of use cases where MySQL is needed in a project, MariaDB is the best choice. MariaDB is faster and more feature-packed in many scenarios. However, sometimes a project really demands plain old MySQL.
This page documents how to create a separate MySQL container to use with ddev. It does not
replace the MariaDB or PHPMyAdmin containers. It also demonstrates how to connect Drupal to that container and database.

## ddev setup
1. Add [docker-compose.mysql.yaml](docker-compose.mysql.yaml) to your project's `.ddev` directory.
2. Start or restart your project with `ddev start` or `ddev restart`.
3. You should see your container start up. You could look for it with `docker ps -a | grep mysql`. The container will be named ddev-[your project name]-mysql.

NB: We use MySQL 5.7, but it should be possible to use a different version. Check out the [MySQL Images on Docker Hub](https://hub.docker.com/_/mysql) for more options. Then change the image key in docker-compose.mysql.yaml.

## Connecting
If your container was successfully started, you should be able to access it from within the ddev webserver container.
1. Execute `ddev ssh` to ssh into your main ddev container.
2. Execute something like `mysql -u root -p -h mysql --database=mysql` to connect.

## Drupal setup
1. After connecting create a database, where you want Drupal to live.
Something like `CREATE DATABASE db`.
2. Create a user with something like `CREATE USER 'db'@'localhost' IDENTIFIED BY 'db';`
3. Grant permissions on that database `GRANT ALL PRIVILEGES ON db.* TO 'db'@'%' IDENTIFIED BY "db";`
4. Configure your site settings as usual. Edit `sites/default/settings.ddev.php` and create a connection that has values like:
```
$databases['default']['default'] = array(
'database' => "db",
'username' => "db",
'password' => "db",
'host' => "mysql",
'driver' => "mysql",
'port' => "3306",
'prefix' => "",
);
```
5. You'll likely want to use the "php" project type, rather than the "drupal7" or "drupal8" project type. This will prevent ddev from trying to adjust your database settings. (This is true for all CMSs using this setup.) `dev config --project-type=php` (or edit .ddev/config.yaml and set `type: php`)
6. Navigate to your site like normal and install.

## Caveats
1. ddev commands that are oriented to mariadb like ddev import-db, ddev export-db, and ddev snapshot won't work with this setup.

30 changes: 30 additions & 0 deletions docker-compose-services/mysql/docker-compose.mysql.yaml
@@ -0,0 +1,30 @@
version: "3.6"
services:
mysql:
hostname: ${DDEV_SITENAME}-mysql
container_name: ddev-${DDEV_SITENAME}-mysql
labels:
com.ddev.site-name: ${DDEV_SITENAME}
com.ddev.approot: $DDEV_APPROOT
# Versions other than 5.7 should work here.
image: mysql:5.7
restart: "no"
environment:
MYSQL_ROOT_PASSWORD: "root"
ports:
# <Port exposed> : < MySQL Port running inside container>
- "3306"
volumes:
- type: "volume"
source: mysql-db
target: "/var/lib/mysql"
volume:
nocopy: true
- type: "bind"
source: "."
target: "/mnt/ddev_config"
# Optionally send startup flags.
# command: --sql_mode=""
# Names our volume
volumes:
mysql-db:

0 comments on commit 6cd72be

Please sign in to comment.