docker 安装 mysql 并且外挂文件目录

1. 拉取docker 镜像

docker  pull mysql

2. 创建挂载目录

mkdir /docker/mysql/conf
mkdir /docker/mysql/data
mkdir /docker/mysql/log

3. 创建my.cnf文件

cd /docker/mysql/conf
vi my.cnf

4. 插入以下内容my.cnf

# Copyright (c) 2017, 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
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
 
# Custom config should go here
!includedir /etc/mysql/conf.d/

5. 创建并且启动docker

docker run 
    -p 3306:3306 
    -e MYSQL_ROOT_PASSWORD=test@123 
    -v /docker/mysql/data:/var/lib/mysql 
    -v /docker/mysql/log:/logs 
    -v /docker/mysql/conf/my.cnf:/etc/mysql/my.cnf 
    --name mysql_1 
    --restart=always 
    -d mysql:latest
原文地址:https://www.cnblogs.com/Jesse-Li/p/14466493.html