Drupal cannot access MySQL Docker container - mysql

This is relevant for Drupal 8.9.3, MySQL 5.7 and Docker (with docker-compose.yml) file.
Specifically on the Drupal Install step "Set up database", Drupal throws out error [Failed to connect to your database server. The server reports the following message: SQLSTATE[HY000] [2002] Connection refused] when I try to connect using Host: 127.0.0.1.
Database name: Hub2
Database username: drupal
Database password: drupal
Host: 127.0.0.1
Port number: 9403 (This is because of resever proxy used / so on purpose not 3306)
If I used hostmachine IP address (e.g. 192.168.2.122) then it works fine, with same settings for Database name, database username, database password and also port number.
I am using DBeaver to work and look at the DBs and DBeaver can connect to mysql container using 127.0.0.1 using same user as above (in the Drupal install).
My docker-compose.yml file below:
version: '3.8'
services:
hub_db:
image: mysql:5.7
container_name: hub_db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: hub2
MYSQL_USER: drupal
MYSQL_PASSWORD: drupal
MYSQL_ROOT_HOST: 127.0.0.1
volumes:
- ./mysql/data:/var/lib/mysql
- ./mysql/mysql_logs:/var/log/mysql
- ./mysql/configuration/my.cnf:/etc/my.cnf
# Traefik port settings
ports:
- "9403:3306"
networks:
dev_local_mysql:
# Traefik label settings
labels:
- traefik.enable=true
- traefik.docker.network=dev_local_mysql
- traefik.tcp.routers.hub_db.entrypoints=mysql
- traefik.tcp.routers.hub_db.tls=true
- traefik.tcp.services.hub_db.loadbalancer.server.port=9403
networks:
dev_local_mysql:
name: dev_local
My my.cnf file below:
[mysqld]
port=3306
max_connections=250
#bind-address=127.0.0.1
max_allowed_packet=1073741824
[client]
port=3306
Drupal_install, image link
DBeaver_successfulconnection, image link

Related

Why can't I run the mysql command in docker compose?

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

Sequel Pro gives 'Connection failed!' error using Docker Mysql container

I am unable to connect my Sequel Pro to my docker mysql container. I keep getting the following error message:
I have a docker-compose.yml ;
db:
image: mysql:5.7
ports:
- "33071:3306"
command: --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=default
volumes:
- ./dump:/docker-entrypoint-initdb.d
- ./db/mysqlconf:/etc/mysql/conf.d
- ./mysqldata:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "8677:80"
links:
- db
I have read multiple forum answers on this but none of them helps. The most relevant is this link but this seems to relate to the mysql on the mac as opposed to a docker container. He talks of editing the my.cnf file located in /etc/my.cnf. on Unix/Linux.
However my docker container does not have a my.cnf file.
So I am not sure what to do.
I used the following settings for Sequel Pro connection:
Host: 127.0.0.1
Username: root
Password: root
Database: (left it blank)
Port: 3306
You are mapping port 33071 of your host, to port 3306 of your docker container in your docker-compose file.
ports:
- "33071:3306"
But in your Sequel Pro settings mentioned above, you are using port 3306 of your host. That is incorrect.
Solution:
Use the host port that is mapped with docker container i.e 33071
(Ideally you should have kept the port as 3307 in port mapping).

Symfony; SQLSTATE[HY000] [2002] No such file or directory while using 127.0.0.1 as database_host

The full error is Doctrine\DBAL\Exception\ConnectionException: An exception occurred in driver: SQLSTATE[HY000] [2002] No such file or directory in /app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php on line 113, but that's too long for the title.
I'm trying to set up a Symfony project locally, but I'm struggling to get the database connection to work. My parameters.yml looks as follows
parameters:
database_host: 127.0.0.1
database_port: 3306
database_name: database_name
database_user: username
database_password: password
I've been googling this issue a lot and most people seem to solve the issue by changing database_host from localhost to 127.0.0.1, but this doesn't work for me. The app itself runs via Docker, but I've set up the database connection once via Brew and once with a MySQL server for Mac. In both cases I can connect via the command line and with SequelPro/TablePlus, but whenever I try to access the website through the browser I get the "No such file or directory" error.
I've also tried multiple ways of setting up a Docker MySQL container, but can't get it to work. My docker-compose.yml looks like this;
nginx:
build: nginx
ports:
- "8080:80"
links:
- php
volumes:
- ../:/app
php:
build: php-fpm
volumes:
- ../:/app
working_dir: /app
extra_hosts:
- "site.dev: 172.17.0.1"
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'database_name'
MYSQL_USER: 'username'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password_root'
ports:
- '3306:3306'
expose:
- '3306'
volumes:
- my-db:/var/lib/mysql
But whenever I run docker-compose up -d I get the error Unsupported config option for services: 'db'.
Another attempt was adding
mysql:
image: mysql:latest
volumes:
- mysql_data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD='password'
- MYSQL_DATABASE='database_name'
- MYSQL_USER='username'
- MYSQL_PASSWORD='password'
To the docker-compose file, and while it does build the mysql image, I can't seem to connect to it with SequelPro/TablePlus. I ran docker-inspect on the container to get the IP (172.17.0.3), but can't seem to get access to it. I can exec into it, login using mysql -u root and create the required user and database, but then I'm still struggling to actually connect to it.
Running docker ps does show the sql container running btw;
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b6de6030791d docker_nginx "nginx -g 'daemon of…" 19 minutes ago Up 14 minutes 0.0.0.0:8080->80/tcp docker_nginx_1
f26b832bb005 docker_php "docker-php-entrypoi…" 19 minutes ago Up 14 minutes 9000/tcp docker_php_1
6c2a9e657435 mysql:latest "docker-entrypoint.s…" 19 minutes ago Up 14 minutes 3306/tcp, 33060/tcp docker_mysql_1
I also thought it might be an issue with changes to the parameters.yml file not properly syncing with the container as I'm using Mac (at my old workplace we had to use docker-sync to make sync changes between our dev environment and the actual container), but when inspecting the container itself using exec I can see the changes in the parameters.yml file.
Could the issue be it trying to connect to a mysql server running outside the Docker container? I'm still very new to Docker so I wouldn't be surprised if that's the mistake. Any tips are appreciated 'cause I'm at a dead end.
Your docker-compose file looks wrong to me, try below docker-compose file.
I removed the links, network is much easier.
version: '3'
services:
nginx:
build: nginx
ports:
- "8080:80"
networks:
- backend
volumes:
- ../:/app
php:
build: php-fpm
volumes:
- ../:/app
working_dir: /app
networks:
- backend
extra_hosts:
- "site.dev: 172.17.0.1"
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'database_name'
MYSQL_USER: 'username'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password_root'
networks:
- backend
ports:
- '3306:3306'
volumes:
- ./my-db:/var/lib/mysql
networks:
backend:
driver: bridge
then use database_host: db in php file.
I would diagnose
Check docker logs in the mysql container => no errors
Login to the mysql container and login to mysql => no errors
Login to mysql from the host (mysql -u username -p since you are mapping to 3306 port of the host)
Make sure mysql.cnf doesn't block connect from outside(check
bind-address in the mysql configuration if it 127.0.0.1 the its only
allow to connect form locally, i would for now make it 0.0.0.0 or
commented that line if exists)
mysqld --verbose --help => you will see all options
mysqld --verbose --help | grep bind-address=> check the bind-address
Make sure the user i tried to login has enough privileges to
connect(SELECT user,host FROM mysql.user;) check your user can
connect from docker network => 172.* or anywhere=> %
I think your issue is with your parameters.yml:
parameters:
database_host: 127.0.0.1
When you run compose, MySQL and PHP will run in their own containers which will have their own IPs: 127.0.0.1 or localhost from the php won't be able to connect to the db container. It's like you deployed PHP on a virtual machine A and MySQL to another virtual machine B, but you try to access MySQL from machine A by using localhost where you should specify machine B IP or hostname.
With Docker Compose the internal DNS will resolve the service name to it's container, so you can use something like:
parameters:
# name of the service in compose should be resolved
database_host: db
The error SQLSTATE[HY000] [2002] No such file or directory may be caused when the client tries to read MySQL socket usually present at /var/lib/mysql/mysql.sock which is probably not present in your PHP container.

Docker django mysql.sock in different location

I'm trying to containerize my django file, and I keep running into the issue:(2006, ’Can\‘t connect to local MySQL server through socket \‘/var/run/mysqld/mysqld.sock\’ (2 “No such file or directory”)
I found out later mysql.sock is in this location:/tmp/mysql.sock instead of /var/run/mysqld/mysqld.sock, how do I change the location for docker to see /tmp/mysql.sock
Here is my docker-composr.yml:
version: '3'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: somepassword
adminer:
image: adminer
restart: always
ports:
- 8080:8080
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
I have followed the instructions on the mysql docker website to link mysql instance to a container
EDIT: I read another stack overflow similar to this, I changed my django code to 'HOST': '127.0.0.1' in DATABASES now I get : (2006, 'Can\'t connect to MySQL server on \'127.0.0.1\' (111 "Connection refused")')
Your host should be db. When using docker-compose, you address different servers by their service name.
So, in settings.py, you should have:
DATABASES = {
'default': {
'HOST': 'db',
...
}
}
If you want to connect to your containerized MySQL server both inside and outside of the container, you'll first need to make sure the port is mapped on the host machine:
services:
db:
image: mysql
ports:
- "3306:3306"
...
That will allow you to access MySQL using localhost or 127.0.0.1 directly on your host machine.
If you want to be able to run Django in both the web container and also on the host, you'll need to override the DATABASES setting depending upon the scenario. The web container will need to use a HOST value of db, whereas your local machine will need a value of localhost.

How to connect locally hosted MySQL database with the docker container

Through docker-compose.yml I am able to run the application. Now we want to move the application to production, But we don't want to use the container database. So is there any way so that I can connect my local MySQL database with the application using docker-compose?
My docker-compose.yml looks like:
version: '3'
services:
web-app:
build:
context: .
dockerfile: web-app/Dockerfile
ports:
- 8080:8080
links:
- app-db
app-db:
build:
context: .
dockerfile: app-db/Dockerfile
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=Optimize
ports:
- 3306:3306
Instead of app-db part I just want to connect to my locally hosted mysql database.
Just use host.docker.internal instead of localhost.
To make it work on Linux, you just need to start your Docker container passing the parameter --add-host host.docker.internal:host-gateway
Find the host machine ip in the docker network. If you use docker-compose.yml version: "3" it's probably that that IP is: 172.18.0.1, but confirm it searching for the "Gateway" of your container (your host):
docker inspect <container-id-or-name> | grep Gateway
"Gateway": "",
"IPv6Gateway": "",
"Gateway": "172.18.0.1",
"IPv6Gateway": "",
So inside your docker application point to MySQL as this: 172.18.0.1:3306 (maybe in a configuration file). Take into account that that IP is fixed as long as the docker network still the same (the network is created by docker-compose, and it is not removed unless you do docker-compose down)
Also, check that your MySQL is listening to all of its interfaces. In your my.cnf search for bind-address that should be 0.0.0.0 (consider security issues if your server has public IP).
As an alternative you can bring to the container the same networking as your host, in order to share the localhost, so the container will find mysql there. Use network mode as "host":
version: '3'
services:
web-app:
build:
context: .
dockerfile: web-app/Dockerfile
ports:
- 8080:8080
network_mode: "host"
Then, point in your hibernate.properties to mysql as this: localhost:3306
Allow permission using mysqld.cnf
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 0.0.0.0
save your file and restart mysql server
sudo systemctl restart mysql.service
login to Mysql
mysql -h localhost -u root -p
GRANT ALL ON *.* TO 'root'#'localhost' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;
GRANT ALL ON *.* TO 'root'#'%' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
Find your IP
ip addr show
And use it in your docker container 192.168.x.x
If anybody is using mac os then please use docker.for.mac.host.internal instead of host.docker.internal as the environment variable.
Problem:
We had a same scenario where we wanted to setup a Development local machine setup for the services where a developer can just make docker-compose up -d and use all the latest dev builds in his local machine.
But we started facing issues while connecting our tomcat based web service configured as service in the docker-compose.yml to **mysql-8.0** service within the docker-compose. So as alternative and quick fix we thought to use local mysql configured on the local laptop with Ubuntu and docker-compose.
Our tomcat-application was using a application-dev.yml file with jdbc config variable to connect to the mysql, say something like
url: jdbc:mysql://db:3306/abcDB?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: abcderf
Docker-compose.yml
version: '3'
services:
db:
image: mysql:8.0
container_name: db
restart: unless-stopped
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=abcd
- MYSQL_PASSWORD=abcd
- MYSQL_DATABASE=abcdDB
volumes:
- ./sql-dump:/docker-entrypoint-initdb.d
command: '--default-authentication-plugin=mysql_native_password'
frontend:
container_name: frontend
image: abc/abc:frontend-1.1.36
depends_on:
- backend
nginx:
container_name: nginx
image: nginx:1.19-alpine
links:
- "frontend"
- "backend"
volumes:
- ./proxyConf:/etc/nginx/conf.d
ports:
- "80:80"
- "443:443"
backend:
container_name: backend
image: abc/abc:server-3.1.161
restart: unless-stopped
env_file: .env
environment:
- SPRING_PROFILES_ACTIVE=devtest
- DB_HOST=db:3306
- DB_NAME=abcDB
depends_on:
- db
Solution worked for us:
As we wanted to connect our docker-compose tomcat service from the local mysql we tried the below routes.
docker inspect <container-id-or-name> | grep Gateway which returned the ip Gateway ip value as`
"Gateway": "",
"IPv6Gateway": "",
"Gateway": "172.18.0.1",
"IPv6Gateway": ""
used the above Ip in the application-dev.yml as below ( URL post resolve of the ${DB_HOST} looks something as below But the IP changes to next highrt post the docker-compose down ex: 172.19.0.1)
url: jdbc:mysql://172.18.0.1:3306/abcDB?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: abcderf
Modified docker-compose.yml
version: '3'
services:
frontend:
container_name: frontend
image: abc/abc:frontend-1.1.36
depends_on:
- backend
nginx:
container_name: nginx
image: nginx:1.19-alpine
links:
- "frontend"
- "backend"
volumes:
- ./proxyConf:/etc/nginx/conf.d
ports:
- "80:80"
- "443:443"
backend:
container_name: backend
image: abc/abc:server-3.1.161
restart: unless-stopped
env_file: .env
environment:
- SPRING_PROFILES_ACTIVE=devtest
- DB_HOST=172.18.0.1:3306
- DB_NAME=abcDB
depends_on:
- db
and Also updated the bind-address = 0.0.0.0 in the /etc/mysql/mysql.conf.d/mysqld.cnf and from the mysql command line allow the root user to connect from any Ip.
Below were the only commands worked for mysql-8.0 as other were failing with error syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY 'abcd'' at line 1
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost';
UPDATE mysql.user SET host='%' WHERE user='root';
Restart the mysql client
sudo service mysql restart
The easist way is use host.docker.internal instead localhost.
MONGO_SERVER = {
'host': 'host.docker.internal',
'port': 27017,
'username': 'admin',
'password': '123456'}
It works for me by adding network_mode in docker-compose.yml
version: '3'
services:
aservice:
image: image:latest
ports:
- "8080:8080"
- "8787:8787"
# Allow docker to connect to localhost
network_mode: "host"
locally I had MySQL running on port 3306 and MySQL container running on the same port 3306
You have to be sure that MySQL or whatever database service that you are using in your container is stopped in your local envirmint before connecting to it
In my case, I had MySQL running through Homebrew
brew services stop mysql