Second node.js can't connect to mysql in docker compose - docker

At the moment, I'm trying to set up my docker-compose file to run multiple (two) Node.js API's.
The first Node.js server connects fine to the database.
The second Node.js server keeps throwing this error
original: Error: connect ECONNREFUSED 172.29.0.3:3307
So my question is how to run multiple Node.js API's in docker?
This is my docker-compose.yaml:
version: '3.7'
services:
ISAAC-floor-database:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: isaac
ports:
- "3306:3306"
volumes:
- ./sql-scripts:/docker-entrypoint-initdb.d
ISAAC-sensor-database:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: isaac
ports:
- "3307:3307"
volumes:
- ./sql-scripts:/docker-entrypoint-initdb.d
ISAAC-floor-back-end:
image: jjuless/isaac-floor-back-end
environment:
DB_HOST: ISAAC-floor-database
DB_USER: root
DB_PASSWORD: isaac
DB_DATABASE: isaac
DB_PORT: 3306
DB_DIALECT: mysql
ports:
- "3000:80"
depends_on:
- ISAAC-floor-database
ISAAC-sensor-back-end:
image: jjuless/isaac-sensor-back-end
environment:
DB_HOST: ISAAC-sensor-database
DB_USER: root
DB_PASSWORD: isaac
DB_DATABASE: isaac
DB_PORT: 3307
DB_DIALECT: mysql
ports:
- "3001:80"
depends_on:
- ISAAC-sensor-database

Mysql is always listening on port 3306 in each container, so if you want to have multiple mysql instances in the same host, you will need to map different host ports to the same guest port
Hence you will need to change your docker-compose file as follows
ISAAC-floor-database:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: isaac
ports:
- "3306:3306" # <-- HOST port same as GUEST port
volumes:
- ./sql-scripts:/docker-entrypoint-initdb.d
ISAAC-sensor-database:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: isaac
ports:
- "3307:3306" # <-- Notice here the GUEST port is the same as the first container!
volumes:
- ./sql-scripts:/docker-entrypoint-initdb.d

Related

Docker. Phpmyadmin does not see the port assigned to it

I am using the following docker configuration:
version: "3"
services:
database:
image: mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 1009
restart: "always"
phpmyadmin:
image: phpmyadmin
ports:
- "8080:80"
environment:
PMA_HOST: database
restart: "always"
depends_on:
- database
It works, but if I change the mysql port for my PC to - "3360:3306" (for example 3306 may be busy with the main mysql service running on the computer) - it stops working. If I do this, I will be able to connect to mysql from my computer in CMD like mysql -u root -P 3360 -p, but phpmyadmin will give the error - connection refused.
No matter what I do, I see this error. For example I used the following docker configuration:
version: "3"
services:
database:
image: mysql
networks:
- my-network
ports:
- "3360:3306"
environment:
MYSQL_ROOT_PASSWORD: 1009
restart: "always"
phpmyadmin:
image: phpmyadmin
links:
- database
networks:
- my-network
ports:
- "8080:80"
environment:
PMA_HOST: database
PMA_PORT: 3360
restart: "always"
depends_on:
- database
networks:
my-network:
Even all of this didn't work. Phpmyadmin gave out one inscription when trying to connect - Connection refused all the time. But it still worked in the terminal.
Why can't I use a different port for phpmyadmin in docker?

Unable to connect mysql after docker mysql created

I setup my docker mysql and phpadmin via
version: '3.2'
services:
mysqllocal:
image: mysql:8.0
container_name: container_mysqllocal
restart: always
ports:
- '6603:3306'
environment:
MYSQL_ROOT_PASSWORD: pass1234
MYSQL_ROOT_HOST: '%'
phpmyadmin:
depends_on:
- mysqllocal
image: phpmyadmin/phpmyadmin
container_name: container_phpmyadmin
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: mysqllocal
UPLOAD_LIMIT: 3000000000
docker inspect container_mysqllocal and got '172.30.0.2'. However when I tried to connect mysql via SequelPro. It gives Connection Failed error. How do I connect mysql to my SequelPro / mysql workbench app, is there any configuration I've missed?
Below is the screenshot of my SequelPro connection:
have you tried to access mysql from your local machine with the port defined, in your config is 6603

Connect to networked mysql docker container with local client

I have just followed a tutorial on using Docker. I started with installing different containers and when I got to mysql, i installed it by running
docker container run -d -p 3306:3306 --name mysql --env MYSQL_ROOT_PASSWORD=123456 mysql
After the container has been created, I have been able to connect to MySQL with Heidi, by using 127.0.0.1, root and 123456 as password.
I eventually moved forward with another tutorial on installing WordPress with docker.
https://www.youtube.com/watch?v=pYhLEV-sRpY
The yaml for setting up the containers is bellow. Everything works just fine, but when it comes to connecting to the database using Heidi, I just do not get what to do - what connection data to use or what to change in the yaml to be able to connect from local machine.
version: '3'
services:
# Database
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
# phpmyadmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: password
networks:
- wpsite
# Wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- '8000:80'
restart: always
volumes: ['./:/var/www/html']
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
networks:
- wpsite
networks:
wpsite:
volumes:
db_data:
You should expose 3306 port to local machine. Then, you can connect to MySQL by using host=127.0.0.1.
Update the yaml file like this:
version: '3'
services:
# Database
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
...

Cannot log in to the MySQL server through browser

Each time when I try to access the phpmyadmin from browser I receive this error:
"Cannot log in to the MySQL server"
I tried to change networks, restart docker.
version: '3'
services:
web:
image: nginx:alpine
ports:
- 80:80
volumes:
- ./public_html:/public_html
- ./conf.d:/etc/nginx/conf.d
networks:
- nginxphp
php:
image: php:7.1.11-fpm-alpine
volumes:
- ./public_html:/public_html
expose:
- 9000
networks:
- nginxphp
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: root
MYSQL_PASSWORD: root
depends_on:
- db
ports:
- "8080:80"
networks:
nginxphp:
Cannot log in to the MySQL server
mysqli_real_connect(): The server requested authentication method unknown to the client [caching_sha2_password]
mysqli_real_connect(): (HY000/2054): The server requested authentication method unknown to the client
Disclaimer: I'm not a Docker user!
Your didn't mention if you're using the browser on the same computer as the sever or remotely. You'll need access to the mysql server (mysqld) through a terminal (command prompt). If this is a new install, it must be on the computer that is running mysql server.
In the Docker mysql page:
"The default configuration for MySQL can be found in /etc/mysql/my.cnf, which may include additional directories such as /etc/mysql/conf.d or /etc/mysql/mysql.conf.d."
Try looking in the /etc/mysql/my.cnf file on the server you're trying access. first. You're looking for:
bind-address = x.x.x.x
This is the address that the mysql server will talk to ("bound to"). Its typically "localhost" or "127.0.0.1".
To eliminate the error message like you see, I had to do two things:
1) change 'bind-address to 0.0.0.0'
this allows the server to connect to any computer. However, THIS IS A SECURITY RISK. Once you get it working, go read about bind addresses on the mysql website and set it appropriately.
2) Create an new account user#ipaddr where user is the new username and IPAddress is the ip4 address of the computer your trying to connect from. i.e.:
CREATE USER 'user'#'192.168.1.68' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'user'#'192.168.1.68';
Now try accessing mysql through the terminal program using the new username on the computer with the ip you entered above by typing:
mysql -uuser -ppassword -hx.x.x.x
Hopefully, this will help point you in the right direction. There's a ton of information about bind addresses and security on the web.
Because the phpmyadmin container has connected to the default host localhost. But your mysql server is located in other container (it means you cannot connect to mysql server by using localhost). So in the phpmyadmin service, you have to set PMA_HOST=db. See full env variables: https://hub.docker.com/r/phpmyadmin/phpmyadmin/
Full docker-compose.yml:
version: '3'
services:
web:
image: nginx:alpine
ports:
- 80:80
volumes:
- ./public_html:/public_html
- ./conf.d:/etc/nginx/conf.d
networks:
- nginxphp
php:
image: php:7.1.11-fpm-alpine
volumes:
- ./public_html:/public_html
expose:
- 9000
networks:
- nginxphp
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: root
MYSQL_PASSWORD: root
depends_on:
- db
ports:
- "8080:80"
networks:
nginxphp:
If you are using phpmyadmin with latest mysql version you will have some login issues:
Cannot log in to the MySQL server mysqli_real_connect():
The server requested authentication method unknown to the client [caching_sha2_password] mysqli_real_connect(): (HY000/2054):
The server requested authentication method unknown to the client
The solution is to downgrade to mysql 5.7 or another.
I tested with mysql 5.7 and it works for me.
If you want to can test with another versions and let the community know.
Below is the docker-compose file that builds and run Nginx + php 7.1 + mysql 5.7 +phpmyadmin
version: '3'
services:
web:
image: nginx:alpine
ports:
- 80:80
volumes:
- ./public_html:/public_html
- ./conf.d:/etc/nginx/conf.d
networks:
- nginxphp
php:
image: php:7.1.11-fpm-alpine
volumes:
- ./public_html:/public_html
expose:
- 9000
networks:
- nginxphp
db:
image: mysql:5.7
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db
environment:
PMA_HOST: db
PMA_PORT: 3306
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
depends_on:
- db
ports:
- "8080:80"
networks:
nginxphp:
Hope this will save some time to someone!

Can't access Docker database using IP (only localhost)

I'm trying to setup Docker to allow connections from other devices on my network. Currently to access Docker I visit localhost on my computer. I'm trying to connect using my computer's local IP (192.168.0.140), which lets me see my files but not connect to my database.
I assume this is a problem with my configuration but I don't know enough about Docker to troubleshoot it.
version: '2'
services:
webserver:
build: ./docker/webserver
image: localdev
ports:
- '80:80'
- '443:443'
volumes:
- ./www:/var/www/html
links:
- db
db:
image: mysql:5.6
ports:
- 3306
volumes:
- ./db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=secret
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
links:
- db
environment:
PMA_HOST: db
PMA_PORT: 3306
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: secret
ports:
- '8080:80'
Any help would be greatly appreciated.
Maybe you should try like this :
version: '2'
services:
webserver:
build: ./docker/webserver
image: localdev
ports:
- '0.0.0.0:80:80' # Map container port 80 on your "public" ip on port 80, it will be available on the network
- '0.0.0.0:443:443' # Same for port 443
volumes:
- ./www:/var/www/html
links:
- db
db:
image: mysql:5.6
# ports:
# - 3306 # You don't need to map port 3306 because mysql already expose this port for others containers, unless you wan't to access to your mysql directly
volumes:
- ./db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=secret
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
#links:
# - db # Not necessary, with docker-compose v2 every containers are in the same network and see each other
environment:
PMA_HOST: db
PMA_PORT: 3306
MYSQL_USER: root
MYSQL_ROOT_PASS
WORD: secret
ports:
- 'localhost:8080:80' # to keep your phpmyadmin only available from localhost.
The reason you are able to connect to the other containers is because you have mapped the ports between the container and the host.
For the database you are using the short port syntax:
ports:
- 3306
This will choose a random port on the host. To be able to connect to the database, use the long form syntax:
ports:
- '3306:3306'
In that case, you will be able to connect to the database on localhost:3306

Resources