一、镜像下载
1、在线下载
在一台能连外网的linux上执行docker镜像拉取命令
docker pull mysql:8.0.41
2、离线包下载
两种方式:
方式一:
-)在一台能连外网的linux上安装docker执行第一步的命令下载镜像
-)导出
# 导出镜像到本地当前目录
docker save -o mysql-8.0.41.tar mysql:8.0.41
方式二:
-)Window桌面程序:Docker Desktop
-)下载镜像
-)安装镜像导出插件
-)将镜像文件保存到window本地(图略)
3、Oracle Container Registry
Home
4、官方安装程序下载
不是docker镜像
https://dev.mysql.com/downloads/mysql/
二、安装
1、在线安装
见离线安装(只比离线安装少一步导入动作)
2、离线安装
-)导入镜像
docker load -i mysql_8_0_38.tar
-)查看镜像
[docker@GZ***-PM153160 mysql]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.0.38 6c54cbcf775a 6 months ago 572MB、
-)创建映射目录
创建mysql配置文件目录、数据文件目录,并修改目录权限
sudo mkdir -p /opt/mysql/docker-mysql/conf
sudo mkdir -p /data1/mysql/docker-mysql/data
sudo chown -R docker.docker /opt/mysql
sudo chown -R docker.docker /data1
-)启动容器
docker run -d \
-p 3307:3306 \
-v /opt/mysql/docker-mysql/conf:/etc/mysql/conf.d \
-v /data1/mysql/docker-mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=F1c1X****** \
--name mysql8.0.38 \
--restart=always \
mysql:8.0.38
-)查看容器
[docker@GZCRM-PM153160 mysql]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
48ea6d74fc20 mysql:8.0.38 "docker-entrypoint.s…" 11 minutes ago Up 11 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp, [::]:3307->3306/tcp mysql8.0.38
-)从容器内部拷贝配置文件
这一步和下一步可跳过(如果嫌麻烦),如果需要添加时区配置,可简单在容器中修改配置文件
[docker@GZ***-PM153160 ~]$ docker cp 48ea6d74fc20:/etc/my.cnf /opt/mysql/docker-mysql/conf/my.cnf
Successfully copied 3.07kB to /opt/mysql/docker-mysql/conf/my.cnf
-)添加时区配置(中国大陆)
vi /opt/mysql/docker-mysql/conf/my.cnf
default-time-zone='+08:00'
-)重启容器
[docker@GZ***-PM153160 ~]$ docker restart 48ea6d74fc20
48ea6d74fc20
三、使用/基本操作
-)进入容器
[docker@GZ***-PM153160 ~]$ docker exec -it 48ea6d74fc20 /bin/bash
bash-5.1#
bash-5.1#
-)命令登陆mysql
[docker@GZ***-PM153160 ~]$ docker exec -it 48ea6d74fc20 /bin/bash
bash-5.1#
bash-5.1#
bash-5.1# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.38 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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> CREATE DATABASE nacos;
Query OK, 1 row affected (0.00 sec)
-)创建用户
# 同时指定密码
CREATE USER 'nacos'@'%' IDENTIFIED BY '#!FlGO****';
-)授权
mysql> GRANT ALL PRIVILEGES ON nacos.* TO 'nacos'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
遇到的问题总结
1、将镜像推到内网镜像仓库时报错
[zhao****@GZ***-PM153160 ~]$ docker push 10.130.***.106:30089/library/zookeeper:3.9.3
The push refers to repository [10.130.153.***:30089/library/zookeeper]
Get "https://10.130.153.***:30089/v2/": x509: cannot validate certificate for 10.130.153.*** because it doesn't contain any IP SANs
解决:换成域名
[zhao***@GZ***-PM153160 ~]$ docker login harbor.***.com:30089
Username: admin
Password:
WARNING! Your password will be stored unencrypted in /home/zhaozheng/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credential-stores
Login Succeeded
可能会用到的其他命令
-)创建卷
docker volume create mysql_data
-)验证卷
docker volume ls
-)使用卷
docker run --name some-mysql -v mysql_data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
这种docker卷作者没有用过,有兴趣的可自行尝试~
如果喜欢我的文章,请帮我点赞,谢谢~