docker container can't connect to localhost mysql server - php

im new to docker, have tried to setup my docker php-apache container to connect to mysql db running on my localhost. i created my docker image as follows:
dockerfile:
FROM php:7.2-apache
RUN docker-php-ext-install mysqli
build new docker image:
docker build -t will-php-env .
my php application is placed in my home folder(/home/will/php/)
i run my container as follows:
sudo docker run -it -p 180:80 -v "$PWD":/var/www/html will_php_env
the code for connection is:
mysqli_real_connect( mysqli_init(),"192.168.1.17", "root", "password", "bugtracker" );
error:
Warning: mysqli_real_connect(): (HY000/1045): Access denied for user 'root'#'172.17.0.2' (using password: YES) in /var/www/html/mantisbt-2.19.0/will.php on line 3
by the way, i will be able to connect to mysql server from the same container if i use the following code:
<?php
$will2k = new mysqli("192.168.1.17", "gkeepa", "password", "php");
please let me know why the container is not able to connect to mysql server on my localhost using mysqli_real_connect. thanks in advance

Related

connection failed between two linked containers : apache2/php & mysql

for my two containers:
Container 1:
- name: boxoffice
- containerPort : 80
- hostPort: 3030
- service installed : apache2 and php
command run container 1:
docker run --name=boxoffice -p 3030:80 -v /Users/mac/Desktop/storage-web:/var/www/app -d --link=db medoneapache:latest
Container 2:
- name: db
- containerPort : 3306
- hostPort: 3306
- service installed : mysql
command run container 2:
docker run --name=db -p 3306:3306 -v /Users/mac/Desktop/storage-db:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=medone -d mysqlmedone:1.0
i linked the first container "boxoffice" to the second container "db" via --link=db
and i create a php file to test the mysql connection that having a database called : profil
connection-php-mysql.php (/var/www/app) in "boxoffice" container:
<?php
$servername = "db";
$username = "root";
$password = "medone";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Here is the error that i got :
Warning: mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password] in /var/www/app/index.php on line 7
Warning: mysqli_connect(): (HY000/2054): The server requested authentication method unknown to the client in /var/www/app/index.php on line 7
Connection failed: The server requested authentication method unknown to the client
i inspected the first container to get the host ip address : 172.17.0.2 and replaced it with db as servername but i got the same error
and i can't get the username as root getting the error:
mysql -u root
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
You need to upgrade to PHP 7.4.4.
The support for caching_sha2_password has been added both in mysqli and PDO only in PHP 7.4.4.
The relevant feature-request: https://bugs.php.net/bug.php?id=79275
The GitHub PR: https://github.com/php/php-src/pull/5210

Docker with PHP and MySql - $servername Error [duplicate]

This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(41 answers)
Closed 5 years ago.
I have a project developed with PHP and MySql.
When I Use the connection to DB I set the connection as a follow:
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];
/* -------------------------------- CONNESSIONE -------------------------------------- */
$servername = "localhosto";
$username_db = "xxxxxx";
$password_db = "yyyyyy";
$db = "zzzzzz";
try {
$conn = new PDO("mysql:host=$servername;dbname=$db", $username_db, $password_db);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//echo "Connected successfully<br/><br/>";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage()."<br/><br/>";
}
the project run correctly and connect to MySql without problems.
Next step I was created a Docker Image with a follow docker file:
FROM php:7.0-apache
RUN apt-get update \
&& apt-get install -y --no-install-recommends libpq-dev \
&& docker-php-ext-install mysqli pdo_pgsql pdo_mysql
COPY / /var/www/html
EXPOSE 80
I have create the image and container:
docker build -t mysoftware C:\Users\mysoftware
docker run -d -p 5000:80 mysoftware
but the Php application don't connect to MySql.
I need to change the connection with the name of server:
$servername = "SERVER_NAME";
Why I have an error if the app run correcly without container ??
When running inside the container, localhost will not resolve to the host machine, rather it will refer to the container IP address.
You need to connect to the host machine from within the container. For that check: From inside of a Docker container, how do I connect to the localhost of the machine?
(Assuming *nix) Once you have the mysql docker container running...
docker ps
and find the name of the mysql container that is running. Then to find the IP address...
docker inspect <name_of_container> | grep IPAddress
Then use this IP address for the server to connect to.

Docker Container Connecting To Host MySQL

I was following along a docker tutorial that used a container for MySQL. That worked, but I am trying to connect to my host's MySQL from a php/apache container.
I created a database called 'weather', a user 'admin'#'%' and granted the user all privileges to the weather database. I also created the necessary table/columns.
However, I keep getting the following message:
Warning: mysqli::__construct(): (HY000/2002): Connection refused in /var/www/html/index.php on line 15
Failed to connect to MySQL: Connection refused
Here's where the php app inside the container fails the connection:
<?php require 'vendor/autoload.php';
// Create a new Container
$container = new \Slim\Container([
// Add Guzzle as 'http'
'http' => function () {
return new GuzzleHttp\Client();
},
// Add mysqli as 'mysql'
'mysql' => function () {
$mysqli = new mysqli(
getenv('DATABASE_HOST'),
getenv('DATABASE_USER'),
getenv('DATABASE_PASSWORD'),
getenv('DATABASE_NAME')
);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
exit;
} else {
return $mysqli;
}
},
]);
I installed all the dependencies with composer and bound my host's ip address (172.17.0.1) in my.cnf. Here's the docker command I'm running:
docker run -d --rm --name=weather-app -p 38000:80 -v \
$(pwd):/var/www/html \
-e DATABASE_HOST='172.17.0.1' \
-e DATABASE_USER='admin' \
-e DATABASE_PASSWORD='password' \
-e DATABASE_NAME='weather' \
shiphp/weather-app
Here's the git repo if more info is needed: https://github.com/shiphp/weather-app.
Thanks in advance.
EDIT:
I figured out what's going on. I forgot to add the [mysqld] header in my.cnf when I bound the address. It works now.

Fail to connect mysql in running wordpress docker

I used the script to start mysql and wordpress through docker. (https://github.com/lamuguo/wordpress-setup/blob/master/start_wordpress.sh)
The xfguo-wp image is built based on docker official wordpress 4.3.1 repository: https://github.com/docker-library/wordpress/commit/4823a04099579f2aafb118ae8177449425cc84d2
Command to build the image: (under apache directory)
~/github/wordpress/apache$ docker build -t xfguo-wp .
However, I can't connect to mysql DB successfully, error below by "docker logs techmeetup-wordpress"
...
WordPress not found in /var/www/html - copying now...
Complete! WordPress has been successfully copied to /var/www/html
MySQL Connection Error: (2002) Connection refused
Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 10
Any thoughts? Feel free to comment in the code of my wordpress-setup repository.
Thanks!
Your shell script doesn't wait for the mysql container to initialise.
Try putting
sleep 10
in your shell script (start_wordpress.sh) after starting mysql and before starting wordpress. This will introduce a delay of 10 seconds before running the wordpress container, allowing it to connect to mysql.

Moving a SIte from Drupal to Raw PHP ... Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I don't understand this the error,
Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I am not familiar with drupal or any frameworks. Please advice me about this error and if I want to host my raw php site which I created in local WAMP server and connect to the database through, ...
#Database Connection
$dbc = mysqli_connect('localhost','****user', '***pw', '***db') OR die('Error: '.mysqli_connect_error());
what should I do?
Check whether mysql is running with the following command:
mysqladmin -u root -p status
And if not try changing your permission to mysql folder. If you are working locally, you can try:
sudo chmod -R 755 /var/lib/mysql/
Reference Connect to Server

Categories