Linux安装docker以及通过docker安装mysql nginx redis es kibana等

文章目录

    • 1. 安装docker
      • 1.1 查看Linux版本和内核版本(Docker支持64位版本的CentOS 7和CentOS 8及更高版本,它要求Linux内核版本不低于3.10)
      • 1.2 docker自动化安装
      • 1.3 docker手动安装(卸载 设置仓库 安装)
        • 1.3.1 卸载
        • 1.3.2 设置仓库
        • 1.3.3 docker安装
        • 1.3.4 启动docker
    • 2. docker安装mysql
      • 2.1 镜像下载:
      • 2.2 在主机中创建目录
      • 2.3 创建实例并启动
      • 2.4 进入容器查看MySQL情况
      • 2.5 通过远程工具查看连接情况
    • 3. docker安装nginx
      • 3.1 创建目录
      • 3.2 拉取镜像并简单启动nginx
      • 3.3 通过 docker cp 命令将容器内的 nginx 配置文件 copy 到刚创建的主机文件目录中
      • 3.4 停止并删除 nginx01 容器
      • 3.5 重新创建并启动一个nginx容器
    • 4. docker安装redis
      • 4.1 创建目录
      • 4.2 镜像拉取
      • 4.3 创建并启动容器
      • 4.4 进入容器操作redis
      • 4.5 查看远程连接情况
    • 5. docker安装elasticsearch和kibana
      • 5.1 创建目录并授权
      • 5.2 镜像拉取
      • 5.3 启动
      • 5.4 查看启动容器

1. 安装docker

1.1 查看Linux版本和内核版本(Docker支持64位版本的CentOS 7和CentOS 8及更高版本,它要求Linux内核版本不低于3.10)

命令:
查看Linux版本: lsb_release -a 或者 cat /etc/redhat-release
查看内核版本: cat /proc/version 或者 uname -a 或者 uname -r

[root@VM-4-7-centos ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@VM-4-7-centos ~]# uname -r
3.10.0-1160.92.1.el7.x86_64
[root@VM-4-7-centos ~]# 

1.2 docker自动化安装

官方的一键安装方式:

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

国内daocloud一键安装:

curl -sSL https://get.daocloud.io/docker | sh

1.3 docker手动安装(卸载 设置仓库 安装)

1.3.1 卸载

如果之前安装过旧版本的docker,需要卸载:

yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine \
                  docker-ce
1.3.2 设置仓库

设置仓库。新主机上首次安装Docker Engine-Community之前,需要设置Docker仓库。此后可从仓库安装和更新Docker。

在设置仓库之前,需先按照所需的软件包。yum-utils提供了yum-config-manager,并且device mapper存储驱动程序需要device-mapper-persistent-data和lvm2。

sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

执行上述命令,安装完毕即可进行残酷的设置

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

官方的可能比较慢,可以将上述地址设置为国内的

清华大学源:https:**//mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/**docker-ce.repo

至此,仓库设置完毕

1.3.3 docker安装

执行以下命令,安装最新版本的 Docker Engine-Community 和 containerd。

sudo yum install -y docker-ce docker-ce-cli containerd.io

示例:

[root@VM-4-7-centos ~]# sudo yum install -y docker-ce docker-ce-cli containerd.io
已加载插件:fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 containerd.io.x86_64.0.1.6.24-3.1.el7 将被 安装
--> 正在处理依赖关系 container-selinux >= 2:2.74,它被软件包 containerd.io-1.6.24-3.1.el7.x86_64 需要
---> 软件包 docker-ce.x86_64.3.24.0.6-1.el7 将被 安装
--> 正在处理依赖关系 docker-ce-rootless-extras,它被软件包 3:docker-ce-24.0.6-1.el7.x86_64 需要
--> 正在处理依赖关系 libcgroup,它被软件包 3:docker-ce-24.0.6-1.el7.x86_64 需要
---> 软件包 docker-ce-cli.x86_64.1.24.0.6-1.el7 将被 安装
--> 正在处理依赖关系 docker-buildx-plugin,它被软件包 1:docker-ce-cli-24.0.6-1.el7.x86_64 需要
--> 正在处理依赖关系 docker-compose-plugin,它被软件包 1:docker-ce-cli-24.0.6-1.el7.x86_64 需要
--> 正在检查事务
---> 软件包 container-selinux.noarch.2.2.119.2-1.911c772.el7_8 将被 安装
--> 正在处理依赖关系 policycoreutils-python,它被软件包 2:container-selinux-2.119.2-1.911c772.el7_8.noarch 需要
---> 软件包 docker-buildx-plugin.x86_64.0.0.11.2-1.el7 将被 安装
---> 软件包 docker-ce-rootless-extras.x86_64.0.24.0.6-1.el7 将被 安装
--> 正在处理依赖关系 fuse-overlayfs >= 0.7,它被软件包 docker-ce-rootless-extras-24.0.6-1.el7.x86_64 需要
--> 正在处理依赖关系 slirp4netns >= 0.4,它被软件包 docker-ce-rootless-extras-24.0.6-1.el7.x86_64 需要
---> 软件包 docker-compose-plugin.x86_64.0.2.21.0-1.el7 将被 安装
---> 软件包 libcgroup.x86_64.0.0.41-21.el7 将被 安装
--> 正在检查事务
---> 软件包 fuse-overlayfs.x86_64.0.0.7.2-6.el7_8 将被 安装
--> 正在处理依赖关系 libfuse3.so.3(FUSE_3.2)(64bit),它被软件包 fuse-overlayfs-0.7.2-6.el7_8.x86_64 需要
--> 正在处理依赖关系 libfuse3.so.3(FUSE_3.0)(64bit),它被软件包 fuse-overlayfs-0.7.2-6.el7_8.x86_64 需要
--> 正在处理依赖关系 libfuse3.so.3()(64bit),它被软件包 fuse-overlayfs-0.7.2-6.el7_8.x86_64 需要
---> 软件包 policycoreutils-python.x86_64.0.2.5-34.el7 将被 安装
--> 正在处理依赖关系 setools-libs >= 3.3.8-4,它被软件包 policycoreutils-python-2.5-34.el7.x86_64 需要
--> 正在处理依赖关系 libsemanage-python >= 2.5-14,它被软件包 policycoreutils-python-2.5-34.el7.x86_64 需要
--> 正在处理依赖关系 audit-libs-python >= 2.1.3-4,它被软件包 policycoreutils-python-2.5-34.el7.x86_64 需要
--> 正在处理依赖关系 python-IPy,它被软件包 policycoreutils-python-2.5-34.el7.x86_64 需要
--> 正在处理依赖关系 libqpol.so.1(VERS_1.4)(64bit),它被软件包 policycoreutils-python-2.5-34.el7.x86_64 需要
--> 正在处理依赖关系 libqpol.so.1(VERS_1.2)(64bit),它被软件包 policycoreutils-python-2.5-34.el7.x86_64 需要
--> 正在处理依赖关系 libapol.so.4(VERS_4.0)(64bit),它被软件包 policycoreutils-python-2.5-34.el7.x86_64 需要
--> 正在处理依赖关系 checkpolicy,它被软件包 policycoreutils-python-2.5-34.el7.x86_64 需要
--> 正在处理依赖关系 libqpol.so.1()(64bit),它被软件包 policycoreutils-python-2.5-34.el7.x86_64 需要
--> 正在处理依赖关系 libapol.so.4()(64bit),它被软件包 policycoreutils-python-2.5-34.el7.x86_64 需要
---> 软件包 slirp4netns.x86_64.0.0.4.3-4.el7_8 将被 安装
--> 正在检查事务
---> 软件包 audit-libs-python.x86_64.0.2.8.5-4.el7 将被 安装
---> 软件包 checkpolicy.x86_64.0.2.5-8.el7 将被 安装
---> 软件包 fuse3-libs.x86_64.0.3.6.1-4.el7 将被 安装
---> 软件包 libsemanage-python.x86_64.0.2.5-14.el7 将被 安装
---> 软件包 python-IPy.noarch.0.0.75-6.el7 将被 安装
---> 软件包 setools-libs.x86_64.0.3.3.8-4.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

============================================================================================================================================================================================================================================================================
 Package                                                                 架构                                                 版本                                                                     源                                                              大小
============================================================================================================================================================================================================================================================================
正在安装:
 containerd.io                                                           x86_64                                               1.6.24-3.1.el7                                                           docker-ce-stable                                                34 M
 docker-ce                                                               x86_64                                               3:24.0.6-1.el7                                                           docker-ce-stable                                                24 M
 docker-ce-cli                                                           x86_64                                               1:24.0.6-1.el7                                                           docker-ce-stable                                                13 M
为依赖而安装:
 audit-libs-python                                                       x86_64                                               2.8.5-4.el7                                                              os                                                              76 k
 checkpolicy                                                             x86_64                                               2.5-8.el7                                                                os                                                             295 k
 container-selinux                                                       noarch                                               2:2.119.2-1.911c772.el7_8                                                extras                                                          40 k
 docker-buildx-plugin                                                    x86_64                                               0.11.2-1.el7                                                             docker-ce-stable                                                13 M
 docker-ce-rootless-extras                                               x86_64                                               24.0.6-1.el7                                                             docker-ce-stable                                               9.1 M
 docker-compose-plugin                                                   x86_64                                               2.21.0-1.el7                                                             docker-ce-stable                                                13 M
 fuse-overlayfs                                                          x86_64                                               0.7.2-6.el7_8                                                            extras                                                          54 k
 fuse3-libs                                                              x86_64                                               3.6.1-4.el7                                                              extras                                                          82 k
 libcgroup                                                               x86_64                                               0.41-21.el7                                                              os                                                              66 k
 libsemanage-python                                                      x86_64                                               2.5-14.el7                                                               os                                                             113 k
 policycoreutils-python                                                  x86_64                                               2.5-34.el7                                                               os                                                             457 k
 python-IPy                                                              noarch                                               0.75-6.el7                                                               os                                                              32 k
 setools-libs                                                            x86_64                                               3.3.8-4.el7                                                              os                                                             620 k
 slirp4netns                                                             x86_64                                               0.4.3-4.el7_8                                                            extras                                                          81 k

事务概要
============================================================================================================================================================================================================================================================================
安装  3 软件包 (+14 依赖软件包)

总计:108 M
总下载量:61 M
安装大小:383 M
Downloading packages:
No Presto metadata available for docker-ce-stable
警告:/var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-24.0.6-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID 621e9f35: NOKEY==============================================-                                              ] 565 kB/s |  35 MB  00:00:47 ETA 
docker-ce-24.0.6-1.el7.x86_64.rpm 的公钥尚未安装
(1/12): docker-ce-24.0.6-1.el7.x86_64.rpm                                                                                                                                                                                                            |  24 MB  00:00:21     
(2/12): docker-ce-cli-24.0.6-1.el7.x86_64.rpm                                                                                                                                                                                                        |  13 MB  00:00:33     
(3/12): fuse-overlayfs-0.7.2-6.el7_8.x86_64.rpm                                                                                                                                                                                                      |  54 kB  00:00:00     
(4/12): fuse3-libs-3.6.1-4.el7.x86_64.rpm                                                                                                                                                                                                            |  82 kB  00:00:00     
(5/12): libsemanage-python-2.5-14.el7.x86_64.rpm                                                                                                                                                                                                     | 113 kB  00:00:00     
(6/12): libcgroup-0.41-21.el7.x86_64.rpm                                                                                                                                                                                                             |  66 kB  00:00:00     
(7/12): python-IPy-0.75-6.el7.noarch.rpm                                                                                                                                                                                                             |  32 kB  00:00:00     
(8/12): policycoreutils-python-2.5-34.el7.x86_64.rpm                                                                                                                                                                                                 | 457 kB  00:00:00     
(9/12): setools-libs-3.3.8-4.el7.x86_64.rpm                                                                                                                                                                                                          | 620 kB  00:00:00     
(10/12): slirp4netns-0.4.3-4.el7_8.x86_64.rpm                                                                                                                                                                                                        |  81 kB  00:00:00     
(11/12): docker-compose-plugin-2.21.0-1.el7.x86_64.rpm                                                                                                                                                                                               |  13 MB  00:02:29     
(12/12): docker-ce-rootless-extras-24.0.6-1.el7.x86_64.rpm                                                                                                                                                                                           | 9.1 MB  00:04:12     
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                                                                                                                                        228 kB/s |  61 MB  00:04:34     
从 https://download.docker.com/linux/centos/gpg 检索密钥
导入 GPG key 0x621E9F35:
 用户ID     : "Docker Release (CE rpm) "
 指纹       : 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
 来自       : https://download.docker.com/linux/centos/gpg
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : libcgroup-0.41-21.el7.x86_64                                                                                                                                                                                                                           1/17 
  正在安装    : docker-buildx-plugin-0.11.2-1.el7.x86_64                                                                                                                                                                                                               2/17 
  正在安装    : audit-libs-python-2.8.5-4.el7.x86_64                                                                                                                                                                                                                   3/17 
  正在安装    : slirp4netns-0.4.3-4.el7_8.x86_64                                                                                                                                                                                                                       4/17 
  正在安装    : setools-libs-3.3.8-4.el7.x86_64                                                                                                                                                                                                                        5/17 
  正在安装    : libsemanage-python-2.5-14.el7.x86_64                                                                                                                                                                                                                   6/17 
  正在安装    : docker-compose-plugin-2.21.0-1.el7.x86_64                                                                                                                                                                                                              7/17 
  正在安装    : 1:docker-ce-cli-24.0.6-1.el7.x86_64                                                                                                                                                                                                                    8/17 
  正在安装    : python-IPy-0.75-6.el7.noarch                                                                                                                                                                                                                           9/17 
  正在安装    : fuse3-libs-3.6.1-4.el7.x86_64                                                                                                                                                                                                                         10/17 
  正在安装    : fuse-overlayfs-0.7.2-6.el7_8.x86_64                                                                                                                                                                                                                   11/17 
  正在安装    : checkpolicy-2.5-8.el7.x86_64                                                                                                                                                                                                                          12/17 
  正在安装    : policycoreutils-python-2.5-34.el7.x86_64                                                                                                                                                                                                              13/17 
  正在安装    : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch                                                                                                                                                                                                    14/17 
setsebool:  SELinux is disabled.
  正在安装    : containerd.io-1.6.24-3.1.el7.x86_64                                                                                                                                                                                                                   15/17 
  正在安装    : docker-ce-rootless-extras-24.0.6-1.el7.x86_64                                                                                                                                                                                                         16/17 
  正在安装    : 3:docker-ce-24.0.6-1.el7.x86_64                                                                                                                                                                                                                       17/17 
  验证中      : checkpolicy-2.5-8.el7.x86_64                                                                                                                                                                                                                           1/17 
  验证中      : fuse3-libs-3.6.1-4.el7.x86_64                                                                                                                                                                                                                          2/17 
  验证中      : containerd.io-1.6.24-3.1.el7.x86_64                                                                                                                                                                                                                    3/17 
  验证中      : python-IPy-0.75-6.el7.noarch                                                                                                                                                                                                                           4/17 
  验证中      : docker-compose-plugin-2.21.0-1.el7.x86_64                                                                                                                                                                                                              5/17 
  验证中      : libsemanage-python-2.5-14.el7.x86_64                                                                                                                                                                                                                   6/17 
  验证中      : setools-libs-3.3.8-4.el7.x86_64                                                                                                                                                                                                                        7/17 
  验证中      : slirp4netns-0.4.3-4.el7_8.x86_64                                                                                                                                                                                                                       8/17 
  验证中      : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch                                                                                                                                                                                                     9/17 
  验证中      : docker-ce-rootless-extras-24.0.6-1.el7.x86_64                                                                                                                                                                                                         10/17 
  验证中      : 3:docker-ce-24.0.6-1.el7.x86_64                                                                                                                                                                                                                       11/17 
  验证中      : policycoreutils-python-2.5-34.el7.x86_64                                                                                                                                                                                                              12/17 
  验证中      : audit-libs-python-2.8.5-4.el7.x86_64                                                                                                                                                                                                                  13/17 
  验证中      : 1:docker-ce-cli-24.0.6-1.el7.x86_64                                                                                                                                                                                                                   14/17 
  验证中      : docker-buildx-plugin-0.11.2-1.el7.x86_64                                                                                                                                                                                                              15/17 
  验证中      : fuse-overlayfs-0.7.2-6.el7_8.x86_64                                                                                                                                                                                                                   16/17 
  验证中      : libcgroup-0.41-21.el7.x86_64                                                                                                                                                                                                                          17/17 

已安装:
  containerd.io.x86_64 0:1.6.24-3.1.el7                                                      docker-ce.x86_64 3:24.0.6-1.el7                                                      docker-ce-cli.x86_64 1:24.0.6-1.el7                                                     

作为依赖被安装:
  audit-libs-python.x86_64 0:2.8.5-4.el7  checkpolicy.x86_64 0:2.5-8.el7      container-selinux.noarch 2:2.119.2-1.911c772.el7_8  docker-buildx-plugin.x86_64 0:0.11.2-1.el7  docker-ce-rootless-extras.x86_64 0:24.0.6-1.el7  docker-compose-plugin.x86_64 0:2.21.0-1.el7 
  fuse-overlayfs.x86_64 0:0.7.2-6.el7_8   fuse3-libs.x86_64 0:3.6.1-4.el7     libcgroup.x86_64 0:0.41-21.el7                      libsemanage-python.x86_64 0:2.5-14.el7      policycoreutils-python.x86_64 0:2.5-34.el7       python-IPy.noarch 0:0.75-6.el7              
  setools-libs.x86_64 0:3.3.8-4.el7       slirp4netns.x86_64 0:0.4.3-4.el7_8 

完毕!
[root@VM-4-7-centos ~]#

docker-ce为社区免费版本。稍等片刻,docker即可安装成功。但安装完成之后的默认是未启动的, 需要进行启动操作。
如果不需要docker-ce-cli或containerd.io可直接执行如下命令:

yum install -y docker-ce

至此,docker安装完成

1.3.4 启动docker

启动docker:

sudo systemctl start docker

通过运行hello-world镜像来验证是否正确安装了Docker Engine-Community。

// 拉取镜像
sudo docker pull hello-world
// 执行hello-world
sudo docker run hello-world

设置docker开机自启动
命令:

systemctl enable docker.service

效果:

[root@VM-4-7-centos ~]# sudo systemctl start docker
[root@VM-4-7-centos ~]# sudo docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
719385e32844: Pull complete 
Digest: sha256:4f53e2564790c8e7856ec08e384732aa38dc43c52f02952483e3f003afbf23db
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
[root@VM-4-7-centos ~]# sudo docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

[root@VM-4-7-centos ~]# systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@VM-4-7-centos ~]# 

至此,docker安装并启动完成


2. docker安装mysql

2.1 镜像下载:

docker pull mysql:5.7
[root@VM-4-7-centos ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
70e9ff4420fb: Pull complete 
dc7fe6a7bf63: Pull complete 
6c47791d5b2f: Pull complete 
8aa08ad32885: Pull complete 
a74a35dfed64: Pull complete 
03a960f04fce: Pull complete 
966afd0d388e: Pull complete 
d7b1625fdc9d: Pull complete 
3428031e976c: Pull complete 
232612f35735: Pull complete 
da583abbf6d0: Pull complete 
Digest: sha256:a06310bb26d02a6118ae7fa825c172a0bf594e178c72230fc31674f348033270
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@VM-4-7-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       a5b7ceed4074   2 months ago   581MB
hello-world   latest    9c7a54a9a43c   5 months ago   13.3kB
[root@VM-4-7-centos ~]# 

2.2 在主机中创建目录

[root@VM-4-7-centos /]# mkdir mydata
[root@VM-4-7-centos /]# cd mydata/
[root@VM-4-7-centos mydata]# ll
总用量 0
[root@VM-4-7-centos mydata]# mkdir mysql
[root@VM-4-7-centos mydata]# cd mysql/
[root@VM-4-7-centos mysql]# mkdir {data,log,conf}
[root@VM-4-7-centos mysql]# ll
总用量 12
drwxr-xr-x 2 root root 4096 10月 10 10:12 conf
drwxr-xr-x 2 root root 4096 10月 10 10:12 data
drwxr-xr-x 2 root root 4096 10月 10 10:12 log

2.3 创建实例并启动

docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7

示例:

[root@VM-4-7-centos ~]# docker run -p 3306:3306 --name mysql \
> -v /mydata/mysql/log:/var/log/mysql \
> -v /mydata/mysql/data:/var/lib/mysql \
> -v /mydata/mysql/conf:/etc/mysql/conf.d \
> -e MYSQL_ROOT_PASSWORD=root \
> -d mysql:5.7

b01b61121405b7eb5e58030037d5ff988a4ba8f3f7a562d7e4adb32b1273b9af
[root@VM-4-7-centos ~]# 

查看启动的镜像

[root@VM-4-7-centos ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND                   CREATED          STATUS                      PORTS                                                  NAMES
b01b61121405   mysql:5.7     "docker-entrypoint.s…"   3 seconds ago    Up 2 seconds                0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
547414e7390d   hello-world   "/hello"                  13 minutes ago   Exited (0) 13 minutes ago                                                          dreamy_hofstadter
[root@VM-4-7-centos ~]# 

设置开机自启动

[root@VM-4-7-centos mysql]# docker update --restart=always mysql 
mysql
[root@VM-4-7-centos mysql]# 

2.4 进入容器查看MySQL情况

进入容器

[root@VM-4-7-centos mysql]# docker exec -it mysql /bin/bash
//登录mysql
bash-4.2# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, 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> use mysql;
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 user,host from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| root          | %         |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

赋予任何主机访问权限:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

允许指定主机(IP地址)访问权限:

GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'root' WITH GRANT OPTION;

通过GRANT命令赋权后,需要通过FLUSH PRIVILEGES刷新权限表使修改生效:

FLUSH PRIVILEGES;

2.5 通过远程工具查看连接情况

我用的dbever,连接成功即可


3. docker安装nginx

3.1 创建目录

命令: mkdir -p /mydata/nginx/{log,ssl,html,conf/conf.d}

[root@VM-4-7-centos mysql]# mkdir -p /mydata/nginx/{log,ssl,html,conf/conf.d}
[root@VM-4-7-centos mysql]# 

3.2 拉取镜像并简单启动nginx

拉取镜像:

[root@VM-4-7-centos mysql]# docker pull nginx:latest
latest: Pulling from library/nginx
a803e7c4b030: Pull complete 
8b625c47d697: Pull complete 
4d3239651a63: Pull complete 
0f816efa513d: Pull complete 
01d159b8db2f: Pull complete 
5fb9a81470f3: Pull complete 
9b1e1e7164db: Pull complete 
Digest: sha256:32da30332506740a2f7c34d5dc70467b7f14ec67d912703568daff790ab3f755
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

启动:

[root@VM-4-7-centos mysql]# docker run --name nginx01 -d -p 80:80 nginx
eebd3152c9bd699131fa4904ca90bbefe9e0b7896e9344ccd754cf67f89372f9
[root@VM-4-7-centos mysql]# 

3.3 通过 docker cp 命令将容器内的 nginx 配置文件 copy 到刚创建的主机文件目录中

[root@VM-4-7-centos mysql]# docker cp nginx01:/etc/nginx/nginx.conf /mydata/nginx/conf/nginx.conf
                                               Successfully copied 2.56kB to /mydata/nginx/conf/nginx.conf
[root@VM-4-7-centos mysql]# docker cp nginx01:/etc/nginx/conf.d/default.conf /mydata/nginx/conf/conf.d/default.conf
                                               Successfully copied 3.07kB to /mydata/nginx/conf/conf.d/default.conf
[root@VM-4-7-centos mysql]# docker cp nginx01:/usr/share/nginx/html /mydata/nginx
                                               Successfully copied 4.1kB to /mydata/nginx
[root@VM-4-7-centos mysql]# 

3.4 停止并删除 nginx01 容器

[root@VM-4-7-centos mysql]# docker stop nginx01 
nginx01
[root@VM-4-7-centos mysql]# docker rm nginx01 
nginx01
[root@VM-4-7-centos mysql]# 

3.5 重新创建并启动一个nginx容器

[root@VM-4-7-centos mysql]# docker run --name nginx01 -d --restart=always --privileged=true -v /mydata/nginx/html:/usr/share/nginx/html -v /mydata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /mydata/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /mydata/nginx/log:/vat/log/nginx -v /mydata//nginx/ssl:/ssl/ -p 443:443 -p 80:80 nginx
0833e34fc39449ae94a7ccf87bccfbad0e1a82bf9ff14f77ddf52988f6ec46c0
[root@VM-4-7-centos mysql]# docker ps -a
CONTAINER ID   IMAGE         COMMAND                   CREATED          STATUS                      PORTS                                                                      NAMES
0833e34fc394   nginx         "/docker-entrypoint.…"   3 seconds ago    Up 3 seconds                0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   nginx01
b01b61121405   mysql:5.7     "docker-entrypoint.s…"   18 minutes ago   Up 18 minutes               0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp                       mysql
547414e7390d   hello-world   "/hello"                  32 minutes ago   Exited (0) 32 minutes ago                                                                              dreamy_hofstadter
[root@VM-4-7-centos mysql]# 

至此,docker方式安装nginx成功


4. docker安装redis

4.1 创建目录

[root@VM-4-7-centos mysql]# mkdir -p /mydata/redis/{conf,data}
[root@VM-4-7-centos mysql]# 

4.2 镜像拉取

[root@VM-4-7-centos mysql]# docker pull redis
Using default tag: latest
Error response from daemon: Get "https://registry-1.docker.io/v2/library/redis/manifests/sha256:b68c6efe2c5f2d7d7d14a2749f66d6d81645ec0cacb92572b2fb7d5c42c82031": net/http: TLS handshake timeout
[root@VM-4-7-centos mysql]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
a803e7c4b030: Already exists 
57e05a1ddec3: Pull complete 
4635c857038a: Pull complete 
10001c6989b4: Pull complete 
f93feb85ceb4: Pull complete 
4f4fb700ef54: Pull complete 
320349bd761d: Pull complete 
Digest: sha256:b68c6efe2c5f2d7d7d14a2749f66d6d81645ec0cacb92572b2fb7d5c42c82031
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest

4.3 创建并启动容器

[root@VM-4-7-centos mysql]# docker run --restart=always --log-opt max-size=100m --log-opt max-file=2 -p 6379:6379 --name redis -v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf -v /mydata/redis/data:/data -d redis redis-server /etc/redis/redis.conf  --appendonly yes  --requirepass root
566acfa83077b69434088a3eafbb986b7a2a5735140e776e0bd1dddd13ed1b9d
[root@VM-4-7-centos mysql]# docker ps -a
CONTAINER ID   IMAGE         COMMAND                   CREATED          STATUS                      PORTS                                                                      NAMES
566acfa83077   redis         "docker-entrypoint.s…"   4 seconds ago    Up 2 seconds                0.0.0.0:6379->6379/tcp, :::6379->6379/tcp                                  redis
0833e34fc394   nginx         "/docker-entrypoint.…"   27 minutes ago   Up 27 minutes               0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   nginx01
b01b61121405   mysql:5.7     "docker-entrypoint.s…"   45 minutes ago   Up 45 minutes               0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp                       mysql
547414e7390d   hello-world   "/hello"                  59 minutes ago   Exited (0) 59 minutes ago                                                                              dreamy_hofstadter
[root@VM-4-7-centos mysql]# 

4.4 进入容器操作redis

[root@VM-4-7-centos mysql]# docker exec -it redis /bin/bash
root@566acfa83077:/data# redis-cli -h 127.0.0.1 -p 6379 -a root
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379>

4.5 查看远程连接情况

成功连接即可


5. docker安装elasticsearch和kibana

5.1 创建目录并授权

创建目录

[root@VM-4-7-centos conf]# mkdir -p /mydata/elasticsearch/{config,data,plugins}
[root@VM-4-7-centos conf]# cd ../..
[root@VM-4-7-centos mydata]# cd elasticsearch/
[root@VM-4-7-centos elasticsearch]# ll
总用量 12
drwxr-xr-x 2 root root 4096 10月 10 11:09 config
drwxr-xr-x 2 root root 4096 10月 10 11:09 data
drwxr-xr-x 2 root root 4096 10月 10 11:09 plugins
[root@VM-4-7-centos elasticsearch]# cd ..

授权

[root@VM-4-7-centos mydata]# chmod 777 elasticsearch/
[root@VM-4-7-centos mydata]# ll
总用量 16
drwxrwxrwx 5 root root 4096 10月 10 11:09 elasticsearch
drwxr-xr-x 5 root root 4096 10月 10 10:12 mysql
drwxr-xr-x 6 root root 4096 10月 10 10:28 nginx
drwxr-xr-x 4 root root 4096 10月 10 10:53 redis
[root@VM-4-7-centos mydata]#

添加配置文件

[root@VM-4-7-centos mydata]# echo "http.host: 0.0.0.0" >> /mydata/elasticsearch/config/elasticsearch.yml
[root@VM-4-7-centos mydata]# cd elasticsearch/
[root@VM-4-7-centos elasticsearch]# ll
总用量 12
drwxr-xr-x 2 root root 4096 10月 10 11:10 config
drwxr-xr-x 2 root root 4096 10月 10 11:09 data
drwxr-xr-x 2 root root 4096 10月 10 11:09 plugins
[root@VM-4-7-centos elasticsearch]# cd config/
[root@VM-4-7-centos config]# ll
总用量 4
-rw-r--r-- 1 root root 19 10月 10 11:10 elasticsearch.yml
[root@VM-4-7-centos config]# 

5.2 镜像拉取

elasticsearch镜像

[root@VM-4-7-centos config]# docker pull elasticsearch:7.8.0
7.8.0: Pulling from library/elasticsearch
524b0c1e57f8: Pull complete 
7a096b8f20be: Pull complete 
9dd8117fbfec: Pull complete 
335891dbdd0e: Pull complete 
dfce820717b4: Pull complete 
82d3459719f7: Pull complete 
2e79822fece3: Pull complete 
2f80b981dd6a: Pull complete 
05f8a08da0ba: Pull complete 
Digest: sha256:945f80960f2ad1bd4b88bd07a9ba160d22d4285bbc8a720d052379006d5d57a6
Status: Downloaded newer image for elasticsearch:7.8.0
docker.io/library/elasticsearch:7.8.0
[root@VM-4-7-centos config]# docker pull kibana:7.8.0
Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

kibana镜像

[root@VM-4-7-centos config]# docker pull kibana:7.8.0
7.8.0: Pulling from library/kibana
524b0c1e57f8: Already exists 
6c3c85c9f549: Pull complete 
75826be8d25e: Pull complete 
f4951867bd56: Pull complete 
e0af11a7b96b: Pull complete 
1f81b8aaeaf3: Pull complete 
1c52e407927e: Pull complete 
6ca195712ee9: Pull complete 
94650bdbb5bf: Pull complete 
c978c1a82f00: Pull complete 
50fe1f68e816: Pull complete 
Digest: sha256:326ac27cbc363e515e1b660d3117bbad3c5cd140db358ade251195c094db6fda
Status: Downloaded newer image for kibana:7.8.0
docker.io/library/kibana:7.8.0
[root@VM-4-7-centos config]# 

5.3 启动

启动elasticsearch

[root@VM-4-7-centos config]# docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
> -e "discovery.type=single-node" \
> -e ES_JAVA_OPTS="-Xms64m -Xmx512m" \
> -v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
> -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
> -v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
> -d elasticsearch:7.8.0
6256fb0630b688ef49e7ea7935d34a81520095e0a740ef8e71e324a1883d74c3

启动kibana

[root@VM-4-7-centos config]# docker run --name kibana -e ELASTICSEARCH_HOSTS=http://43.143.114.178:9200 -p 5601:5601 \
> -d kibana:7.8.0
8e564a97f0f10ccc95e91834b3def4ffb3c3c7719bb456a233483007c3911156

5.4 查看启动容器

[root@VM-4-7-centos config]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND                   CREATED             STATUS             PORTS                                                                                  NAMES
80acdf6ca7c8   elasticsearch:7.8.0   "/tini -- /usr/local…"   3 seconds ago       Up 2 seconds       0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp   elasticsearch
8e564a97f0f1   kibana:7.8.0          "/usr/local/bin/dumb…"   2 minutes ago       Up 2 minutes       0.0.0.0:5601->5601/tcp, :::5601->5601/tcp                                              kibana
566acfa83077   redis                 "docker-entrypoint.s…"   20 minutes ago      Up 20 minutes      0.0.0.0:6379->6379/tcp, :::6379->6379/tcp                                              redis
0833e34fc394   nginx                 "/docker-entrypoint.…"   47 minutes ago      Up 47 minutes      0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp               nginx01
b01b61121405   mysql:5.7             "docker-entrypoint.s…"   About an hour ago   Up About an hour   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp                                   mysql
[root@VM-4-7-centos config]# 

至此,elasticsearch以及kibana安装成功

你可能感兴趣的:(linux,docker,mysql)