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

久未见 提交于 2021-01-08 23:00:11
环境

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

1.首先创建mysql容器
sudo docker run --name mysql5.7 -p 3306:3306 -e MYSQL\_ROOT\_PASSWORD=123456 -d mysql:5.7
 
2.创建成功,查看一下运行状态
[root@localhost ~]# 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容器,后面挂载了外部数据的时候重新创建。
[root@localhost ~]# docker stop mysql5.7
[root@localhost ~]# docker rm mysql5.7
 
数据和配置挂载到宿主机

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=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="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参数,容器创建后不能正常启动,查看日志发现有权限的错误。
 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!