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
I simply organized the process of this article :
- Write a Dockerfile to build MySQL image.
- Create a MySQL container.
- Create user in mysql .
- Verify the mysql container — from another container.
We also sorts out possible problems and solutions during the installation process.
System requirement :
- Ubuntu 18.04
- 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.
- Use the docker exec command to connect the terminal to the MySQL container created earlier and execute bash.
- Log in to MySQL as root.
- Create an account named guest and grant permissions, which can log in to the mysql container remotely.
- 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.
- Use the docker exec command to connect the terminal to the MySQL container created earlier and execute bash.
- Log in to MySQL as root.
- Set the root to use the mysql_native_password plugin.
- Flush privileges.
- Exit MySQL.
- Restart MySQL service