Dcoker实战-安装打包MySql
-
- 安装mysql
-
- [1. docker hub.上面查找mysql镜像](#1. docker hub.上面查找mysql镜像)
- [2. 从docker hub.上(阿里云加速器)拉取mysql镜像到本地标签为5.7](#2. 从docker hub.上(阿里云加速器)拉取mysql镜像到本地标签为5.7)
- 3.使用mysql:5.7镜像创建容器(也叫运行镜像)
-
- [简单版 -- 有坑版](#简单版 -- 有坑版)
-
- [1. 查看端口 并启动mysql5.7](#1. 查看端口 并启动mysql5.7)
- [2. 进入mysql 建库建表插入数据](#2. 进入mysql 建库建表插入数据)
- [3. 外部Win10也来连接运行在dokcer上的mysql容器实例服务](#3. 外部Win10也来连接运行在dokcer上的mysql容器实例服务)
- [实战版 -- 埋坑版⭐⭐](#实战版 -- 埋坑版⭐⭐)
-
- [1. 新建容器实例](#1. 新建容器实例)
- [2. 新建my.cnf](#2. 新建my.cnf)
- 3.重新启动mysql2容器实例再重新进入并查看字符编码
- 4.再新建库新建表再插入中文测试
- [5.测试删除容器 从新加载数据是否从新显示](#5.测试删除容器 从新加载数据是否从新显示)
- 答疑解惑
- 配置好的mysql上传到私有库进行保存
-
- [1. 查看私有库已存在的镜像](#1. 查看私有库已存在的镜像)
- [2. 推送至私有镜像](#2. 推送至私有镜像)
- [一个问题 -- 待解答 ??](#一个问题 -- 待解答 ??)
-
docker 优点
安装mysql
1. docker hub.上面查找mysql镜像
2. 从docker hub.上(阿里云加速器)拉取mysql镜像到本地标签为5.7
clike
[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
20e4dcae4c69: Pull complete
1c56c3d4ce74: Pull complete
e9f03a1c24ce: Pull complete
68c3898c2015: Pull complete
6b95a940e7b6: Pull complete
90986bb8de6e: Pull complete
ae71319cb779: Pull complete
ffc89e9dfd88: Pull complete
43d05e938198: Pull complete
064b2d298fba: Pull complete
df9a4d85569b: Pull complete
Digest: sha256:4bc6bc963e6d8443453676cae56536f4b8156d78bae03c0145cbe47c2aad73bb
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
crpi-yy9yj7syn08stogn.cn-shanghai.personal.cr.aliyuncs.com/zhenh/zyan 1.1 ca7abefb7af7 40 hours ago 191MB
172.21.47.251:5000/zyan/hgubuntu 1.2 59ab366372d5 2 weeks ago 78.1MB
ubuntu latest 59ab366372d5 2 weeks ago 78.1MB
mysql 5.7 5107333e08a8 10 months ago 501MB
registry latest 75ef5b734af4 13 months ago 25.4MB
redis 6.0.8 16ecd2772934 4 years ago 104MB
3.使用mysql:5.7镜像创建容器(也叫运行镜像)
简单版 -- 有坑版
1. 查看端口 并启动mysql5.7
clike
# 查看端口3306 是否占用
[root@localhost ~]# ps -ef | grep mysql
root 4468 2013 0 12:30 pts/0 00:00:00 grep --color=auto mysql
# 执行 ps -ef | grep mysql 后,只显示了一个进程信息,这个进程实际上是你自己运行的 grep 命令本身。这意味着在执行该命令时,并没有找到其他与 mysql 相关的正在运行的进程。
docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
- 成功启动
2. 进入mysql 建库建表插入数据
语句后面一定要加 ;
clike
mysql> show database; # 错误语句
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1
mysql> show databases
-> ^C
mysql> show databases; # 正确语句
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database db01;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db01 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use db01;
Database changed
mysql> create table t1(id int,name varchar(20));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t1 values(1,'hg');
Query OK, 1 row affected (0.02 sec)
mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | hg |
+------+------+
1 row in set (0.00 sec)
3. 外部Win10也来连接运行在dokcer上的mysql容器实例服务
WIN10插入数据--英文
clike
# 再进入docker服务里的 查看数据 发现 win10操作的 同样可以看到
mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 1 | hg |
| 2 | lisi4 |
+------+-------+
2 rows in set (0.00 sec)
mysql>
WIN10插入数据--中文
- 分析 可能是安装后 和字符集编码相关
clike
mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
mysql>
一定要以容器内部显示的编码格式为依据 删除容器后,里面的nysql数据如何办? 同样,容器数据卷技术也适用于数据库上,需要备份进行持久化存储
实战版 -- 埋坑版⭐⭐
clike
docker run -p 3306:3306 --name mysql -v /zza/mysql/conf:/etc/mysql/conf.d -v /zza/mysql/logs:/logs -v /zza/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
以后类比 , 重要的数据都需要挂在容器数据卷
1. 新建容器实例
clike
docker run -p 3306:3306 --privileged=true -v /zza/mysql/conf:/etc/mysql/conf.d -v /zza/mysql/log:/var/log/log -v /zza/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
2. 新建my.cnf
通过容器卷同步给mysql容器实例
clike
[root@localhost conf]# ls -l
总用量 0
[root@localhost conf]# vim my.cnf
[root@localhost conf]# cat my.cnf
[client]
default_character_set=utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8
[root@localhost conf]#
# 要重启 保证配置文件生效
[root@localhost conf]# docker restart 2390dee74aa9
2390dee74aa9
3.重新启动mysql2容器实例再重新进入并查看字符编码
clike
mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
mysql>
clike
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database db01;
Query OK, 1 row affected (0.00 sec)
mysql> use db01;
Database changed
mysql> create table t1(id int,name varchar(15));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t1(1,'fff');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1,'fff')' at line 1
mysql> insert into t1 values(1,'fff');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | fff |
+------+------+
1 row in set (0.00 sec)
mysql>
4.再新建库新建表再插入中文测试
clike
mysql> select * from t1;
+------+--------+
| id | name |
+------+--------+
| 1 | fff |
| 3 | 昊哥 |
+------+--------+
2 rows in set (0.00 sec)
mysql>
- 结论
5.测试删除容器 从新加载数据是否从新显示
clike
[root@localhost mysql]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2390dee74aa9 mysql:5.7 "docker-entrypoint.s..." 15 minutes ago Up 10 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp fervent_mclean
[root@localhost mysql]# ls -l
总用量 4
drwxr-xr-x. 2 root root 20 10月 30 13:09 conf
drwxr-xr-x. 6 polkitd root 4096 10月 30 13:13 data
drwxr-xr-x. 2 root root 6 10月 30 13:05 log
[root@localhost mysql]# coker rm -f 2390dee74aa9
bash: coker: 未找到命令...
[root@localhost mysql]# docker rm -f 2390dee74aa9
2390dee74aa9
[root@localhost mysql]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost mysql]#
这个实例再次验证了容器卷挂载的必要性 ,即使日常手误删除了相关的操作, 仍有机会进行数据复原
答疑解惑
关于命令挂载那一块
clike
-v /zza/mysql/conf:/etc/mysql/conf.d
-v /zza/mysql/log:/var/log/log
-v /zza/mysql/data:/var/lib/mysql
配置好的mysql上传到私有库进行保存
1. 查看私有库已存在的镜像
- 运行registry 镜像
- 查看镜像
- 查看私有库存在的镜像
clike
[root@localhost conf]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
crpi-yy9yj7syn08stogn.cn-shanghai.personal.cr.aliyuncs.com/zhenh/zyan 1.1 ca7abefb7af7 41 hours ago 191MB
172.21.47.251:5000/zyan/hgubuntu 1.2 59ab366372d5 2 weeks ago 78.1MB
ubuntu latest 59ab366372d5 2 weeks ago 78.1MB
mysql 5.7 5107333e08a8 10 months ago 501MB
registry latest 75ef5b734af4 13 months ago 25.4MB
redis 6.0.8 16ecd2772934 4 years ago 104MB
[root@localhost conf]# docker run -d -p 5000:5000 -v /zzyyuse/myregistry/:/tmp/registry --privileged=true registry
9090b5e984a0ded93b5ff2a00ce4251566ae6422e6162df6b4dfc36caf5a7e92
[root@localhost conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9090b5e984a0 registry "/entrypoint.sh /etc..." 3 seconds ago Up 3 seconds 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp vigilant_euler
78ba41711142 mysql:5.7 "docker-entrypoint.s..." 8 minutes ago Up 8 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp jovial_bell
[root@localhost conf]# curl -XGET http://172.21.47.251:5000/v2/_catalog
{"repositories":[]}
2. 推送至私有镜像
clike
[root@localhost conf]# docker tag 镜像:Tag Host:Port/Repository:Tag
Error parsing reference: "镜像:Tag" is not a valid repository/tag: invalid reference format
[root@localhost conf]# docker tag mysql:5.7 172.21.47.251:5000/mysql_rqj:0.1
[root@localhost conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9090b5e984a0 registry "/entrypoint.sh /etc..." 15 minutes ago Up 15 minutes 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp vigilant_euler
78ba41711142 mysql:5.7 "docker-entrypoint.s..." 23 minutes ago Up 23 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp jovial_bell
[root@localhost conf]#
[root@localhost conf]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
crpi-yy9yj7syn08stogn.cn-shanghai.personal.cr.aliyuncs.com/zhenh/zyan 1.1 ca7abefb7af7 41 hours ago 191MB
172.21.47.251:5000/zyan/hgubuntu 1.2 59ab366372d5 2 weeks ago 78.1MB
ubuntu latest 59ab366372d5 2 weeks ago 78.1MB
172.21.47.251:5000/mysql_rqj 0.1 5107333e08a8 10 months ago 501MB
mysql 5.7 5107333e08a8 10 months ago 501MB
registry latest 75ef5b734af4 13 months ago 25.4MB
redis 6.0.8 16ecd2772934 4 years ago 104MB
[root@localhost conf]# docker push 172.21.47.251:5000/mysql_rqj:0.1
The push refers to repository [172.21.47.251:5000/mysql_rqj]
441e16cac4fe: Pushed
73cb62467b8f: Pushed
337ec6bae222: Pushed
532b66f4569d: Pushed
0d9e9a9ce9e4: Pushed
4555572a6bb2: Pushed
8527ccd6bd85: Pushed
d76a5f910f6b: Pushed
8b2952eb02aa: Pushed
7ff7abf4911b: Pushed
cff044e18624: Pushed
0.1: digest: sha256:4b6c4935195233bc10b617df3cc725a9ddd5a7f10351a7bf573bea0b5ded7649 size: 2618
[root@localhost conf]# curl -XGET http://172.21.47.251:5000/v2/_catalog
{"repositories":["mysql_rqj"]}
[root@localhost conf]#
一个问题 -- 待解答 ??
docker push后 我stop了容器 从新run镜像实例,再次curl查看私有库镜像内容 发现没有任何镜像
bash
[root@localhost conf]# curl -XGET http://172.21.47.251:5000/v2/_catalog
{"repositories":["mysql_rqj"]}
[root@localhost conf]# ^C
[root@localhost conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9090b5e984a0 registry "/entrypoint.sh /etc..." 21 minutes ago Up 21 minutes 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp vigilant_euler
78ba41711142 mysql:5.7 "docker-entrypoint.s..." 29 minutes ago Up 29 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp jovial_bell
[root@localhost conf]# docker stop 9090b5e984a0
9090b5e984a0
[root@localhost conf]# curl -XGET http://172.21.47.251:5000/v2/_catalog
curl: (7) Failed connect to 172.21.47.251:5000; 拒绝连接
[root@localhost conf]# docker run -d -p 5000:5000 -v /zzyyuse/myregistry/:/tmp/registry --privileged=true registry
90fb924e35e3fcef4103f9d7b642e57af281f379988041f466b739028e9e56c4
[root@localhost conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
90fb924e35e3 registry "/entrypoint.sh /etc..." 5 seconds ago Up 4 seconds 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp sharp_fermi
78ba41711142 mysql:5.7 "docker-entrypoint.s..." 30 minutes ago Up 30 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp jovial_bell
[root@localhost conf]# curl -XGET http://172.21.47.251:5000/v2/_catalog
{"repositories":[]}
此问题存疑 等解决了之后再补充!!!!