Warning: mysqli_connect(): (HY000/2002): Connection refused in - php

Cannot figure out why I am getting this error?
Warning: mysqli_connect(): (HY000/2002): Connection refused in
Here the code:
$con = mysqli_connect('localhost:3306', '********', '********');
mysqli_select_db ($con, "id9873966_search") or die mysqli_error($con));

If you're using the standard port 3306 you should just be able to write
$con = mysqli_connect("localhost", "username", "password", "id9873966_search");
Assuming you actually have a db named id9873966_search
Additionally, if you're on Windows PC you can open CMD as admin and run
netstat -a -b
This will generate a list of services listening on what ports so you can verify you are in fact using port 3306 on localhost. You can also open "Services" in Windows and ensure that your DB is installed and listening.
It may also be necessary to add a firewall rule on you machine.

Related

Warning: mysqli::mysqli(): (HY000/2002): Connection timed out in /opt/lampp/htdocs/

I use ubuntu 18 server by google cloud instance
I installed xammp, status is:
sudo /opt/lampp/lampp status
Version: XAMPP for Linux 5.6.15-1
Apache is running.
MySQL is running.
ProFTPD is running.
I send request http post by my angular application from localhost to this server.
It's not working.
this server application is PHP and mySql
when i browser the url server, i get error:
Warning: mysqli::mysqli(): (HY000/2002): Connection timed out in
/opt/lampp/htdocs/dashboard/models/sql.php on line 12 Connection
failed: Connection timed out
sql.php (line 12):
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
when I run those from localhost by xammp. it's working.
Thank!

MySQL Error mysqli::__construct(): (HY000/2002): Connection refused

I'm trying to run a PHP Script that contains a MySQL connection, when I run the command I get the following error in the terminal:
'Warning: mysqli::__construct(): (HY000/2002): Connection refused'
Even with PDO, I get the error:
'Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in ...'
I'm running the script on Mac OS X, and I have MAMP as APACHE/MySQL local server.
The command that I use for running the script is:
'php main.php'
and the content of the 'main.php' file with PDO is:
$db = new PDO('mysql:host=localhost;dbname=test',root,root);
with mysqli connector:
$db = new mysqli("localhost", "test", "root", "root");
I tried with '127.0.0.1' instead of 'localhost' and I get the same errors in both cases.
First of all, using localhost as a domain name is not supported in PHP. It will not be resolved as 127.0.0.1.
Second, even using 127.0.0.1 might not work in servers with multiple interfaces or with special routing rules. Run mysqlcheck --help and take the IP address shown at the bottom in the "host" field. For example:
port 3306
host 192.168.1.23
Use the IP and port that MySQL recognizes:
$db = new mysqli("192.168.1.23:3306", "test", "root", "root");

Warning: mysqli_connect(): (HY000/1049): Unknown database in mac terminal only

in mac i want to create cronjob but when i run the php file with database i am getting error in terminal.
Warning: mysqli_connect(): (HY000/1049): Unknown database
And In the browser run perfect.
check your port that mysql run .
in many cases mysql run in port 3306 but in my mysql it run in port 3308
and I add this:
$conn = new mysqli("localhost", "root", "", "myDB","3308");
I had a similar problem. My code was like this:
$conn=mysqli_connect($servername,$username,$password,$dB) or die("connection failed");
echo "connection success";
Problem: I did not indicate the port number.
Solution: I just included the port number, like below:
$conn=mysqli_connect($servername,$username,$password,$db,"3308") or die("connection failed");
echo "connection success";
Is your PHP environment the same as your browsers PHP? command line php.ini can differ from, for example, your XAMP or other installed webserver
I would recommend checking this first
for me this did the trick
edit your .bash_profile file like
export PATH=/Applications/MAMP/bin/php/php7.1.1/bin:$PATH
edit the path to your PHP bin from your XAMP
If you have this code:
$conn = new mysqli("localhost", "root", "", "myDB");
Try with creating new database called myDB in localhost/phpmyadmin.
In my case I got similar error
Warning: mysqli_connect(): (HY000/1049): Unknown database 'login.db' in D:\xampp\htdocs\login\functions\db.php on line 2
so i change [$con=mysqli_connect('localhost', 'root', '', 'login.db');] into
[$con=mysqli_connect('localhost', 'root', '', 'login');]
Ii changed 'login.db' to 'login' and error disappear
For me, the problem was the "Port number" (on Step 3 of OpenCart 3.0.3.2 installation) which was set as 3306 but in MySQL, it was showing as 3308 so I just changed the same and all went good.
enter image description here

How to connect to a database (unknown host, no ip)

I have a database hosted by one.com with these settings:
PhpMyAdmin: https://dbadmin.one.com
Host: axelerate.be.mysql
Database: axelerate_be
Username: axelerate_be
Password: *****
I want to make connection with this database by using a php file runned by the LXTerminal of the raspberry pi. When i try to make connection i get this error (host unknown):
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo
failed: Host is onbekend. in C:\xampp\htdocs\twitter\index.php on line
3
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses:
getaddrinfo failed: Host is onbekend. in
C:\xampp\htdocs\twitter\index.php on line 3 Failed to connect to
MySQL: php_network_getaddresses: getaddrinfo failed: Host is onbekend.
This is my code:
<?php
// Create connection
$con=mysqli_connect("axelerate.be.mysql","axelerate_be","*******","axelerate_be");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo "it works!";
}
?>
My question:
How can i make this connection work without edditing any config files? (so if there is something to do, I want only to write in the php file)
Thanks!
The .mysql based hosts are internal hosts within the one.com network that resolves to the server that hosts your database. These hosts are not meant for connections outside of one.com hosting, and probably doesn't accept connections from the world if they're even available on a public IP.
Make sure the host (axelerate.be.mysql) exists, try to ping it. AS i can see it doesnt exist.
One.com doesn't support mysqli. You have to use
$db=mysql_connect("example.com.mysql", "username", "password");
mysql_select_db("database", $db);

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