环境

  • CentOS:7
  • Docker:1.31.1
  • MySql:5.7
 
拷贝mysql配置文件

1.首先创建mysql容器
sudo docker run --name mysql5. -p : -e MYSQL\_ROOT\_PASSWORD= -d mysql:5.7
 
2.创建成功,查看一下运行状态
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c84a366e3abf mysql:5.7 "docker-entrypoint..." minutes ago Up seconds /tcp, 0.0.0.0:->/tcp mysql5.
 
3.可以看到我们的容器正在运行中,现在进入容器,查看一下配置文件
[root@localhost ~]# docker exec -it mysql5. /bin/bash
root@c84a366e3abf:/# cat /etc/mysql/my.cnf
# Copyright (c) , Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., Franklin St, Fifth Floor, Boston, MA - USA !includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
root@c84a366e3abf:/# cat /etc/mysql/mysql.conf.d/mysqld.cnf
# Copyright (c) , , Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., Franklin St, Fifth Floor, Boston, MA - USA #
# 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
lower_case_table_names=
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=
 
4.把输出的内容复制一下,我们在本地先建一个同名文件,把内容粘贴进去,此时也可以修改添加自己想要的配置,例如加上忽略表名大小写的配置:lower_case_table_names=1。
 
5.把这个文件保存一下,然后切换到命令行,输入exit退出容器。
 
6.下一步停止并删除mysql5.7容器,后面挂载了外部数据的时候重新创建。
[root@localhost ~]# docker stop mysql5.
[root@localhost ~]# docker rm mysql5.
 
数据和配置挂载到宿主机

1.在linux环境下创建挂载目录,把前面拷出来的配置复制到下面创建的mysqld.cnf中
[root@localhost ~]# cd /opt
[root@localhost ~]# mkdir mysql
[root@localhost ~]# cd mysql
[root@localhost ~]# mkdir data
[root@localhost ~]# mkdir config
[root@localhost ~]# cd config
[root@localhost ~]# touch mysqld.cnf
 
2.mysqld.cnf的配置内容如下
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
lower_case_table_names=
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=
 
3.执行下面的命令创建容器
sudo docker run --name mysql5. --restart always --privileged=true -p : -v /opt/mysql/config/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf -v /opt/mysql/data:/var/lib/mysql -e MYSQL_USER="fengwei" -e MYSQL_PASSWORD="pwd123" -e MYSQL_ROOT_PASSWORD="rootpwd123" -d mysql:5.7
参数说明:
–restart always:开机启动
–privileged=true:提升容器内权限
-v /opt/mysql/config/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf:映射配置文件
-v /opt/mysql/data:/var/lib/mysql:映射数据目录
-e MYSQL_USER=”fengwei”:添加用户fengwei
-e MYSQL_PASSWORD=”pwd123”:设置fengwei的密码伟pwd123
-e MYSQL_ROOT_PASSWORD=”rootpwd123”:设置root的密码伟rootpwd123
特别说明:如果没有添加--privileged=true参数,容器创建后不能正常启动,查看日志发现有权限的错误。
 

Docker安装MySql-挂载外部数据和配置的更多相关文章

  1. docker安装mysql挂载外部配置和数据目录

    从docker hub上可以找到mysql外挂配置和数据目录的一些文档说明 https://hub.docker.com/_/mysql 从该文档中可以了解到,mysql的默认配置为/etc/mysq ...

  2. Docker安装mysql镜像并进行主从配置

    Docker安装mysql镜像并进行主从配置 1.下载需要的mysql版本镜像 docker pull mysql:5.6 2.启动mysql服务实例(基本启动) #启动主mysql docker r ...

  3. Docker 安装mysql以及外部访问

    (1)因为我们的镜像是linux环境下的,我所在的系统是windows系统.首先通过docker客户端切换到linux环境下. (2)使用docker pull mysql/mysql-server ...

  4. 35.Docker安装Mysql挂载Host Volume

    连个文件系统有块区域Area,我们要做的是把两个Area做文件映射 jesse腾讯云上有个linux的环境,版本比较老了 简书的地址: https://www.jianshu.com/p/b3bf64 ...

  5. docker安装mysql挂载宿主本地目录资源后无法启动的问题

    可能是权限问题,添加--privileged=true参数: docker run -p : --name zsmysql -v $PWD/data:/var/lib/mysql -v $PWD/lo ...

  6. Docker学习实践 - Docker安装MySql数据库

    Docker安装MySQL数据库 1.Ubuntu安装MySQL安装 (1)安装编译源码需要的包 sudo apt-get install make cmake gcc g++ bison libnc ...

  7. docker 安装mysql mongo

    Docker安装Mysql 1.拉取镜像 docker pull mysql/mysql-server 2.运行mysql docker run -d -p : --name [Name] [Imag ...

  8. 使用docker安装mysql和redis

    本文介绍在linux下使用docker安装mysql和redis. 原文地址:代码汇个人博客 http://www.codehui.net/info/59.html 测试环境:centos7.6,do ...

  9. CentOS 中利用docker安装MySQL

    1.前提条件 centos7 且内核版本高于3.10, 可通过命令: uname -r 查看内核版本 2.利用yum 安装docker 安装一些必要的系统工具: sudo yum install -y ...

随机推荐

  1. Leetcode 100 Same Tree 二叉树

    就是判断两棵树的值和结构是否相同 注意:要判断是否所有的树节点是否为NULL /** * Definition for a binary tree node. * struct TreeNode { ...

  2. Go程序开发---Go环境配置:CentOS6.5+Go1.8标准包安装

    1.Go安装 1.1Go的三种安装方式 Go有多种安装方式,可以选择自己习惯的方式进行,这里介绍三种安装方式: 1)Go源码安装 2)Go标准包安装 3)第三方工具安装 这里主要介绍下Go标准包在Ce ...

  3. C/S和B/S两种架构区别与优缺点分析

    C/S和B/S,是再普通不过的两种软件架构方式,都可以进行同样的业务处理,甚至也可以用相同的方式实现共同的逻辑.既然如此,为何还要区分彼此呢?那我们就来看看二者的区别和联系. 一.C/S 架构 1. ...

  4. windows 7 SDK和DDK下载

    检查小数据,获取地址.顺便记录下来. Windows Driver Kit Version 7.1.0 http://www.microsoft.com/downloads/details.aspx? ...

  5. opencart源码解析之 index.php

    //访问index.php,安全过滤.加载配置文件.核心启动文件.函数库.类库 //转载请注明: http://blog.csdn.net/dabao1989/article/details/2122 ...

  6. boost库asio详解8——几个TCP的简单例子

    摘于boost官网的几个例子, 做了点小修改, 笔记之. 同步客户端 void test_asio_synclient() { typedef boost::asio::io_service IoSe ...

  7. 读取spring工程中resource的文件

    Resource resource = new ClassPathResource("data.json"); // 读文件到字符串 String fileContent = Fi ...

  8. aspx页面@Page指令解析

    @Page指令位于每个ASP.NET页面的顶部,告诉ASP.NET这个具体页面使用什么属性,以及该页面继承的用户控件.ASP.NET页面@Page指令属性有:AspCompat.Async.Async ...

  9. 深入理解Amazon Alexa Skill(二)

    理解skill调用 本节来更详细的讨论alexa是如何确定调用哪个skill的. 参考:https://developer.amazon.com/zh/docs/custom-skills/under ...

  10. Python写的嗅探器——Pyside,Scapy

    使用Python的Pyside和Scapy写的嗅探器原型,拥有基本框架,但是功能并不十分完善,供参考. import sys import time import binascii from PySi ...