Docker Compose MySQL 8 SequelizeJS ER_NOT_SUPPORTED_AUTH_MODE - mysql

I don't know how unique my problem is but I see similar issues throughout the web but I have the scenario that doesn't match any of the problems I've seen.
I have a pretty standard Express app that uses SequelizeJS and MySQL 8. Sequlize has migration which I run using entrypoint.sh that runs sequelize migration command.
# Run migrations
npx sequelize-cli db:migrate
# Preload initial data
npx sequelize-cli db:seed:all
When I run docker compose command the project builds without any issues and sequelize migration and seeds runs. Database connection from sequelize command line works perfectly!
But when I load my homepage the SQL query from the application throws error. The error itself is quite common
{
code: 'ER_NOT_SUPPORTED_AUTH_MODE',
errno: 1251,
sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading MySQL client',
sqlState: '08004',
fatal: true
}
I have seen this error while mysql 8 and the package mysql used together with sequlizejs. The common solution is to use mysql2 which I am using in my project. I've also tried to run command --default-authentication-plugin=mysql_native_password inside mysql container which didn't help.
Docker Compose
version: '3.9'
services:
linkfy-dashboard:
container_name: linkfy-dashboard
restart: always
depends_on:
- linkfy-mysql
build:
context: .
dockerfile: Dockerfile.dev
ports:
- '10200:3000'
environment:
APP_NAME: Linkfy
DB_HOST: linkfy-mysql
DB_USER: linkfy
DB_PASS: password
DB_NAME: linkfy
COOKIE_SECRET: secret
NONCE_SECRET: secret
COOKIE_SECURE: 'false'
volumes:
- /app/node_modules
- .:/app
networks:
- linkfy
linkfy-mysql:
container_name: linkfy-mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: linkfy
MYSQL_USER: linkfy
MYSQL_PASSWORD: password
volumes:
- database:/var/lib/mysql
networks:
- linkfy
adminer:
image: adminer
restart: always
ports:
- 10210:8080
networks:
- linkfy
networks:
linkfy:
name: linkfy
volumes:
database:
Dockerfile
FROM node:14.17-alpine
# source directory
WORKDIR /app
# global dependancy
RUN yarn global add sequelize-cli nodemon
# dependency file (package.json and package-lock.json)
COPY package.json .
RUN apk add --update python2 make g++ && rm -rf /var/cache/apk/*
RUN yarn install
# copy the sources
COPY . .
# listener
EXPOSE 3000
ENTRYPOINT ["./entrypoint.sh"]
# command to run
CMD [ "npm", "run", "dev" ]
I need some direction how to troubleshoot this.

Related

connect ECONNREFUSED 127.0.0.1:3306 in adonis 5 docker mysql [duplicate]

This question already has an answer here:
Node.js connect to MySQL Docker container ECONNREFUSED
(1 answer)
Closed last year.
I'm using Adonis 5 and Mysql database , after following some tutorials i added this code to connect mysql. but it shows me "connect ECONNREFUSED 127.0.0.1:3306" error. Here is my code below:
My Dockerfile:
FROM node:14
RUN mkdir -p /home/node/app/node_modules
WORKDIR /home/node/app
COPY package*.json ./
# RUN apk add --no-cache git
COPY . /home/node/app/
RUN chown -R node:node /home/node
RUN npm install
USER node
EXPOSE 3333
ENTRYPOINT ["node","ace","serve","--watch"]
my docker-compose.yml file:
# For more information: https://github.com/julien-r44/adonis-sail
version: '3'
services:
mysql:
image: 'mysql/mysql-server:8.0'
container_name: mysql
expose:
- "3307"
ports:
- '${MYSQL_PORT:-3307}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${MYSQL_PASSWORD}'
MYSQL_ROOT_HOST: "%"
MYSQL_DATABASE: '${MYSQL_DB_NAME:?err}'
MYSQL_USER: '${MYSQL_USER:?err}'
MYSQL_PASSWORD: '${MYSQL_PASSWORD?:err}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
phpmyadmin:
image: 'phpmyadmin/phpmyadmin'
links:
- 'mysql:mysql'
ports:
- 8080:80
environment:
MYSQL_USERNAME: "${MYSQL_USER}"
MYSQL_ROOT_PASSWORD: "${MYSQL_PASSWORD}"
PMA_HOST: mysql
networks:
- sail
app:
links:
- mysql
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/home/node/app
- /home/node/app/node_modules
ports:
- 3333:3333
depends_on:
- mysql
- phpmyadmin
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: "local"
My .env file:
HOST=0.0.0.0
PORT=3333
APP_URL=http://${HOST}:${PORT}
NODE_ENV=development
APP_KEY=wH94pOFV47NO0dJtE_S6-TDRgcjgZXGM
SESSION_DRIVER=cookie
CACHE_VIEWS=false
DB_CONNECTION=mysql
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_USER=adonis
MYSQL_PASSWORD=adonis
MYSQL_DB_NAME=social
Error:
it shows this error
What is my mistake?
Change from MYSQL_HOST=localhost to MYSQL_HOST=mysql
When your app is running inside a docker container, localhost points no longer to your laptop (or server) but to the container itself.
As each service is running in separated containers when one application is trying to access the database service you cannot use localhost. As localhost points to that container, and the database is not installed there.
You should use the container name instead localhost when specificying connection urls.
You can also check Docker Compose network documentation for detailed explanations.

MySQL Docker Compose password not working

What I want to do is:
Create a MySQL8 docker container
The MySQL container should run a dump file
I was successful in creating the basic container, however, that are several issues:
The password that I added in docker-compose.yml is ignored, when I run:
"docker exec -it mysqlDB bash" followed by "MySQL -u admin -p" I get Acess denied, and also with root
I don't know if the dump is being used because I can access the DB
I'm also getting this error:
[ERROR] [MY-000061] [Server] 1105 Input Output error while reading file /docker-entrypoint-initdb.d/, line 0, I/O error code 1
I tried many things for hours and it only got worse like not running at all.
I always run with: "docker-compose --log-level DEBUG -verbose up"
I always retry with the sequence:
ctrl+c
docker-compose down
docker system prune -a
docker volume prune
After running these prunes I need to run twice, or else I got the error:
"The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it."
Dockerfile(at /MySQL), there's also a LastDump.sql in this directory
EDIT: Later I deleted this file, and got the same result
FROM mysql:8.0.21
RUN chown -R mysql:root /var/lib/mysql/
ENV MYSQL_DATABASE=Olimpo
ENV MYSQL_USER=admin
ENV MYSQL_PASSWORD=senha
ENV MYSQL_ROOT_PASSWORD=senha
ADD LastDump.sql /etc/mysql/LastDump.sql
RUN sed -i 's/MYSQL_DATABASE/'$MYSQL_DATABASE'/g' /etc/mysql/LastDump.sql
RUN cp /etc/mysql/LastDump.sql /docker-entrypoint-initdb.d
EXPOSE 3306
docker-compose.yml(at / main folder)
version: "3.8"
# Define services
services:
# Database Service (Mysql)
mysqldb:
image: mysql:8.0.21
container_name: mysqlDB
command: --default-authentication-plugin=mysql_native_password --init-file /docker-entrypoint-initdb.d/
ports:
- "3307:3306"
restart: always
environment:
MYSQL_DATABASE: Olimpo
MYSQL_USER: admin
MYSQL_PASSWORD: senha
MYSQL_ROOT_PASSWORD: senha
volumes:
- mysql_data:/var/lib/mysql
# next line is commented doesn't run with it
#- ./MySQL/LastDump.sql:/docker-entrypoint-initdb.d
networks:
- backend
# Volumes
volumes:
mysql_data:
driver: local
# Networks to be created to facilitate communication between containers
networks:
backend:
Your problem seem to be the parameter in the command of your yml file. It worked in my machine when I took it out.
Change from command: --default-authentication-plugin=mysql_native_password --init-file /docker-entrypoint-initdb.d/ to command: command: --default-authentication-plugin=mysql_native_password
Fixed file is below:
# Define services
services:
# Database Service (Mysql)
mysqldb:
image: mysql:8.0.21
container_name: mysqlDB
command: --default-authentication-plugin=mysql_native_password
ports:
- "3307:3306"
restart: always
environment:
MYSQL_DATABASE: Olimpo
MYSQL_USER: admin
MYSQL_PASSWORD: senha
MYSQL_ROOT_PASSWORD: senha
volumes:
- mysql_data:/var/lib/mysql
# next line is commented doesn't run with it
#- ./MySQL/LastDump.sql:/docker-entrypoint-initdb.d
networks:
- backend
# Volumes
volumes:
mysql_data:
driver: local
# Networks to be created to facilitate communication between containers
networks:
backend:
The mysql dockerfile is useless.
Create a folder called mysql-dump with the dump.sql inside.
Add the line "USE db_name" to dump.sql
In docker-compose.yml:
remove "--init-file /docker-entrypoint-initdb.d/"
add the line in volumes "- ./mysql-dump:/docker-entrypoint-initdb.d"

Can't connect to MySQL container from other container

I'm trying to connect my FASTAPI app container to a MySQL database container using the docker-compose file. In the Docker documentation it says that docker creates a default network for both containers. However, I would like to use a pre-existing network that I've created(app-net).
This is my docker-compose file:
version: '3.4'
services:
mysql:
image: mysql:latest
command: mysqld --default-authentication-plugin=mysql_native_password
container_name: mysql
restart: always
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- mysql-data:/var/lib/mysql
app:
build: .
image: app:1.0
container_name: app
ports:
- 8000:8000
environment:
PASSWORD: password
USERNAME: root
networks:
default:
external: true
name: app-net
volumes:
mysql-data:
driver: local
this is the output I get when i run docker inspect mysql -f "{{json .NetworkSettings.Networks }}":
{"app-net":{"IPAMConfig":null,"Links":null,"Aliases":["mysql","5e998f9fb646"],"NetworkID":"7f60c83e4c88d25e674461521446ec9fa98baca8639c782c79671c4fcb77ba88","EndpointID":"","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"","DriverOpts":null}}
However, when I run each container individually using CMD with the --network app-net the output is different:
{"app-net":{"IPAMConfig":null,"Links":null,"Aliases":["46157e588c87"],"NetworkID":"7f60c83e4c88d25e674461521446ec9fa98baca8639c782c79671c4fcb77ba88","EndpointID":"6a6922a9a6ea8f9d113447decbbb927cb93ddffd3b9563ee882fa2e44970cde5","Gateway":"172.20.0.1","IPAddress":"172.20.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:14:00:02","DriverOpts":null}}
In my app code in order to connect the mysql server, I specified the container name as the hostname since they are supposed to share the same network. But, as I mentioned it seems both containers can't talk to each other.
I'm pretty sure that is the reason I can't connect the database through my app and get that error when I run:
docker-compose -f docker-compose.yml up
I get this error:
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2005, "Unknown MySQL server host 'mysql' (0)")
What am I missing?
If the error you're getting is specifically "unknown host" or something similar, this can happen if your application container starts before the database container exists. You can work around this situation by telling Compose about the dependency:
version: '3.8'
services:
mysql: { ... }
app:
depends_on:
- mysql
This has two key effects. If you try to start only the application container docker-compose up app, it will also start the mysql container, following the depends_on: chain. When Compose does start containers, it at least creates the mysql container before creating the app container.
Note that this doesn't guarantee the database will actually be running by the time your application starts up. If you do encounter this, you will get a different error like "connection refused". You can work around this by embedding a script in your image that waits for the database to be available, or by using a version 2 Compose file with health-check support; see Docker Compose wait for container X before starting Y for more details on this specific problem.
You could add the key networks to both containers, this way:
version: '3.4'
services:
mysql:
image: mysql:latest
command: mysqld --default-authentication-plugin=mysql_native_password
container_name: mysql
restart: always
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- mysql-data:/var/lib/mysql
networks:
- app-net
app:
build: .
image: app:1.0
container_name: app
ports:
- 8000:8000
environment:
PASSWORD: password
USERNAME: root
networks:
- app-net
networks:
default:
external: true
name: app-net
volumes:
mysql-data:
driver: local

How to fix 'ECONNREFUSED' error in my nodejs app using docker?

I am using Docker container for my node app which is a REST API Server. It use multiple backend (currently mongoDB and mysql). My mongoclient is connecting to the port defined in my docker-compose.yml but my mysql is not configuring to it, throwing ECONNREFUSED error.
Also i like to add In my localhost I have both mongodb and mysql installed on my system.
Below is the code of my Dockerfile and docker-compose.yml
FROM node:8
RUN mkdir -p /home/sharad/eapp
WORKDIR /home/sharad/eapp
COPY package.json /home/sharad/eapp/
RUN npm install
COPY . /home/sharad/eapp
EXPOSE 8016
CMD [ "npm","start" ]
version: '3.1'
services:
server:
image: sharadm20/eyantra-app
restart: always
ports:
- 8016:8016
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: secret
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
links:
- mongo
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: secret
ME_CONFIG_MONGODB_ENABLE_ADMIN: 'true'
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: secret
adminer:
image: adminer
restart: always
ports:
- 8082:8080
And below is the error which I am encountering:
Unable to connect to the mysql database: { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306 }
It worked by changing the Hostname in mysql config to the name similar to docker image name( in my case mysql is called db) so the hostname+port would look like db:3306.
Thanks to Marc Sances for pointing it out.

Rails app using docker-compose wont connect to the mysql container

Trying to get a rails app running using docker-compose. I run docker-compose build and it completes with no errors. I then run docker-compose up and both of the containers start. Then I run docker-compose run web rake db:create db:migrate and run into an error:
rake aborted!
Mysql2::Error::ConnectionError: Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")
My Dockerfile:
FROM ruby:2.5.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs mysql-client sqlite3 zlib1g-dev libxslt-dev git && \
gem install bundler
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
Docker-compose
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'online_community_development'
MYSQL_ROOT_PASSWORD: '******'
MYSQL_HOST: 'localhost'
ports:
# <Port exposed> : < MySQL Port running inside container>
- '3306:3306'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- 'my-db:/var/lib/mysql'
container_name: datab
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- web-app:/myapp
ports:
- "3000:3000"
depends_on:
- db
links:
- db:db
restart: always
volumes:
my-db: {}
web-app: {}
Database.yml
development:
adapter: mysql2
encoding: utf8
database: online_community_development
pool: 5
username: root
password: slumland
socket: /var/run/mysqld/mysqld.sock
host: db
New to docker so not even sure if I am issuing the commands correctly. I need to create and seed the database and have the app container connect to it. I think the issue is related to it trying to connect via localhost, but even when I changed the host to db I still got the same results. Any help would be greatly appreciated.
tl;dr:
Try this docker-compose.yml:
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'online_community_development'
MYSQL_ROOT_PASSWORD: '******'
MYSQL_HOST: 'localhost'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- 'my-db:/var/lib/mysql'
container_name: datab
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
environment:
DATABASE_URL: 'mysql2://db'
RAILS_ENV: 'development'
volumes:
- web-app:/myapp
ports:
- "3000:3000"
depends_on:
- db
restart: always
volumes:
my-db: {}
web-app: {}
In detail:
First of all your docker-compose-file is kinda misconfigured.
Therefore I will do a small instruction on docker-compose.yml-files before heading over to your question.
The links:-thing is a legacy feature of docker which you should strictly avoid:
https://docs.docker.com/compose/compose-file/#links
Moreover, it is not even required when you're working with docker-compose
Also, you're using expose: and ports: when defining your database-service.
The ports-key is opening up a port on your host-machine and forwards it to your docker-service:
https://docs.docker.com/compose/compose-file/#ports
Using ports-key on database-definitions makes your database accessible to the www - which is not what you want in most cases.
The expose-key opens the port locally, in the created docker network.
https://docs.docker.com/compose/compose-file/#expose
This is what we want for databases.
Editing your docker-compose.yml-file results in the following:
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'online_community_development'
MYSQL_ROOT_PASSWORD: '******'
MYSQL_HOST: 'localhost'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- 'my-db:/var/lib/mysql'
container_name: datab
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- web-app:/myapp
ports:
- "3000:3000"
depends_on:
- db
restart: always
volumes:
my-db: {}
web-app: {}
Getting back to your primary problem:
rake aborted! Mysql2::Error::ConnectionError: Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")
It looks like rake expects the database to be available at localhost - therefore I think that the cause of this issue is a misconfiguration.
Unfortunately I'm not very familiar with rake but nevertheless your database.yaml looks correct.
I did some research and came across this post:
Rake tasks seem to ignore database.yml configuration
Maybe you could try to set the environment-variable DATABASE_URL in your docker-compose-file: DATABASE_URL=mysql://db
I'd also recommend to set the correct context-environment-variable: RAILS_ENV=development
When you do alle these changes you should come up with the following docker-compose.yml:
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'online_community_development'
MYSQL_ROOT_PASSWORD: '******'
MYSQL_HOST: 'localhost'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- 'my-db:/var/lib/mysql'
container_name: datab
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
environment:
DATABASE_URL: 'mysql2://db'
RAILS_ENV: 'development'
volumes:
- web-app:/myapp
ports:
- "3000:3000"
depends_on:
- db
restart: always
volumes:
my-db: {}
web-app: {}
Hope this helps.
Regards,
Hermsi
your db seems to be configured to listen on localhost only. Unfortunately this will not work since your app is technically another network host (container) within the compose network. Try making your db listen on all network interfaces.