DevOps – Bonus Lesson: MySQL Docker Service

Add the following block of code to the bottom of the docker-compose.yaml file. This will give you a MySQL database for your application. Note that indentation is very important. Always refer to this block of code instead of the snippets since the snippets aren’t properly indented.

    # Database
    database:
        image: mysql:8.0
        command: --default-authentication-plugin=mysql_native_password
        volumes:
            - "./docker/data/db/mysql:/var/lib/mysql"
        environment:
            MYSQL_DATABASE: ${DB_DATABASE}
            MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
            MYSQL_PASSWORD: ${DB_PASSWORD}
            MYSQL_USER: ${DB_USERNAME}
        ports:
            - 33061:3306

image: mysql:8.0

We’re going to use version 8 of MySQL.

command: --default-authentication-plugin=mysql_native_password

This is important if you are using MySQL 8. Version 8 using a different kind of authentication method, and you’ll get an error if you’re using an application like Laravel tinker which uses the old password way.

PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]

volumes:
  - "./docker/data/db/mysql:/var/lib/mysql"

We’re going to store the MySQL data in a folder called mysql. This folder will be created automatically when you build the container (~/Code/appone/docker/data/db/mysql). Alternatively, you could use an invisible Docker volume to store the data, but I prefer being able to see the data because I can easily delete if I run into an error (which is often). Fun fact. If you delete the data folder, your database will also be deleted and a new one will be created when you start up docker.

volumes:
  - dbdata:/var/lib/mysql
volumes:
  dbdata:

These are the two snippets you’d need if you wanted to use Docker’s volumes instead of a folder to store your MySQL data/files.

MYSQL_USER: ${DB_USERNAME}

PRO TIP. I had quite the time trying to figure out why I couldn’t connect to my database. In fact, I spent an hour or two stuck. Using docker logs command helped me figure out what the issue was.

It spit out the following error.

[ERROR] [Entrypoint]: MYSQL_USER=”root”, MYSQL_USER and MYSQL_PASSWORD are for configuring a regular user and cannot be used for the root user
Remove MYSQL_USER=”root” and use one of the following to control the root user password:
– MYSQL_ROOT_PASSWORD
– MYSQL_ALLOW_EMPTY_PASSWORD
– MYSQL_RANDOM_ROOT_PASSWORD

Don’t set the MYSQL_USER option to root. I noticed that the mysql:5.7 container let me do that, but when I changed the version to mysql:8.0, it stopped working. To make it more confusing, sometimes it worked, and sometimes it didn’t. I finally narrowed it down to that one line.

Also, note that ${DB_USERNAME} comes from your .env file, and certain applications may share that (such as Laravel).

I would recommend setting the DB_USERNAME to dev or anything other than root.

ports: – 33061:3306Map port 33061 on your host machine (mac/windows/linux) to port 3306 in the MySQL container. The port 3306 is the default port for MySQL databases.

You can login to the MySQL database using your favorite database client.

  • Sequel Ace (replaced Sequel PRO, available in the app store)
  • PHPMyAdmin
  • TablePlus (my favorite)

Use the following info when logging in.

  • Host: 127.0.0.1
  • Port: 33061
  • User: dev
  • Password: secret

If everything worked, you should be able to log in and see your database. Congrats!