docker安装MySQL

2021-02-01  本文已影响0人  神易风

本文出处shenyifengtk.github.io
转载请说明出处

都2021了,学会用docker 来构建软件开发吧,快速构建环境,容器比虚拟机更加轻量化,拥有诸多好处。下面我使用docker安装运行MySQL例子,为大家演示docker使用方法。

搜索镜像

docker search mysql

PS C:\Users\syf> docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10425               [OK]
mariadb                           MariaDB is a community-developed fork of MyS…   3868                [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   763                                     [OK]
percona                           Percona Server is a fork of the MySQL relati…   524                 [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   87
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   79
centurylink/mysql                 Image containing mysql. Optimized to be link…   59                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      48                                      [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
prom/mysqld-exporter                                                              37                                      [OK]
databack/mysql-backup             Back up mysql databases to... anywhere!         37
tutum/mysql                       Base docker image to run a MySQL database se…   35
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   29                                      [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   27
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   20
circleci/mysql                    MySQL is a widely used, open-source relation…   20
arey/mysql-client                 Run a MySQL client from a docker container      17                                      [OK]
mysql/mysql-router                MySQL Router provides transparent routing be…   17
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   10                                      [OK]
yloeffler/mysql-backup            This image runs mysqldump to backup data usi…   7                                       [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6
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…   1                                       [OK]

这些都是镜像都是dockerhub官方镜像,如果想了解它们详细信息,可以去官网搜索关键字。这里我选择第一个,MySQL官方镜像。

拉取镜像

docker pull mysql

这里还可以选择MySQL版本

docker pull [选项] [Docker Registry地址]<仓库名>:<标签>

运行镜像

简单运行镜像

docker run --name mysql-name -e MYSQL_ROOT_PASSWORD=password -d mysql:tag

还可以通过-p 网络映射,将容器内端口映射到运行主机上某一个端口,这个我们访问这个端口,就可以连接到容器内MySQL了。

docker run --name mysql-name -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql:tag

将容器内3306端口映射到主机上3306,使用方式 -p 主机端口:容器端口

MySQL容器数据保存

在MySQL容器的数据一般有两个存储方式。

docker run --name mysql-name -p 3306:3306l -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password -d mysql:tag

-v 将主机路径/my/own/datadir 路径挂载到容器内/var/lib/mysql路径,这样在/var/lib/mysql下保存MySQL data 就会保存到主机/my/own/datadir 下,当容器再次启动时MySQL就会从/my/own/datadir 加载数据。前提是要保证/my/own/datadir 目录文件时存在的。如果不知道MySQL容器保存数据在哪里,可以进入容器查看配置文件

进入容器

docker exec -it mysql-name /bin/bash

查看配置文件

cat /etc/mysql/my.cnf

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL

# Custom config should go here
!includedir /etc/mysql/conf.d/

数据保存到/var/lib/mysql 路径下,exit 退出,将容器data 路径下文件copy到主机下。

docker cp mysql-name:/var/lib/mysql/ /my/own/datadir

上一篇下一篇

猜你喜欢

热点阅读