Mysqli Error 2002: no such file or directory [duplicate] - php

This question already has answers here:
Celery workers unable to connect to redis on docker instances
(3 answers)
Closed 5 years ago.
I am trying to run nginx, php, mysql together with docker.
When i run docker-compose, everything looks fine, but when i try to make a connection to mysql via php code in my index page i'm getting this error.
Warning: mysqli::__construct(): (HY000/2002): No such file or directory in /code/index.php on line 8
Connection failed: No such file or directory
I have tried changing localhost to 127.0.0.1 and both with :3306 after it.
When changing localhost --> 127.0.0.1, i'm getting another connection failed statement:
Warning: mysqli::__construct(): (HY000/2002): Connection refused in /code/index.php on line 8
Connection failed: Connection refused
php code:
<?php
$servername = "localhost";
$username = "root";
$password = "toor";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
echo phpinfo();
?>
Also when i look at phpinfo();, there is no value given to mysqli.default_socket.
when going into the mysql image after starting docker-compose, i'm able to go into mysql shell and execute commands just fine.
Any help is appreciated!

Read my answer from this other question. Localhost doesn't resolve like you think it should inside a container.
When you use a docker-compose.yml, you can reference the other services by their service name as specified in the docker-compose.yml. Your hostnames are not localhost, rather they are web, php, and mysql. So in your php code, you should use $servername = "mysql"; instead of $servername = "localhost";

Related

No connection could be made because the target machine actively refused it. PHP error [duplicate]

This question already has answers here:
mysql_connect(): No connection could be made because the target machine actively refused it
(12 answers)
Closed last month.
I was trying to make a simple database to write and read from and on pretty much one of the first steps I encountered a problem: it would not connect. I got the error message: "No connection could be made because the target machine actively refused it.". I looked at other stack overflow questions and they all had complex solutions involving changing the code of some things. I tried some, and nothing changed. I am using USBWebServer and phpMyAdmin. This is the index.php code that failed:
$servername = "127.0.0.1";
$username = "root";
$password = "usbw";
$databasetouse = "work times web database";
// Create connection
$mysqli = new mysqli($servername, $username, $password, $databasetouse);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
echo "Connected successfully";
I tried changing $servername to "localhost" and nothing changed. When I first tried to log in to phpmyadmin it showed that my password was usbw and my username was root.
Maybe you should start your Apache and MySQL server through Xampp control panel and then try to connect.
I faced this error when I was learning laravel. Actually this happens when you database server is not being started.

Not able to make connection to mysql 8 in php

I am trying to connect to mysql server using php but it gives the following error
Connection failed: The server requested authentication method unknown to the client
mysql server version is 8.0.12 and php version is 7.2.9.
My code connect to mysql server
<?php
$servername = "127.0.0.1";
$username = "root";
$password = "mypassword";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
This question has been previously asked before here but the solution did not work for me so I am asking this again.
Thanks
edit: I re-installed php now it gives the following for the same code
This page isn’t working
127.0.0.1 is currently unable to handle this request.
HTTP ERROR 500
I had a similar problem and found that you can change mysql authentication by running the following code via Workbench
ALTER USER root#localhost IDENTIFIED WITH mysql_native_password BY 'new-password-here'

"Warning: mysqli::mysqli() [mysqli.mysqli]: [2002] No connection could be made because the target machine actively", when running on xampp [duplicate]

This question already has answers here:
mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket 'MySQL' (2)
(5 answers)
No connection could be made because the target machine actively refused it
(10 answers)
Closed 4 years ago.
I am getting this error. dont know why. just installed xampp. my code is:
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
I got the answer. I just had to keep the mysql_start.bat file open while using sql.

PHP remote connection on live is not working [duplicate]

This question already has answers here:
PHP Warning: mysqli_connect(): (HY000/2002): Connection refused
(8 answers)
Closed 4 years ago.
I am trying to access remote MySQL server by its IP using PHP.
Here is my code:
$servername = "100.XXX.XXX.XXX:3306"; // MySQL IP
$username = "root";
$password = "Password012";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "success";
}
This is working fine on my local machine but not on live.
I am getting this error:
Warning: mysqli::mysqli(): (HY000/2002): Connection refused in [...]
Any suggestions?
You need to allow remote connection on your mysql server.
To do that, execute the following command in your mysql server. Please change the databasename and username from the following command, before execute the command
GRANT ALL PRIVILEGES ON databasename . * TO 'username'#'%';

mysqli_connect(): (HY000/2002) failed to connect, remote server [duplicate]

This question already has an answer here:
SQLSTATE[HY000] [2002] A connection attempt failed.. - When attempting to connect from Local to remote server
(1 answer)
Closed 3 years ago.
I am trying to connect to a remote server database using PHP code (mysqli)
$host = 'myIP:3306';
$user = 'root';
$pass = 'pass';
$conn = mysqli_connect("$host", "$user", "$pass");
//check connection
if (mysqli_connect_errno())
{
echo "Error: " . mysqli_connect_error();
}
I used myIP/phpmyadmin to make a database and i used putty to check if mysql was running correctly. I also checked the default port for mysql on my server, which was the default (3306).
The error given is described as either a connection fail/time-out/no response.
I also tried using PDO instead of mysqli but it gave me the same results. All other examples i could find where about localhosts and the solution given in those posts is using the socketpath to connect (which doesn't seem like a solution for me as it's remote..).
It's a linux server provided by school.
Any ideas? Thanks in advance.
With mysqli_connect(), you can use 6 parameters:
host, username, password, dbname, port, socket
Host and Port are separate.
Try using:
$conn = mysqli_connect("myIP", "$user", "$pass", "dbname", "3306");
Also, the default port is 3306 so you probably do not even have to provide it.

Categories