0


【Linux】手把手教你在CentOS上使用docker 安装MySQL8.0

文章目录

前言

大家好,又见面了,我是沐风晓月,本文主要讲解如何用docker在centos7系统上安装MySQL8.0,以及如何设置MySQL的远程登录。

文章收录到【容器管理】和【数据库入门到精通专栏】,此专栏是沐风晓月对linux云计算架构实战方向的内容进行的总结,希望能够加深自己的印象,以及帮助到其他的小伙伴😉😉。

如果文章有什么需要改进的地方还请大佬不吝赐教👏👏。

🏠个人主页:我是沐风晓月
🧑个人简介:大家好,我是沐风晓月,双一流院校计算机专业,阿里云社区专家博主😉😉
💕 座右铭: 先努力成长自己,再帮助更多的人 ,一起加油进步🍺🍺🍺
💕欢迎大家:这里是CSDN,我总结知识的地方,喜欢的话请三连,有问题请私信😘

一. docker的安装

1.1 从阿里下载repo镜像

[root@mufenggrow ~]# 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包,如果没有,说明没有下载成功:

在这里插入图片描述

1.2 安装docker

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

安装速度可能稍慢,这时候只需要耐心等待完成即可

1.3 启动docker并查看版本

启动docker

[root@mufenggrow ~]# systemctl  start docker

查看版本

[root@mufenggrow ~]# docker --version
Docker version 20.10.22, build 3a2c30b
[root@mufenggrow ~]# 
[root@mufenggrow ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.22
 API version:       1.41
 Go version:        go1.18.9
 Git commit:        3a2c30b
 Built:             Thu Dec 1522:30:24 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

二. 使用docker安装MySQL8.0

2.1 拉取MySQL镜像

查看镜像

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

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

开始拉取:

[root@mufenggrow ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete 
93619dbc5b36: Pull complete 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
688ba7d5c01a: Pull complete 
00e060b6d11d: Pull complete 
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

查看拉取下来的镜像:

[root@mufenggrow ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@mufenggrow ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
mysql        latest    3218b38490ce   14 months ago   516MB

2.2 创建容器

[root@mufenggrow ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
mysql        latest    3218b38490ce   14 months ago   516MB
[root@mufenggrow ~]# docker run -id --name=mysql8 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql:latest
c3e44b10b7fac5427bb0ac145b4bf70bd7c09174aa48186457250e491aa17821
[root@mufenggrow ~]# 

备注:

-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 上可用的最新版本。

这个命令将创建一个名为 “mysql8” 的新 Docker 容器,使用 “mysql” 镜像,将 root 密码设置为 root,并将 MySQL 端口从容器映射到主机。

翻译成英文,此处可以不看,直接跳转到2.3即可:

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".-p3306: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.

-eMYSQL_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.

2.3 操作MySQL容器

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

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

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

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

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

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

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

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

  • 登录数据库
root@c3e44b10b7fa:/# mysql -uroot -p

在这里插入图片描述

  • 创建远程登录用户
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’。
  • 为远程用户开放权限
mysql> grant all privileges on *.* to admin@'%';
Query OK, 0 rows affected (0.00 sec)

flush privileges

2.4 远程登录测试

在这里插入图片描述

总结

以上就是在centos7.6系统上使用docker安装MySQL8.0的全部内容了,感谢观看,欢迎点赞收藏。

我是沐风晓月,文章首发于csdn,你的支持就是我的动力。

💕💕💕 好啦,这就是今天要分享给大家的全部内容了,我们下期再见!✨ ✨ ✨
🍻🍻🍻如果你喜欢的话,就不要吝惜你的一键三连了~

标签: docker centos linux

本文转载自: https://blog.csdn.net/wisdom_futrue/article/details/129342031
版权归原作者 我是沐风晓月 所有, 如有侵权,请联系我们删除。

“【Linux】手把手教你在CentOS上使用docker 安装MySQL8.0”的评论:

还没有评论