【DOCKER】WIN10发布SPRINGBOOT项目(二)

2021-11-09  本文已影响0人  地主家有30头牛

在DOCKER建一个MYSQL的容器,并建立一张表。

1。摘取DOCKER自带的MYSQL镜像

docker pull mysql:5.7

拉取成功后,用docker images查看,可以看到多了一个mysql:5.7的镜像


图片.png

2.启动mysql容器
➤先在宿主机的C:/WORK/docker/mysql/conf目录下,放一个以前运行正常的mysql配置文件,并把改名成mysqld.cnf
这里一知半解的时候搞了个乌龙,一直以为docker -v是把docker容器里的内容先挂载到宿主机目录,没想到是反过来,所以本地目录为空的话是会把容器里的目录给覆盖的。补救的时候不论是改宿主机端的还是直接在容器端的配置文件,中文配置一直没有成功,就放弃重建镜像了。

执行命令

docker run  --name mysql -p 3307:3306 -v C:/WORK/docker/mysql/conf:/etc/mysql/mysql.conf.d -v C:/WORK/docker/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7

➤如果报以下错误,可能是DOCKER的端口号已占用,可以尝试修改DOCKER的端口号再执行

5411a2b1a7f928575302006a485aad9a77642add55f1b4f596b24364d835c548
docker: Error response from daemon: failed to create endpoint mysql on network nat: failed during hnsCallRawResponse: hnsCall failed in Win32: The process cannot access the file because it is being used by another process. (0x20).

➤尝试过程中,遇到一个不能理解的现象。
第一次启动MYSQL的时候,遇到了上面说的端口占用的报错,那我的理解是报错即说明未启动成功,所以我改了端口号再执行,接着就报了mysql容器重复的错,用docker ps -a查询了一下,端口占用的那次执行其实已经创建了一个小mysql的容器,导致我下次执行就变成了容器重复错误。。。

➤mysql容器启动命令执行成功后

C:\Users>docker run  --name mysql -p 3307:3306 -v C:/WORK/docker/mysql/conf:/etc/mysql/mysql.conf.d -v C:/WORK/docker/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (windows/amd64) and no specific platform was requested
66ae97a7c6777bbdff1eae88ef6f74881a58c82ffa45eb24f1ee069ab7d024c3

但使用docker ps查询时并没有看到运行中的容器

C:\Users>docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

使用docker ps -a可以看到,该容器是Exited状态

C:\Users>docker ps -a
CONTAINER ID   IMAGE                          COMMAND                   CREATED          STATUS                               PORTS                    NAMES
66ae97a7c677   mysql:5.7                      "docker-entrypoint.s…"    17 seconds ago   Exited (4294967295) 11 seconds ago                            mysql

使用docker logs -f 容器ID查询了一下容器LOG,看错误提示是data文件夹下已有数据,因为是新建的mysql,所以简单粗暴直接物理删除

C:\Users>docker logs -f 66ae97a7c677
2021-11-10 05:45:55+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
2021-11-10 05:45:55+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2021-11-10 05:45:55+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
2021-11-10 05:45:55+00:00 [Note] [Entrypoint]: Initializing database files
2021-11-10T05:45:55.798437Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2021-11-10T05:45:55.798572Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
2021-11-10T05:45:55.798795Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-11-10T05:45:55.804553Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2021-11-10T05:45:55.804636Z 0 [ERROR] Aborting

因为容器已经存在,所以用docker start 窗口ID来启动,显示成功

C:\Users>docker start 66ae97a7c677
66ae97a7c677

再用docker ps来检查一下,可以看见启动成功了

C:\Users>docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED       STATUS              PORTS                               NAMES
66ae97a7c677   mysql:5.7   "docker-entrypoint.s…"   3 hours ago   Up About a minute   33060/tcp, 0.0.0.0:3307->3306/tcp   mysql

这个时候再去查看data文件夹,跟之前只有一个二进制文件不一样,已经是很有可读性的目录结构,跟自己安装mysql类似了


图片.png

3.建数据库,表
进入容器:docker exec -it mysql env LANG=C.UTF-8 /bin/bash
登录mysql:mysql -uroot -proot

C:\Users>docker exec -it mysql bash
root@66ae97a7c677:/# mysql -uroot -proot
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 3
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>

后面的操作就跟平时操作mysql一样了,造完数据后,来查询一下

mysql> select * from pms_brand
    -> ;
+----+------+--------------+------+----------------+-------------+---------------+-----------------------+--------------------------------------------------------------------------------+---------+-------------------+
| id | name | first_letter | sort | factory_status | show_status | product_count | product_comment_count | logo                                                                           | big_pic | brand_story       |
+----+------+--------------+------+----------------+-------------+---------------+-----------------------+--------------------------------------------------------------------------------+---------+-------------------+
|  1 |      | W            |    0 |              1 |           1 |           100 |                   100 | http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg(5).jpg |         | Victoria's Secret |
+----+------+--------------+------+----------------+-------------+---------------+-----------------------+--------------------------------------------------------------------------------+---------+-------------------+
1 row in set (0.00 sec)

mysql>

现在来重启一下mysql容器,看看数据是否还在。
退出mysql:exit;
退出容器:ctrl+D
重启容器:docker restart 容器ID

mysql> exit
Bye
root@66ae97a7c677:/# exit

C:\Users>docker restart 66ae97a7c677
66ae97a7c677
C:\Users>docker exec -it mysql bash
root@66ae97a7c677:/# mysql -uroot -proot
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 2
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> use mall_tiny
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from pms_brand;
+----+------+--------------+------+----------------+-------------+---------------+-----------------------+--------------------------------------------------------------------------------+---------+-------------------+
| id | name | first_letter | sort | factory_status | show_status | product_count | product_comment_count | logo                                                                           | big_pic | brand_story       |
+----+------+--------------+------+----------------+-------------+---------------+-----------------------+--------------------------------------------------------------------------------+---------+-------------------+
|  1 |      | W            |    0 |              1 |           1 |           100 |                   100 | http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg(5).jpg |         | Victoria's Secret |
+----+------+--------------+------+----------------+-------------+---------------+-----------------------+--------------------------------------------------------------------------------+---------+-------------------+
1 row in set (0.00 sec)

➤突然发现mysql没有显示中文,修改一下mysql的配置文件

为了在容器里安装vim进行了漫长的探索,网上找到的国内源也太老了,都是执行一半拉跨。
终于找到一个比较新的是可以成功的

echo 'deb http://mirrors.163.com/debian/ stretch main non-free contrib' > /etc/apt/sources.list
echo 'deb http://mirrors.163.com/debian/ stretch-updates main non-free contrib' >> /etc/apt/sources.list
echo 'deb http://mirrors.163.com/debian-security/ stretch/updates main non-free contrib' >> /etc/apt/sources.list

参考:https://www.jianshu.com/p/2db874ab7bbd

把/etc/apt/sources.list里的源替换了以后,就可以愉快的安装vim了
1)apt-get update
2)apt-get install libtinfo5
3)apt-get install vim

vim安装成功以后,执行
vim /etc/mysql/mysql.conf.d/mysqld.cnf

上一篇下一篇

猜你喜欢

热点阅读