PHP not seeing mysql.sock from the web - php

I have a script that connects to a MySQL database in localhost, when I run it from the console it runs just fine, but when I run it from the web, I get this:
Warning: mysqli_connect(): (HY000/2002): No such file or directory in /var/www/html/tests/mysql.php on line 3
Error: Unable to connect to MySQL. Debugging errno: 2002 Debugging error: No such file or directory
I checked my php.ini file and it has the correct location of mysql.sock, the file exist, but php won't connect via web.
Also, I have Fedora Linux with Firewall disabled as well as SELinux, so I have no idea what can be causing this.
This is the test code I'm using:
<?php
$link = mysqli_connect("localhost", "root", "password", "my_db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
Any ideas?

Related

Connecting to remote MySQL server using PHP7.3 and proxy

I am attempting to connect to a remote MySQL server from my local machine virtualhost using Proxifier Proxy (Socks5).
I am connect is working on Navicat, And using mysql -h 192.168.0.44 -u my_user -p -P port cmd is working too. Then I attempting using the following code:
$link = mysqli_connect('192.168.0.44', 'my_user', 'my_pwd', 'db_name', 'port');
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
} else {
echo 'Connected successfully';
}
exit();
My problem is that I am unable to connect locally using PHP, receiving the error:
mysqli_connect(): (HY000/2002): Operation timed out
It's connecting success on Navicat For Mysql And commands:

Encountering error 1045 while connecting php file to mysql database

I have been trying to connect my PHP file to MySQL database for a while, now I'm using WAMP server and encountering error 1045, I have also setup a password and tried various settings given on net.
this is the scenario
<?php
$link = mysqli_connect("127.0.0.1", "root", "password", "registration");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
here is the code I'm running, registration is the database,I have created it using phpmyadmin
<?php
$link = mysqli_connect("127.0.0.1", "root", "", "registration");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>

Can't connect to localhost MySQL server with PHP

I have Apache (with PHP) and MySQL installed and running on my Raspberry Pi. I've done some simple tests with PHP, and it seems to be working. And MySQL is working perfectly from the terminal, and even another computer.
I made this PHP file:
<?php
echo("Connecting");
$connection = new mysqli("127.0.0.1", "admin", "password", "test");
if ($connection->connect_error) {
die("Connection error");
}
echo("Connection successful");
?>
Yet when I go to this page in my web browser, all I see is "Connecting". If I comment out the connection command, I see "Connecting Connection successful" in the browser.
It seems as if the PHP code stops running or hangs at the connection command.
Any ideas why I'm having this strange behavior?
Try adding these lines at the top: error_reporting(E_ALL);
ini_set('display_errors',1);
I hope You should do following
if ($connection->connect_error) {
echo "Connection error";
}else{
echo "Connection successful";
}
Try this instead
<?php
$link = mysqli_connect("127.0.0.1", "admin", "password", "test");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>

websites ip address leading to different website

so i was connecting to my database using php's following code
$connect = mysqli_connect("websiteIP/domainName", "username", "password", "databasename");
if(!$connect){
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
and i got this error
Error: Unable to connect to MySQL.
Debugging errno: 1045
Debugging error: Access denied for user
'username'#'www.365casino.online' (using password: YES)
Now the real confusing past is www.365casino.online is not my domain but i don't know why is it showing and it is blocking me access to connect to my database.
P.S. i have checked and the issue is not of username or password or databasename.
So i found the answer myself, it's very simple just use localhost in place of your website Domain Name or IP address. like,
$connect = mysqli_connect("localhost", "username", "password", "databasename");

Connect db it shows error (Headers and client library minor version mismatch. Headers:50552 Library:50631)

When running below code it shows error like this
$link = mysqli_connect($server, $user, $password, $db);
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
Warning: mysqli_connect(): Headers and client library minor version mismatch. Headers:50552 Library:50631
I am using php 7.1.2

Categories