[docker] docker deployment mysql

Table of contents

1. Steps

  • 1. Search mysql mirror
  • 2. Pull the mysql image
  • 3. Create a container
  • 4. Operate mysql in the container

2. Description

  • 1. The network service in the container cannot communicate directly with the external machine
  • 2. The mysql port 3306 deployed in the container cannot be directly communicated between the external machine and the host
  • 3. Direct communication between external machine and host machine
  • 4. The host and container can communicate directly
  • 5. When the network service in the container needs to be accessed by an external machine, the port providing the service in the container can be mapped to the port of the host machine
  • 6. The external machine accesses the port of the host machine, thereby indirectly accessing the service of the container

3. Example

  • 1. Search mysql mirror
docker search mysql
  • 2. Pull the mysql image
docker pull mysql:5.6
  • 3. Create a container, set port mapping, directory mapping
# 在/root目录下创建mysql目录用于存储mysql数据信息
mkdir ~/mysql
cd ~/mysql

Excuting an order:

docker run -id \
-p 3307:3306 \
--name=my_mysql \
-v $PWD/conf:/etc/mysql/conf.d \
-v $PWD/logs:/logs \
-v $PWD/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:5.6

Parameter Description:

1.-p 3307:3306:将容器的3306端口映射到宿主机的3307端口
2.-v $PWD/conf:/etc/mysql/conf.d:将主机当前目录下的conf/my.cnf挂载到容器的/etc/mysql/my.cnf。  配置的目录
3.-v $PWD/logs:/logs:将主机当前目录下的logs目录挂载到容器的/logs。 日志的目录
4.-v $PWD/data:/var/lib/mysql:将主机当前目录下的data目录挂载到容器的/var/lib/mysql。 数据的目录
5.-e MYSQL_ROOT_PASSWORD=123456:初始化root用户的密码, e是env环境的意思
  • 4. Enter the container
docker exec -it my_mysql /bin/bash
// my_mysql是容器名
  • 5. Login to mysql
mysql -u root -p
// 输入密码 123456
  • 6. Login screenshot
    insert image description here

    insert image description here

  • 7. Exit

exit // 退出mysql
exit // 退出容器

Guess you like

Origin blog.csdn.net/qq_32088869/article/details/131883817