I have a problem with mysql 5.7 container denying access to my nodeJS container. I'm using docker-compose and I'm running docker on Ubuntu 18.04 lts.
I apologize for the mistakes in English. This is not my mother language.
I replaced all confidential information with *** and in the images I put a red line
Here's my docker-compose.yml
version: '3.1'
services:
db:
container_name: '***-db'
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=***
- MYSQL_DATABASE=***
- MYSQL_USER=***
- MYSQL_PASSWORD=***
- TZ=America/Sao_Paulo
command: mysqld --sql_mode="" --character-set-server=utf8 --collation-server=utf8_slovenian_ci --init-connect='SET NAMES UTF8;' --innodb-flush-log-at-trx-commit=0
ports:
- 3306:3306
volumes:
- ./db_data:/var/lib/mysql
network_mode: "host"
server:
image: server
restart: always
ports:
- "2000:22"
- "3000:3000"
network_mode: "host"
front:
image: portal
restart: always
links:
- server:server
ports:
- 4200:80
I am using a volume with the database for my mysql container. In this volume I have my user and password set and I also have my schema with every table. I copy everything from this path:
/var/lib/mysql
that I have on my configuration (I am not using docker on this one) in another computer with the same version of mysql and that I know it is working.
Here is my server Dockerfile:
FROM ubuntu:18.04
LABEL maintainer = "***"
LABEL build_date="2020-04-24"
RUN apt-get update && apt-get install -y curl
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get update && apt-get install -y nodejs
#COPY AN CONFIGURATION OF APP
COPY ***-server /home/root/***-server
WORKDIR /home/root/***-server
RUN npm install
EXPOSE 22
EXPOSE 3000
CMD ["npm", "start"]
Here is my nodeJS file to connect with the mysql:
"use strict";
module.exports = {
config: {
connectionLimit : 200,
host: 'localhost',
user : '***',
password : '***',
database: '***'
}
}
this is the error mesage that docker compose send to me, when I try to connect:
And this is image of the container of the mysql that I am trying to access. I make access to the mysql inside the container using the same user that I am using on the server.
Related
I have a project with a mysql database in a container. I use docker-compose to set my project up. And I want to run the mysql command to inspect te database.
So I did, and get:
docker-compose run --rm database mysql
Creating myproject_database_run ... done
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
However when I tried this it works:
docker exec -it myproject_database_1 mysql
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Can anybody explain me this?
My docker-compose file:
version: "3.7"
services:
database:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "127.0.0.1:3306:3306"
env_file: .env
volumes:
- type: volume
source: db_data
target: /var/lib/mysql
- type: bind
source: ./my.cnf
target: /etc/my.cnf
read_only: true
volumes:
db_data:
testing_images:
docker-compose run creates a new container. That's perfectly normal, but if your mysql client is configured to connect via a Unix socket, the new container will have a new filesystem and won't be able to see the main database container's /var/run directory.
When you use docker-compose run, you need to specify a TCP connection, using the setup described in Networking in Compose in the Docker documentation. For example,
docker-compose run --rm database \
mysql -h database
Since you publish ports: out of the container, you should be able to reach it from the host, without Docker. The trick here is that the mysql command-line client interprets localhost as a magic term to use a Unix socket and not a normal host name, so you need to specifically use the IP address 127.0.0.1 instead.
# From the same host, without anything Docker-specific
mysql -h 127.0.0.1
Try adding MYSQL_ROOT_PASSWORD in the environment.
environment:
MYSQL_ROOT_PASSWORD: root
This is from one of my working compose file
services:
## -----------------------------------------------
## MySql database
## -----------------------------------------------
db_mysql:
image: mysql:8.0
restart: always
volumes:
- db_mysql:/var/lib/mysql
- ./mysql:/docker-entrypoint-initdb.d
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- app-network
deploy:
mode: global
ports:
- "3306:3306"
## map volume
volumes:
db_mysql:
## in network, we can define any name under networks:
networks:
app-network:
driver: bridge
FYI: Official MySQL docker image - Docker Hub
I have the following docker compose:
version: '3.1'
services:
db:
image: mysql:5.5
container_name: mysql
environment:
- MYSQL_ROOT_PASSWORD=some_root_pw
- MYSQL_DATABASE=some_db
- MYSQL_USER=db_user
- MYSQL_PASSWORD=user_pw
ports:
- 8306:3306
volumes:
- ./db:/var/lib/mysql/some_db
I am accessing the container with the command:
docker-compose exec db /bin/sh
Which drops me at the shell as expected. However, I am unable to access mysql with the information from the docker-compose file.
mysql -u root -p
Prompts for the password, then rejects for bad username or password. I have tried with both root and user. Is there a way I can troubleshoot what is happening with user creation?
NOTE: intentionally older version of MySQL.
I have built my django docker with hostname server_default and with --network=server_default and ran mysql with same network(mysql container has ran before django server) when I check my mysql container everything is ok but when I run my django server it fails with error :
"Can't connect to MySQL server on 'server_default' ([Errno -2] Name or service not known)"
I attached to my server container and I couldn't connect to mysql container.
server_default is a bridge type.
my run commands :
sudo docker run -d -p 8000:8000 --network=server_default scotech-server
sudo docker run -d --network=server_default scotech-db
I couldn't do it with docker individually but with docker-compose.yml and 2 build context I could connect them together : it seems a bad solution :(
version: '3'
services:
scotech-db:
build:
context: ./scotech-mysql-docker
expose:
- 3306
web:
build:
context: ./server
depends_on:
- scotech-db
restart: on-failure
ports:
- "8002:8000"
I setup a django project in docker container and every thing is working as expected, except I don't find the project database in mysql image.
Dockerfile
FROM python:3
RUN mkdir /django-website
WORKDIR /django-website
COPY . /django-website
RUN pip install -r requirements.txt
docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=mywebsite
- MYSQL_USER=root
- MYSQL_PASSWORD=root
ports:
- '33060:3306'
volumes:
- /var/lib/mysql
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/django-website
ports:
- '8000:8000'
links:
- db
settings.py
DATABASES = {
'default': {
'ENGINE': "django.db.backends.mysql",
'NAME': "mywebsite",
'USER': "root",
'PASSWORD': "root",
'HOST': 'db',
'PORT': '3306',
}
}
I ran migrate and it worked:
docker-compose run web python manage.py migrate
I createdsuperuser:
docker-compose run web python manage.py createsuperuser
The development server is working docker-compose up and the site is working as expected, the issue when I navigate in mysql image I don't find my project related database which is mywebsite .
can you please tell me what is missing? if the database is not created, where has the migration been applied?
Thanks in advance.
I'm not sure what you mean by "I logged in mysql image shell but didn't find mywebsite database"
You are migrated the DB successfully, which means, the DB connections are valid and working.
In your docker-compose.yml file, the port mapping done like this, '33060:3306', which means the db's port 3306 is mapped to host machine's port 33060. So, this may be the issue (it's not an issue, kind of typo)
How to check the DB contents?
METHOD-1: check through django-shell of web container
1. run docker-compose up
2. open a new terminal in the same path and run docker ps
you'll get something like below
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
795093357f78 django_1_11_web "python manage.py ru…" 34 minutes ago Up 11 minutes 0.0.0.0:8000->8000/tcp django_1_11_web_1
4ae48f291e34 mysql:5.7 "docker-entrypoint.s…" 34 minutes ago Up 12 minutes 0.0.0.0:33060->3306/tcp django_1_11_db_1
3.Get into the web container by docker exec -it 795093357f78 bash command, where 795093357f78 is the respective container id
4. now you're inside the container. Then, run the command python manage.py dbshell. Now you will be in MYSQL shell of mywebsite (Screenshot)
5. run the command show tables;. It will display all the tables inside the mywebsite DB
METHOD-2: check through db container
1. repeat the steps 1 and 2 in above section
2. get into db container by docker exec -it 4ae48f291e34 bash
3. Now you'll be in bash terminal of MYSQL. Run the following commmand mysql -u root -p and enter the password when prompt
4. now you're in MYSQL server. run the command, show databases;. This will show all the databases in the server.
Have you tried defining the database image in the dockerfile? The following link is somewhat related to your problem:
https://medium.com/#lvthillo/customize-your-mysql-database-in-docker-723ffd59d8fb
I supposed that ports value of host container should be 3306 not 33060.
Use docker-compose.yml with value 3306 :
version: '3'
services:
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=mywebsite
- MYSQL_USER=root
- MYSQL_PASSWORD=root
ports:
- '3306:3306'
volumes:
- /var/lib/mysql
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/django-website
ports:
- '8000:8000'
links:
- db
Hope this works!
You should change the compose specification to version '2'. Take down the container and bring it back up with docker-compose up -d. Or if you intend to stay with version 3, you can instead use the following specification for database environment parameters
```
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mywebsite
MYSQL_USER: root
MYSQL_PASSWORD: root
```
When you have problems with containers not coming up, docker logs <container-name> --tail 25 -f can give you a lot of information about the cause.
I´m trying to built a docker-compose file to deploy locally my NodeJS app that connects to a mysql server. I´ve tried everything ( read a lot of tutorials and some questions here in Stackoverflow ) but I keep getting the ECONNREFUSED error.
This is my Dockerfile from NodeJS:
##Nodejs
FROM node:latest
RUN useradd --user-group --create-home --shell /bin/false app
ENV HOME=/home/app
COPY package.json npm-shrinkwrap.json $HOME/playerground/
RUN chown -R app:app $HOME/*
USER app
WORKDIR $HOME/playerground
RUN npm cache clean && npm install --silent --progress=false
USER root
COPY . $HOME/playerground
RUN chown -R app:app $HOME/*
USER app
This is my Mysql Dockerfile:
FROM mysql:latest
ENV MYSQL_ROOT_PASSWORD root
ENV MYSQL_DATABASE playerground
ENV MYSQL_USER root
ENV MYSQL_PASSWORD root
And this is my docker-compose:
version: '2'
services:
db:
build: ./database
ports:
- "3307:3306"
playerground:
build:
context: .
dockerfile: Dockerfile
command: node_modules/.bin/nodemon --exec npm start
environment:
ENVIRONMENT: development
ports:
- "9090:9090"
links:
- db
volumes:
- .:/home/app/playerground
- /home/app/playerground/node_modules
Another thing is my configuration file from Nodejs:
//Database
'development' : {
'host' : process.env.HOSTNAME || 'localhost',
'user' : 'root',
'password' : 'root',
'database' : 'playerground'
},
//Server
'development' : {
'host' : process.env.HOSTNAME || '127.0.0.1',
'port' : 3000
},
Can you help me?
Thanks
In your app config, the database host isn't pointing at your MySql container. You're pointing it at the local host, which is the app container. In the Compose file you can explicitly name the link to the db container using:
links:
- db:db
And then your database container can be reached at the hostname db. You can hard-code that host in your app config, because it will be the same for all environments, as long as you use the same Compose file.
Also, if you're using a recent version of Docker you don't need to publish ports between containers in the same Docker network - so you can remove this from your Compose file:
ports:
- "3307:3306"
Then the db container will be accessible by the app container, but not externally.