1.搜索MySQL镜像

$ docker search mysql
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/mysql MySQL is a widely used, open-source relati... 6008 [OK]
docker.io docker.io/mariadb MariaDB is a community-developed fork of M... 1891 [OK]
docker.io docker.io/mysql/mysql-server Optimized MySQL Server Docker images. Crea... 427 [OK]
docker.io docker.io/percona Percona Server is a fork of the MySQL rela... 335 [OK]

备注:STARS数最多,OFFICIAL是[OK]的这个就是官方的centos镜像。

2.下载MySQL镜像

$ docker pull docker.io/mysql
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql latest 5195076672a7 4 weeks ago 371.4 MB

3.运行容器

$ docker run -d --name liying-mysql -e MYSQL_ROOT_PASSWORD=attack docker.io/mysql
$ docker exec -it liying-mysql /bin/bash ##进入容器

4.进入mysql

root@3d21d8918d31:/# mysql -u root -pattack
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 2
Server version: 5.7.21 MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)

以上就创建了一个mysql的docker容器,可以看到版本为5.7.21。但是这样创建的容器有两个问题,一是容器删除后,数据就丢失了,二是要访问数据库,必须进入到容器里面才可以。

5.持久化数据,映射开放mysql端口
a、创建宿主机数据存放目录
$ mkdir -p /opt/data/mysql

b、启动容器
$ docker run -d -v /opt/data/mysql/:/var/lib/mysql -p 3306:3306 --name liying-mysql -e MYSQL_ROOT_PASSWORD=attack docker.io/mysql
$ docker logs liying-mysql ##查看日志 
$ docker ps #查看容器

c、查看端口
$ lsof -i:3306

d、查看宿主机上的mysql数据

$ cd /opt/data/mysql
$ ll
总用量 188484
-rw-r-----. 1 systemd-bus-proxy input 56 4月 17 11:53 auto.cnf
-rw-------. 1 systemd-bus-proxy input 1679 4月 17 11:53 ca-key.pem
-rw-r--r--. 1 systemd-bus-proxy input 1107 4月 17 11:53 ca.pem
-rw-r--r--. 1 systemd-bus-proxy input 1107 4月 17 11:53 client-cert.pem
-rw-------. 1 systemd-bus-proxy input 1679 4月 17 11:53 client-key.pem
-rw-r-----. 1 systemd-bus-proxy input 1335 4月 17 11:54 ib_buffer_pool
-rw-r-----. 1 systemd-bus-proxy input 79691776 4月 17 11:54 ibdata1
-rw-r-----. 1 systemd-bus-proxy input 50331648 4月 17 11:54 ib_logfile0
-rw-r-----. 1 systemd-bus-proxy input 50331648 4月 17 11:53 ib_logfile1
-rw-r-----. 1 systemd-bus-proxy input 12582912 4月 17 11:54 ibtmp1
drwxr-x---. 2 systemd-bus-proxy input 4096 4月 17 11:53 mysql
drwxr-x---. 2 systemd-bus-proxy input 8192 4月 17 11:53 performance_schema
-rw-------. 1 systemd-bus-proxy input 1679 4月 17 11:53 private_key.pem
-rw-r--r--. 1 systemd-bus-proxy input 451 4月 17 11:53 public_key.pem
-rw-r--r--. 1 systemd-bus-proxy input 1107 4月 17 11:53 server-cert.pem
-rw-------. 1 systemd-bus-proxy input 1679 4月 17 11:53 server-key.pem
drwxr-x---. 2 systemd-bus-proxy input 8192 4月 17 11:53 sys

-p 3306:3306->把容器的mysql端口3306映射到宿主机的3306端口,这样想访问mysql就可以直接访问宿主机的3306端口。
-v /opt/data/mysql:/var/lib/mysql->把宿主机/opt/data/mysql/目录映射到容器的/var/lib/mysql目录

注意事项:
在使用-v选项映射目录时,宿主机需关闭SElinux:
$ setenforce 0

6、Navicat for MySQL客户端访问mysql

 

docker上运行mysql服务器的更多相关文章

  1. Docker上运行MySQL服务

    1.搜索MySQL镜像 $ docker search mysql INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.i ...

  2. ELK 性能(3) — 在 Docker 上运行高性能容错的 Elasticsearch 集群

    ELK 性能(3) - 在 Docker 上运行高性能容错的 Elasticsearch 集群 介绍 在 Docker 上运行高性能容错的 Elasticsearch 集群 内容 通常熟悉的开发流程是 ...

  3. 在Docker上构建mysql容器

    1.查看docker上的镜像是否有 mysql,如果没有下载则列表中没有  [root@holly holly]# docker images; 如果没有只会看到如下结构 REPOSITORY  TA ...

  4. 在OSX和Windows版本Docker上运行GUI程序

    看到很多人在Docker问题区讨论:如何在OS X和Windows的Docker上运行GUI程序, 随手记录几个参考资料: https://github.com/docker/docker/issue ...

  5. .NET Core 3.0 部署在docker上运行

    自从.NET Core3.0发布之后,写了几篇关于.NET Core 3.0的文章,有助于你快速入门.NET Core3.0. 本篇文章主要讲解如何一步步创建一个mvc项目,然后发布并部署在Docke ...

  6. linux安装docker,并在docker上运行springboot项目

    docker架构示例图 仓库---> 镜像 --->  容器 一.安装docker 1.通过 uname -r 命令查看你当前的内核版本 uname -r 2使用 root 权限登录 Ce ...

  7. 试试将.NET7编译为WASM并在Docker上运行

    之前有听到说Docker支持Wasmtime了,刚好.NET7也支持WASM,就带大家来了解一下这个东西,顺便试试它怎么样. 因为WASM(WebAssembly) 一开始是一个给浏览器的技术,比起J ...

  8. 在docker中运行mysql实例

    Docker是一种新兴的虚拟化技术,能够一定程度上的代替传统虚拟机.下图是容器跟虚拟机的对比 对docker有个大致了解,学习docker断断续续,虽说学习不能急于求成,但断断续续学的话,浪费的碎片化 ...

  9. docker 中运行 mysql

    在宿主机Host的终端执行: 1. 搜索docker镜像仓库中提供的mysql docker search mysql 2.下载mysql镜像 docker pull mysql 3.改变目录权限,非 ...

随机推荐

  1. excel VBA中Xldown和xlup用法

    1.Worksheets("Sheet1").Range("A1").End(xlDown).Select     '意思为自A1起,返回从上往下的最后一个非空 ...

  2. js 获取系统当前时间,判断时间大小

    1.获取系统当前时间 getNowTime(tempminit) { if (!tempminit) { tempminit = 0; } var date = new Date(); date.se ...

  3. Burning Bridges 求tarjan求割边

    Burning Bridges 给出含有n个顶点和m条边的连通无向图,求出所有割边的序号. 1 #include <cstdio> 2 #include <cstring> 3 ...

  4. Laravel使用Observer(观察者)

      1.创建observer文件,我这里是要记录仓库库存模块的操作日志,所以执行下面的语句,会在app/Observers下面创建WarehouseInventoryObserver文件. php a ...

  5. gitlab找回管理员密码

    1.登陆后台服务器,切换git用户 su - git 2.登录GitLab的Rails控制台 gitlab-rails console production 另一种 切换root账户 执行:  git ...

  6. Archive for required library:’ WebRoot/WEB-INF/Mytag.tld’in project ‘TagTest’ cannot be read or is not a valid ZIP file

    Description::部署tld文件时报错 我的解决方法: 右击(当前项目)->Build Path->Java Build Path ( Configure Build Path.. ...

  7. 剑指offer 1-5

    二维数组中的查找 题目:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中 ...

  8. mac-webui-selenium下的webdriver selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH

    from selenium import webdriver def test1(): url='http://www.baidu.com' driver=webdriver.Chrome(" ...

  9. vue(17)vue-route路由管理的安装与配置

    介绍 Vue Router 是 Vue.js官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌.包含的功能有: 嵌套的路由/视图表 模块化的.基于组件的路由配置 路由参 ...

  10. python找出字典中value最大值的几种方法

    假设定义一字典,m = {"a":3,"e":6,"b":2,"g":7,"f":7,"c ...