Access denied to MySQL database in Docker - php

My Docker (v.2) docker-compose.yml file has 2 Ubuntu images and 2 MariaDB images:
app:
container_name: app
image:php-apache-dev:ubuntu-16.04
links:
- mysql
depends_on:
- mysql
ports:
- '8443:443'
volumes:
- .:/app
environment:
docker: 'true'
PHP_DEBUGGER: 'xdebug'
WEB_DOCUMENT_ROOT: '/app/public'
WEB_NO_CACHE_PATTERN: '\.(.*)$$'
working_dir: '/app'
CONF_FILE: 'conf/local/develop.php'
test:
container_name: test
image:php-apache-dev:ubuntu-16.04
links:
- mysql_test
depends_on:
- mysql_test
ports:
- '8444:443'
volumes:
- .:/app
environment:
docker: 'true'
WEB_DOCUMENT_ROOT: '/app/public'
WEB_NO_CACHE_PATTERN: '\.(.*)$$'
working_dir: '/app'
CONF_FILE: 'conf/local/test.php'
mysql:
image: mariadb:latest
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: 'dev'
MYSQL_DATABASE: 'dev'
MYSQL_USER: 'dev'
MYSQL_PASSWORD: 'dev'
mysql_test:
image: mariadb:latest
ports:
- '3307:3306'
environment:
MYSQL_ROOT_PASSWORD: 'dev'
MYSQL_DATABASE: 'dev_test'
MYSQL_USER: 'dev'
MYSQL_PASSWORD: 'dev'
One is for development, one is for testing. When I access the first one (on localhost:4443) it works. When I access the second one (on localhost:4444) I am able to view the website, but it denies access to the user:
Access denied for user 'dev'#'%' to database 'dev_test'
My configuration for the second site is set up to access the second MySQL port/user:
'db_port' => env('DB_PORT', '3307'),
'db_name' => env('DB_NAME', 'dev_test'),
I tried the answer to this question but without success. If I docker exec bash into the test database image, and run SHOW GRANTS FOR dev, it says:
GRANT ALL PRIVILEGES ON `dev_test`.* TO 'dev'#'%'

You probably forgot to use the correct MySQL host in your second site.

Related

Dockerization PHP-APACHE MYSQL on centos7

i have tried creating a docker container on centos7 but
i could access phpmyadmin page but not index.php page
could you see the docker yml and docker file and tell what happened
like im running through kali linux to a centos7 docker its been difficult i have been trying since yesterday
the give code is docker-compose.yml
version: "3"
services:
www:
build: .
ports:
- "8001:80"
volumes:
- ./www:/var/www/html/
links:
- db
networks:
- default
db:
image: mysql:8.0.16
command: ['--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci','--default-authentication-plugin=mysql_native_password']
ports:
- "8002:3306"
environment:
MYSQL_DATABASE: myDb
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump:/docker-entrypoint-initdb.d
- persistent:/var/lib/mysql
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin:4.9
links:
- db:db
ports:
- 8000:80
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
persistent:
and the dockerfile
FROM php:7.1.19-apache
RUN docker-php-ext-install mysqli mbstring
i dont know what is the issue like when access localhost:8000 on browser
im getting forbidden access 403 error
i just need to access index.php when i enter localhost:8000 on browser
please help me out,
thank you in advance

How to make API calls within docker environment?

I am building an app wrapped in docker, that consist of PHP backend ("API") and NODE frontend, they are united by NGINX, where php app is served by the means of php-fpm and my node app is served by the reverse proxy. NGINX exposes phpMyAdmin app (phpmyadmin.test) and "API" (api.php.test) for dev purposes and NODE api (nodeapp.test).
NODE apps SSR ("Server-Side Rendering") needs to fetch some data from an API within docker the network, and because domains such as api.php.test can't be recognized from within docker I have to make calls to NGINX container which serves 3 different domains mentioned above, so I either need to fake 'HOST' header to get appropriate response from an API via NGINX which leads problems. Such as: Refused to set unsafe header "Host", Error: unable to verify the first certificate in nodejs etc.
Do I have to spin up Nginx container for each endpoint to avoid these issues? Or is there a better way to go around this?
Here is an example of my docker-compose.yml to give you a better idea of what happens in my app.
version: "3.7"
services:
workspace:
build:
context: workspace
args:
WORKSPACE_USER: ${WORKSPACE_USER}
volumes:
- api:/var/www/api
- site:/var/www/site
ports:
- "2222:22"
environment:
S3_KEY: ${S3_KEY}
S3_SECRET: ${S3_SECRET}
S3_BUCKET: ${S3_BUCKET}
DB_CONNECTION: ${DB_CONNECTION}
MYSQL_HOST: ${MYSQL_HOST}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MEDIA_LIBRARY_ENDPOINT_TYPE: ${MEDIA_LIBRARY_ENDPOINT_TYPE}
MEDIA_LIBRARY_IMAGE_SERVICE: ${MEDIA_LIBRARY_IMAGE_SERVICE}
tty: true
php-fpm:
build:
context: ./php-fpm
depends_on:
- nodejs
volumes:
- api:/var/www/api
- ./certs:/certs
environment:
S3_KEY: ${S3_KEY}
S3_SECRET: ${S3_SECRET}
S3_BUCKET: ${S3_BUCKET}
DB_CONNECTION: ${DB_CONNECTION}
MYSQL_HOST: ${MYSQL_HOST}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MEDIA_LIBRARY_ENDPOINT_TYPE: ${MEDIA_LIBRARY_ENDPOINT_TYPE}
MEDIA_LIBRARY_IMAGE_SERVICE: ${MEDIA_LIBRARY_IMAGE_SERVICE}
nodejs:
build:
context: ./nodejs
args:
NODEJS_SITE_PATH: ${NODEJS_SITE_PATH}
NODEJS_VER: ${NODEJS_VER}
volumes:
- site:${NODEJS_SITE_PATH}
- ./certs:/certs
environment:
NODEJS_ENV: ${NODEJS_ENV}
ports:
- 3000:3000
- 3001:3001
nginx:
build:
context: nginx
depends_on:
- php-fpm
- mariadb
restart: always
volumes:
- api:/var/www/api
- site:/var/www/site
- ./nginx/global:/etc/nginx/global
- ./nginx/sites:/etc/nginx/sites-available
- ./nginx/logs:/var/log/nginx
- ./certs:/certs
ports:
- 80:80
- 443:443
mariadb:
image: mariadb
restart: always
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
volumes:
- db:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- mariadb
restart: always
environment:
PMA_HOST: ${MYSQL_HOST}
PMA_USER: root
PMA_PASSWORD: ${MYSQL_ROOT_PASSWORD}
UPLOAD_LIMIT: 2048M
volumes:
phpmyadmin:
db:
site:
external: true
api:
external: true

Three container docker-composition : PHP can't connect to Maria DB

My docker-compose.yml :
version: '3'
services:
php-apache:
build:
context: ./php-apache
ports:
- 80:80
volumes:
- ./DocumentRoot:/var/www/html
links:
- 'db'
networks:
- default
db:
image: mariadb:10.1
volumes:
- ./db:/var/lib/mysql
- ./dump.sql:/docker-entrypoint-initdb.d/dump.sql
ports:
- "3306:3306"
environment:
TZ: "Europe/London"
MYSQL_ALLOW_EMPTY_PASSWORD: "no"
MYSQL_ROOT_PASSWORD: "rootpwd"
MYSQL_USER: 'testuser'
MYSQL_PASSWORD: 'testpassword'
MYSQL_DATABASE: 'testdb'
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- 'db'
ports:
- 8000:80
environment:
MYSQL_USER: 'testuser'
MYSQL_PASSWORD: 'testpassword'
MYSQL_ROOT_PASSWORD: 'rootpwd'
PMA_HOST: db
PMA_PORT: 3306
This is based on a tutorial I found online.
When I run it, I can see that the PHP container is up. I can see that the db and php-mydmin are up. In fact I can successfully access the db from php-myadmin using the testuser / testpassword credentials.
But when I try to access the db from the main PHP application using
$conn = mysqli_connect("localhost","testuser",'testpassword','testdb');
I get
Warning: mysqli_connect(): (HY000/2002): No such file or directory in /var/www/html/index.php on line 8
Is this a problem with the docker config? Can anyone see anything missing?

Unable to connect phpmyadmin to database using docker compose

May be this question asked few times before but I did't get a valid answer which can solve my problem.
I am trying to run phpmyadmin in docker on different container using docker-compose but It always through the following error:
#2002 - Connection refused — The server is not responding (or the local server's socket is not correctly configured).
My docker compose file contains the following code:
version: "2"
services:
web:
build: .
ports:
- "80:80"
networks:
- web
volumes:
- .:/code
restart: always
db:
image: "mysql:5"
volumes:
- ./mysql:/etc/mysql/conf.d
environment:
MYSQL_ROOT_PASSWORD: toor
MYSQL_DATABASE: phpapp
networks:
- web
restart: always
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_PORT: 3306
PMA_HOST: db
PMA_USER: root
PMA_PASSWORD: toor
ports:
- "8000:80"
restart: always
networks:
- web
networks:
web:
driver: bridge
In web container I am trying to connect with database and it works fine, but the problem occur with phpmyadmin connection
Any help would be appreciated. :)
Interestng enough, I have your compose-file running and phpmyadmin is accessible
from host.
Had to change port 8000 to 8004 though (port 8000 is occupied on my host).
In case your db-container does not start fast enough for phpmyadmin to connect, I suggest adding depends_on into phpmyadmin service. Makes sure db starts before phpmyadmin.
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_PORT: 3306
PMA_HOST: db
PMA_USER: root
PMA_PASSWORD: toor
ports:
- "8004:80"
restart: always
depends_on:
- db
networks:
- web
Please show logs from docker-compose up if problem persists.
Now you need to add command to mysql service for connecting to phpmyadmin.
command: --default-authentication-plugin=mysql_native_password
version: "2"
services:
db:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: drupal
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump:/docker-entrypoint-initdb.d
- /var/lib/mysql
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8000:80
environment:
PMA_HOST: db
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test

Problems PHP MARIADB PHPMYADMIN using docker-compose

I've been this last two weeks searching how to link my phpmyadmin and my database, to create a PHP development workspace, through docker-compose but I am not able to connect to the database through the phpmyadmin port.
version: '3'
services:
servicio_php:
image: php:7.3-rc-apache
volumes:
- ./web/:/var/www/html
expose:
- "80"
ports:
- "80:80"
links:
- db
db:
image: mariadb:latest
restart: always
volumes:
- ./Volumenes/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root123
MYSQL_USER: user
MYSQL_PASSWORD: user123
MYSQL_DATABASE: bbdd1
expose:
- "3306"
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: phpmyadmin
environment:
PMA_ARBITRARY: 1
MYSQL_ROOT_PASSWORD: root123
MYSQL_USERNAME: user
PMA_HOST: db
MYSQL_PASSWORD: user123
restart: always
ports:
- "8080:80"
volumes:
- ./sessions:/sessions/
depends_on:
- db
links:
- db
I am using windows 10 version of dockers 18.06.1-ce and also when i run docker-compose up i see there are some probles with Innodb but ive read is not a problem.
I am not able to see whats wrong and probably I am not the only one with this problem.

Categories