【在Centos7.6上使用docker 安装MySQL8.0】

一、Docker的安装

1.从阿里下载

[root@localhost ~]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
#这条命令下载完成后,会把repo包放在/etc/yum.repos.d/下

在这里插入图片描述
使用命令可以查看到我们的repo包,如果没有,说明没有下载成功:**

[root@localhost yum.repos.d]# yum repolist

在这里插入图片描述

2.安装Docker

[root@localhost ~]# yum -y install docker-ce
# 使用yum安装

3.启动Docker并查看版本

3.1.启动Docker

[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl status docker
[root@localhost ~]# ps -ef |grep docker

在这里插入图片描述
3.2.查看版本

root@localhost ~]# docker --version
Docker version 23.0.1, build a5ee5b1
[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           23.0.1
 API version:       1.42
 Go version:        go1.19.5
 Git commit:        a5ee5b1
 Built:             Thu Feb  9 19:51:00 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          23.0.1
  API version:      1.42 (minimum version 1.12)
  Go version:       go1.19.5
  Git commit:       bc3805a
  Built:            Thu Feb  9 19:48:42 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.18
  GitCommit:        2456e983eb9e37e47538f59ea18f2043c9a73640
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

二、使用Docker安装MySQL8.0

1.拉取MySQL镜像

[root@localhost ~]# docker search mysql --limit 3
NAME      DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   13891     [OK]       
mariadb   MariaDB Server is a high performing open sou…   5296      [OK]       
percona   Percona Server is a fork of the MySQL relati…   600       [OK]

这里有个percona,Percona是一个开源的数据库管理系统,提供了多个解决方案和服务,尤其是针对MySQL、MongoDB和其他数据库管理系统。

2.开始拉取

[root@localhost ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
b4ddc423e046: Pull complete 
b338d8e4ffd1: Pull complete 
b2b1b06949ab: Pull complete 
daf393284da9: Pull complete 
1cb8337ae65d: Pull complete 
f6c2cc79221c: Pull complete 
4cec461351e0: Pull complete 
ab6bf0cba08e: Pull complete 
8df43cafbd11: Pull complete 
c6d0aac53df5: Pull complete 
b24148c7c251: Pull complete 
Digest: sha256:d8dc78532e9eb3759344bf89e6e7236a34132ab79150607eb08cc746989aa047
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

#查看拉取下来的镜像:
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mysql        latest    4f06b49211c0   10 days ago   530MB

3.创建容器

[root@localhost ~]# docker run -id --name=mysql8 -p 3306:3306 MYSQL_ROOT_PASSWORD=root mysql:latest
31bf6681e266d7ae625dc16e66ed3628d1631a35d486abf2c324e4c01d1e7042
#这个命令将创建一个名为 “mysql8” 的新 Docker 容器,使用 “mysql” 镜像,将 root 密码设置为 root,并将 MySQL 端口从容器映射到主机。

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS                                                  NAMES
fad4cb40e4a9   mysql:latest   "docker-entrypoint.s…"   3 seconds ago   Up 3 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql8

备注:

-i:保持 STDIN 开放,即使容器未连接。这个选项允许我们在需要的时候与容器的命令行进行交互。

-d:告诉 Docker 在后台运行容器并将容器 ID 打印到控制台。当我们不想直接与容器交互,只想让它在后台运行时,这个选项非常有用。
–name=mysql8:给容器分配一个名称。在这种情况下,容器将被命名为 “mysql8”。

-p 3306:3306:将主机上的端口 3306 映射到容器内部的端口 3306。这是必需的,以允许连接到容器内运行的 MySQL 数据库。

-e MYSQL_ROOT_PASSWORD=root:在容器内设置一个名为 MYSQL_ROOT_PASSWORD 的环境变量,并将其值设置为 root。这将为容器内运行的 MySQL 数据库设置 root 密码。

mysql:latest:指定要为容器使用的 Docker 镜像的名称。在这种情况下,镜像是 mysql,并且我们正在使用 Docker Hub 上可用的最新版本。

#翻译成英文模式:
docker run: This tells Docker to create and start a new container from a specified image.

-i: This specifies that we want to keep STDIN open even if the container is not attached, which allows us to interact with the container's command line later if needed.

-d: This tells Docker to run the container in the background and print the container ID to the console. This option is useful when we don't want to interact with the container directly and just want it to run in the background.

--name=mysql8: This assigns a name to the container. In this case, the container will be named "mysql8".

-p 3306:3306: This maps port 3306 on the host to port 3306 inside the container. This is necessary to allow connections to the MySQL database running inside the container.

-e MYSQL_ROOT_PASSWORD=root: This sets an environment variable inside the container called MYSQL_ROOT_PASSWORD with a value of root. This sets the root password for the MySQL database running inside the container.

mysql:latest: This specifies the name of the Docker image to use for the container. In this case, the image is mysql and we're using the latest version available on Docker Hub.

So, altogether, this command creates a new Docker container called mysql8 using the mysql image, sets the root password to root, and maps the MySQL port from the container to the host.

4.操纵MySQL容器

[root@localhost ~]# docker exec -it mysql8 /bin/bash
root@7148b7152e74:/# mysql -V
mysql  Ver 8.0.27 for Linux on x86_64 (MySQL Community Server - GPL)

当容器正在运行时,使用 docker exec 命令可以在容器内部运行额外的命令,其选项和参数如下:

docker exec:告诉 Docker 要在运行的容器内运行额外的命令。

-i:保持 STDIN 开放,允许交互式地输入命令。

-t:分配一个伪终端(pseudo-tty)来启用交互式操作。

mysql8:指定要在其中运行命令的容器的名称或 ID。

/bin/bash:要在容器内运行的命令。在这种情况下,我们要进入容器并打开一个 Bash shell。

因此,这个命令将打开一个交互式的 Bash shell,允许我们在名为 “mysql8” 的运行容器中执行命令。

4.1.登录数据库

root@7148b7152e74:/# 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.27 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> 

4.2.创建远程登录用户

mysql> create user 'admin'@'%' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.06 sec)

mysql>

注释:

create user:告诉 MySQL 创建一个新用户。

‘admin’@‘%’:指定新用户的用户名和主机名。在这种情况下,用户名为 ‘admin’,‘%’ 表示可以从任何 IP 地址连接。

identified with mysql_native_password:指定新用户使用 MySQL 原生密码进行身份验证。

by ‘123456’:指定新用户的密码为 ‘123456’。

4.3.为远程用户开放权限

mysql> grant all privileges on *.* to admin@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

5.远程登录测试

[root@localhost ~]# yum -y install mysql
[root@localhost ~]# mysql -uadmin -p123456 -h 192.168.66.61
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.27 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> 

猜你喜欢

转载自blog.csdn.net/m0_67159981/article/details/129365451