Docker系列之MySQL安装教程
有了前面的基础教程Docker系列之常用命令操作手册之后,本博客记录一篇mysql的安装教程
mysql镜像查询命令
docker search mysql
几个关键参数解释一下:
- INDEX docker.io就是docker官网
- NAME 镜像的名称
- DESCRIPTION 镜像描述
- STARS 关注数目
- OFFICIAL 是否官方
- AUTOMATED 是否自动的
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/mysql MySQL is a widely used, open-source relati... 8930 [OK]
docker.io docker.io/mariadb MariaDB is a community-developed fork of M... 3140 [OK]
docker.io docker.io/mysql/mysql-server Optimized MySQL Server Docker images. Crea... 659 [OK]
docker.io docker.io/percona Percona Server is a fork of the MySQL rela... 462 [OK]
docker.io docker.io/centos/mysql-57-centos7 MySQL 5.7 SQL database server 66
docker.io docker.io/centurylink/mysql Image containing mysql. Optimized to be li... 61 [OK]
docker.io docker.io/mysql/mysql-cluster Experimental MySQL Cluster Docker images. ... 59
docker.io docker.io/deitch/mysql-backup REPLACED! Please use http://hub.docker.com... 41 [OK]
docker.io docker.io/bitnami/mysql Bitnami MySQL Docker Image 35 [OK]
docker.io docker.io/tutum/mysql Base docker image to run a MySQL database ... 34
docker.io docker.io/schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic back... 28 [OK]
docker.io docker.io/prom/mysqld-exporter 23 [OK]
docker.io docker.io/linuxserver/mysql A Mysql container, brought to you by Linux... 22
docker.io docker.io/centos/mysql-56-centos7 MySQL 5.6 SQL database server 17
docker.io docker.io/circleci/mysql MySQL is a widely used, open-source relati... 16
docker.io docker.io/mysql/mysql-router MySQL Router provides transparent routing ... 14
docker.io docker.io/arey/mysql-client Run a MySQL client from a docker container 13 [OK]
docker.io docker.io/openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6
docker.io docker.io/fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron t... 4 [OK]
docker.io docker.io/genschsa/mysql-employees MySQL Employee Sample Database 3 [OK]
docker.io docker.io/ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
docker.io docker.io/devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offi... 2
docker.io docker.io/jelastic/mysql An image of the MySQL database server main... 1
docker.io docker.io/monasca/mysql-init A minimal decoupled init container for mysql 0
docker.io docker.io/widdpim/mysql-client Dockerized MySQL Client (5.7) including Cu... 0 [OK]
[root@localhost ~]#
mysql镜像pull
# 不指定版本,默认latest版本
docker pull mysql
查看本地仓库镜像列表
docker images
docker run --name mysql01 -d mysql
查看运行的容器
docker ps
docker ps -a
docker logs c1a7aceff20b
发现如下报错,意思是要指定MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD其中之一,意思就是要设置密码,允许空密码,或者随机生成密码
docker rm c1a7aceff20b
查看所有容器
docker ps -a
再次启动容器并指定端口映射3306
docker run -p 3306:3306 --name mysql02 -e MYSQL_ROOT_PASSWORD=11 -d mysql
查看,容器启动成功,如果出现name重复的情况,可以指定另外的name
看官方文档也找到了指定编码的启动方法
docker run -p 3306:3306 --name mysql02 -e MYSQL_ROOT_PASSWORD=11 -d mysql --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
因为是最新版本,客户端登录时候会提示错误:
Error No.2058 Plugin caching_sha2_password could not be loaded
解决方法是,修改加密方式,进入mysql容器
docker exec -it mysql02 bash
mysql root登录
mysql -u root -p
修改加密规则
#修改加密规则
ALTER USER 'root'@'%' IDENTIFIED BY '11' PASSWORD EXPIRE NEVER;
对于学习可以指定'root'@'%',表示所有ip都能访问,对于生产安全性考虑,可以指定指定ip
#更新一下用户的密码
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '11';
需要刷新权限,才起效
#刷新权限
FLUSH PRIVILEGES;
重启一下密码,也可以不管
# 重置密码
alter user 'root'@'%' identified by '11';
# 把主机的/conf/mysql文件夹挂载到 mysqldocker容器的/etc/mysql/conf.d文件夹里面,改mysql的配置文件就只需要把mysql配置文件放在/conf/mysql既可
docker run ‐‐name mysql03 ‐v /conf/mysql:/etc/mysql/conf.d ‐e MYSQL_ROOT_PASSWORD=my‐secret‐pw
‐d mysql:tag
详细操作,建议参考Docker官方手册,入门及其它docker教程可以参考我的Docker博客专栏