環(huán)境: Docker 19.03.1
1.拉取鏡像
docker pull mysql
2.掛載目錄
mkdir -p /home/mysql/data/mysql #創(chuàng)建MySQL數(shù)據(jù)目錄
mkdir -p /home/mysql/logs #創(chuàng)建MySQL日志目錄
mkdir -p /home/mysql/conf #創(chuàng)建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.啟動(dòng)容器
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.進(jìn)入容器
docker exec -it mysql /bin/bash
5.連接MySQL數(shù)據(jù)庫(kù)
mysql -uroot -p #輸入密碼即可連接數(shù)據(jù)庫(kù)進(jìn)行操作
以上為Docker環(huán)境下MySQL數(shù)據(jù)庫(kù)的安裝流程,MySQL遠(yuǎn)程訪問(wèn)請(qǐng)參考
MySQL 新增賬號(hào)并授權(quán)