使用Docker快速部署Mysql服务器

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,高可用系列 2核4GB
云数据库 RDS MySQL,高可用系列 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)]>
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
目录
相关文章
|
17天前
|
存储 Docker Python
docker 部署 sftp
本文介绍SFTP服务的部署与配置,包括users.conf用户配置规则、Docker容器运行命令及上传目录权限说明,重点解析atmoz/sftp镜像的chroot机制与子目录映射,确保用户登录后正确访问/upload目录,并提供Python脚本实现文件上传示例。
66 12
docker 部署 sftp
|
18天前
|
弹性计算 安全 Linux
使用阿里云服务器安装Z-Blog博客网站流程,新手一键部署教程
本教程教你如何在阿里云99元服务器上,通过宝塔Linux面板一键部署Z-Blog博客。基于CentOS 7.9系统,从远程连接、安装宝塔面板、开放端口到部署Z-Blog全流程详解,操作简单,新手也能轻松搭建个人博客网站。
246 13
|
18天前
|
运维 Linux 数据库
基于 Docker 部署 n8n 指南,新手一看就会
本教程详解如何通过 Docker 快速部署开源自动化工具 n8n,适合新手快速上手。内容涵盖官方部署步骤、常见难点及第三方一键部署方案,助你高效搭建自动化工作流平台。
319 6
|
18天前
|
弹性计算 Devops Shell
用阿里云 DevOps Flow 实现 ECS 部署自动化:从准备到落地的完整指南
阿里云 DevOps Flow 是一款助力开发者实现自动化部署的高效工具,支持代码流水线构建、测试与部署至ECS实例,显著提升交付效率与稳定性。本文详解如何通过 Flow 自动部署 Bash 脚本至 ECS,涵盖环境准备、流水线搭建、源码接入、部署流程设计及结果验证,助你快速上手云上自动化运维。
79 0
监控 安全 Linux
43 0
|
24天前
|
前端开发 JavaScript 应用服务中间件
在Docker部署的前端应用中使用动态环境变量
以上步骤展示了如何在 Docker 配置过程中处理并注入环墨遁形成可执行操作流程,并确保最终用户能够无缝地与之交互而无须关心背后复杂性。
83 13
|
25天前
|
JavaScript 算法 前端开发
【Docker项目实战】使用Docker部署paopao-ce微社区
【Docker项目实战】使用Docker部署paopao-ce微社区
199 84
【Docker项目实战】使用Docker部署paopao-ce微社区
|
28天前
|
存储 Kubernetes 持续交付
为什么Docker容器化改变了开发与部署?
为什么Docker容器化改变了开发与部署?
|
2月前
|
运维 Cloud Native 开发者
Docker:现代化应用开发与部署的神器
Docker:现代化应用开发与部署的神器
186 101