用docker创建MySQL容器并设置使用navicat连接

安装一个MySQL环境,为后期的学习做准备。这种在docker里运行MySQL的方式还是很方便的,有需要的时候运行一下,不需要的时候也不会占用本地的资源。

一、 准备MySQL的镜像

$docker pull mysql 			//直接下载最新版本MySQL的image
$docker images              //查看images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              102816b1ee7d        4 weeks ago         486MB

二、生成容器

$ docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 --name my_mysql mysql
f2d73a2d0fdb15f421cdbd4a0a9a7c8732f3fb4739e58fbd7b280aa06679dfeb

简单注释:
-d 后台运行
-p 端口映射,把容器中的3306端口映射到宿主机的3307端口
-e MySQL参数,设置root密码为123456
-name 给容器命名为my_mysql

三、容器内的配置

现在mysql容器已经运行起来了,但我喜欢用navicat来查看数据库,但目前这样是无法用navicat连接的(这里因为是Windows环境安装的DockerToolbox,在宿主机连接容器都使用192.168.99.100这个IP)。
用docker创建MySQL容器并设置使用navicat连接
需要进入MySQL容器内做配置

$ docker exec -it my_mysql mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
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>

设置允许远程连接,刷新权限

mysql> alter user 'root'@'%' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.35 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

再次在navicat里测试连接,已经可以正常使用了。
用docker创建MySQL容器并设置使用navicat连接