MySQL Connection in Docker container - php

this is driving me crazy ...
I am building a simple LEMP stack with Docker on my Mac.
This is my docker-compose file:
nginx:
image: tutum/nginx
ports:
- "3333:80"
links:
- phpfpm
- db
volumes:
- ./nginx/default.conf:/etc/nginx/sites-available/default
- ./nginx/default.conf:/etc/nginx/sites-enabled/default
- ./logs/nginx-error.log:/var/log/nginx/error.log
- ./logs/nginx-access.log:/var/log/nginx/access.log
phpfpm:
build: ./phpfpm
ports:
- "9000:9000"
volumes:
- ./code:/usr/share/nginx/html
db:
image: mysql
tty: true
restart: always
volumes:
- /Users/mauo/local/mysql/data:/var/lib/mysql
ports:
- "3308:3306"
environment:
MYSQL_DATABASE: 'docker'
MYSQL_ROOT_PASSWORD: 'root'
MYSQL_USER: 'root'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
Everything works fine, with exception on mysql container.
This refuses me connection.
This is the message on my php file inside code folder:
(HY000/2002): Connection refused in /usr/share/nginx/html/index.php on line 2
When I enter in mysql container with exec -it bash and I type 'mysql', the same thing, access denied for root#localhost.
From within the container, I tried to launch 'mysqld' and it tells me
2016-07-16T09:56:02.777893Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2016-07-16T09:56:02.778010Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
2016-07-16T09:56:03.779967Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2016-07-16T09:56:03.780182Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
Any help would be appreciated!!!!
Thank you
M.

Related

Database connection refused for Laravel artisan command with Docker

I am trying to run Laravel using Docker locally. Everything works fine expect the database. This is how my docker-compose looks like:
services:
app:
build:
context: ./
dockerfile: ./docker/Dockerfile
container_name: my-app
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- my-network
db:
image: mysql:8.0
container_name: my-db
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
ports:
- 3307:3306
volumes:
- ./docker/mysql:/docker-entrypoint-initdb.d
networks:
- my-network
nginx:
image: nginx:alpine
container_name: my-nginx
ports:
- 8001:80
volumes:
- ./:/var/www
- ./docker/nginx/nginx.conf:/etc/nginx/conf.d
networks:
- my-network
My .env file looks like this:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=admin
DB_PASSWORD=
When I run php artisan migrate:fresh within the app container, I am getting the following message:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
Can someone see what is the problem here?
UPDATE: When I run docker-compose ps I can see
NAME COMMAND SERVICE STATUS PORTS
my-app "docker-php-entrypoi…" app running 9000/tcp
my-db "docker-entrypoint.s…" db restarting
my-nginx "/docker-entrypoint.…" nginx running 0.0.0.0:8001->80/tcp, :::8001->80/tcp
This is the log when I run docker-compose logs db
my-db | 2021-07-29T20:18:37.340769Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.20) initializing of server in progress as process 104
my-db | 2021-07-29T20:18:37.354423Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
my-db | 2021-07-29T20:18:37.354605Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
my-db | 2021-07-29T20:18:37.356184Z 0 [ERROR] [MY-010119] [Server] Aborting
my-db | 2021-07-29T20:18:37.358746Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.20) MySQL Community Server - GPL.

Link between symfony and docker

I'm on a symfony project and i'm using docker here's my docker-compose.yml :
version: '3.7'
services:
db:
image: mysql:latest
container_name: ruakh_db
restart: always
volumes:
- db-data:/var/lib/mysql
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
networks:
- dev
phpmyadmin:
image: phpmyadmin:latest
container_name: ruakh_phpmyadmin
restart: always
depends_on:
- db
ports:
- 8080:80
environment:
PMA_HOST: db
networks:
- dev
maildev:
image: maildev/maildev
container_name: ruakh_mail_dev
restart: always
command: bin/maildev --web 80 --smtp 25 --hide-exetensions STARTTLS
ports:
- 8081:80
networks:
- dev
apache:
build: php
container_name: ruakh_www
ports:
- 8088:80
volumes:
- ./php/vhosts:/etc/apache2/sites-enabled
- ./:/var/www
restart: always
networks:
- dev
networks:
dev:
volumes:
db-data:`
here's the database url used in my symfony project :
DATABASE_URL=mysql://root:root#ruakh_db/ruakh
I'm trying to run a php bin/console make:migration however when i'm running I get this error :
An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
I'm assuming that the database url is incorrect, when I changed it to :
DATABASE_URL=mysql://root:root#127.0.0.1:8080/ruakh
This error is thrown:
An exception occurred in driver: SQLSTATE[HY000] [2006] MySQL server has gone away
How can I manage to connect my symfony project to my docker database ?
There is a couple things wrong with your DATABASE_URL.
DATABASE_URL=mysql://root:root#ruakh_db/ruakh
You don't set the password in your service config
You have the wrong host name
You don't specify the database
You are using native authentication, which must be enabled when using MySQL 8
(optional) your database port is not exposed
Password
In your docker-compose.yml. you do not specify the root password and instead allow for an empty password. You can set the root password using an environment variable:
services:
db:
...
environment:
MYSQL_ROOT_PASSWORD: example
Database name
In order to create a default database named ruakh you need to provide another environment variable MYSQL_DATABASE.
Host
The next issue is the host name in your DATABASE_URL: ruakh_db. This is the name of the container, but not the name of the service (i.e. the host in the virtual network that docker-compose sets up). This should be db instead.
Port
You will not be able to access your mysql database from outside the docker container, because you do not expose any ports. That is not necessarily an issue, but you should be aware of it. Looking at your other database url DATABASE_URL=mysql://root:root#127.0.0.1:8080/ruakh this will not work, because the port 8080 is for the web interface provided by phpmyadmin. It is not the port of the actual database. Also, the port for the database is not exposed. You probably want to add:
services:
db:
...
ports:
- 3306:3306
Authentication
Another issue, you will face is using the image mysql:latest this will use MySQL 8.0 which does not allow the authentication mechanism you want to use by default. You will have to change the command executed when running the container or downgrade to MySQL 5.7. If you want to keep MySQL 8, you should add this:
services:
db:
...
command:
- 'mysqld'
- '--character-set-server=utf8mb4'
- '--collation-server=utf8mb4_unicode_ci'
- '--default-authentication-plugin=mysql_native_password'
Summary
This is roughly what your db service should look like to work with your DATABASE_URL:
db:
image: mysql:latest
ports:
- '3306:3306'
environment:
MYSQL_DATABASE: ruakh
MYSQL_ROOT_PASSWORD: 'root'
command:
- 'mysqld'
- '--character-set-server=utf8mb4'
- '--collation-server=utf8mb4_unicode_ci'
- '--default-authentication-plugin=mysql_native_password'

Can't connect to MYSQL container from Drupal container through drush

I try to figure out this problem since a long time now and I've got to admit that I'm out of responses.
Here is the case:
docker-compose.yml
version: "3"
services:
mysql:
image: mysql:8.0
container_name: mysql
hostname: mysql
command: --default-authentication-plugin=mysql_native_password
restart: unless-stopped
env_file: .env
volumes:
- db-data:/var/lib/mysql
networks:
- internal
drupal:
build:
context: .
dockerfile: Dockerfile
container_name: drupal
depends_on:
- mysql
restart: unless-stopped
volumes:
- ./web:/var/www/html/web:rw
- ./vendor:/var/www/html/vendor:rw
- ./drush:/var/www/html/drush
- drupal-data:/var/www/html
- ./php-conf/php.ini:/usr/local/etc/php/php.ini
networks:
- internal
- external
webserver:
image: nginx:1.19.1-alpine
container_name: webserver
depends_on:
- drupal
restart: unless-stopped
ports:
- 80:80
- 443:443
volumes:
- drupal-data:/var/www/html
- ./web:/var/www/html/web:rw
- ./nginx-conf/snippets/self-signed.conf:/etc/nginx/conf.d/snippets/self-signed.conf
- ./nginx-conf/snippets/ssl-params.conf:/etc/nginx/conf.d/snippets/ssl-params.conf
- ./nginx-conf/sites-available/default.conf:/etc/nginx/conf.d/default.conf
- ./ca-certificates/qiminfo-docker.dev+4.pem:/etc/ssl/certs/qiminfo-docker.dev+4.pem
- ./ca-certificates/qiminfo-docker.dev+4-key.pem:/etc/ssl/private/qiminfo-docker.dev+4-key.pem
networks:
- external
networks:
external:
driver: bridge
internal:
driver: bridge
volumes:
drupal-data:
db-data:
Dockerfile (only for drupal container)
FROM drupal:8.9.2-fpm-alpine
RUN apk add mysql-client && apk add openssh
I manage all dependencies via the mounted files in volumes (it works nice btw), but when I run drush through my host machine or through the container it doesn't see the Drupal instance (neither root or database) and I get the classic error message in this case ...
In BootstrapHook.php line 32:
[Exception]
Bootstrap failed. Run your command with -vvv for more information.
But, I use the Dockerfile to get mysql-client installed on drupal container and to allow connection from drupal container to mysql container. And when I try to connect to mysql (which has 'mysql' hostname) container database it works !
➜ docker exec -it drupal sh
/var/www/html # mysql -u drupal -p drupal -h mysql
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.21 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [drupal]>
The question is:
Why drush doesn't has access to mysql server container while mysql-cli does the job fine ?!
Please help T-T

Multiple applications running with docker

I have a symfony application running with docker following this repository
https://github.com/maxpou/docker-symfony and everything is ok.
But now I am starting another symfony project and I cloned the same docker-symfony repository, but I get this error doing docker-compose up -d
ERROR: for nginx Cannot start service nginx: driver failed
programming external connectivity on endpoint
symfony2restapidocker_nginx_1
(d736a2c930368e1cd94f71e502bfe3ffb991cf8d63ae0b0d412c189c8e5b504f):
Bind for 0.0.0.0:80 failed: port is already allocated
Is it not possible to have two applications running at the same time each with its docker?
Thanks
Port 80 is already allocated for some other application/service.
Try to stop 80 port running application. otherwise you can change it your current application port to some other like
Instead of 80 use other port number 7080, etc. something else
Yes it is possible.
The error that you are encoutering is caused by the port 80 being already reserved for the first nginx container. If you want to start the same container you need to give it a new port on the host.
The docker-compose file must be updated for the second run as such:
version: '2'
services:
db:
image: mysql
volumes:
- "./.data/db:/var/lib/mysql"
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
php:
build:
context: php7-fpm
args:
TIMEZONE: ${TIMEZONE}
volumes:
- ${SYMFONY_APP_PATH}:/var/www/symfony
- ./logs/symfony:/var/www/symfony/app/logs
nginx:
build: nginx
ports:
- 82:80
volumes_from:
- php
volumes:
- ./logs/nginx/:/var/log/nginx
elk:
image: willdurand/elk
ports:
- 83:80
volumes:
- ./elk/logstash:/etc/logstash
- ./elk/logstash/patterns:/opt/logstash/patterns
volumes_from:
- php
- nginx
In this case, port 82 and 82 will be reserved for the new containers nginx and elk respectively.

Mysql Exited on Docker Container

I want to make a web development platform on MacOS by using Docker. I installed nginx, and php7-fpm container and they're running and communicating each other. But after installing mysql container, mysql container was exited. I don't know why it exited.
This is docker ps -a output:
2955d2d5c392 nginx "/sbin/my_init" 38 seconds ago Up 36 seconds 0.0.0.0:8080->80/tcp dockertutorial_web_1
ec3c16795f05 php:7-fpm "docker-php-entrypoin" 38 seconds ago Up 37 seconds 9000/tcp dockertutorial_php_1
835e91ba927a mysql:latest "docker-entrypoint.sh" 38 seconds ago Exited (0) 37 seconds ago dockertutorial_mysql_1
As you can see, mysql was exited.
This is my docker-compose.yml file:
web:
image: nginx
ports:
- "8080:80"
volumes:
- ./src/public:/var/www/public
- ./src/vhost.conf:/etc/nginx/sites-enabled/vhost.conf
links:
- php
php:
image: php:7-fpm
volumes:
- ./src/public:/var/www/public
links:
- mysql
mysql:
image: mysql:latest
ports:
- "3306:3306"
volumes:
- /var/lib/mysql
command: "true"
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: project
MYSQL_USER: project
MYSQL_PASSWORD: project
Any suggesstion to solve it?
You are overriding command for MySQL image with true which will be run instead of mysqld.
Remove command: "true" from docker-compose.yml from mysql service and it will start mysqld.
See this Dockerfile for reference.
https://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/Dockerfile#L63

Categories