前言:在部署hive或airflow 升级过程中,总需要一个对应的数据库存储元数据,一个轻量级的mysql容器刚刚好。轻量、可快速移植、具有隔离性。
# redhat版
> cat /etc/redhat-release # redhat系列存在此文件
'CentOS Linux release 7.9.2009 (Core)'
# Ubuntu
> lsb_release -a
#1. 有网
> yum install -y docker
#2. 离线(tar包下载地址 https://download.docker.com/linux/static/stable/x86_64/)
# 解压(解压后生成 docker 文件夹)
> tar -zxvf docker-23.0.6.tgz
# 将解压后的命令全部加入到 $PATH 否则 start docker 会报错 (因为dockerd 服务启动时,会自动启动其他服务如containerd)
> sudo ln -s /home/zyp/opt/docker/* /usr/bin
# 注册系统服务
> sudo vim /etc/systemd/system/docker.service
'
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd # 启动主命令
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
'
# docker 相关配置
> sudo mkdir /etc/docker
> sudo vim /etc/docker/daemon.json
'
{
"data-root": "/home/zyp/lib/docker_data" # docker 数据保存地址默认保存地址/var/lib/docker
}
'
# 给系统服务添加执行权限
> sudo chmod +x /etc/systemd/system/docker.service
# 重载配置文件c
> sudo systemctl daemon-reload
# 启动
> sudo systemctl start docker
# 启动时报错,查看日志(journalctl -xe:查看系统日志,并输出最近的错误消息和事件)
> sudo journalctl -xe
# 报错,docker 用户组不存在
'could not change group /var/run/docker.sock to docker: group docker not found'
# 解决添加用户组
> sudo groupadd docker
# 添加自己账户名进docker 用户组,目的:不使用 sudo 访问docker服务
> sudo usermod 自己用户名 -aG docker
# 查看 docker 启动信息 #dockerd 服务本地默认使用unix 套接字进行通讯
> docker info
"'
Client:
Context: default
Debug Mode: false
Server:
Containers: 0 # 容器数量
Running: 0
Paused: 0
Stopped: 0
Images: 0 # 镜像数量
Server Version: 23.0.6
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc io.containerd.runc.v2
Default Runtime: runc
Init Binary: docker-init
containerd version: 3dce8eb055cbb6872793272b4f20ed16117344f8
runc version: v1.1.7-0-g860f061
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 3.10.0-1160.42.2.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64 # 系统信息
CPUs: 18
Total Memory: 35.2GiB
Name: dp96
ID: 1334551c-2ea3-4711-b0a6-b6cee4c63566
Docker Root Dir: /home/zyp/lib/docker_data # docker数据存储指定目录(默认/var/lib/docker)
Debug Mode: false
Registry: https://index.docker.io/v1/ # 默认镜像仓库
Experimental: false
Insecure Registries: 127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine
'"
docker version
:检查Docker客户端和服务端版本。docker info
:显示有关Docker系统的详细信息,包括运行中的容器数量、镜像数量、存储驱动等。docker images
:列出本地所有的镜像。docker pull
:从Docker Hub或其他注册表下载镜像。docker run
:创建并启动一个新的容器。docker ps
:列出正在运行的容器。 # 查看所有容器 docker ps -adocker logs
:查看容器的日志输出。docker stop
:停止容器。docker rm
:删除容器。docker rmi
:删除本地的一个或多个镜像。docker exec
:在运行中的容器中执行命令。docker build
:通过Dockerfile构建一个新的镜像。docker save imageid > newname
: 保存镜像到本地docker load < newname
: 加载本地镜像# 1. 有网
> docker pull mysql:5.7
# 2. 离线 (镜像市场 网易峰巢 https://c.163yun.com/hub#/library/repository;)
# 找有网主机拉取相应镜像
> docker pull hub.c.163.com/library/mysql:5.7
# 保存镜像
> docker save 9e64176cd8a2 > mysql5.7
# 离线传输 并 加载镜像
> docker load < mysql5.7
# 加载后镜像重命名(加载后 REPOSITORY TAG 为<none>)
> docker tag 9e64176cd8a2 mysql:5.7
# 初次启动容器
> docker run --name mysql5.7 # 定义容器名称
-e MYSQL_ROOT_PASSWORD='123456' # mysql root账户密码
-p 3306:3306 # 将主机端口3306映射到容器MySQL服务端口
-d mysql:5.7 # 镜像文件
# 进入容器内确认容器内MySQL 配置文件、数据存储地址、日志地址
> docker exec -it mysql5.7 bash
# 将mysql 容器中的配置文件、初始化日志、初始化数据 复制到宿主机上
> docker cp mysql5.7:/etc/mysql/. /home/zyp/lib/mysql/conf
> docker cp mysql5.7:/var/log/. /home/zyp/lib/mysql/log
> docker cp mysql5.7:/var/lib/mysql/. /home/zyp/lib/mysql/datadir
# 删除已启动的容器
> docker stop mysql5.7
> docker rm -f mysql5.7
# 以卷映射的方式重新创建并启动 mysql 容器
#(原因:容器运行停止后内数据会自动清空,将容器内相关文件进映射到宿主机,可持久化存储数据)
> docker run --name mysql5.7 # 定义容器名称
-e MYSQL_ROOT_PASSWORD='123456' # mysql root账户密码
-p 3306:3306 # 将主机端口3306映射到容器MySQL服务端口
-v /home/zyp/lib/mysql/conf:/etc/mysql # 将宿主机mysql配置文件、日志、数据路径挂载到容器中,实现数据持久化存储
-v /home/zyp/lib/mysql/datadir:/var/lib/mysql
-v /home/zyp/lib/mysql/log:/var/log
-d mysql:5.7 # 镜像文件
import pymysql
conn = pymysql.connect(host='10.18.xx.xx',port=3306,user='root',password='123456',database='mysql',charset='utf8')
cursor = conn.cursor()
cursor.execute('show tables;')
print(cursor.fetchall())
cursor.close()
conn.close()