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

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");

Related

Mysql Remote Connection Failed

Im trying with php to connect my remote server's mysql im getting this error:
"Database Connection Error: SQLSTATE[HY000] [2002] connection refused"
I made what said in that page : https://www.configserverfirewall.com/ubuntu-linux/enable-mysql-remote-access-ubuntu/
this is my setting now:
and ofcourse restarted mysql.
ANd its still saying same error "Database Connection Error: SQLSTATE[HY000] [2002] connection refused"
What can i do else?
this is my connection setting:
When i try port 3306 same error. What can cause that?
ps: also this is a remote connection from a hosting shared to my dedicated server
it was about cloudflare or other proxies. Fixed.

when I start apache MySQL server, I get error " (HY000/2002): No connection could be made because the target machine actively refused it."

after the start apache server, i could not access PHPMyAdmin. when i try to access i get an error and auto-stop MySQL server.
- mysqli_real_connect(): (HY000/2002): No connection could be made
because the target machine actively refused it.
- Connection for controluser as defined in your configuration failed.
mysqli_real_connect(): (HY000/2002): No connection could be made
because the target machine actively refused it.
- phpMyAdmin tried to connect to the MySQL server, and the server
rejected the connection..
how can i solve this error
Add below line under config.inc.php
$cfg['Servers'][$i]['port'] = 8111;
If you are on Windows os and the above solution does not work, just check if your mysql is running with "netstat -ano" command, if the services are not running just try starting the mysql service.

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

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.

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

Memcache connect throws error in cron job

When i try to connect to memcached server from a php page, it works without any problem.
Using this code
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
But when it tries to connect to server from a php script which is fired by cron job it throws this error
Warning: Memcache::connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/...../cron/acts_cron.php on line 3
Warning: Memcache::connect(): Can't connect to localhost:11211, php_network_getaddresses: getaddrinfo failed: Name or service not known (0) in /home/...../cron/acts_cron.php on line 3
Could not connect
What can cause this problem ?
Seems like you're missing an entry for localhost inside your hosts file. Try updating /etc/hosts and make sure that you got a line like the following in there:
127.0.0.1 localhost.localdomain localhost
using 127.0.0.1 instead of localhost fixed the issue.

Categories