草庐IT

把MySQL放进Docker,总共需要几步?

数据库干货铺 2023-03-28 原文
把MySQL放进Docker,总共需要几步?本次就通过社区版容器安装2个mysql实例,看一下部署有多简单。

1、 安装docker

操作系统我使用的Centos7 x64系统,而Docker 目前看仅CentOS 7 及以上版本。本次采用Docker 仓库进行安装 ,具体步骤如下:

1.1设置仓库

因本机首次安装 Docker,所以需要先设置 Docker 仓库,以后的安装可以直接从仓库安装。

/* 安装所需的软件包 */
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
使用以下命令来设置稳定的仓库

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

1.2 安装Docker Engine-Community

安装最新版本的 Docker Engine-Community 和 containerd

yum install docker-ce docker-ce-cli containerd.io
这一步有的包下载可能比较慢,要耐心等待,如果失败再重新执行几遍。

如果有同学配置了多个 Docker 仓库,而且在 yum install 或 yum update 命令中未指定版本时,则会安装或更新最新版本的包,如果对稳定性 或版本有要求,则安装时一定要指定特定版本。

1.3 启动docker

经过上述安装后,启动docker服务即可

systemctl start docker

1.4 测试docker 部署是否成功

可以运行经典的hello-world 来测试

可见docker已部署成功并可以正常运行。

2、部署MySQL

2.1 查看可用的mysql镜像

[root@c7_2 local]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9453 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3415 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 691 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 75
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 68
centurylink/mysql Image containing mysql. Optimized to be link… 61 [OK]
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
bitnami/mysql Bitnami MySQL Docker Image 39 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 30 [OK]
prom/mysqld-exporter 27 [OK]
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 25
centos/mysql-56-centos7 MySQL 5.6 SQL database server 19
circleci/mysql MySQL is a widely used, open-source relation… 19
databack/mysql-backup Back up mysql databases to... anywhere! 17
mysql/mysql-router MySQL Router provides transparent routing be… 15
arey/mysql-client Run a MySQL client from a docker container 14 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 6 [OK]
genschsa/mysql-employees MySQL Employee Sample Database 5 [OK]
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
jelastic/mysql An image of the MySQL database server mainta… 1
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 0 [OK]
monasca/mysql-init A minimal decoupled init container for mysql 0

2.2 部署最新版本mysql

拉取最新版本mysql,不指定版本默认拉取最新版。

[root@c7_2 local]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
54fec2fa59d0: Pull complete
bcc6c6145912: Pull complete
951c3d959c9d: Pull complete
05de4d0e206e: Pull complete
319f0394ef42: Pull complete
d9185034607b: Pull complete
013a9c64dadc: Pull complete
42f3f7d10903: Pull complete
c4a3851d9207: Pull complete
82a1cc65c182: Pull complete
a0a6b01efa55: Pull complete
bca5ce71f9ea: Pull complete
Digest: sha256:61a2a33f4b8b4bc93b7b6b9e65e64044aaec594809f818aeffbff69a893d1944
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

注: 拉取镜像的时候容易出现如下超时错误。

Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled (Client.Timeout exceeded while awaiting headers)
建议修改国内镜像地址,例如修改为网易或阿里云的镜像地址(我采用的是个人阿里镜像的方式, 下载速度很理想,基本一分钟内下载完毕)

修改源的方法:

vim /etc/docker/daemon.json
/* 添加如下内容 */
{
"registry-mirrors": ["http://hub-mirror.c.163.com""https://registry.docker-cn.com"]
}
使用阿里云镜像需要自己登录到阿里云,配置后复制自己的地址再使用,需要的小伙伴可以联系我获取指引。

2.3 查看已下载的镜像

[root@c7_2 containers]# docker image ls # 或使用 docker images 查看
REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest a7a67c95e831 7 days ago 541MB hello-world latest bf756fb1ae65 4 months ago 13.3kB

2.4 运行mysql容器

[root@c7_2 local]# docker run -di --name=mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=Admin@123 mysql
9f6668b5d0292b30308cfc5c6a6b88a34c4d62d9e5c70dff9bfce9f090117968
其中主要参数说明如下:

  • --name 后面配置容器名
  • -p代表端口映射, 格式为 宿主机映射端口:容器运行端口
  • -e代表添加环境变量 MYSQL_ROOT_PASSWORD 是root用户的登陆密码
  • 最后的mysql代码容器镜像名
启动成功后

2.5 进入mysql容器

指定进入mysql容器中

[root@c7_2 local]# docker exec -it mysql /bin/bash
root@9f6668b5d029:/#
在容器内登录mysql

root@9f6668b5d029:/# mysql -u root -p'Admin@123'
结果见如下截图,可以看到部署的是最新的MySQL8.0.20版本

注: MySQL8.0 用户的加密组件做了变更,低版本客户端登录会报错。处理的方式有多种,主要的方式有2种:

  • 修改对应用户的密码加密方式
  • 升级客户端或驱动
具体方式可参考 MySQL8.0用户登录那些事。

2.6 再部署一个mysql5.7的容器

上面部署的是最新版mysql8.0.20,想部署5.7版本该如何部署?其实就是拉取镜像的时候指定选择MySQL5.7版本的即可。具体步骤如下:

拉取mysql5.7版本镜像

[root@c7_2 local]# docker pull centos/mysql-57-centos7
Using default tag: latest
latest: Pulling from centos/mysql-57-centos7
d8d02d457314: Pull complete
a11069b6e245: Pull complete
596303fb1aa3: Pull complete
a29499e779a7: Pull complete
17d1a52c2e00: Pull complete
ed24591227fe: Pull complete
de0ad46e3ed9: Pull complete
c62e4a984a9c: Pull complete
01d54c6bda68: Pull complete
Digest: sha256:e08ee4d43b7356607685b69bde6335e27cf20c020f345b6c6c59400183882764
Status: Downloaded newer image for centos/mysql-57-centos7:latest
docker.io/centos/mysql-57-centos7:latest

运行mysql5.7的docker

docker run -di --name=mysql5.7 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=Admin@123 centos/mysql-57-centos7

不进入容器,在本地或其他机器上登录mysql5.7

[root@c7_2 local]# /usr/local/mysql5.7/bin/mysql -uroot -p'Admin@123' -P3307 -h 192.168.28.129
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.24 MySQL Community Server (GPL)

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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> select version();
+-----------+
| version() |
+-----------+
| 5.7.24 |
+-----------+
1 row in set (0.01 sec)

mysql>
可以看到 该版本为MySQL 社区版的5.7.24

2.7 查看正在运行的docker

查看一台机器上运行的docker信息可以通过 docker ps 命令查看

本地端口信息如下

3、结语

将MySQL放进docker主要就这几步。不过其中修改数据库配置文件等相关内容本次来不及细说,有兴趣的同学可以自行测试,相对也必将简单,可以在启动的时候指定。

有关把MySQL放进Docker,总共需要几步?的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby - rspec 需要 .rspec 文件中的 spec_helper - 2

    我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只

  3. ruby - 如何在 Lion 上安装 Xcode 4.6,需要用 RVM 升级 ruby - 2

    我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121

  4. ruby - 为什么在 ruby​​ 中创建 Rational 不需要新方法 - 2

    这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Rubysyntaxquestion:Rational(a,b)andRational.new!(a,b)我正在阅读ruby镐书,我对创建有理数的语法感到困惑。Rational(3,4)*Rational(1,2)产生=>3/8为什么Rational不需要new方法(我还注意到例如我可以在没有new方法的情况下创建字符串)?

  5. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  6. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  7. ruby-on-rails - 需要帮助最大化多个相似对象中的 3 个因素并适当排序 - 2

    我需要用任何语言编写一个算法,根据3个因素对数组进行排序。我以度假村为例(如Hipmunk)。假设我想去度假。我想要最便宜的地方、最好的评论和最多的景点。但是,显然我找不到在所有3个中都排名第一的方法。Example(assumingthereare20importantattractions):ResortA:$150/night...98/100infavorablereviews...18of20attractionsResortB:$99/night...85/100infavorablereviews...12of20attractionsResortC:$120/night

  8. ruby - 我需要从 facebook 游戏中抓取数据——使用 ruby - 2

    修改(澄清问题)我已经花了几天时间试图弄清楚如何从Facebook游戏中抓取特定信息;但是,我遇到了一堵又一堵砖墙。据我所知,主要问题如下。我可以使用Chrome的检查元素工具手动查找我需要的html-它似乎位于iframe中。但是,当我尝试抓取该iframe时,它​​是空的(属性除外):如果我使用浏览器的“查看页面源代码”工具,这与我看到的输出相同。我不明白为什么我看不到iframe中的数据。答案不是它是由AJAX之后添加的。(我知道这既是因为“查看页面源代码”可以读取Ajax添加的数据,也是因为我有b/c我一直等到我可以看到数据页面之后才抓取它,但它仍然不存在)。发生这种情况是因为

  9. ruby-on-rails - 无法安装 mysql2 0.3.14 gem - 2

    我看到其他人也遇到过类似的问题,但没有一个解决方案对我有用。0.3.14gem与其他gem文件一起存在。我已经完全按照此处指示完成了所有操作:https://github.com/brianmario/mysql2.我仍然得到以下信息。我不知道为什么安装程序指示它找不到include目录,因为我已经检查过它存在。thread.h文件存在,但不在ruby​​目录中。相反,它在这里:C:\RailsInstaller\DevKit\lib\perl5\5.8\msys\CORE\我正在运行Windows7并尝试在Aptana3中构建我的Rails项目。我的Ruby是1.9.3。$gemin

  10. ruby - 需要重构为新的 Ruby 1.9 哈希语法 - 2

    这个问题在这里已经有了答案:HashsyntaxinRuby[duplicate](1个回答)关闭5年前。我有一个Recipe,其中包含以下未通过lint测试的代码:service'apache'dosupports:status=>true,:restart=>true,:reload=>trueend失败并出现错误:UsethenewRuby1.9hashsyntax.supports:status=>true,:restart=>true,:reload=>true不确定新语法是什么样的...有人可以帮忙吗?

随机推荐