DOCKER

Docker — Use dockerfile to install MySQL 5.7 on Ubuntu 18.04

Create a MySQL container and connect it from another container, including Solving possible problems

Ivan Chuang (莊子毅)

--

I simply organized the process of this article :

  1. Write a Dockerfile to build MySQL image.
  2. Create a MySQL container.
  3. Create user in mysql .
  4. Verify the mysql container — from another container.

We also sorts out possible problems and solutions during the installation process.

System requirement :

  1. Ubuntu 18.04
  2. MySQL Community

Write a Dockerfile:

We use Ubuntu 18.04 as the base image to build the MySQL container.

Add maintainer information.

Update software information and install gnupg. If you do not install gnupg, you will get an error when downloading the GPG key.

Add the MySQL APT repository and configure it. The MySQL version is 5.7. In addition, we will also install the software required for MySQL database initialization.

Install Mysql

ERROR 2003 (HY000): Can’t connect to MySQL server on ‘127.0.0.1’ (111)

In general, we will create another container that provides services to users, and then this container will exchange data with the container that provides MySQL services.

When you try to log in from another container to the container running MySQL Server, you may get this error message. You need to run the following steps to avoid it.

Mount MySQL data storage directory with data volume.

Expose the default port.

Specify the command executed by the container by default.

The following is the complete dockerfile content.

Construct an image file

Build the image file through the docker build command.

Create MySQL container

Use the image file created earlier to create a MySQL container.

Log in to the MySQL container locally and create a MySQL account

We can use the following steps to create a MySQL account instead of logging in as root.

  1. Use the docker exec command to connect the terminal to the MySQL container created earlier and execute bash.
  2. Log in to MySQL as root.
  3. Create an account named guest and grant permissions, which can log in to the mysql container remotely.
  4. Flush privileges.

Verify the mysql container — from another container

Now that all the work is completed, we can create a new container to connect to the previously created mysql-server container for verification.

Mission Completed!!

If you want to log in remotely as root ……

ERROR 1698 (28000): Access denied for user ‘root’@’localhost’

If you log in to the MySQL database as the root , you will get this error message. This is because MySQL uses the UNIX auth_socket plugin by default in Ubuntu. You need to run the following steps to avoid it.

  1. Use the docker exec command to connect the terminal to the MySQL container created earlier and execute bash.
  2. Log in to MySQL as root.
  3. Set the root to use the mysql_native_password plugin.
  4. Flush privileges.
  5. Exit MySQL.
  6. Restart MySQL service

--

--

Ivan Chuang (莊子毅)

Keep learning…..Working in the telecommunications…Live in New Taipei City, Taiwan