admin管理员组

文章数量:1241084

In a node project I create a basic mysql container exposing the 3307 port because the 3306 is already occupied by the local MySQL server. Apparently it all runs fine, but I'm unable to connect with workbench to it. I checked the firewall's rules, added a wide one, and removed all related to docker so that it prompted me again to accept them, but when try to connect with both "user" or "root" it responds "Access denied for user 'user'@'172.18.0.1' using password yes". With 172.18.0.1 being the docker's bridge. I'm out of ideas.

The docker-compose.yml

version: '3.9'
services:
  db:
    image: mysql:8.3.0
    container_name: mysql-db
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
    ports:
      - ${DB_PORT}:3306
    expose:
      - 3307
    volumes:
      - db_data:/var/lib/mysql
volumes:
  db_data:

and these variables in the .env file

NODE_ENV=development
DB_HOST=localhost
DB_USER=user
DB_PASSWORD=supersecret
DB_NAME=test_database
DB_PORT=3307

and this is the log

mysql-db  | 2025-02-19 08:17:28+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.3.0-1.el8 started.
mysql-db  | 2025-02-19 08:17:28+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql-db  | 2025-02-19 08:17:28+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.3.0-1.el8 started.
mysql-db  | '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
mysql-db  | 2025-02-19T08:17:28.637905Z 0 [System] [MY-015015] [Server] MySQL Server - start.
mysql-db  | 2025-02-19T08:17:28.847850Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.3.0) starting as process 1
mysql-db  | 2025-02-19T08:17:28.854518Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql-db  | 2025-02-19T08:17:28.982700Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql-db  | 2025-02-19T08:17:29.138583Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql-db  | 2025-02-19T08:17:29.138633Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql-db  | 2025-02-19T08:17:29.141681Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.        
mysql-db  | 2025-02-19T08:17:29.162045Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
mysql-db  | 2025-02-19T08:17:29.162170Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.3.0'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

and this is docker ps

CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS         PORTS                                         NAMES
fda5732ca548   mysql:8.3.0   "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes   3307/tcp, 33060/tcp, 0.0.0.0:3307->3306/tcp   mysql-db

and this is the netstat -ano | findstr :3307

  TCP    0.0.0.0:3307           0.0.0.0:0              LISTENING       6884
  TCP    [::]:3307              [::]:0                 LISTENING       6884
  TCP    [::1]:3307             [::]:0                 LISTENING       8324

In a node project I create a basic mysql container exposing the 3307 port because the 3306 is already occupied by the local MySQL server. Apparently it all runs fine, but I'm unable to connect with workbench to it. I checked the firewall's rules, added a wide one, and removed all related to docker so that it prompted me again to accept them, but when try to connect with both "user" or "root" it responds "Access denied for user 'user'@'172.18.0.1' using password yes". With 172.18.0.1 being the docker's bridge. I'm out of ideas.

The docker-compose.yml

version: '3.9'
services:
  db:
    image: mysql:8.3.0
    container_name: mysql-db
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
    ports:
      - ${DB_PORT}:3306
    expose:
      - 3307
    volumes:
      - db_data:/var/lib/mysql
volumes:
  db_data:

and these variables in the .env file

NODE_ENV=development
DB_HOST=localhost
DB_USER=user
DB_PASSWORD=supersecret
DB_NAME=test_database
DB_PORT=3307

and this is the log

mysql-db  | 2025-02-19 08:17:28+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.3.0-1.el8 started.
mysql-db  | 2025-02-19 08:17:28+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql-db  | 2025-02-19 08:17:28+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.3.0-1.el8 started.
mysql-db  | '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
mysql-db  | 2025-02-19T08:17:28.637905Z 0 [System] [MY-015015] [Server] MySQL Server - start.
mysql-db  | 2025-02-19T08:17:28.847850Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.3.0) starting as process 1
mysql-db  | 2025-02-19T08:17:28.854518Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql-db  | 2025-02-19T08:17:28.982700Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql-db  | 2025-02-19T08:17:29.138583Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql-db  | 2025-02-19T08:17:29.138633Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql-db  | 2025-02-19T08:17:29.141681Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.        
mysql-db  | 2025-02-19T08:17:29.162045Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
mysql-db  | 2025-02-19T08:17:29.162170Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.3.0'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

and this is docker ps

CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS         PORTS                                         NAMES
fda5732ca548   mysql:8.3.0   "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes   3307/tcp, 33060/tcp, 0.0.0.0:3307->3306/tcp   mysql-db

and this is the netstat -ano | findstr :3307

  TCP    0.0.0.0:3307           0.0.0.0:0              LISTENING       6884
  TCP    [::]:3307              [::]:0                 LISTENING       6884
  TCP    [::1]:3307             [::]:0                 LISTENING       8324
Share Improve this question asked 18 hours ago FehuFehu 4011 gold badge4 silver badges19 bronze badges 4
  • 2 The error message you get means that you can connect to the database. But you're using a wrong username/password. When you have an existing database, the username and password environment variables you pass aren't used. They are only used when initializing a fresh database. You need to use a username/password that exists in the database already. See the first paragraph under 'Environment variables' here. – Hans Kilian Commented 18 hours ago
  • Ehm... I still can't make it work ˙◠˙. I read that page, and tried both deleting the image and downloaded it fresh, and setting MYSQL_ALLOW_EMPTY_PASSWORD: 'yes', and even if it says "using password: NO", still doesn't allows me. – Fehu Commented 17 hours ago
  • 1 It's the data in your db_data volume that contains the database data. Try bringing the containers down with docker compose down -v. The -v option will delete the named volumes. Then bring it up again and you should get a fresh database. – Hans Kilian Commented 16 hours ago
  • It worked! Thankyou! – Fehu Commented 16 hours ago
Add a comment  | 

1 Answer 1

Reset to default 1

First connect to your docker container, via

docker exec -it <the hash> mysql -u <your username> -p

and when prompted, type in the password. Then run

select user, host
from mysql.user
where user = '<your username>';

In the result you will see what hosts are supported, likely % and localhost. Then create a user with the host you want and grant privileges to it:

CREATE USER 'yourusername'@'172.18.0.1' IDENTIFIED BY 'yourpassword';
GRANT SELECT, INSERT, UPDATE, DELETE on MyDatabse.* to 'yourusername'@'172.18.0.1';
FLUSH PRIVILEGES;

本文标签: windowsUnable to connect to mysql container from workbenchStack Overflow