一、安装数据库
1、导入镜像:
说明:考虑到从网上镜像不好下载,采用导入镜像方式,镜像可从我百度网盘下载:
通过网盘分享的文件:mysql-5.7.32.tar
链接:
https://pan.baidu.com/s/1a2EyfNbGzT4yEhs323qDkg 提取码: zbit
下载下来镜像后用下面命令导入。
docker load < D:\soft\java\docker\images\mysql-5.7.32.tar
或
docker load -i D:\soft\java\docker\images\mysql-5.7.32.tar
截屏如下
查看本地docker镜像
docker images
-----------
2、创建mysql容器并运行
先创建 D:\data\dockerData\mysql5.7.32\conf文件夹、 D:\data\dockerData\mysql5.7.32\log和 D:\data\dockerData\mysql5.7.32\data 文件夹,D:\data\dockerData\mysql5.7.32\conf下面创建my.cnf配置文件。
说明:
mysql5.7.32默认没有创建my.cnf配置文件,如果需要可手动添加,放到 /mysql5.7.32/conf 下面。
配置文件如下:
# 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, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# 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, version 2.0, 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@689036e28de4:/etc/mysql# pwd
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#日志文件,如果没有需要手动创建,并赋予相应写权限,否则会导致mysql启动失败。
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
lower_case_table_names=1
max_connections = 2000
max_allowed_packet=500M
# 设置时区为当前时间
default-time_zone = '+8:00'
给文件夹赋予权限
本次我给所有用户赋予了写权限
#(注意一定要设置时区) default-time_zone = '+8:00'
再运行如下命令。
docker run -d -p 13306:3306 --name mysql --restart=always --privileged=true -v /d/data/dockerData/mysql5.7.32/conf:/etc/mysql -v /d/data/dockerData/mysql5.7.32/data:/var/lib/mysql -v /d/data/dockerData/mysql5.7.32/log:/var/log -e MYSQL_ROOT_PASSWORD=MyStrogePwd@0418 mysql:5.7.32
说明:
--privileged=true 设置权限,否则容器启动不起来
在 Windows 系统中,路径通常以盘符开头(如 D:/),而在 Linux 系统中,路径以 / 开头。Docker 容器基于 Linux 环境运行,因此挂载路径需要符合 Linux 的路径格式。
例如:windows中路径是:D:\data\dockerData,则需要写成:/d/data/dockerData
执行结果截屏如下:
++++++以下是确认 my.cnf data log等目录路径++++++++++++++++++++++++++++++++++++
进入Docker容器内
docker exec -it mysql bash
确定Docker内 MySQL 文件相关路径
查找my.cnf
1、Linux下MySQL的配置文件是my.cnf,一般会放在/etc/my.cnf,/etc/mysql/my.cnf。如果找不到,可以用find命令查找。
2、mysql help命令
# Docker内,MySQL配置文件my.cnf的位置
mysql --help | grep my.cnf
# 显示如下,意思是路径按优先排序,会是在以下路径里:
order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
# 配置文件的路径不一定都一样,有些文章介绍的位置是在/etc/my.cnf。而我的docker虚拟机系统上,实际存在位置是在/etc/mysql/my.cnf
3、docker登录到mysql
docker exec-it mysql bash
4、相关命令
停止正在运行中的容器
docker stop 容器的id(或names)
启动容器
docker start 容器id(或names)
二、创建数据库实例和用户
1、创建数据库实例
使用 sqlyol的UI界面创建一个数据库,命名为wsclc,指定其默认字符集为 utf8,默认排序规则为 utf8_general_ci(简体中文,不区分大小写),输入的 SQL 语句与执行结果如下所示:
CREATE DATABASE IF NOT EXISTS wsclc DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
2、执行sql语句创建相关表。
截屏如下: