MySQL Server Won't Connect to Docker Container in Ubuntu 22.04
In this article, we will cover the steps required to connect a MySQL server running on a legacy collection of MySQL data and databases stored on an external hard drive to a Docker container using a MySQL image in Ubuntu 22.04. We will discuss the key concepts involved in this process, including Docker, MySQL, and Ubuntu 22.04, and provide detailed instructions on how to configure and connect the MySQL server to the Docker container.
Prerequisites
Before we begin, it is assumed that you have the following:
- A working Ubuntu 22.04 system
- MySQL 8 installed and running on the Ubuntu 22.04 system
- A legacy collection of MySQL data and databases stored on an external hard drive
- Docker installed on the Ubuntu 22.04 system
Step 1: Create a Docker Image
The first step in this process is to create a Docker image that contains the MySQL server and the necessary configurations to connect to the legacy MySQL data and databases stored on the external hard drive. To do this, we will create a Dockerfile with the following contents:
FROM mysql:8.0
COPY my.cnf /etc/mysql/my.cnf
In this Dockerfile, we are using the official MySQL 8.0 Docker image as the base image. We then copy a custom my.cnf file into the /etc/mysql/ directory of the Docker image. This my.cnf file will contain the necessary configurations to connect to the legacy MySQL data and databases stored on the external hard drive.
The my.cnf file should contain the following configurations:
[mysqld]
datadir=/mnt/external/mysql
socket=/mnt/external/mysql/mysql.sock
symbolic-links=0
[client]
socket=/mnt/external/mysql/mysql.sock
In this my.cnf file, we are setting the datadir to /mnt/external/mysql, which is the location of the legacy MySQL data and databases stored on the external hard drive. We are also setting the socket to /mnt/external/mysql/mysql.sock, which is the location of the MySQL socket file. Finally, we are setting symbolic-links to 0 to disable symbolic links, which can cause issues when using external data directories.
Once you have created the Dockerfile and my.cnf file, you can build the Docker image using the following command:
docker build -t my-mysql .
Step 2: Run the Docker Container
Once you have created the Docker image, you can run a Docker container using the following command:
docker run -d -p 3306:3306 --name my-mysql -v /mnt/external/mysql:/mnt/external/mysql my-mysql
In this command, we are running the Docker container in detached mode (-d) and mapping port 3306 on the host to port 3306 in the Docker container (-p 3306:3306). We are also giving the container a name (--name my-mysql) and mounting the external hard drive as a volume (-v /mnt/external/mysql:/mnt/external/mysql).
Step 3: Connect to the MySQL Server
Once you have started the Docker container, you can connect to the MySQL server using the following command:
mysql -u root -p -h 127.0.0.1
In this command, we are connecting to the MySQL server as the root user (-u root) with the password specified in the MySQL configuration (-p). We are also specifying the host as 127.0.0.1, which is the IP address of the Docker container.
In this article, we have discussed the steps required to connect a MySQL server running on a legacy collection of MySQL data and databases stored on an external hard drive to a Docker container using a MySQL image in Ubuntu 22.04. We have covered the key concepts involved in this process, including Docker, MySQL, and Ubuntu 22.04, and provided detailed instructions on how to configure and connect the MySQL server to the Docker container.