- 首先我们需要拉取镜像到本地来,这里我们拉取最新的下来
docker pull mysql:latestlatest: Pulling from library/mysql80369df48736: Pull completee8f52315cb10: Pull completecf2189b391fc: Pull completecc98f645c682: Pull complete27a27ac83f74: Pull completefa1f04453414: Pull completed45bf7d22d33: Pull complete3dbac26e409c: Pull complete9017140fb8c1: Pull completeb76dda2673ae: Pull completebea9eb46d12a: Pull completee1f050a38d0f: Pull completeDigest: sha256:7345ce4ce6f0c1771d01fa333b8edb2c606ca59d385f69575f8e3e2ec6695eeeStatus: Downloaded newer image for mysql:latestdocker.io/library/mysql:latestdocker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmysql latest c8ee894bd2bd 11 days ago 456MBdocker run --name wtp-mysql -p 33006:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest821eed795a6671c2d0abe0a715317f3a537f77f6179f95a674ce399ed0248656# --name: 容器名字# -p 容器暴漏的端口# 33006:3306 外部的33006对应容器内部的3306端口# -e 容器的环境变量# -d 容器后台运行
这个时候我们使用navicat连接一下看看
Test Connection出错:
权限验证规则不能加载,我们进入容器看看
docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES821eed795a66 mysql:latest "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 33060/tcp, 0.0.0.0:33006->3306/tcp wtp-mysqldocker exec -it wtp-mysql /bin/bash# docker exec :在运行的容器中执行命令# -i 即使没有附加也保持STDIN 打开# -t 分配一个伪终端root@821eed795a66:/# lsbin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr varmysql -u root -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 11Server version: 8.0.18 MySQL Community Server - GPLCopyright (c) 2000, 2019, 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> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys |+--------------------+4 rows in set (0.00 sec)mysql> use mysql;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> show tables;+---------------------------+| Tables_in_mysql |+---------------------------+| columns_priv || component || db || default_roles || engine_cost || func || general_log || global_grants || gtid_executed || help_category || help_keyword || help_relation || help_topic || innodb_index_stats || innodb_table_stats || password_history || plugin || procs_priv || proxies_priv || role_edges || server_cost || servers || slave_master_info || slave_relay_log_info || slave_worker_info || slow_log || tables_priv || time_zone || time_zone_leap_second || time_zone_name || time_zone_transition || time_zone_transition_type || user |+---------------------------+33 rows in set (0.00 sec)### 查看root用户的密码验证规则,修改一下mysql> select `plugin` from user where User='root';+-----------------------+| plugin |+-----------------------+| caching_sha2_password || caching_sha2_password |+-----------------------+2 rows in set (0.00 sec)mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';Query OK, 0 rows affected (0.01 sec)
这个时候我们再进行navicat连接测试下
这样我们就可以访问容器里面的mysql数据库了。
