花了一点时间,在MAC上实现了用Navicat连接Docker中的Mysql,使用了最新的Mysql8.0版本。
首先MAC上要安装Docker(免费版官方下载地址:Docker免费版),下载安装并运行Docker。然后打开MAC终端,按以下步骤进行:
1. 下载Mysql的Docker镜像:
$ docker search mysql (搜索mysql镜像)
$ docker pull mysql (下载mysql镜像,默认最新版本)
2. 运行镜像,设置root账号初始密码(123456),映射本地宿主机端口3306到Docker端口3306。测试过程没有挂载本地数据盘:
$ docker run -it --rm --name test_mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql
3. 查看已运行的容器:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a42f31094df5 mysql "docker-entrypoint.s…" 7 seconds ago Up 6 seconds 0.0.0.0:3306->3306/tcp test_mysql
4. 进入test_mysql容器:
docker exec -it test_mysql bash
root@a42f31094df5:/#
5. 在容器内登陆Mysql:
root@a42f31094df5:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.11 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>
6. 查看root等用户信息
mysql> select user,host,authentication_string from mysql.user;
+------------------+-----------+------------------------------------------------------------------------+
| user | host | authentication_string |
+------------------+-----------+------------------------------------------------------------------------+
| root | % | $A$005$!RE#bo G5=W;nzz+m82hawoLg9Y0Ixi2R/5clltxXqHCPmRXPt0YP/QuDgB |
| mysql.infoschema | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| root | localhost | $A$005$Q#CKQ'AR6cT_o3-
no3BEJw4GpvTJWrLyK28RB74UXBhN.9u2QBYxA7OYBC |
+------------------+-----------+------------------------------------------------------------------------+
5 rows in set (0.00 sec)
7. 为root分配权限,以便可以远程连接:
mysql> grant all PRIVILEGES on *.* to root@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.01 sec)
8. 使用Navicatlian连接Mysql:
点击 Test Connection, 此时会报错:
这是因为Mysql5.6以上的版本修改了Password算法,这里需要更新密码算法,操作步骤如下:
mysql> ALTER user 'root'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.09 sec)
mysql>
mysql> ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
mysql>
再次点击 Test Connection,连接成功:
接下来就可以使用navicat建表了。