Mysql refuses Connection to Adminer on Docker - mysql

I know this is a duplicate of this, but since that was never answered, I am re-posting this question.
I am trying to build a basic connection of php-apache and mysql containers.
docker-compose.yml
version: '3.1'
services:
php:
build:
context: .
dockerfile: Dockerfile
ports:
- 80:80
volumes:
- ./src:/var/www/html
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
# Exposing the ports doesn't help either
# ports:
# - 3306:3306
environment:
MYSQL_ROOT_PASSWORD: example
adminer:
image: adminer
restart: always
ports:
- 8080:8080
Dockerfile
FROM php:7.2-apache
RUN docker-php-ext-install mysqli
I run a simple docker-compose up command, and access Adminer on localhost:8080.
However, despite using the default login details, i.e
server: db
username: root
password: example
I get the error SQLSTATE[HY000] [2002] Connection refused.
Screenshot:
I think the issue might be some configuration issue on my local machine, because I couldn't use docker based LAMP stacks made by other people too.
Any help is greatly appreciated,
Thank You!

Turns out Patience was the answer.
If I wait long enough(around 4.5 minutes), the mysql container lets out a small log detail.
db_1 | 2021-03-09T08:21:03.882088Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.23' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
After that the login in works.
Note: There is a similar line that is logged, but that is regarding port 0, this is 3306. Attempting a failure immediately after that fails.
One must wait for the line with port 3306.
If any of you can give me feedback or advice on how I could make my mysql container initialize faster,
I'd greatly appreciate it.

Related

Mysql port forwarding with docker and proxy

I am facing a problem trying to use docker and two port forwardings. Basically I have:
A docker container hosting a MySQL database running on port 3306 in the container
The host of the container, where port XXXX is linked to the 3306 of the container with the docker-compose command ports: - XXXX:3306; I can access my container within the host using PhpMyadmin. So, so far so good
I create a bridge with a proxy server on port 3336 created with a command: ssh -i key.pem -R 3336:localhost:XXXX ubuntu#IP
I then have a client (say Mysql Workbench) which is connected to the proxy using another tunnel : ssh -i key.pem -L 3306:3336 ubuntu#IP
I tried to summarize everything in the following picture with XXXX being 3306 (the green box).
When I try to connect to the database using this rather complex method, it succeeds when XXXX=3306. However, when XXXX=8701 for example, it does not work anymore. Do you have any idea why ? The error I get is a classic timeout: UnhandledPromiseRejectionWarning: Error: connect ETIMEDOUT
Thank you in advance for your help.
Best,
B
I found the issue, which was related to the docker-compose.yml file;
Previously I had:
version: "3.7"
services:
db:
build:
context: ./database
command: --default-authentication-plugin=mysql_native_password --sql_mode=""
restart: always
cap_add:
- SYS_NICE
volumes:
- db_data:/var/lib/mysql
ports:
- ${MYSQL_HOST_PORT}:${MYSQL_PORT}
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_TCP_PORT: ${MYSQL_HOST_PORT}
env_file: ./.env
The trick was to have the same port in and outside of the container ${MYSQL_HOST_PORT}:${MYSQL_HOST_PORT}
Hope it would help others in the same situation

docker-compose mysql persistent data

I'm new to docker and I'm following this guide to setup my mac (the guide is for linux but is not the point) for a PHP dev env with docker-compose.
The guide is quite good and everything seems to work correctly, there is just a part that doesn't seem to work.
If you go to Step 9 — Creating a User for MySQL, I'm running docker-compose up and I have these errors so I can't access the db container.
mysqld: Error on realpath() on '/var/lib/mysql-files' (Error 2 - No such file or directory)
db | 2020-04-16T18:22:19.603226Z 0 [ERROR] [MY-010095] [Server] Failed to access directory for --secure-file-priv. Please make sure that directory exists and is accessible by MySQL Server. Supplied value : /var/lib/mysql-files
db | 2020-04-16T18:22:19.603246Z 0 [ERROR] [MY-010119] [Server] Aborting
If I remove the - ./mysql/my.cnf:/etc/mysql/my.cnf from the volumes everything seem to work but then for some reason my laravel db is not created.
these command is not supposed to already create a DB?
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: root
another question is where the data is stored?
db:
...
volumes:
- dbdata:/var/lib/mysql
...
#Volumes
volumes:
dbdata:
driver: local
how it works that driver local and what is the location of it?
I just found out that the tutorial uses a wrong path, the correct path is:
- ./mysql/my.cnf:/etc/my.cnf
and not:
- ./mysql/my.cnf:/etc/mysql/my.cnf
docker-compose.yml
version: '3.7'
services:
db:
image: mysql:8.0.21
container_name: dbweb
restart: always
environment:
MYSQL_ROOT_PASSWORD: 'deivypassword'
TZ: America/New_York
ports:
# : < MySQL Port running inside container>
- '3306:3306'
# Where our data will be persisted
volumes:
- dbweb2:/var/lib/mysql
- ./my.cnf:/etc/mysql/my.cnf
Names our volume
volumes:
dbweb2:

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.

nextcloud and mariadb (both) on docker: SQLSTATE[HY000] [2002] No such file or directory

I've been trying to set up nextlcoud and mariadb with the linuxserver images and hit my road block when I want to get through the first run wizard of nextcloud:
Error message incl. all settings of first run wizard
Problem
I.e. the first time wizard gives me Error while trying to create admin user: Failed to connect to the database: An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory.
Question:
Where is that coming from and how to solve the problem?
System
I'm using Amahi 11 and have installed docker from the repositories. Docker verision:
Client:
Version: 18.09.0
API version: 1.39
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:48:52 2018
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.0
API version: 1.39 (minimum version 1.12)
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:19:08 2018
OS/Arch: linux/amd64
Experimental: false
I am starting nextcloud and mariadb with docker-compose. Following the content for mariadb:
version: '2'
services:
mariadb:
image: linuxserver/mariadb
container_name: mariadb
environment:
- PUID=XX
- PGID=YYY
- MYSQL_ROOT_USER=root
- MYSQL_ROOT_PASSWORD=secret
- MYSQL_DATABASE=nextcloud
volumes:
- <path/to/my/folder>:/config
ports:
- 3307:3306
restart: unless-stopped
Already tried:
A lot of research, which came up empty or lead me to do the next point:
So from the error info I started checking if the database actually exists:sudo docker exec -it mariadb bash. There I figured, that access to command-line with 'mysql' for root was denied because the password was not set. (mmmh... is ther something wrong with my docker-compose-file?)
Anyway I corrected that one with mysql -u root -pSECRETand mysql -u root --password=SECRET. With show databases; I found no nextcloud database. (There MUST be something wrong with my docker-compose-file.) So I created it as well (create database nextcloud;). Database is now shown properly and I found it in <path/to/my/folder>. Result: No change, problem still there.
I did some more editing with on my docker-compose-file:
version: '2'
services:
mariadb:
image: linuxserver/mariadb
container_name: mariadb
environment:
- MYSQL_ROOT_PASSWORD=secret
- MYSQL_DATABASE=nextcloud
- PUID=XX
- PGID=YYY
volumes:
- <path/to/my/folder>:/config
ports:
- 3307:3306
restart: unless-stopped
So I changed the hierarchy and eliminated the MYSQL_ROOT_USER=rootline. When restarting I can mysql -u root --password=SECRET and show the nextcloud database. YET, I am not sure, if these changes remained in the volume from my last (manual) changes. Result: Problem still there.
Just for curiousity I started playing around with the localhost-port. I chose 3307 because my host-system has a mariadb running on 3306, which I do not want to use. So altering port localhost:3307 to localhost:WXYZ - you name it gives the same error... mmmh - changeing localhost to <your host-IP>!!!
SUCCESS
I changed localhost -> mariadb and it worked!
I had to use nextcloud-mariadb:3306 as the connection string. I figured it out by running $ docker ps -a which lists the name and the port.
Solution:
As other answers have mentioned, the solution is don't use localhost.
Even changing to 127.0.0.1 appears to be sufficient *(see note below)
Explanation:
No such file or directory is the result of mysql attempting to connect over a local socket. This happens when either of these settings is set to localhost:
Database host field of the WebUI
environment variable MYSQL_HOST
*Note: in the case of #2, it is not sufficient to "fix" the Database host field in the WebUI, the environment variable MYSQL_HOST always takes precedence.
(This is true as of NextCloud version 25.0.0.18)
When you run Nextcloud in docker, add --link mariadb:mariadb. You can then use mariadb to replace localhost
I had to use my custom server hostname, instead of localhost. On Linux you can get it by executing the command hostname.
I solved the same problem when I changed the Nexcloud MYSQL_HOST environment parameter from localhost to database service/image name (in my case MYSQL_HOST: mysql ) in the docker-compose.yml
version: "3.7"
services:
mysql:
image: mysql
container_name: mysql-nextcloud
restart: unless-stopped
ports:
- 3306:3306
environment:
...
volumes:
...
app:
image: nextcloud
container_name: nextcloud
restart: unless-stopped
ports:
- 80:80
links:
- mysql
volumes:
...
environment:
MYSQL_PASSWORD: ...
MYSQL_DATABASE: ...
MYSQL_USER: ...
MYSQL_HOST: mysql
I changed the database name(localhost:PORT -> container_name), and it worked!
By the way, the [localhost:PORT] wroked well before!

Docker on Windows gives error when trying to start mysql container

I'm new on Docker and trying now to create my first docker-compose file.
The apache works so far but i'm struggeling a bit with mysql.
Here is my docker-compose.yaml
version: '3'
services:
web:
build: .
ports:
- "8083:80"
volumes:
- ./public_html/:/usr/local/apache2/htdocs/
networks:
- appnet
db:
image: mysql
volumes:
- db-data:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- appnet
adminer:
image: adminer
restart: always
ports:
- 8080:8080
volumes:
db-data:
networks:
appnet:
The error is:
[ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
I alread tried to remove the volume totally or added a /data at the end. But it gives me all the time the same error. Also Google didn't give me any good hints.
Does someone has an idea?
I figured out now. I'm running Docker on a Windows Machine, so i had to reset the credentials ond Docker App and type in again. Now it works. Hope that will help others as well. It seems on Docker for Windows, when something not work as expected it make sense to reset the credentials.