Home > Net >  Docker, node.js, mysql: Unhandled rejection SequelizeAccessDeniedError
Docker, node.js, mysql: Unhandled rejection SequelizeAccessDeniedError

Time:11-30

I try to dockerize a project which consists of a node.js app which connects to a mysql database.

Both containers start, but the node container always gets an "SequelizeAccessDeniedError"

> [email protected] start
> node app.js

Use no environment variables
{
  username: 'scrumboard',
  password: 'scrumboard',
  database: 'scrumboard',
  host: 'mysql',
  dialect: 'mysql',
  port: 3306,
  operatorsAliases: false
}
(node:20) [SEQUELIZE0004] DeprecationWarning: A boolean value was passed to options.operatorsAliases. This is a no-op with v5 and should be removed.
(Use `node --trace-deprecation ...` to show where the warning was created)
server is running on port 3000
Unhandled rejection SequelizeAccessDeniedError: Access denied for user 'scrumboard'@'172.23.0.3' (using password: YES)
    at /scrumboard-app/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:125:19
    at tryCatcher (/scrumboard-app/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/scrumboard-app/node_modules/bluebird/js/release/promise.js:547:31)
    at Promise._settlePromise (/scrumboard-app/node_modules/bluebird/js/release/promise.js:604:18)
    at Promise._settlePromise0 (/scrumboard-app/node_modules/bluebird/js/release/promise.js:649:10)
    at Promise._settlePromises (/scrumboard-app/node_modules/bluebird/js/release/promise.js:725:18)
    at _drainQueueStep (/scrumboard-app/node_modules/bluebird/js/release/async.js:93:12)
    at _drainQueue (/scrumboard-app/node_modules/bluebird/js/release/async.js:86:9)
    at Async._drainQueues (/scrumboard-app/node_modules/bluebird/js/release/async.js:102:5)
    at Async.drainQueues [as _onImmediate] (/scrumboard-app/node_modules/bluebird/js/release/async.js:15:14)
    at process.processImmediate (node:internal/timers:471:21)

a user is connected

The mysql container says the same:

2022-11-29T15:11:59.491328Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2022-11-29T15:11:59.491368Z 0 [Note] IPv6 is available.
2022-11-29T15:11:59.491377Z 0 [Note]   - '::' resolves to '::';
2022-11-29T15:11:59.491389Z 0 [Note] Server socket created on IP: '::'.
2022-11-29T15:11:59.495079Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2022-11-29T15:11:59.499174Z 0 [Note] Event Scheduler: Loaded 0 events
2022-11-29T15:11:59.499449Z 0 [Note] mysqld: ready for connections.
Version: '5.7.40'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)
2022-11-29T15:12:00.101178Z 2 [Note] Access denied for user 'scrumboard'@'172.23.0.3' (using password: YES)

So it looks like the communication between them is working.

This is my docker-compose file:

version: '3.8'

services: 
  mysql:
    image: mysql:5.7
    restart: unless-stopped
    env_file: ./.env
    environment:
      - MYSQL_ROOT_PASSWORD=scrumboard
      - MYSQL_PASSWORD=scrumboard
      - MYSQL_DATABASE=scrumboard
      - MYSQL_USER=scrumboard
    ports:
      - 3306:3306
      # [HOST:]CONTAINER

    volumes:
      - db:/var/lib/mysql

  app:
    depends_on:
      - mysql
    build: ./scrumboard-app
    restart: unless-stopped
    env_file: ./.env
    ports:
     - 3000:3000
    stdin_open: true
    tty: true

volumes:
  db:

And this is the config file which is used by sequelize:

  "development": {
    "username": "scrumboard",
    "password": "scrumboard",
    "database": "scrumboard",
    "host": "mysql",
    "dialect": "mysql",
    "port": 3306,
    "operatorsAliases": false
  }

This is the code where the connection to the DB is established:

const config = require(__dirname   '/../config/config.json')[env];

let sequelize;

 console.info('Use no environment variables');
 console.info(config);
 sequelize = new Sequelize(config.database, config.username, config.password, config);

I have no idea what's wrong here because I tripple checked the username, password, etc. Any ideas what the problem could be?

CodePudding user response:

My guess is that the user is not allowed to connect from outside?

Could you check the result of

SELECT user, host, account_locked, password_expired FROM user

You should see your user there and it should not be locked or expired.

Also, host should either match the IP 172.23.0.3 or a wildcard that would fit the IP.

To allow connection from anywhere to your user, you can do:

GRANT ALL PRIVILEGES
ON *.*
TO 'scrumboard'@'%';

Note that this query will grant ALL privileges on EVERYTHING. This is good to test first, so we can troubleshoot your connection, but appropriate privileges should be put in place afterwards.

If you still cannot connect, check that you can connect from inside the mysql container directly, if you can this will mean at least that the credentials are valid. If not, you'll need to try and reset the password, double check username etc.

  • Related