简体   繁体   English

如何使用 Navicat 或 MySQL Workbench 将 MySQL 与 Docker 连接?

[英]How to connect MySQL with Docker using Navicat or MySQL Workbench?

I am using the MySQL Docker images, and I can connect to this Docker with mysqli_connect('mysql', 'docker', 'docker');我正在使用 MySQL Docker 映像,我可以使用mysqli_connect('mysql', 'docker', 'docker'); in php在php中

Here is docker ps :这是docker ps

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                          NAMES
c271f8e68378        afraidjpg/nginx:nginx   "nginx -g 'daemon of…"   2 hours ago         Up 2 hours          0.0.0.0:80->80/tcp, 0.0.0.0:10086->10086/tcp   nginx
92d7c923f777        afraidjpg/php:php56     "docker-php-entrypoi…"   2 hours ago         Up 2 hours          0.0.0.0:9000->9000/tcp                         php
68b818bd68c4        afraidjpg/mysql:mysql   "docker-entrypoint.s…"   2 hours ago         Up 2 hours          0.0.0.0:3306->3306/tcp, 33060/tcp              mysql

I use --link mysql option when starting PHP Docker.我在启动 PHP Docker 时使用--link mysql选项。

IP of MySQL Docker is 172.17.0.2 . MySQL Docker 的 IP 是172.17.0.2

My mac's IP is 192.168.1.50我的mac的IP是192.168.1.50

here is user in mysql:这是mysql中的用户:

mysql> select User, Host from mysql.user;
+--------+-----------+
| User   | Host      |
+--------+-----------+
| docker | %         |
| root   | localhost |
+--------+-----------+
2 rows in set (0.00 sec)

then I tried to connect mysql with navicat and mysql-workbench然后我尝试用navicat和mysql-workbench连接mysql

host:192.168.1.50, user:docker, pass:docker Access denied for user 'docker'@'%%' (using password: YES)主机:192.168.1.50,用户:docker,pass:docker Access denied for user 'docker'@'%%' (using password: YES)

host:127.0.0.1, user:docker, pass:docker Access denied for user 'docker'@'%%' (using password: YES)主机:127.0.0.1,用户:docker,pass:docker Access denied for user 'docker'@'%%' (using password: YES)

host:172.17.0.2, user:docker, pass:docker Can't connect to MySQL server on '172.17.0.2主机:172.17.0.2,用户:docker,pass:docker Can't connect to MySQL server on '172.17.0.2

all get the error with Access denied for user 'docker'@'%%' (using password: YES)所有人都收到Access denied for user 'docker'@'%%' (using password: YES)

if l tried to change user from docker to root , error become Access denied for user 'docker'@'172.17.0.1' (using password: YES)如果我尝试将用户从docker更改为root ,则错误变为Access denied for user 'docker'@'172.17.0.1' (using password: YES)

I found some article before, and followed their step, but always failed.我之前找到了一些文章,并按照他们的步骤进行操作,但总是失败。 I don't where I was wrong我不知道我错在哪里

here are some screenshot这是一些截图

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

update I cannot ping from local to docker, and there is no ping command in docker so I cannot do the test to ping from docker to local更新我无法从本地 ping 到 docker,并且 docker 中没有 ping 命令,所以我无法进行从 docker ping 到本地的测试

zk ~/Documents/PHP/www/docker $ ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
Request timeout for icmp_seq 3

more update更多更新

I can connect to mysql with command in terminal with mysql -udocker -p --portocol=tcp我可以使用mysql -udocker -p --portocol=tcp在终端中使用命令连接到 mysql

more details更多细节

error message is Access denied for user 'docker'@'%%' (using password: YES) , It said @'%%' , I know what the '%' is, but what's mean of '%%'?错误消息是Access denied for user 'docker'@'%%' (using password: YES) ,它said @'%%' ,我知道 '%' 是什么,但 '%%' 是什么意思?

Do try host as mysql instead of localhost from the connected container.请尝试将主机作为mysql而不是连接容器中的 localhost。 If you are on windows try 192.168.99.100 as host.如果您在 Windows 上尝试192.168.99.100作为主机。

If still having issue try retest with mysql-compose.yml如果仍然有问题,请尝试使用mysql-compose.yml重新测试

version: "3.1"
services:

  mysql:
    image: mysql:5.7.22
    command: "--innodb_use_native_aio=0"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
    env_file:
      - .env
    ports:
      - "3306:3306"
    volumes:
      - /var/database/docker/common:/var/lib/mysql
    networks:
      - mysqlnet
      - mysqlnetfpm

networks:
  mysqlnet:
    driver: bridge
  mysqlnetfpm:
    driver: bridge

Note: Before running new mysql container don't forgot to delete the older mysqldata files.注意:在运行新的 mysql 容器之前不要忘记删除旧的 mysqldata 文件。

Now you can use the username as root , password as root and for the internal containers(attached containers) use the host as mysql.现在您可以使用用户名作为root ,密码作为root并且对于内部容器(附加容器)使用主机作为 mysql。

If you are try to connect from outside the docker do try with host as localhost or 127.0.0.1 or your public docker ip .如果您尝试从 docker 外部连接,请尝试使用主机作为localhost127.0.0.1或您的公共docker ip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM