() 环境

docker Version: 18.06.3-ce
mysql 5.7

问题描述:

Docker 如何部署mysql 5.7

解决方案:

1.搜索docker hub上的mysql镜像

docker search mysql

[root@localhost ~]# docker search mysqlNAME DESCRIPTION STARS OFFICIAL AUTOMATEDmysql MySQL is a widely used, open-source relation… 12338 [OK] mariadb MariaDB Server is a high performing open sou… 4746 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 916 [OK]percona Percona Server is a fork of the MySQL relati… 572 [OK] phpmyadmin phpMyAdmin - A web interface for MySQL and M… 488 [OK] mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 93 centos/mysql-57-centos7 MySQL 5.7 SQL database server 92 bitnami/mysql Bitnami MySQL Docker 电脑 Image 67 [OK]ubuntu/mysql MySQL open source fast, stable, multi-thread… 28 circleci/mysql MySQL is a widely used, open-source relation… 25 mysql/mysql-router MySQL Router provides transparent routing be… 23 centos/mysql-56-centos7 MySQL 5.6 SQL database server 22 google/mysql MySQL server for Google Compute Engine 21 [OK]vmware/harbor-db Mysql container for Harbor 10 mysqlboy/docker-mydumper docker-mydumper containerizes MySQL logical … 3 电脑 mysqlboy/mydumper mydumper for mysql logcial backups 3 bitnami/mysqld-exporter 2 ibmcom/mysql-s390x Docker image for mysql-s390x 1 ibmcom/tidb-ppc64le TiDB is a distributed NewSQL database compat… 0 mirantis/mysql 0 mysqlboy/elasticsearch 0 mysqleatmydata/mysql-eatmydata 0 cimg/mysql 0 mysql/mysql-operator MySQL Operator for Kubernetes 0 mysql/ndb-operator MySQL NDB Operator for Kubernetes 0

2.下载镜像mysql5.7

docker pull mysql:5.7

[root@localhost ~]# docker pull mysql:5.75.7: Pulling from library/mysqlf003217c5aae: Pull complete 65d94f01a09f: Pull complete 43d78aaa6078: Pull complete a0f91ffbdf69: Pull complete 59ee9e07e12f: Pull complete 04d82978082c: Pull complete 70f46ebb971a: Pull complete ba61822c65c2: Pull complete dec59acdf78a: Pull complete 0a05235a6981: Pull complete c87d621d6916: Pull complete Digest: sha256:1a73b6a8f507639a8f91ed01ace28965f4f74bb62a9d9b9e7378d5f07fab79dcStatus: Downloaded newer image for mysql:5.7

3.准备部署

本地系统创建部署目录,进入部署目录
创建conf,data,logs目录分别用来存放mysql配置,mysql数据,mysql日志

mkdir conf
mkdir data
mkdir logs

批量创建

[root@localhost srv]# mkdir conf data logs

更改3个文件夹权限

chmod 777 logs
chmod 777 data
chmod 777 conf

进入conf目录,写入mysql配置如下

[root@localhost srv]# cd conf
[root@localhost conf]# touch my.cnf
[root@localhost conf]# vim my.cnf

# 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 = 2Mdatadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pidcharacter_set_server=utf8init_connect='SET NAMES utf8

4.创建并启动镜像

退出conf目录,使用如下命令创建并启动mysql容器。

[root@w srv]# docker run -itd --name="mysql" -p3307:3306 -v$PWD/conf/:/etc/mysql/conf.d -v$PWD/data:/var/lib/mysql -v$PWD/logs:/var/log -eMYSQL_ROOT_PASSWORD="mysql8@#1k" mysql:5.7

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

docker run:创建并启动应用-i:开启容器的标准输入STDIN-t:容器分配伪终端,用于再终端登录容器-d:后台运行容器--name:容器名称-p3307:3306:打开容器的3306端口并且映射到本机的3307端口-v$PWD/conf/:/etc/mysql/conf.d,挂载($PWD)当前目录下的conf目录到容器的/etc/mysql/conf.d目录v$PWD/data:/var/lib/mysql,挂载($PWD)当前目录下的data目录到容器的/var/lib/mysql目录 -v$PWD/logs:/var/log,挂载($PWD)当前目录下的logs目录到容器的/var/log/目录-eMYSQL_ROOT_PASSWORD="mysql",增加环境变量MYSQL_ROOT_PASSWORD,mysql容器创建并启动时,设置mysql的登录密码mysql:5.7,要创建并启动容器的mysql镜像

命令执行后

083041526b249f02c79bef77dcdf4ae42110fdfad3fdf1624e0334cfcf4606aa

查看容器信息

[root@localhost srv]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES848a2581da80 mysql:5.7 "docker-entrypoint.s…" 7 minutes ago Up 3 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp mysql

5.测试

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

[root@localhost srv]# docker exec -it 848a2581da80 /bin/bash

执行mysql -uroot -p命令后输入启动容器时设置的mysql密码,我的密码是mysql,连接mysql数据库成功。

mysql远程登录权限管理

root@848a2581da80:/# mysql -uroot -p Enter password: 之前设置的密码Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 3Server version: 5.7.37 MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.mysql> grant all privileges on *.* to 'root'@'%' identified by 'mysql8@#1k' 命令 -> ;Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> flush privileges; 命令Query OK, 0 rows affected (0.00 sec)mysql> exit 退出

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

root@848a2581da80:/# cat /etc/hosts127.0.0.1localhost::1localhost ip6-localhost ip6-loopbackfe00::0ip6-localnetff00::0ip6-mcastprefixff02::1ip6-allnodesff02::2ip6-allrouters172.17.0.2848a2581da80

Ctrl+P+Q退出容器

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

docker container inspect 848a2581da80 | grep IP

[root@localhost srv]# docker container inspect 848a2581da80 | grep IP "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "IPAMConfig": null, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0,

查看data文件夹

[root@localhost srv]# ls dataauto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sysca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem

6.Navicat 测试连接


7.完成


电脑