目录
(1)创建文件夹:
在自定义文件夹中创建以下文件夹:
data:数据存放的文件夹
logs:日志存放的文件夹
conf:数据库配置文件夹
mysql-files:数据库文件夹,高版本需要这个文件夹映射
(2)编写docker命令:
docker run -d --name container_name \ //指定容器名字,保证唯一要
-p 3306:3306 \ //映射容器的端口号到宿主机的端口号
--restart=always \ //容器开机自启
-v /wocloud/soft/mysql/data:/var/lib/mysql \ //把容器内的数据目录挂载到宿主机的对应目录下
-v /wocloud/soft/mysql/logs:/var/log/mysql \ //挂载日志目录
-v /wocloud/soft/mysql/conf:/etc/mysql \ //挂载配置文件
-v /wocloud/soft/mysql/mysql-files:/var/lib/mysql-files \ //挂载数据库文件
-e MYSQL_ROOT_PASSWORD=123456 \ //设置root密码
mysql
说明:-d:表示容器后台运行
--name:指定容器名字,保证唯一要
-p:映射容器的端口号到宿主机的端口号,冒号前端为宿主机端口号,冒号后为docker容器端口号
-v:映射目录,注意my.cnf要在docker默认的my.cnf基础上增加自定义的参数,docker的my.cnf默认配置文件可以在我的文件夹中找到,也可以自己启动一个新的mysql容器,不映射文件,然后复制mysql容器中的:/etc/mysql/my.cnf文件出来即可获取。在docker中mysql的默认配置文件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/
-e:设置环境变量
mysql:指定使用的镜像
最终的docker命令:
docker run -d --name mysql_test \
-p 3306:3306 \
--restart=always \
-v /wocloud/soft/mysql/data:/var/lib/mysql \
-v /wocloud/soft/mysql/logs:/var/log/mysql \
-v /wocloud/soft/mysql/conf:/etc/mysql \
-v /wocloud/soft/mysql/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:8.0.15
(3)创建多个docker命令:
在上述基础上,一定要修改的地方有:
--name的参数;
-v各种映射目录;
(4)构建容器:
执行编写的docker命令,完成容器的构建和启动。
(5)设置远程连接:
进入到mysql容器,然后登录到数据库中,给root用户授权可远程登录,执行SQL:
授权远程登录SQL:
grant all PRIVILEGES on *.* to root@'%' WITH GRANT OPTION;
更新授权SQL:
FLUSH PRIVILEGES;
(6)修改password算法:
由于Mysql5.6以上的版本修改了Password算法,这里需要更新密码算法,便于使用Navicat连接:
ALTER user 'root'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
FLUSH PRIVILEGES;
(7)访问数据库:
使用navicat或者sqlYog等工具远程连接mysql容器数据库。