Warning: mysqli::__construct(): (HY000/2002): Connection refused - php

They are newbie to docker.
Get phpmyadmin, php, home assistant working. But I can't configure the docker well to be able to connect from php to a database. Could you help me see what the problem is.
I tried everything. I read many posts with the same error but could not get it to work.
Thank you very much
This is my modified docker-compose
version: '3.4'
services:
web:
build:
context: ./php
dockerfile: Dockerfile
container_name: php73
depends_on:
- db
volumes:
- ./php:/var/www/html/
environment:
MYSQL_HOST: mysql8
MYSQL_USER: pf
MYSQL_PASSWWORD: 123456
MYSQL_DB: ha
ports:
- 3001:80
db:
container_name: mysql8
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_USER: pf
MYSQL_PASSWWORD: 123456
MYSQL_DATABASE: ha
volumes:
- /var/lib/mysql
ports:
- 6033:3306
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
depends_on:
- db
restart: always
ports:
- 8080:80
environment:
PMA_ARBITRARY: 1
PMA_HOST: db
PMA_PORT: 3306
MYSQL_USER: pf
MYSQL_PASSWWORD: 123456
MYSQL_ROOT_PASSWORD: 123456
volumes:
- /sessions
The new php example
<?php
$host = 'db';
$user = 'pf';
$password = '123456';
$db = 'ha';
$conn = new mysqli($host,$user,$password,$db,3306);
if($conn->connect_error) {
echo 'connection failed' . $conn->connect_error;
}
echo 'Sucessfully connected msql';
?>
The Dockerfile
FROM php:7.3.3-apache
RUN apt-get update && apt-get upgrade -y
RUN docker-php-ext-install mysqli
EXPOSE 80
My docker-compose
version: '3.3'
services:
web:
build:
context: ./php
dockerfile: Dockerfile
container_name: php73
depends_on:
- db
volumes:
- ./php:/var/www/html/
ports:
- 80:80
db:
container_name: mysql8
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mediciones
MYSQL_USER: root
MYSQL_PASSWWORD: root
ports:
- 6033:3306
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
depends_on:
- db
restart: always
ports:
- 8080:80
environment:
PMA_ARBITRARY: 1
PMA_HOST: db
MYSQL_ROOT_PASSWORT: root
homeassistant:
container_name: homeassistant
restart: unless-stopped
image: homeassistant/home-assistant
devices:
- /dev/ttyUSB0:/dev/ttyUSB0
- /dev/ttyUSB1:/dev/ttyUSB1
- /dev/ttyACM0:/dev/ttyACM0
volumes:
- ${USERDIR}/docker/homeassistant:/config
- /etc/localtime:/etc/localtime:ro
- ${USERDIR}/docker/shared:/shared
- /dev/serial/by-id/:/dev/serial/by-id/
network_mode: host
privileged: true
My php example
<?php
$host = '127.0.0.1';
$user = 'root';
$password = 'root';
$db = 'ha';
$conn = new mysqli($host,$user,$password,$db);
if($conn->connect_error) {
echo 'connection failed' . $conn->connect_error;
}
echo 'Sucessfully connected msql';
?>

Assuming its your example code running in the php container:
In which case use db as the hostname and mediciones as the database name in the connection.
For the db container:
MYSQL_USER set to non-root as this will cause potential errors. This user is already given access on the MYSQL_DATABASE.
use a persistent volume for /var/lib/mysql
(optionally), if your home assistant or php doesn't create the tables, you can use [mysql "Initializing a fresh instance"]https://hub.docker.com/_/mysql) to initialize some tables.

You are exposing mysql on host port: 6033
Did you try:
$conn = new mysqli($host,$user,$password,$db,6033);

Related

Getting error mysqli::real_connect(): (HY000/2002): No such file or directory when trying to use phpma after running docker-compose script

I am getting the following error after running my docker-compose script:
mysqli::real_connect(): (HY000/2002): No such file or directory.
I've looked at Getting error mysqli::real_connect(): (HY000/2002): No such file or directory when I try to access my project on live server for answers but the given solutions don't work for me.
My docker-compose script looks like this:
version: '3'
services:
# Database
db:
image: mysql:latest
volumes:
- database:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: Password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: Password
networks:
- wpsite
# phpMyAdmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin:latest
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: Password
networks:
- wpsite
volumes: ['./config:/etc/phpmyadmin/']
# Wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- '80:80'
restart: always
volumes: ['./wordpress:/var/www/html']
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: Password
networks:
- wpsite
networks:
wpsite:
volumes:
database:
So far I've tried changing $cfg['Servers'][$i]['host'] = 'localhost'; in the /etc/phpmyadmin/config.inc.php to both $cfg['Servers'][$i]['host'] = '127.0.0.1'; and $cfg['Servers'][$i]['host'] = 'db';. Neither of these options fix the problem. I made sure they get changed properly after rerunning the script.

Docker-Compose Cannot Connect to Server w/ mysqli_connect

I have recently installed docker-compose on Ubuntu. I can connect to my local host, phpmyadmin and stuff. However, when I try to connect to it with mysqli_connect, I can reach the server but I cannot access it with Warning: mysqli_connect(): (HY000/2002).
Here is the php code I am using to try to connect:
// username and password belong to the phpmyadmin, before that I tried docker for both username and pwd.
Edit: staj is the name of database i created in phpmyadmin.
<?php
$dbHost="127.0.0.1";
$dbUser="root";
$dbPass="root";
$dbName="staj";
$conn=mysqli_connect($dbHost, $dbUser, $dbPass, $dbName);
if ($conn){
}else{
die("Connection Failed!");
}
?>
And here is my docker-compose.yml:
version: "3"
services:
webserver:
build:
context: ./bin/${PHPVERSION}
container_name: '${COMPOSE_PROJECT_NAME}-${PHPVERSION}'
restart: 'always'
ports:
- "${HOST_MACHINE_UNSECURE_HOST_PORT}:80"
- "${HOST_MACHINE_SECURE_HOST_PORT}:443"
links:
- database
volumes:
- ${DOCUMENT_ROOT-./www}:/var/www/html
- ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
- ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
- ${LOG_DIR-./logs/apache2}:/var/log/apache2
environment:
APACHE_DOCUMENT_ROOT: ${APACHE_DOCUMENT_ROOT-/var/www/html}
PMA_PORT: ${HOST_MACHINE_PMA_PORT}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
database:
build:
context: "./bin/${DATABASE}"
container_name: '${COMPOSE_PROJECT_NAME}-${DATABASE}'
restart: 'always'
ports:
- "127.0.0.1:${HOST_MACHINE_MYSQL_PORT}:3306"
volumes:
- ${MYSQL_INITDB_DIR-./config/initdb}:/docker-entrypoint-initdb.d
- ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
- ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: '${COMPOSE_PROJECT_NAME}-phpmyadmin'
links:
- database
environment:
PMA_HOST: database
PMA_PORT: 3306
PMA_USER: root
PMA_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
UPLOAD_LIMIT: ${UPLOAD_LIMIT}
MEMORY_LIMIT: ${MEMORY_LIMIT}
ports:
- '${HOST_MACHINE_PMA_PORT}:80'
volumes:
- /sessions
- ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/conf.d/php-phpmyadmin.ini
redis:
container_name: '${COMPOSE_PROJECT_NAME}-redis'
image: redis:latest
ports:
- "127.0.0.1:${HOST_MACHINE_REDIS_PORT}:6379"
Solution:
$dbHost="database";
$dbUser="docker";
$dbPass="docker";
$dbName="docker";
It turns out that docker for username and pwd was correct but the host name was incorrect.
I got that "docker" from the .env.
Seems you've already found the way to connect, but here's why it works that way:
You're using distinct docker containers for each service, which is correct, but that means your application isn't trying to connect locally (127.0.0.1) because the database isn't running on the same container.
Docker Compose networking allows hosts defined in the same docker-compose file to communicate with each other by using the host names which are defined as the service name (the next level under service), so in your case you have hosts webserver, database, phpmyadmin, and redis.
Simply updating the host name you're trying to connect to from 127.0.0.1 to database should be all you need to do.

docker-compose: Why does my webapp fail to connect to mysql?

I am trying to connect two running containers: (1) a webapp and (2) a mysql db using docker compose. I setup the belowmentioned docker-compose.yml but my webapp fails to open a socket to mysql. More specifically, this php #fsockopen function throws an error ( second case ). Is there something wrong with the docker-compose config?
Note: idoit is the name of the webapp
php case
if (#fsockopen($l_dbHost, $l_t, $t_errno, $t_errstr, 5)) {
$l_success = true;
$l_message = "CONNECTED";
$l_dbPort = $l_t;
} else {
$l_success = false;
$l_message = "ERROR (" . $t_errno . ")";
}
The docker-compose.yml
version: "3"
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: ****
MYSQL_DATABASE: ****
MYSQL_USER: ****
MYSQL_PASSWORD: ****
ports:
- '3306:3306'
expose:
- '3306'
volumes:
- db_data:/var/lib/mysql
idoit:
depends_on:
- db
build:
context: .
dockerfile: Dockerfile
ports:
- "8000:80"
restart: always
environment:
IDOIT_DB_HOST: db:3306
IDOIT_DB_USER: ****
IDOIT_DB_PASSWORD: ****
IDOIT_DB_NAME: ****
volumes:
db_data: {}
seems like idoit service doesn't have access to network with mysql database.
db service have
networks:
- backend
and idoit service - doesn't

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

Categories