MySQL 是世界上最受欢迎的开源数据库。凭借其可靠性、易用性和性能,MySQL 已成为 Web 应用程序的数据库优先选择。
访问 MySQL 镜像库地址:https://hub.docker.com/_/mysql?tab=tags 。
可以通过 Sort by 查看其他版本的 MySQL,默认是最新版本 mysql:latest 。
当然我们通常是通过命令查看,用 docker search mysql 命令来查看可用版本:
$ docker search mysql
[root@Roker ~]# docker search mysql INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/mysql MySQL is a widely used, open-source relati... 9697 [OK] docker.io docker.io/mariadb MariaDB is a community-developed fork of M... 3529 [OK] docker.io docker.io/mysql/mysql-server Optimized MySQL Server Docker images. Crea... 708 [OK] docker.io docker.io/centos/mysql-57-centos7 MySQL 5.7 SQL database server 77 docker.io docker.io/mysql/mysql-cluster Experimental MySQL Cluster Docker images. ... 71 docker.io docker.io/centurylink/mysql Image containing mysql. Optimized to be li... 61 [OK] docker.io docker.io/bitnami/mysql Bitnami MySQL Docker Image 43 [OK] docker.io docker.io/deitch/mysql-backup REPLACED! Please use http://hub.docker.com... 41 [OK] docker.io docker.io/tutum/mysql Base docker image to run a MySQL database ... 35 docker.io docker.io/schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic back... 30 [OK] docker.io docker.io/prom/mysqld-exporter 28 [OK] docker.io docker.io/databack/mysql-backup Back up mysql databases to... anywhere! 25 docker.io docker.io/linuxserver/mysql A Mysql container, brought to you by Linux... 25 docker.io docker.io/centos/mysql-56-centos7 MySQL 5.6 SQL database server 19 docker.io docker.io/circleci/mysql MySQL is a widely used, open-source relati... 19 docker.io docker.io/mysql/mysql-router MySQL Router provides transparent routing ... 16 docker.io docker.io/arey/mysql-client Run a MySQL client from a docker container 14 [OK] docker.io docker.io/fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron t... 7 [OK] docker.io docker.io/openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6 docker.io docker.io/genschsa/mysql-employees MySQL Employee Sample Database 5 [OK] docker.io docker.io/devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offi... 3 docker.io docker.io/ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK] 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]这里我们拉取官方的最新版本的镜像:
$ docker pull mysql:latest [root@Roker ~]# docker pull mysql:latest Trying to pull repository docker.io/library/mysql ... latest: Pulling from docker.io/library/mysql 8559a31e96f4: Pull complete d51ce1c2e575: Pull complete c2344adc4858: Pull complete fcf3ceff18fc: Pull complete 16da0c38dc5b: Pull complete b905d1797e97: Pull complete 4b50d1c6b05c: Pull complete c75914a65ca2: Pull complete 1ae8042bdd09: Pull complete 453ac13c00a3: Pull complete 9e680cd72f08: Pull complete a6b5dc864b6c: Pull complete Digest: sha256:8b7b328a7ff6de46ef96bcf83af048cb00a1c86282bfca0cb119c84568b4caf6 Status: Downloaded newer image for docker.io/mysql:latest使用以下命令来查看是否已安装了 mysql:
$ docker images [root@Roker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/mysql latest be0dbf01a0f3 3 weeks ago 541 MB安装完成后,我们可以使用以下命令来运行 mysql 容器:
$ docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql [root@Roker ~]# docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=000000 -d mysql eb31302470a446421a4c53712437e10e3d17e4d052181154cb1871c9e0b7e30c如果我们想挂载配置文件,那么按如下操作
1、在宿主机上,创建一个数据和配置文件的挂载路径
mkdir -p /usr/local/src/mysql/conf && mkdir -p /usr/local/src/mysql/data cd usr/local/src/mysql/conf touch my.cnf2、创建好宿主机的挂载数据路径后,按照如下命令执行启动容器。
docker run -p 3306:3306 --name mysql -v /usr/local/src/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /usr/local/src/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql命令说明:
-p 3306:3306:将容器的3306端口映射到主机的3306端口-v /usr/local/src/mysql/conf/my.cnf:/etc/mysql/my.cnf:将主机/usr/local/src/mysql/conf/my.cnf目录挂载到容器的/etc/mysql/my.cnf-e MYSQL_ROOT_PASSWORD=123456:初始化root用户的密码-d: 后台运行容器,并返回容器ID为什么要挂载?- 戳 《》学习。
查看是否启动成功
$ docker ps
[root@Roker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eb31302470a4 mysql "docker-entrypoint..." About an hour ago Up About an hour 0.0.0.0:3306->3306/tcp, 33060/tcp mysql查看启动日志
$ docker logs -f mysql
[root@Roker ~]# docker logs -f mysql 2020-07-03 09:14:08+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.20-1debian10 started. 2020-07-03 09:14:09+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' 2020-07-03 09:14:09+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.20-1debian10 started. 2020-07-03 09:14:09+00:00 [Note] [Entrypoint]: Initializing database files 2020-07-03T09:14:09.250716Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. 2020-07-03T09:14:09.250870Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.20) initializing of server in progress as process 41 2020-07-03T09:14:09.257527Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2020-07-03T09:14:14.931261Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 2020-07-03T09:14:23.708588Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option. 2020-07-03 09:14:44+00:00 [Note] [Entrypoint]: Database files initialized 2020-07-03 09:14:44+00:00 [Note] [Entrypoint]: Starting temporary server如果在链接过程中报错误:2059 - Authentication plugin 'caching_sha2_password' cannot be loaded:
从错误信息可知caching_sha2_password不能加载。
以上报错是由于目前已有的客户端连接软件还不支持Mysql8新增加的加密方式caching_sha2_password,所以我们需要修改用户的加密方式,将其改为老的加密验证方式。
所以接着操作,进入Mysql容器并在容器内登陆Mysql:
docker exec -it mysql bashmysql -u root -p;Enter password: [root@Roker ~]# docker exec -it mysql bash root@eb31302470a4:/# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 13 Server version: 8.0.20 MySQL Community Server - GPL Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>查看用户信息
可以看到当前用户的加密方式为caching_sha2_password
备注:host为 % 表示不限制ip , localhost表示本机使用 , plugin非mysql_native_password 则需要修改密码 。
执行命令
alter user 'root'@'%' identified with mysql_native_password by 'XXXXXX';或者(对IP做限制)
alter user 'root'@‘localhost' identified with mysql_native_password by 'XXXXXX';将用户的加密方式改为mysql_native_password。
执行命令flush privileges使权限配置项立即生效。
FLUSH PRIVILEGES;再次尝试链接,成功!
创建用户
CREATE USER user IDENTIFIED BY 'password';也可以这样 给与ip地址权限
CREATE USER `user`@`%` IDENTIFIED BY 'passowrd';授权
GRANT ALL PRIVILEGES ON *.* TO 'user'@'%';刷新权限
FLUSH PRIVILEGES;数据库时区问题:
链接数据库时serverTimezone=UTC这个参数出的问题 只要改成serverTimezone=Asia/Shanghai即可启动/停止容器
启用: docker start mysql 停止: docker stop mysql