Running a PHP development site with MySQL 8 in Docker

<%- if @topic_view.topic.tags.present? %>
<%= t 'js.tagging.tags' %>: <%- @topic_view.topic.tags.each do |t| %> <%= t %> <%- end %>
<% end %>

This post explains how to configure a Silverstripe CMS project to use a dockerised MySQL8 server to store its data.

This is only meant to be use for a development environment and is not a secure set up.

Background

I wanted to test Silverstripe CMS with MySQL8. I normally use the version of MySQL that ships with Ubuntu (5.7) for all my sandboxes. Since I didn’t want to change my default set up, I decided to run my MySQL 8 server in docker.

It took me a couple hours to figure out how to do this, so I’m documenting how to do it for future reference.

Steps

I’m presuming you have docker running on your machine and a pre-existing Silverstripe CMS project.

I’ve only tested this on Ubuntu. It probably will work on MacOS or Windows, but I don’t know for sure.

Step 1 - Create a directory to store your data

By default, each time you shutdown your docker container, your data will be lost. This step creates a folder on your local system to store the data.

If your data is meant to be ephemeral, you can skip this step.

I’ve created my folder in ~/docker/mysql8/data, but you can create it anywhere you want.

mkdir -p ~/docker/mysql8/data

Step 2 - Pick a port to access your MySQL8 server

Because I already have my native MySQL 5.7 running on port 3306, I need to use a different port for my MySQL 8 docker server. I decided to use 3308, but you can use whatever you want as long as it doesn’t conflict with another service on your machine. If you do not, have another MySQL instance running, you can use the default 3306 port.

Step 3 - Run your docker instance

Punch your desired values into the following bash script and run it to get your MySQL instance started.

DATA_DIR=~/docker/mysql8/data
PORT=3308
ROOT_PASSWORD=mypassword

docker run -d \
  -p $PORT:3306 \
  -v $DATA_DIR:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=$ROOT_PASSWORD \
  -e MYSQL_ROOT_HOST=% \
  mysql/mysql-server:latest \
  --default_authentication_plugin=mysql_native_password

You can omit -v $DATA_DIR:/var/lib/mysql \ if you don’t care about preserving your data.

Step 4 - Test your connection

Assuming you have the MySQL client on your machine, you can use the following command to test that you can connect to your MySQL server.

mysql -uroot -P3308 -h127.0.0.1 -pmypassword

Note that you can not omit -h127.0.0.1 or substitute it for -hlocalhost. The docker container considers any connection coming from its host as a remote connection.

You can run the following query to double-check what version of MySQL you are running.

SHOW VARIABLES LIKE "version%";

Step 5 - Configure Silverstripe CMS to use your MySQL Docker server

Update your Silverstripe CMS project’s .env file to look like this.

SS_BASE_URL="http://localhost"

# localhost won't work here
SS_DATABASE_SERVER="127.0.0.1"

SS_DATABASE_NAME="test_site"
SS_DATABASE_CLASS="MySQLDatabase"
SS_DATABASE_PASSWORD="mypassword"
SS_DATABASE_USERNAME="root"
SS_DATABASE_PORT=3308

Note

You’ll need to re-run your the docker command each time you reboot your workstation. If this is a problem for you, you can put the command in a bash script and have it executed and on boot.

For more information and config options, read:

2 Likes