docker+mysql快速部署

916 阅读3分钟

Docker+MySQL

拉取镜像

参考命令:

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

测试命令:

镜像地址( hub.docker.com/_/mysql)

先安装docker,直接执行以下命令,拉取MySQL镜像

docker pull mysql:5.7.36
└─[1] <> docker pull mysql:5.7.36
8.0.27: Pulling from library/mysql
b380bbd43752: Already exists
f23cbf2ecc5d: Already exists
30cfc6c29c0a: Already exists
b38609286cbe: Already exists
8211d9e66cd6: Already exists
2313f9eeca4a: Already exists
7eb487d00da0: Already exists
4d7421c8152e: Pull complete
77f3d8811a28: Pull complete
cce755338cba: Pull complete
69b753046b9f: Pull complete
b2e64b0ab53c: Pull complete
Digest: sha256:6d7d4524463fe6e2b893ffc2b89543c81dec7ef82fb2020a1b27606666464d87
Status: Downloaded newer image for mysql:5.7.36
docker.io/library/mysql:5.7.36

查看镜像

└─[0] <>  docker images
REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
mysql                                5.7.36              938b57d64674        6 days ago          448MB

启动实例

参考命令:

docker 
     --name 指定容器名字
     -p [container port]:[host port]
     -e 设置root用户的密码
└─[0] <> docker run -itd --name mysql_test_5736 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

将容器默认的3306端口,映射到本地的3307端口,本地程序连接3307就可以访问数据库

查看容器进程

└─[0] <> docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
b238e18a0a8a        mysql:5.7.36        "docker-entrypoint.s…"   2 hours ago         Up 2 hours          33060/tcp, 0.0.0.0:3307->3306/tcp   mysql_test_5736

连接

通过 docker ps 查看容器的 ID 执行以下命令登录到容器里面

└─[0] <> docker exec -it b238e18a0a8a /bin/bash
root@b238e18a0a8a:/#

在容器中执行MySQL登录命令:mysql -uroot -plocalhost -p123456 -P3307

root@b238e18a0a8a:/# mysql -uroot -plocalhost -p123456 -P3307
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 23
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

至此,通过docker就部署好了MySQL单机实例

导入测试数据

下载测试数据

测试数据地址:github.com/datacharmer…

将压缩数据包下载到本地,并解压

将解压包 test_db-master 复制到 容器:mysql_test_5736 下的 /root/目录下

复制命令:

└─[0] <> docker cp test_db-master mysql_test_5736:/root/

测试数据导入MySQL中

登录容器的MySQL 通过source命令导入

└─[0] <> docker exec -it b238e18a0a8a /bin/bash
root@b238e18a0a8a:/# mysql -uroot -plocalhost -p123456 -P3307
mysql> system ls /root/
mysql> source employees.sql
.......
+---------------------+
| data_load_time_diff |
+---------------------+
| 00:00:31            |
+---------------------+
1 row in set (0.00 sec)

命令列表

1. docker pull mysql:5.7.36
2. docker run -itd --name mysql_test_5736 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
3. docker ps 
4. docker exec -it b238e18a0a8a /bin/bash
5. git clone https://github.com/datacharmer/test_db
6. unzip xxx.zip
7. docker cp test_db-master mysql_test_5736:/root/
8. mysql -uroot -plocalhost -p123456 -P3307