使用Docker部署MySQL

环境: Docker 19.03.1


1.拉取镜像
docker pull mysql
2.挂载目录
mkdir -p /home/mysql/data/mysql    #创建MySQL数据目录
mkdir -p /home/mysql/logs          #创建MySQL日志目录
mkdir -p /home/mysql/conf          #创建MySQL配置目录
touch /home/mysql/conf/my.cnf      #添加配置文件
vim /home/mysql/conf/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/

:wq 保存退出

3.启动容器
docker run -d --name mysql \
--restart=always \
-p 3306:3306 \
-v /home/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-v /home/mysql/logs:/logs \
-v /home/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=password mysql
4.进入容器
docker exec -it mysql /bin/bash
5.连接MySQL数据库
mysql -uroot -p        #输入密码即可连接数据库进行操作

以上为Docker环境下MySQL数据库的安装流程,MySQL远程访问请参考MySQL 新增账号并授权

你可能感兴趣的:(使用Docker部署MySQL)