docker安装mysql

2019-01-06  本文已影响0人  问瑾遗

上文: win10安装docker入门

启动cmd

1.搜索mysql
C:\Users\Administrator>docker search mysql
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                                                  MySQL is a widely used, open-source relation…   7641                [OK]
mariadb                                                MariaDB is a community-developed fork of MyS…   2492                [OK]
mysql/mysql-server                                     Optimized MySQL Server Docker images. Create…   579                                     [OK]
zabbix/zabbix-server-mysql                             Zabbix Server with MySQL database support       158                                     [OK]
hypriot/rpi-mysql                                      RPi-compatible Docker Image with Mysql          103
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   84                                      [OK]
centurylink/mysql                                      Image containing mysql. Optimized to be link…   59                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          48                                      [OK]
centos/mysql-57-centos7                                MySQL 5.7 SQL database server                   45
mysql/mysql-cluster                                    Experimental MySQL Cluster Docker images. Cr…   39
tutum/mysql                                            Base docker image to run a MySQL database se…   31
bitnami/mysql                                          Bitnami MySQL Docker Image                      23                                      [OK]
schickling/mysql-backup-s3                             Backup MySQL to S3 (supports periodic backup…   23                                      [OK]
zabbix/zabbix-proxy-mysql                              Zabbix proxy with MySQL database support        19                                      [OK]
linuxserver/mysql                                      A Mysql container, brought to you by LinuxSe…   18
centos/mysql-56-centos7                                MySQL 5.6 SQL database server                   12
circleci/mysql                                         MySQL is a widely used, open-source relation…   9
mysql/mysql-router                                     MySQL Router provides transparent routing be…   8
openshift/mysql-55-centos7                             DEPRECATED: A Centos7 based MySQL v5.5 image…   6
dsteinkopf/backup-all-mysql                            backup all DBs in a mysql server                5                                       [OK]
openzipkin/zipkin-mysql                                Mirror of https://quay.io/repository/openzip…   1
jelastic/mysql                                         An image of the MySQL database server mainta…   1
ansibleplaybookbundle/mysql-apb                        An APB which deploys RHSCL MySQL                0                                       [OK]
cloudfoundry/cf-mysql-ci                               Image used in CI of cf-mysql-release            0
cloudposse/mysql                                       Improved `mysql` service with support for `m…   0                                       [OK]

2.下载mysql镜像
C:\Users\Administrator>docker pull mysql
3.查看镜像,可见已经有了mysql镜像
C:\Users\Administrator>docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        5 days ago          1.84kB
tomcat              latest              16c3009a3981        10 days ago         475MB
mysql               latest              f991c20cb508        7 weeks ago         486MB

4.启动mysql
C:\Users\Administrator>docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=mypwd -d mysql --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
-p 3306:3306 将mysql容器的3306端口映射到docker宿主机的3306端口,格式为:-p 宿主机端口:mysql容器端口
--name mysql 将容器命名为mysql
-e MYSQL_ROOT_PASSWORD=mypwd 设置mysql容器root用户密码为mypwd,
-d 让容器后台运行
mysql 第三步中容器的REPOSITORY,有时候格式可能为REPOSITORY:TAG,tag表示版本号,如果不加默认为latest
 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci 关于mysql容器编码的一些配置

5.查看运行中的容器
C:\Users\Administrator>docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
92d7fb46aff2        mysql               "docker-entrypoint.s…"   27 minutes ago      Up 27 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp   mysql

由于我本地主机中也安装了mysql,而docker也是安装在我本地主机的,访问mysql容器时也是用的localhost:3306,所以我先把本地主机的mysql关掉:
以管理员身份运行cmd:

net stop mysql

此时使用navicat连接docker中的mysql容器会报错:
Client does not support authentication protocol requested by server;consider upgrading MySQL client
客户端不支持服务器请求的身份验证协议;考虑升级MySQL客户端

解决方案一:

1.进入mysql交互端
C:\Users\Administrator>docker exec -it mysql bash
root@92d7fb46aff2:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.13 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 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>
2.查看用户信息
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host      | user             | plugin                | authentication_string                                                  |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| %         | root             | caching_sha2_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9                              |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session    | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys        | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root             | caching_sha2_password | $A$005$5       E'>8/ke(u\jN'�rOs�eaTTZakHfcPsxxp97i3zwDjUsxR66ARWIVtnpBt3Aa9 |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
5 rows in set (0.00 sec)
备注:host为 % 表示不限制ip    localhost表示本机使用    plugin非mysql_native_password 则需要修改密码
3.这里我们修改host为%,user为root的信息,至于修改localhost root的行不行,未测试。
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'newpassword'; 
#更新一下用户的密码 root用户密码为newpassword
4.退出mysql容器交互
mysql> exit;
Bye
root@92d7fb46aff2:/# exit
exit

C:\Users\Administrator>

此时再次连接,则连接成功,这里连接用的root用户,当然也可以新建用户,赋予权限,不再赘述。

其他解决思路:

刚才不才运行mysql时使用的命令为:

docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

可以在运行mysql容器时指定用户名密码,参考文档:https://blog.csdn.net/MaxWoods/article/details/81329953
也可以通过自定义配置文件的方式来运行容器。
上述方案并未亲测,自行解决。

上一篇 下一篇

猜你喜欢

热点阅读