Connect mysql to docker image

Introduction: Docker is an open source application container engine that allows developers to package their applications and dependency packages into a portable image, and then publish it to any popular Linux or Windows operating system machine, and can also implement virtualization . Containers completely use the sandbox mechanism and will not have any interfaces with each other. Through this article, you can learn how to connect to mysql in docker locally.

  • mac

Step 1: Install docker

1. Official website installation

https://www.docker.com/get-started/icon-default.png?t=N7T8https://www.docker.com/get-started/2, homebrew installation

brew install --cask --appdir=/Applications docker
# After installation, enter to check whether the installation is successful. The result is as follows: success
docker -v

Picture 1

Step 2: Pull the image

1. Enter docker images to view the local image, as shown in Figure 1. It is empty.

2. Enter docker search mysql to find available mysql images as shown in Figure 2.

3. Enter docker pull mysql:latest to pull mysql, as shown in Figure 3.

4. Enter docker images to check the image. You can see that there is an additional mysql, as shown in Figure 4.

figure 1
figure 2
image 3
Figure 4

Step 3: Start the container

docker run -itd --name mysql-docker -p 33060:3306 -e MYSQL_ROOT_PASSWORD=root mysql

docker run: Start the container.

-itd: Start a virtual interactive window in the background.

–name: Start the container name, which can be used to stop the container later.

-p 33060:3306: Maps port 33060 on the host machine to port 3306 on the container. You can connect to the MySQL server in the container using port 33060 on your local machine

-e MYSQL_ROOT_PASSWORD=root: Set the environment variable and set the password of the mysql server in the container to root.

mysql: This is the name of the Docker image to use from which to create the container.

Step 4: Test the connection

1. Terminal connection: Be careful not to use localhost here, use 127.0.0.1, enter the password root

mysql -h 127.0.0.1 -P 33060 -u root -p 

2. Third-party tool connection: localhost can be used here

  • Window (10 and above)

Step 1: Install docker

Install Docker Desktop on Windows | Docker DocsGet started with Docker for Windows. This guide covers system requirements, where to download, and instructions on how to install and update.icon-default.png?t=N7T8https ://docs.docker.com/desktop/install/windows-install/

After installation, double-click the whale icon to open the application. You may need to log in first. After completion, enter docker -v to check the version docker images to check the image.

Step 2: Pull the image

Before pulling the image, enter docker search mysql to find the mysql image source, enter docker pull mysql to pull the mysql image locally, and enter docker images< after the download is successful. /strong> Check that the local mirror has one more mysql, successful

Step 3: Start the mysql container

## There is an explanation in mac
## Map the local port 33060 to the container's port 3306
## Set mysql password root
docker run -itd --name docker_mysql -p 33060:3306 -e MYSQL_ROOT_PASSWORD=root mysql

Enter docker ps -a to view the newly started mysql container. You can see that a mysql has been started here, and we can try to connect.

Step 4: Test the connection

1. Terminal connection

mysql -h localhost -P 33060 -u root -p

Enter the password root set to start the container. You can see that it is successful here.

2. Third-party tool connection

I am using Navicate Premium to connect, and there is a problem with the Authentication plugin caching_sha2_password’. I don’t know if it’s caused by mysql5.7 being installed locally, but gpt says it’s caused by mysql8.0.

Successful solution If you encounter the above situation

Connect to mysql from a local terminal and enter

## The root root here is the username and password we use to log in to mysql.
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';

## refresh
FLUSH PRIVILEGES;

## Exit mysql
exit; 

Docker common commands

1. Mirror related commands:

docker images: List local Docker images.
docker pull <image>: Pull the image from Docker Hub or other repositories.
docker rmi <image>: Delete the local Docker image.

2. Container related commands:

docker ps: List running containers.
docker ps -a: List all containers, including stopped ones.
docker run <options> <image>: Run a container.
docker exec -it <container> <command>: Execute the command in the running container.
docker stop <container>: Stop a running container.
docker rm <container>: delete a container.

3. Network related commands:

docker network ls: List Docker networks.
docker network create <network>: Create a custom network.
docker network connect <network> <container>: Connect the container to the network.
docker network disconnect <network> <container>: Disconnect the container from the network.

4. Data volume related commands:

docker volume ls: List Docker data volumes.
docker volume create <volume>: Create a data volume.
docker run -v <host-path>:<container-path>: Mount the data volume to the container.

5. Build and publish commands:

docker build -t <tag> <path>: Build the image based on Dockerfile.
docker push <image>: Push the image to Docker Hub or other repositories.

6. Log and information commands:

docker logs <container>: View the container's logs.
docker inspect <container>: Get detailed information about the container.

7. Cleanup command:

docker system df: displays the disk space occupied by Docker.
docker system prune: Clean up unused resources, including stopped containers, unused images, etc.
syntaxbug.com © 2021 All Rights Reserved.