Docker安装MySql-挂载外部数据和配置

15 篇文章 0 订阅
15 篇文章 2 订阅

 

拷贝mysql配置文件


1.首先创建mysql容器

sudo docker run --name mysql5.7 -p 3306:3306 -e MYSQL\_ROOT\_PASSWORD=123456 -d mysql:5.7

 

2.创建成功,查看一下运行状态

docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
c84a366e3abf        mysql:5.7           "docker-entrypoint..."   4 minutes ago       Up 15 seconds       33060/tcp, 0.0.0.0:4306->3306/tcp   mysql5.7

 

3.可以看到我们的容器正在运行中,现在进入容器,查看一下配置文件

[root@localhost ~]# docker exec -it mysql5.7 /bin/bash
root@c84a366e3abf:/# cat /etc/mysql/my.cnf
# Copyright (c) 2016, 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 2 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
root@c84a366e3abf:/# cat /etc/mysql/mysql.conf.d/mysqld.cnf
# Copyright (c) 2014, 2016, 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 2 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 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=1
#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=0

4.把输出的内容复制一下,我们在本地先建一个同名文件,把内容粘贴进去,此时也可以修改添加自己想要的配置,例如加上忽略表名大小写的配置:

lower_case_table_names=1

5.把这个文件保存一下,然后切换到命令行,输入exit退出容器。

6.下一步停止并删除mysql5.7容器,后面挂载了外部数据的时候重新创建。

 docker stop mysql5.7
 docker rm mysql5.7

 

数据和配置挂载到宿主机


1.在linux环境下创建挂载目录,把前面拷出来的配置复制到下面创建的mysqld.cnf中

 cd /opt
 mkdir mysql
 cd mysql
 mkdir data
 mkdir config
 cd config
 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=1
#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=0

 

3.执行下面的命令创建容器

sudo docker run --name mysql5.7 --restart always --privileged=true -p 4306:3306 -v /opt/mysql/config/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf -v /opt/mysql/data:/var/lib/mysql -e MYSQL_USER="lxq" -e MYSQL_PASSWORD="lxq123" -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=”lxq”:添加用户lxq
-e MYSQL_PASSWORD=”lxq123”:设置密码lxq123
-e MYSQL_ROOT_PASSWORD=”rootpwd123”:设置root的密码rootpwd123

特别说明:如果没有添加--privileged=true参数,容器创建后不能正常启动,查看日志发现有权限的错误。

查看mysql log

docker logs --tail=20 -f mysql5.7

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值