Windows/Linux下Docker部署自己MYSQL 5.7容器并挂载本地目录(超详细)

1.先拉取mysql镜像

docker pull mysql:5.7

2.创建三个文件夹用于挂载MYSQL配置目录

docker怎么挂载本地目录 docker挂载本地文件_mysql


docker怎么挂载本地目录 docker挂载本地文件_docker怎么挂载本地目录_02


my.ini先简约配置一下防止乱码,具体配置可以自己配

[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
lower_case_table_names=1
character-set-server = utf8

[client]
default-character-set=utf8

3.到刚刚创建的MYSQL5.7目录,使用如下命令创建并启动MYSQL容器

1.Linux命令

docker run -itd --name="mysql" -p 3306:3306 -v $PWD/conf/:/etc/mysql/conf.d -v $PWD/data:/var/lib/mysql -v $PWD/logs:/var/log -eMYSQL_ROOT_PASSWORD="mysql"  mysql:5.7

2.windows命令

docker run -itd --name="mysql" -p 3306:3306 -v G:\DockerVolumes\mysql5.7\conf\:/etc/mysql/conf.d -v G:\DockerVolumes\mysql5.7\data:/var/lib/mysql -v G:\DockerVolumes\mysql5.7\logs:/var/log -eMYSQL_ROOT_PASSWORD="123456"  mysql:5.7

其中命令各部分的意义分别是:

docker run:创建并启动应用
-i:开启容器的标准输入STDIN
-t:容器分配伪终端,用于再终端登录容器
-d:后台运行容器
–name:容器名称
-p3307:3306:打开容器的3306端口并且映射到本机的3307端口
-vdocker怎么挂载本地目录 docker挂载本地文件_linux_03PWD)当前目录下的conf目录到容器的/etc/mysql/conf.d目录
vdocker怎么挂载本地目录 docker挂载本地文件_mysql_04PWD)当前目录下的data目录到容器的/var/lib/mysql目录
-vdocker怎么挂载本地目录 docker挂载本地文件_mysql_05PWD)当前目录下的logs目录到容器的/var/log/目录
-eMYSQL_ROOT_PASSWORD=“123456”,增加环境变量MYSQL_ROOT_PASSWORD,mysql容器创建并启动时,设置mysql的登录密码
mysql:5.7,要创建并启动容器的mysql镜像,也可以是自己的镜像 windwos下挂载目录卷需要写全局路径

4,测试

进入容器终端测试mysql,并进行远程登录权限管理

docker exec -it e28d93330023 /bin/bash

其中e28d93330023为容器ID,查看容器信息可以获得。
执行命令后进入容器终端
执行mysql -uroot -p命令后输入启动容器时设置的mysql密码,我的密码是mysql,连接mysql数据库成功。
mysql远程登录权限管理

mysql> grant all privileges on . to ‘root’@’%’ identified by ‘mysql’
 -> ; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql>

退出mysql客户端,查看容器的ip地址

cat /etc/hosts127.0.0.1	localhost::1	localhost ip6-localhost ip6-loopbackfe00::0	ip6-localnetff00::0	ip6-mcastprefixff02::1	ip6-allnodesff02::2	ip6-allrouters172.17.0.2	e28d93330023

或者通过 docker container inspect命令查看容器的详细信息

docker container inspect e28d93330023| grep IP  
       "LinkLocalIPv6Address": "",   
       "LinkLocalIPv6PrefixLen": 0,       
       "SecondaryIPAddresses": null,      
      "SecondaryIPv6Addresses": null,            
        "GlobalIPv6Address": "",       
          "GlobalIPv6PrefixLen": 0,        
          "IPAddress": "172.17.0.2",         
         "IPPrefixLen": 16,

退出容器

使用容器的IP地址再宿主机登录方才启动的mysqlServer。

[root@ecs-7bc6-0001 mysql]# mysql -h172.17.0.2 -P3306 -uroot -pEnter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 3Server version: 5.7.26 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>

补充:MySQL镜像环境变量

环境变量

备注

MYSQL_ROOT_PASSWORD

此环境变量是必须的,mysql的root超级用户的初始化密码。

MYSQL_DATABASE

此环境变量是可选的,指定容器在创建时要创建的数据库,如果容器创建命令同时指定了用户名/密码,则该用户会被授予该数据库的超级访问权限。

MYSQL_USER,MYSQL_PASSWORD

变量可选,用于指定容器在创建时要创建的用户名密码,如果同时指定了数据库,会授予该用户指定数据库的超级访问权限。

MYSQL_ALLOW_EMPTY_PASSWORD

可选环境变量,指定mysql初始化root用户可以不需要密码就可以登录。

MYSQL_RANDOM_ROOT_PASSWORD

可选环境变量,指定mysql初始化root用户使用随机密码。

MYSQL_ONETIME_PASSWORD

可选环境变量,指定root在首次登录时强制修改密码。注意不是MYSQL_USER指定的用户。