go web app get mysql dial connect err:
docker-compose.yml
version: '3'
services:
db:
build:
context: ./docs/mysql
environment:
MYSQL_ROOT_PASSWORD: root
container_name: puzzle-mysql
ports:
- 3306:3306
tty: true
restart: always
networks:
- puzzle_network
redis:
image: redis:6.2.4
ports:
- 6379:6379
container_name: puzzle-redis
restart: always
networks:
- puzzle_network
server:
build:
context: .
volumes:
- "./:/workspace"
container_name: puzzle-server
environment:
env: production
ports:
- 8081:8081
tty: true
restart: on-failure
depends_on:
- db
- redis
networks:
- puzzle_network
networks:
puzzle_network:
driver: bridge
docker-compose up -d then all containers is running:
containers running
get server error log:
[2022-05-23 07:38:36.544] production.info bootstrap/db.go:37 mysql connect info: {"info": "root:root#tcp(db:3306)/puzzle?charset=utf8mb4&parseTime=True&loc=Local"}
[2022-05-23 07:38:36.546] production.error bootstrap/db.go:52 Mysql connect failed, err: {"err": "dial tcp 172.30.0.3:3306: connect: connection refused"}
mysql contaienr info
help, how to solution?
solved!
Before running the server, you need to wait for the mysql service to be started.
Related
I receive the following error:
WARNING: The requested image is platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
standard_init_linux.go:228: exec user process caused: exec format error
#!/bin/bash
version: "3.7"
services:
db:
platform: linux/amd64
image: mysql
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: "Root193782"
MYSQL_DATABASE: "test"
ports:
- "3306:3306"
volumes:
- /data/mysql:/var/lib/mysql
restart: always
networks:
- mysql-network
phpmyadmin:
platform: linux/amd64
image: phpmyadmin/phpmyadmin
environment:
MYSQL_ROOT_PASSWORD: "Root193782"
ports:
- "8080:80"
volumes:
- /data/php/admin/uploads.ini:/usr/local/etc/php/conf.d/php-phpmyadmin.ini
networks:
- mysql-network
networks:
mysql-network:
I have the following docker-compose file.
version: "3.7"
services:
main:
container_name: buspack_main
build:
context: .
target: development
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- ${API_PORT}:${API_PORT}
command: bash -c "npm install --save && npm run start:dev"
env_file:
- .env
networks:
- webnet
depends_on:
- db
links:
- db
restart: always
db:
container_name: buspack_db
image: mysql:5.7.33
networks:
- webnet
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: "backendnest"
ports:
- 23306:3306
restart: always
volumes:
- ./db:/var/lib/mysql:rw
phpmyadmin:
container_name: buspack_phpmyadmin
image: phpmyadmin/phpmyadmin
depends_on:
- db
restart: always
ports:
- '8030:80'
environment:
PMA_HOST: buspack_db
MYSQL_ROOT_PASSWORD: 123456
networks:
webnet:
volumes:
db: {}
My problem Is that when I try to login with phpmyadmin I get the following error:
mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
And also
mysqli::real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
Is there something I'm missing?
networks:
- webnet
add webnet network to phpmyadmin service
There are more options to solve the connection.
Add phpmyadmin container to webnet network as Def Soudani suggests. The YML example provided overrides the default network. Since the phpMyAdmin is lacking the network config, it won't be on the webnet docker network by default.
Remove all networks related config from the YML. Since compose already creates a default network (docs) for the containers within one YML file.
Use the PMA_ARBITRARY=1 (docs )which allows you to enter a database server hostname on login form.
Example of my YML setup with custom bridge networking:
version: '3.8'
services:
mysql:
image: mysql:5.7
container_name: myapp-mysql-db
restart: unless-stopped
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
volumes:
- ./database/dbdata:/var/lib/mysql
networks:
- myapp-network
phpmyadmin:
image: phpmyadmin:latest
container_name: phpmyadmin
ports:
# 8080 is the host port and 80 is the docker port
- 8080:80
environment:
PMA_HOST: mysql
UPLOAD_LIMIT: 20M
MYSQL_USERNAME: ${DB_USERNAME}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
restart: unless-stopped
networks:
# define your network where all containers are connected to each other
- myapp-network
networks:
myapp-network:
driver: bridge
flask can not connect mysql in docker compose
in connect string, I already use db service name as host, still can't connect db
My docker-compose.yml:
services:
api:
build:
context: ./api
dockerfile: Dockerfile
volumes:
- './api:/usr/src/app'
ports:
- 5002:5000
environment:
- FLASK_CONFIG=development
- FLASK_ENV=development
- APP_SETTINGS=project.config.DevelopmentConfig
- DATABASE_URL=mysql+pymysql://root:xxxxx#mysql-db:3307/gaojiesi
- SECRET_KEY=ZQbn05PDeA7v11
depends_on:
- mysql-db
links:
- mysql-db
restart: unless-stopped
mysql-db:
image: mysql:8.0
container_name: mysql-db
command: --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_ROOT_PASSWORD=frikyalong
ports:
- "3307:3306"
volumes:
- ./db/mysql/conf/my.cnf:/etc/my.cnf.d/
- ./db/mysql/logs:/logs
- ./db/mysql/data:/var/lib/mysql
you need to change :
DATABASE_URL=mysql+pymysql://root:xxxxx#mysql-db:3307/gaojiesi
to :
DATABASE_URL=mysql+pymysql://root:xxxxx#mysql-db:3306/gaojiesi
port 3307 in not reachable from inside the container
As in the title:
Caused by: java.net.ConnectException: Connection refused (Connection refused)
This worked for me sometime ago but now unfortunately not.
Script that I execute contains:
mvn clean install -> docker-compose build -> docker-compose up
Dockerfile:
FROM openjdk:8
ADD target/grades.jar grades.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "grades.jar"]
docker-compose.yaml
version: '3'
services:
mysql-standalone:
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=grades
- MYSQL_USER=root
- MYSQL_PASSWORD=password
ports:
- "33061:3306"
volumes:
- /data/mysql
grades:
image: grades
build:
context: ./
dockerfile: Dockerfile
depends_on:
- mysql-standalone
ports:
- 8080:8080
volumes:
- /data/grades
And application.properties:
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:${MYSQL_PORT:3306}/${MYSQL_DB:aws_eb_db}
spring.datasource.username=${MYSQL_USERNAME:root}
spring.datasource.password=${MYSQL_PASSWORD:password}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
localhost for you Docker container is not the localhost of the host machine (the one where your Docker containers live). Basically it points to your Docker container itself, where MySQL doesn't live. So you have to point to your MySQL instance, or the host for your containers, as you are mapping the 3306 port of your MySQL to your host's 3306 port.
I would definitely point to the MySQL itself as #LinPy suggested:
spring.datasource.url=jdbc:mysql://mysql-standalone:3306/grades
This should help somebody.
Make sure to run the command below to invalidate the current image:
docker rmi -f <your image id>
version: '3.1'
services:
db:
image: mariadb:10.5.5
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=softeasydb
- MYSQL_USER=root
- MYSQL_PASSWORD=root
volumes:
- ./db:/var/lib/mysql
ports:
- 3306:3306
networks:
- common-network
adminer:
image: adminer
restart: always
ports:
- 8080:8080
networks:
- common-network
api-users:
build: .
depends_on:
- db
ports:
- 9090:9090
environment:
- SPRING_PROFILES_ACTIVE=docker
- DATABASE_HOST=db
- DATABASE_USER=root
- DATABASE_PASSWORD=root
- DATABASE_NAME=softeasydb
- DATABASE_PORT=3306
- SERVER_PORT=9090
restart: always
networks:
- common-network
networks:
common-network:
driver: bridge
In your docker-compose.yaml you have a typo in the port mappings for the mysql-standalone container, you need to change the following:
ports:
- "33061:3306"
to
ports:
- "3306:3306"
I have a docker compose file which is basically trying to build a WAMP/LAMP style environment.
Basically, PHPMyAdmin can't seem to connect to MySQL. Looking through answers here, it appeared that it was an issue with legacy auth using MySQL Image 8 so I added:
command: --default-authentication-plugin=mysql_native_password
but that didn't work, so I dropped down to mysql image5.7 and the issue is still present. For some reason, I can't connect to MySQL and I get the error:
mysqli_real_connect(): php_network_getaddresses: getaddrinfo failed: Try again
and
mysqli_real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Try again
Here is a copy of my Docker Compose which I don't think is doing anything weird.
Hoping someone can clarify that for me.
version: "3.1"
services:
site:
build: .
ports :
- "80:80"
volumes:
- ./www:/var/www/html/
links:
- database
networks:
- php-network
#######################################
# PHP MY ADMIN
#######################################
phpmyadmin:
build:
context: .
dockerfile: PHPMYADMIN.Dockerfile
restart: always
links:
- database:mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- "8080:80"
environment:
- MYSQL_USERNAME=admin
- MYSQL_PASSWORD=root
networks:
- php-network
#######################################
# MySQL server
#######################################
database:
image: mysql:5.7.25
ports:
- "3306:3306"
container_name: db-mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=test_db
- MYSQL_USER=admin
- MYSQL_PASSWORD=root
networks:
- php-network
networks:
php-network:
driver: bridge
I also saw mention of a "depends on" flag which I tried too but that also failed to allow me to connect. I have had the same error throughout.
It's about the container name.
database:
image: mysql:5.7.25
ports:
- "3306:3306"
- **container_name: db-mysql**
and in your phpcontainer:
phpmyadmin:
build:
context: .
dockerfile: PHPMYADMIN.Dockerfile
restart: always
links:
- database:mysql
environment:
**PMA_HOST: mysql**
PMA_PORT: 3306
you define host as mysql, which in docker network will be unrecognizable.
try switching PMA_HOST to db-mysql.
Sorry for bad formatting.
Also use docker ps to see docker container names and to figure out which hosts do you need to connect.
I've got this working now with the following:
version: "3.1"
services:
www:
build: .
ports:
- "8081:80"
volumes:
- ./www:/var/www/html/
links:
- db
networks:
- default
#######################################
# MySQL server
#######################################
db:
image: mysql:5.7.25
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: test_db
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump:/docker-entrypoint-initdb.d
- persistent:/var/lib/mysql
networks:
- default
#######################################
# PHP MY ADMIN
#######################################
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8080:80
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
persistent:
According to your phpmyadmin service configuration, you are trying to reach mysql container by mysql address:
environment:
PMA_HOST: mysql
PMA_PORT: 3306
But container with mysql server is accessible by database or db-mysql addresses.
So, you need to change phpmyadmin service configuration to:
environment:
PMA_HOST: database
PMA_PORT: 3306