PHP not connecting to Remote MySQL [duplicate] - php

This question already has answers here:
php can't connect to mysql with error 13 (but command line can)
(3 answers)
Closed 3 years ago.
I need to connect to a remote MySQL server with PHP but I get the following error:
Warning: mysqli::mysqli(): (HY000/2003): Can't connect to MySQL server on ... (13) in /var/www/html/index.php on line 16
Warning: mysqli::query(): Couldn't fetch mysqli in /var/www/html/index.php on line 17
Fatal error: Call to a member function fetch_assoc() on a non-object in /var/www/html/index.php on line 18
The remote server firewal is open, user has permission to connect from any Host, and I'm able to connect from the local server with mysql command line, but not able to connect with PHP.
What may be the problem here?
EDIT:
Already tried with different connection code, but always same error.
This is the latest one:
$mysqli = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$result = $mysqli->query("SELECT 'Hello, dear MySQL user!' AS msg");
$row = $result->fetch_assoc();
echo $row['msg'];

Found this to be an issue with SELinux that is not allowing httpd network connections.
Executing:
setsebool -P httpd_can_network_connect=1
Solved the problem.

Make sure you inserted information In the connection info. Replace DB_DATABASE and other keywords with your database info in single quotes.
$mysqli = new mysqli('localhost', 'MyUsername', ''mypassword', 'database1');

Related

Warning: mysqli::__construct(): (HY000/2002): No such file or directory in [duplicate]

This question already has answers here:
Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in
(17 answers)
Closed 2 years ago.
I'm totally stuck having spent a couple of days trying to figure this out...
I have set up an Apache web server (version 2.4.34) on my Mac OS running Mojave (10.14.3) and have installed the current version of MySQL Community Server (8.0.15). Both running perfectly as I can determine (from command line). I have PHP 7.1.23 installed. When I create a .php file with the following code and put it in my Sites directory and then in Safari go to http://localhost/~dave/hello.php I get this error:
"Warning: mysqli::__construct(): (HY000/2002): No such file or
directory in /Users/dave/Sites/hello.php on line 7 Connection failed:
No such file or directory".
Code in hello.php:
<?php
$servername = "localhost";
$username = "dave";
$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 have tried everything I can think of, including those suggestions in the other two similar posts (this one: My database user exists, but I still get an (HY000/2002): No such file or directory and this one: PHP Warning: mysqli::__construct(): (HY000/2002): No such file or directory (Debian GNU/Linux 9)), and I cannot connect to MySQl with php. In particular, I checked with the php info page and the socket for mysqli is given as var/mysql/mysql.sock. In the relevant table in mysql the value for socket is given as /tmp/mysql.sock. I tried changing the php.ini.default file by adding each of these sockets (in place of the empty value) and neither worked (still generates the same error). I thought that the 2002 MySQL socket error might be the problem, so I made a symlink by doing this:
sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
but all that did was generate a new error:
Warning: mysqli::__construct(): The server requested authentication
method unknown to the client [caching_sha2_password] in
/Users/dave/Sites/hello.php on line 7
Warning: mysqli::__construct(): (HY000/2054): The server requested
authentication method unknown to the client in
/Users/dave/Sites/hello.php on line 7 Connection failed: The server
requested authentication method unknown to the client
So I removed the symlink...and returned to the original error.
I also tried replacing "localhost" with the IP address 127.0.0.1 as well as "localhost:3306" as other question threads have suggested. Nothing worked here either.
Please help, I am new to this kind of use of the computer/web design and I'm out of obvious (googleable/understandable) options.
Use 127.0.0.1 instead of localhost.
I got some help from some local tech guys in my area and it turns out that fixing the socket error was the first step, like I tried to do above. I eventually did it this way, using the solution by noun:
Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in
The second error message was then generated (same one as in my question, here again):
mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in /Users/dave/Sites/hello.php on line 7
Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in /Users/dave/Sites/hello.php on line 7
This was fixed by the SQL-based solution (June 9 2018 answer) to this question:
php mysqli_connect: authentication method unknown to the client [caching_sha2_password]
This SQL statement was simply run in MySQL from the terminal. After restarting the Apache server I reloaded the .php file in Safari and this resulted in a successful mysqli connection to MySQL with PHP. I also tried a PDO connection and this also worked.

PHP - Apache - MySQL Server | Could not connect to host

I'm facing this problem since yesterday evening. I have been now over 6 hours on Google, trying to figure out what the solution could be.
I have tried this, setting the timeout on all php.ini files to 300 or unlimited, I have tried setting the buffer of MySQL higher, tried to restart both Apache and MySQL server, ...
Problem: What I'm trying to do, where the error occurs, is to execute a SOAP-command (command is correct) $this -> soap -> executeCommand(new SoapParam($command, 'command'));, which results in:
"Could not connect to host" --> if I set define('DB_HOST', '127.0.0.1'); or
Problem 2:
//Warning: mysqli::mysqli() [function.mysqli-mysqli]: (HY000/2003): Can't connect to MySQL server on 'blacktempel.dyndns-home.com' (10060) in C:\Users\NAME\Desktop\Server_09122014\Apache\_Server\htdocs\SOAPRegistration.php on line 109
//Warning: mysqli::close() [function.mysqli-close]: Couldn't fetch mysqli in C:\Users\NAME\Desktop\Server_09122014\Apache\_Server\htdocs\SOAPRegistration.php on line 154
$this -> db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT); //Line 109
$this -> db -> close(); //Line 154
"Database connection failed: Can't connect to MySQL server on 'blacktempel.dyndns-home.com' (10060)" --> if I set define('DB_HOST', 'blacktempel.dyndns-home.com');.
My firewall is not the problem, I have checked this multiple times.
My hosts file contains this:
# 127.0.0.1 localhost
# ::1 localhost
MyExternalIp blacktempel.dyndns-home.com
SOAP:
define('SOAP_IP', '127.0.0.1');
define('SOAP_PORT', '7878');
MySQL is running on port 3306 and listening. My other defines, such as DB_USER, DB_PASS, are correct.
Q: Why won't it work ? What could be wrong ? Is there anything I could possibly have missed ?
Fixed the issue.
The SOAP problem was caused by a setting in the config file of the program accessed by SOAP.

Connection issue with mysqli

$db= mysqli_connect('localhost', "root", "root");
if(!$db) die("Error connecting to MySQL database.");
mysqli_select_db("onlineform", $db);
As I try to connect to the database like shown above, I am getting the following error:
Warning: mysqli_connect(): [2002] No such file or directory (trying to connect
via unix:///var/mysql/mysql.sock) in - on line 3 Warning: mysqli_connect():
(HY000/2002): No such file or directory in - on line 3 Error connecting
to MySQL database.
It's my first time using mysqli, I thought it would be a big improvement over mysql. I checked with MAMP if it's enabled and it is.
Any suggestions?
Check if your socket is correct. If not, override the default socket. I guess you have to try something like this in MAMP:
$link = mysql_connect(
':/Applications/MAMP/tmp/mysql/mysql.sock',
'root',
'root'
);

mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 1 year ago.
<?php
function getMysqlConnection($host, $user, $pass, $database) {
global $mysqli;
global $Mysqlerror;
$mysqli = new mysqli('$host', '$user', '$pass', '$database');
if (empty($mysqli->connect_errorno) == false) {
$Mysqlerror = "true";
} else {
$Mysqlerror = "false";
}
}
I have created a function as above which implement connection on given MySQL user credentials and I implemented this function by using
<?php
require 'myFunc.php';
getMysqlConnection("localhost", "wronguser", "wrongpass", " test");
echo $Mysqlerror;
Although i used wrong password and username the Mysqlerror is false . I also used or die function nothing happen
Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /opt/lampp/htdocs/mysite/myFunc.php on line 5
Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in /opt/lampp/htdocs/mysite/myFunc.php on line 5
I restarted lampp as usual it showed
Starting XAMPP for Linux 1.8.0...
XAMPP: Starting Apache with SSL (and PHP5)...
XAMPP: Starting MySQL...
Warning: World-writable config file '/opt/lampp/etc/my.cnf' is ignored
XAMPP: Starting ProFTPD...
/opt/lampp/share/lampp/alladdons: line 23: /opt/lampp/share/addons/: is a directory
XAMPP for Linux started.
Is the problem with my code or with my server some thing. How to fix it?
Problem is with this line.
$mysqli = new mysqli('$host','$user','$pass','$database');
Change it to
$mysqli = new mysqli($host,$user,$pass,$database);
In PHP variables within single quotes are not interpolated with their values.
Check PHP's variable parsing mechanism here
Check your mysql first..provide username and password correctly(wat u gave in mysql)..
sample database connection is available in w3schools.com and tizag.com

No connection could be made using mysql_* API [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
I tried to connect mysql database.but am getting the following error.
Warning: mysql_connect(): [2002] No connection could be made because the target machine actively (trying to connect via tcp://localhost:3306) in test.php on line 5
Warning: mysql_connect(): No connection could be made because the target machine actively refused it. in test.php on line 5 Warning: mysql_close() expects parameter 1 to be resource, boolean given in test.php on line 15
test.php:
<?php
$link = mysql_connect(localhost, dbuser, dbpass);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
This happened to us when running a PHP & MySQL on IIS7 in Windows Server 2008 in VirtualBox. It turns out that the Windows hosts file didn't have an IPv4 entry for localhost (127.0.0.1) so the route to MySQL on localhost was not found.
We added the following to the hosts file which resolved it:
127.0.0.1 localhost # Additional IPv4 entry
::1 localhost # Existing IPv6 localhost entry
Reference: A brief description of PHP/MySQL Localhost via IPv4 & IPv6
The MySQL server isn't running, or you have a firewall blocking port 3306.
This error message means the system did not accept the TCP connection request.
In my.ini find string:
bind-address = 127.0.0.1
Change from localhost to network IP.
Got the same message trying to connect to 'localhost' by mistake (on a Windows server). After fixing the connection string to the correct (remote) server the error disappeared.

Categories