Docker安装MySql-挂载外部数据和配置
2024-08-25 14:42:51
环境
- 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参数,容器创建后不能正常启动,查看日志发现有权限的错误。
最新文章
- 在终端terminal运行ReactNative程序经常出现的问题
- css3 text-overflow和white-space,word-wrap,word-break进阶
- Linux 多线程可重入函数
- css 基础---选择器
- SQL Server代理(5/12):理解SQL代理错误日志
- jqMobile中pageinit,pagecreate,pageshow等函数的执行顺序
- SetHandleInformation设置内核对象标志
- Zooming MKMapView to fit annotation pins
- CentOS6.4安装LAMP环境
- 删除svn密码方法
- Android应用程序启动过程源代码分析
- win32 字体变换与窗口同大同小
- $(document).ready()和window.onload之间的差异
- jsp笔记----jsp常用的的获取项目的根路径
- odoo 11 配置nginx反向代理
- 2017 browser market share
- 5210: 最大连通子块和 动态DP 树链剖分
- React Native 组建之IOS和Android通用抽屉
- cloudera cdh5.13.0 vmware 快速安装
- crc16.c