使用Docker快速部署Mysql服务器

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: 本文介绍了如何使用Docker快速部署MySQL服务器,包括下载官方MySQL镜像、启动容器、设置密码、连接MySQL服务器以及注意事项。

作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。

  请原谅我是一个标题党,我个人觉得没有必要从头做MySQL的镜像,可以在官方提供的MySQL镜像的基础之上更迭咱们自己的软件。接下来我们谈谈一下如何下载官方的镜像并启动过程。

一.下载MySQL镜像并启动

1>.访问Docker的官方镜像仓库(https://hub.docker.com/)

2>.选择好MySQL分支后,点击"tag",如下图所示,会列出响应的版本

3>.选择相应的MySQL版本

4>.二话不说,直接使用Docker**下载对应的Mysql镜像**

[root@docker201.yinzhengjie.org.cn ~]# docker image ls
REPOSITORY            TAG                  IMAGE ID            CREATED             SIZE
nginx                 v0.2-20200118-1225   35c0c66e03ac        2 hours ago         448MB
jason/centos7-nginx   v0.1.20200114        7372d16c99bc        27 hours ago        448MB
jason/centos7-nginx   latest               c4e0980a825a        28 hours ago        448MB
centos                centos7.6.1810       f1cb7c7d58b7        10 months ago       202MB
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker image pull mysql:5.6.44
5.6.44: Pulling from library/mysql
0a4690c5d889: Pull complete 
98aa2fc6cbeb: Pull complete 
0777e6eb0e6f: Pull complete 
2464189c041c: Pull complete 
b45df9dc827d: Pull complete 
8f57052b58bf: Pull complete 
ee774b34419e: Pull complete 
bcb6c29a9771: Pull complete 
f5eace967cb6: Pull complete 
fe457a2a894d: Pull complete 
a3266082cf3b: Pull complete 
Digest: sha256:02b3ddb41d6e5d48d24aa8e59e1cd5870ddaca8ba7cdedf6602f7e6266240d64
Status: Downloaded newer image for mysql:5.6.44
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker image ls
REPOSITORY            TAG                  IMAGE ID            CREATED             SIZE
nginx                 v0.2-20200118-1225   35c0c66e03ac        2 hours ago         448MB
jason/centos7-nginx   v0.1.20200114        7372d16c99bc        28 hours ago        448MB
jason/centos7-nginx   latest               c4e0980a825a        28 hours ago        448MB
mysql                 5.6.44               c30095c52827        6 months ago        256MB
centos                centos7.6.1810       f1cb7c7d58b7        10 months ago       202MB
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]#

5>.启动MySQL容器的镜像

[root@docker201.yinzhengjie.org.cn ~]# docker container run -it --rm mysql:5.6.44             #直接启动时可能会报错,如下所示,那是因为咱们没有为容器的MySQL指定密码,使用"-e"参数指定密码即可。
error: database is uninitialized and password option is not specified 
  You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker container run -it --rm -p 3306:3306 -e MYSQL_ROOT_PASSWORD=yinzhengjie mysql:5.6.44           #注意,该容器退出后会自动删除哟~
Initializing database
2020-01-18 14:26:19 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-01-18 14:26:19 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2020-01-18 14:26:19 0 [Note] /usr/sbin/mysqld (mysqld 5.6.44) starting as process 34 ...
2020-01-18 14:26:19 34 [Note] InnoDB: Using atomics to ref count buffer pool pages
2020-01-18 14:26:19 34 [Note] InnoDB: The InnoDB memory heap is disabled
2020-01-18 14:26:19 34 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2020-01-18 14:26:19 34 [Note] InnoDB: Memory barrier is not used
2020-01-18 14:26:19 34 [Note] InnoDB: Compressed tables use zlib 1.2.11
2020-01-18 14:26:19 34 [Note] InnoDB: Using Linux native AIO
2020-01-18 14:26:19 34 [Note] InnoDB: Using CPU crc32 instructions
2020-01-18 14:26:19 34 [Note] InnoDB: Initializing buffer pool, size = 128.0M

......
MySQL init process done. Ready for start up.

2020-01-18 14:26:29 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-01-18 14:26:29 0 [Note] mysqld (mysqld 5.6.44) starting as process 1 ...
2020-01-18 14:26:29 1 [Note] Plugin 'FEDERATED' is disabled.
2020-01-18 14:26:29 1 [Note] InnoDB: Using atomics to ref count buffer pool pages
2020-01-18 14:26:29 1 [Note] InnoDB: The InnoDB memory heap is disabled
2020-01-18 14:26:29 1 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2020-01-18 14:26:29 1 [Note] InnoDB: Memory barrier is not used
2020-01-18 14:26:29 1 [Note] InnoDB: Compressed tables use zlib 1.2.11
2020-01-18 14:26:29 1 [Note] InnoDB: Using Linux native AIO
2020-01-18 14:26:29 1 [Note] InnoDB: Using CPU crc32 instructions
2020-01-18 14:26:29 1 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2020-01-18 14:26:29 1 [Note] InnoDB: Completed initialization of buffer pool
2020-01-18 14:26:29 1 [Note] InnoDB: Highest supported file format is Barracuda.
2020-01-18 14:26:29 1 [Note] InnoDB: 128 rollback segment(s) are active.
2020-01-18 14:26:29 1 [Note] InnoDB: Waiting for purge to start
2020-01-18 14:26:29 1 [Note] InnoDB: 5.6.44 started; log sequence number 1625997
2020-01-18 14:26:29 1 [Note] Server hostname (bind-address): '*'; port: 3306
2020-01-18 14:26:29 1 [Note] IPv6 is available.
2020-01-18 14:26:29 1 [Note]   - '::' resolves to '::';
2020-01-18 14:26:29 1 [Note] Server socket created on IP: '::'.
2020-01-18 14:26:29 1 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2020-01-18 14:26:29 1 [Warning] 'proxies_priv' entry '@ root@d1197bf0e557' ignored in --skip-name-resolve mode.
2020-01-18 14:26:29 1 [Note] Event Scheduler: Loaded 0 events
2020-01-18 14:26:29 1 [Note] mysqld: ready for connections.
Version: '5.6.44'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)

6>.使用容器启动MySQL时的注意事项(应该单独指定存储卷路径,否则容器消亡后数据也会跟着丢失啦)

[root@docker201.yinzhengjie.org.cn ~]# docker ps
CONTAINER ID        IMAGE                      COMMAND                  CREATED              STATUS              PORTS                         NAMES
d1197bf0e557        mysql:5.6.44               "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp        vibrant_rosalind
d517b3a369ef        nginx:v0.2-20200118-1225   "nginx"                  2 hours ago          Up 2 hours          0.0.0.0:80->80/tcp, 443/tcp   myNginx
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# docker container exec -it d1197bf0e557 bash
root@d1197bf0e557:/# 
root@d1197bf0e557:/# ls -l /var/lib/mysql/
total 110604
-rw-rw----. 1 mysql mysql       56 Jan 18 14:26 auto.cnf
-rw-rw----. 1 mysql mysql 50331648 Jan 18 14:26 ib_logfile0
-rw-rw----. 1 mysql mysql 50331648 Jan 18 14:26 ib_logfile1
-rw-rw----. 1 mysql mysql 12582912 Jan 18 14:26 ibdata1
drwx------. 2 mysql mysql     4096 Jan 18 14:26 mysql
drwx------. 2 mysql mysql     4096 Jan 18 14:26 performance_schema
root@d1197bf0e557:/# 
root@d1197bf0e557:/# 
root@d1197bf0e557:/# 
root@d1197bf0e557:/# 
root@d1197bf0e557:/# grep data /etc/mysql/ -R
/etc/mysql/my.cnf.fallback:# The MySQL database server configuration file.
/etc/mysql/mysql.conf.d/mysqld.cnf:datadir        = /var/lib/mysql              #生产环境中,我们应该将该目录挂载到宿主机对应的路径哟~
root@d1197bf0e557:/# 
root@d1197bf0e557:/#

二.连接MySQL镜像的服务器

1>.宿主机安装mysql的客户端

[root@docker201.yinzhengjie.org.cn ~]# yum -y install mysql
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.bit.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirror.bit.edu.cn
base                                                                                                                                                                                                                                                   | 3.6 kB  00:00:00     
docker-ce-stable                                                                                                                                                                                                                                       | 3.5 kB  00:00:00     
extras                                                                                                                                                                                                                                                 | 2.9 kB  00:00:00     
pouch-stable                                                                                                                                                                                                                                           | 2.9 kB  00:00:00     
updates                                                                                                                                                                                                                                                | 2.9 kB  00:00:00     
updates/7/x86_64/primary_db                                                                                                                                                                                                                            | 5.9 MB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package mariadb.x86_64 1:5.5.64-1.el7 will be installed
--> Processing Dependency: mariadb-libs(x86-64) = 1:5.5.64-1.el7 for package: 1:mariadb-5.5.64-1.el7.x86_64
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.60-1.el7_5 will be updated
---> Package mariadb-libs.x86_64 1:5.5.64-1.el7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================================================================================================
 Package                                                             Arch                                                          Version                                                                  Repository                                                   Size
==============================================================================================================================================================================================================================================================================
Installing:
 mariadb                                                             x86_64                                                        1:5.5.64-1.el7                                                           base                                                        8.7 M
Updating for dependencies:
 mariadb-libs                                                        x86_64                                                        1:5.5.64-1.el7                                                           base                                                        759 k

Transaction Summary
==============================================================================================================================================================================================================================================================================
Install  1 Package
Upgrade             ( 1 Dependent package)

Total download size: 9.5 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/2): mariadb-libs-5.5.64-1.el7.x86_64.rpm                                                                                                                                                                                                            | 759 kB  00:00:00     
(2/2): mariadb-5.5.64-1.el7.x86_64.rpm                                                                                                                                                                                                                 | 8.7 MB  00:00:01     
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                                                         7.8 MB/s | 9.5 MB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : 1:mariadb-libs-5.5.64-1.el7.x86_64                                                                                                                                                                                                                         1/3 
/sbin/ldconfig: /lib64/libnvidia-container.so.1 is not a symbolic link

  Installing : 1:mariadb-5.5.64-1.el7.x86_64                                                                                                                                                                                                                              2/3 
  Cleanup    : 1:mariadb-libs-5.5.60-1.el7_5.x86_64                                                                                                                                                                                                                       3/3 
/sbin/ldconfig: /lib64/libnvidia-container.so.1 is not a symbolic link

  Verifying  : 1:mariadb-libs-5.5.64-1.el7.x86_64                                                                                                                                                                                                                         1/3 
  Verifying  : 1:mariadb-5.5.64-1.el7.x86_64                                                                                                                                                                                                                              2/3 
  Verifying  : 1:mariadb-libs-5.5.60-1.el7_5.x86_64                                                                                                                                                                                                                       3/3 

Installed:
  mariadb.x86_64 1:5.5.64-1.el7                                                                                                                                                                                                                                               

Dependency Updated:
  mariadb-libs.x86_64 1:5.5.64-1.el7                                                                                                                                                                                                                                          

Complete!
[root@docker201.yinzhengjie.org.cn ~]#

[root@docker201.yinzhengjie.org.cn ~]# yum -y install mysql

2>.连接MySQL Docker容器

[root@docker201.yinzhengjie.org.cn ~]# hostname -i
192.168.6.201
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                                                                                          Local Address:Port                                                                                                         Peer Address:Port              
LISTEN     0      128                                                                                                         *:22                                                                                                                      *:*                  
LISTEN     0      100                                                                                                 127.0.0.1:25                                                                                                                      *:*                  
LISTEN     0      20480                                                                                                      :::3306                                                                                                          :::*                  
LISTEN     0      20480                                                                                                      :::80                                                                                                                     :::*                  
LISTEN     0      128                                                                                                        :::22                                                                                                                     :::*                  
LISTEN     0      100                                                                                                       ::1:25                                                                                                                     :::*                  
[root@docker201.yinzhengjie.org.cn ~]# 
[root@docker201.yinzhengjie.org.cn ~]# mysql -h 192.168.6.201 -uroot -pyinzhengjie
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.44 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)]> 
MySQL [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MySQL [(none)]>
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
2月前
|
域名解析 人工智能 弹性计算
DeepSeek服务器繁忙解决方法:使用阿里云一键部署DeepSeek个人网站!
通过阿里云一键部署DeepSeek个人网站,解决服务器繁忙问题。学生用户可领取300元代金券实现0成本部署,普通用户则可用99元/年的服务器。教程涵盖从选择套餐、设置密码到获取百炼API-KEY的全流程,助您快速搭建专属大模型主页,体验DeepSeek、Qwen-max、Llama等多款模型,无需代码,最快5分钟完成部署。支持绑定个人域名,共享亲友使用,日均成本仅约1元。
178 10
|
2月前
|
关系型数据库 MySQL 数据库连接
docker拉取MySQL后数据库连接失败解决方案
通过以上方法,可以解决Docker中拉取MySQL镜像后数据库连接失败的常见问题。关键步骤包括确保容器正确启动、配置正确的环境变量、合理设置网络和权限,以及检查主机防火墙设置等。通过逐步排查,可以快速定位并解决连接问题,确保MySQL服务的正常使用。
425 82
|
2月前
|
NoSQL Redis Docker
Docker——阿里云服务器利用docker搭建redis集群
本文详细记录了使用Docker搭建Redis集群的过程,包括检查Docker和Docker Compose的安装、创建Redis配置文件、编写`docker-compose.yml`文件、启动Redis节点、创建Redis集群的具体步骤,以及最终的验证方法。文章还提供了在多服务器环境下搭建Redis集群的注意事项,帮助读者全面了解 Redis 集群的部署流程。
286 70
|
29天前
|
关系型数据库 MySQL Linux
在Linux环境下备份Docker中的MySQL数据并传输到其他服务器以实现数据级别的容灾
以上就是在Linux环境下备份Docker中的MySQL数据并传输到其他服务器以实现数据级别的容灾的步骤。这个过程就像是一场接力赛,数据从MySQL数据库中接力棒一样传递到备份文件,再从备份文件传递到其他服务器,最后再传递回MySQL数据库。这样,即使在灾难发生时,我们也可以快速恢复数据,保证业务的正常运行。
108 28
|
1月前
|
Ubuntu 关系型数据库 MySQL
在Ubuntu系统的Docker上安装MySQL的方法
以上的步骤就是在Ubuntu系统的Docker上安装MySQL的详细方法,希望对你有所帮助!
151 12
|
2月前
|
JavaScript 应用服务中间件 nginx
Vue项目部署:如何打包并上传至服务器进行部署?
以上就是Vue项目打包及部署的方法,希望对你有所帮助。描述中可能会有一些小疏漏,但基本流程应该没有问题。记住要根据你的实际情况调整对应的目录路径和服务器IP地址等信息。此外,实际操作时可能会遇到各种问题,解决问题的能力是每一位开发者必备的技能。祝你部署顺利!
289 17
|
2月前
|
Ubuntu 关系型数据库 MySQL
容器技术实践:在Ubuntu上使用Docker安装MySQL的步骤。
通过以上的操作,你已经步入了Docker和MySQL的世界,享受了容器技术给你带来的便利。这个旅程中你可能会遇到各种挑战,但是只要你沿着我们划定的路线行进,你就一定可以达到目的地。这就是Ubuntu、Docker和MySQL的灵魂所在,它们为你开辟了一条通往新探索的道路,带你亲身感受到了技术的力量。欢迎在Ubuntu的广阔大海中探索,用Docker技术引领你的航行,随时准备感受新技术带来的震撼和乐趣。
121 16
|
2月前
|
安全 关系型数据库 MySQL
【YashanDB知识库】YMP从mysql迁移到崖山,报错:服务器错误
【YashanDB知识库】YMP从mysql迁移到崖山,报错:服务器错误
|
2月前
|
安全 关系型数据库 MySQL
【YashanDB知识库】YMP从mysql迁移到崖山,报错:服务器错误
【YashanDB知识库】YMP从mysql迁移到崖山,报错:服务器错误
【YashanDB知识库】YMP从mysql迁移到崖山,报错:服务器错误
|
2月前
|
Docker Python 容器
Docker——阿里云服务器使用Docker部署python项目全程小记
本文记录了我在阿里云服务器上使用Docker部署python项目(flask为例)的全过程,在这里记录和分享一下,希望可以给大家提供一些参考。
219 1