一.创建网络
# 创建网络
docker network create liming
# 查看网络
docker network ls
二.部署mysql
删除并重新创建mysql容器,并完成本地目录挂载:
- 挂载
/software/mysql/data
到容器内的/var/lib/mysql
目录 - 挂载
/software/mysql/init
到容器内的/docker-entrypoint-initdb.d
目录(初始化的SQL脚本目录) - 挂载
/software/mysql/conf
到容器内的/etc/mysql/conf.d
目录(这个是MySQL配置文件目录)
配置MySQL的默认编码
[client]
default_character_set=utf8mb4
[mysql]
default_character_set=utf8mb4
[mysqld]
character_set_server=utf8mb4
collation_server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
初始化SQL脚本目录中增加sql脚本
我们直接将初始化SQL脚本和配置文件上传至虚拟机的/software
目录下:
本地目录挂载
# 1.删除原来的MySQL容器
docker rm -f mysql
# 2.进入software目录
cd /software
# 3.创建并运行新mysql容器,挂载本地目录
docker run -d \
--name mysql \
-p 3306:3306 \
-e TZ=Asia/Shanghai \
-e MYSQL_ROOT_PASSWORD=123 \
-v ./mysql/data:/var/lib/mysql \
-v ./mysql/conf:/etc/mysql/conf.d \
-v ./mysql/init:/docker-entrypoint-initdb.d \
mysql
# 将mysql容器加入到自己的网络
docker network connect liming mysql
# 4.查看root目录,可以发现~/mysql/data目录已经自动创建好了
ls -l mysql
# 结果:
total 12
drwxr-xr-x 2 root root 4096 Nov 6 16:16 conf
drwxr-xr-x 7 polkitd root 4096 Nov 6 16:20 data
drwxr-xr-x 2 root root 4096 Nov 6 16:16 init
# 查看data目录,会发现里面有大量数据库数据,说明数据库完成了初始化
ls -l data
# 5.查看MySQL容器内数据
# 5.1.进入MySQL
docker exec -it mysql mysql -uroot -p123
# 5.2.查看编码表
show variables like "%char%";
# 5.3.结果,发现编码是utf8mb4没有问题
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8mb3 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
# 6.查看数据
# 6.1.查看数据库
show databases;
# 结果,hmall是黑马商城数据库
+--------------------+
| Database |
+--------------------+
| hmall |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
# 6.2.切换到hmall数据库
use hmall;
# 6.3.查看表
show tables;
# 结果:
+-----------------+
| Tables_in_hmall |
+-----------------+
| address |
| cart |
| item |
| order |
| order_detail |
| order_logistics |
| pay_order |
| user |
+-----------------+
# 6.4.查看address表数据
+----+---------+----------+--------+----------+-------------+---------------+-----------+------------+-------+
| id | user_id | province | city | town | mobile | street | contact | is_default | notes |
+----+---------+----------+--------+----------+-------------+---------------+-----------+------------+-------+
| 59 | 1 | 北京 | 北京 | 朝阳区 | 13900112222 | 金燕龙办公楼 | 李佳诚 | 0 | NULL |
| 60 | 1 | 北京 | 北京 | 朝阳区 | 13700221122 | 修正大厦 | 李佳红 | 0 | NULL |
| 61 | 1 | 上海 | 上海 | 浦东新区 | 13301212233 | 航头镇航头路 | 李佳星 | 1 | NULL |
| 63 | 1 | 广东 | 佛山 | 永春 | 13301212233 | 永春武馆 | 李晓龙 | 0 | NULL |
+----+---------+----------+--------+----------+-------------+---------------+-----------+------------+-------+
4 rows in set (0.00 sec)
三.部署nginx(采用数据卷方式)
创建nginx容器并完成两个挂载:
- 把/software/nginx/conf挂载到/etc/nginx/nginx.conf
- 把/software/nginx/html挂载到/usr/share/nginx/html
# 1.拉取Nginx镜像
docker pull nginx
# 2.创建容器
docker run -d --name nginx -p 80:80 nginx
# 3.再software创建文件
mkdir -p ./nginx/{conf,html,logs}
# 4.将容器内的nginx.conf与default.conf文件分别拷贝到宿主机/software/nginx和/software/nginx/conf下,分别执行 cp:命令代表复制 nginx:是我们nginx容器的ID
docker cp nginx:/etc/nginx/nginx.conf ./
docker cp nginx:/etc/nginx/conf.d/default.conf ./conf/
# 5.删除刚才创建的容器
docker rm -f nginx
# 6.重新创建nginx容器
# -v 挂载目录,格式 -v: 表示将宿主机目录与容器目录之间进行共享,
# --privileged=true 容器内部对挂载的目录拥有读写等特权
docker run -d \
--name nginx \
-p 80:80 \
-v /software/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /software/nginx/logs:/var/log/nginx \
-v /software/nginx/html:/usr/share/nginx/html \
-v /software/nginx/conf:/etc/nginx/conf.d \
--privileged=true \
nginx
四.部署redis
# 第1步,拉取Nginx镜像
docker pull redis
# 第2步,创建并允许Nginx容器
docker run -d \
--name redis \
-p 6379:6379 \
--network liming \
redis