0

I am using the MySQL Docker images, and I can connect to this Docker with mysqli_connect('mysql', 'docker', 'docker'); in php

Here is 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.

IP of MySQL Docker is 172.17.0.2.

My mac's IP is 192.168.1.50

here is user in 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

host:192.168.1.50, user: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)

host:172.17.0.2, user: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)

if l tried to change user from docker to root, error become 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

enter image description here

enter image description here

enter image description here

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

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

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 '%%'?

afraid.jpg
  • 965
  • 2
  • 14
  • 31
  • You can install a ping command if you wish. – halfer May 29 '19 at 09:30
  • If you're getting "access denied", you're connecting successfully but don't have an acceptable username/password/source. The `docker inspect` IP address is unreachable from a MacOS host, and you don't need to look it up on any platform. – David Maze May 29 '19 at 09:49
  • were you able to login to mysql container and login to mysql? – Ntwobike May 29 '19 at 09:56
  • ?@DavidMaze So how can l login successful? I can login with terminal by specify `--portocol` option, but cannot connect by navicat or mysql-workbench – afraid.jpg May 29 '19 at 09:58
  • @Ntwobike I can login by terminal, login in the docker, login by php script. but just cannot login by any GUI tool – afraid.jpg May 29 '19 at 09:59

1 Answers1

0

Do try host as mysql instead of localhost from the connected container. If you are on windows try 192.168.99.100 as host.

If still having issue try retest with 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.

Now you can use the username as root, password as root and for the internal containers(attached containers) use the host as 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.

Vikash Pathak
  • 3,444
  • 1
  • 18
  • 32
  • hostname with mysql: Unknown MySQL server host 'mysql' (0), l am running on MacOS, So my docker ip is 172.17.0.2 – afraid.jpg May 29 '19 at 09:11
  • Are you still getting issue with try this `192.168.1.50` – Vikash Pathak May 29 '19 at 09:16
  • yes, tried this ip with user 'docker' and root, all failed. my local machine's mysql server has been stopped. Now the running server of mysql is only the mysql8.0 in docker – afraid.jpg May 29 '19 at 09:20
  • 1
    Try run this docker-compose file.. it's for mysql 5.7 https://gist.github.com/vikash-aika/2aae0b23cde9884291088faf341891f9 – Vikash Pathak May 29 '19 at 09:30
  • Now just try using `root` as password along with your docker ip or `127.0.0.1` as host. also make sure the given path for database root in docker-compose is correct as per your environment. – Vikash Pathak May 29 '19 at 09:33
  • still didn't word – afraid.jpg May 30 '19 at 02:02
  • l delete the mysqldata then restart docker images. It works, So, finally It may be may fault(even I don't know what's happened), But still thank you for your docker-compose file – afraid.jpg May 30 '19 at 02:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194153/discussion-between-vikash-pathak-and-afraid-jpg). – Vikash Pathak May 30 '19 at 07:22
  • Hi, please see this answer in link below, I think that help you with this problem. https://stackoverflow.com/a/53431294/1939983 Thanks, – LandiLeite Aug 03 '20 at 17:26