Home  >  Article  >  Database  >  How docker deploy mysql8 and set up remote connection

How docker deploy mysql8 and set up remote connection

PHPz
PHPzforward
2023-06-02 10:13:151873browse

The key to docker deployment of mysql lies in two points

In order to avoid data loss, we need to mount the data folder generated by mysql in the production environment outside the container instead of placing it Inside the container, because storage inside the container is unsafe.

2. When running the mysql mirror, you need to set an initial password, and set mysql to be able to connect remotely.

Next we start docker to deploy mysql

First we search for the mysql image:

docker search mysql

Lists many mysql images, we choose the A

How docker deploy mysql8 and set up remote connection

We use docker to pull down the mysql image

docker pull mysql

Docker runs the mysql image

docker run -itd --name=mysql -p 3306:3306 -v /www/docker/course/mysql/:/var/lib/mysql/ -e MYSQL_ROOT_PASSWORD=mmr702351mysql -d mysql
 
-i 表示交互模式运行容器
-t 为容器分配一个伪输入终端
-d 表示后台运行容器 并返回容器Id
 
-v 把mysql产生的数据同步到本地 防止数据丢失
 
-e 容器传参  设置mysql的初始密码
 
-d 镜像名

Copy and paste the above command without thinking Run and enter! If the operation returns a string of IDs, it means success.

We use docker ps to look at the running image list, and we will find that the mysql service has been run successfully. You're done!

How docker deploy mysql8 and set up remote connection

mysql resets the password and allows remote connections (you don’t need to read the additional courses)

First we need to enter the mysql container, Use the following command

docker exec -it mysql /bin/bash

The running effect is as follows

How docker deploy mysql8 and set up remote connection

Careful students found that there is an ID marked in the red box, indicating that we have entered the mysql container

Next we enter the following command to enter mysql management. You need to enter the mysql password (set above)

mysql -u root -p

How docker deploy mysql8 and set up remote connection

How docker deploy mysql8 and set up remote connection

When the above screen appears, it is clear that we have entered mysql

We can use the command to check which databases there are

How docker deploy mysql8 and set up remote connection

And reset the password and settings to remotely connect the command As follows, run the following command directly and return OK, which means the operation is successful

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

Next try to connect with navivat and you're done!

How docker deploy mysql8 and set up remote connection

The above is the detailed content of How docker deploy mysql8 and set up remote connection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete