1. 首先我们需要拉取镜像到本地来,这里我们拉取最新的下来
    1. docker pull mysql:latest
    2. latest: Pulling from library/mysql
    3. 80369df48736: Pull complete
    4. e8f52315cb10: Pull complete
    5. cf2189b391fc: Pull complete
    6. cc98f645c682: Pull complete
    7. 27a27ac83f74: Pull complete
    8. fa1f04453414: Pull complete
    9. d45bf7d22d33: Pull complete
    10. 3dbac26e409c: Pull complete
    11. 9017140fb8c1: Pull complete
    12. b76dda2673ae: Pull complete
    13. bea9eb46d12a: Pull complete
    14. e1f050a38d0f: Pull complete
    15. Digest: sha256:7345ce4ce6f0c1771d01fa333b8edb2c606ca59d385f69575f8e3e2ec6695eee
    16. Status: Downloaded newer image for mysql:latest
    17. docker.io/library/mysql:latest
    18. docker images
    19. REPOSITORY TAG IMAGE ID CREATED SIZE
    20. mysql latest c8ee894bd2bd 11 days ago 456MB
    21. docker run --name wtp-mysql -p 33006:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
    22. 821eed795a6671c2d0abe0a715317f3a537f77f6179f95a674ce399ed0248656
    23. # --name: 容器名字
    24. # -p 容器暴漏的端口
    25. # 33006:3306 外部的33006对应容器内部的3306端口
    26. # -e 容器的环境变量
    27. # -d 容器后台运行

    这个时候我们使用navicat连接一下看看
    image.png
    Test Connection出错:
    image.png
    权限验证规则不能加载,我们进入容器看看

    1. docker ps
    2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    3. 821eed795a66 mysql:latest "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 33060/tcp, 0.0.0.0:33006->3306/tcp wtp-mysql
    4. docker exec -it wtp-mysql /bin/bash
    5. # docker exec :在运行的容器中执行命令
    6. # -i 即使没有附加也保持STDIN 打开
    7. # -t 分配一个伪终端
    8. root@821eed795a66:/# ls
    9. 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
    10. mysql -u root -p
    11. Enter password:
    12. Welcome to the MySQL monitor. Commands end with ; or \g.
    13. Your MySQL connection id is 11
    14. Server version: 8.0.18 MySQL Community Server - GPL
    15. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
    16. Oracle is a registered trademark of Oracle Corporation and/or its
    17. affiliates. Other names may be trademarks of their respective
    18. owners.
    19. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    20. mysql> show databases;
    21. +--------------------+
    22. | Database |
    23. +--------------------+
    24. | information_schema |
    25. | mysql |
    26. | performance_schema |
    27. | sys |
    28. +--------------------+
    29. 4 rows in set (0.00 sec)
    30. mysql> use mysql;
    31. Reading table information for completion of table and column names
    32. You can turn off this feature to get a quicker startup with -A
    33. Database changed
    34. mysql> show tables;
    35. +---------------------------+
    36. | Tables_in_mysql |
    37. +---------------------------+
    38. | columns_priv |
    39. | component |
    40. | db |
    41. | default_roles |
    42. | engine_cost |
    43. | func |
    44. | general_log |
    45. | global_grants |
    46. | gtid_executed |
    47. | help_category |
    48. | help_keyword |
    49. | help_relation |
    50. | help_topic |
    51. | innodb_index_stats |
    52. | innodb_table_stats |
    53. | password_history |
    54. | plugin |
    55. | procs_priv |
    56. | proxies_priv |
    57. | role_edges |
    58. | server_cost |
    59. | servers |
    60. | slave_master_info |
    61. | slave_relay_log_info |
    62. | slave_worker_info |
    63. | slow_log |
    64. | tables_priv |
    65. | time_zone |
    66. | time_zone_leap_second |
    67. | time_zone_name |
    68. | time_zone_transition |
    69. | time_zone_transition_type |
    70. | user |
    71. +---------------------------+
    72. 33 rows in set (0.00 sec)
    73. ### 查看root用户的密码验证规则,修改一下
    74. mysql> select `plugin` from user where User='root';
    75. +-----------------------+
    76. | plugin |
    77. +-----------------------+
    78. | caching_sha2_password |
    79. | caching_sha2_password |
    80. +-----------------------+
    81. 2 rows in set (0.00 sec)
    82. mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
    83. Query OK, 0 rows affected (0.01 sec)

    这个时候我们再进行navicat连接测试下
    image.png
    这样我们就可以访问容器里面的mysql数据库了。