- 首先我们需要拉取镜像到本地来,这里我们拉取最新的下来
docker pull mysql:latest
latest: Pulling from library/mysql
80369df48736: Pull complete
e8f52315cb10: Pull complete
cf2189b391fc: Pull complete
cc98f645c682: Pull complete
27a27ac83f74: Pull complete
fa1f04453414: Pull complete
d45bf7d22d33: Pull complete
3dbac26e409c: Pull complete
9017140fb8c1: Pull complete
b76dda2673ae: Pull complete
bea9eb46d12a: Pull complete
e1f050a38d0f: Pull complete
Digest: sha256:7345ce4ce6f0c1771d01fa333b8edb2c606ca59d385f69575f8e3e2ec6695eee
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest c8ee894bd2bd 11 days ago 456MB
docker run --name wtp-mysql -p 33006:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
821eed795a6671c2d0abe0a715317f3a537f77f6179f95a674ce399ed0248656
# --name: 容器名字
# -p 容器暴漏的端口
# 33006:3306 外部的33006对应容器内部的3306端口
# -e 容器的环境变量
# -d 容器后台运行
这个时候我们使用navicat连接一下看看
Test Connection出错:
权限验证规则不能加载,我们进入容器看看
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
821eed795a66 mysql:latest "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 33060/tcp, 0.0.0.0:33006->3306/tcp wtp-mysql
docker exec -it wtp-mysql /bin/bash
# docker exec :在运行的容器中执行命令
# -i 即使没有附加也保持STDIN 打开
# -t 分配一个伪终端
root@821eed795a66:/# ls
bin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
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.18 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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> 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 names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> 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数据库了。