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'#'%';
Related
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'
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.
Hi so im trying to do a conection betwen my Apache server ( Xampp instalation ) with a remotely machine with a Mysql Server.
My php code :
<?php
$servername = '10.4.41.164:3306';
$username = 'admin';
$password = 'admin';
$db = 'AssistMe';
echo "Pepito";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
echo "Connected succesfully";
}
?>
The error that i get :
Warning: mysqli_connect(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a
period of time, or established connection failed because connected
host has failed to respond
So before puting my question here i chequed that:
If i can do a ping to the server :
Ping to the server
I added a user in mysql that allows conections from anywhere : User in Mysql
I also allow the firewall of the server to connect from the 3306 port : Firewall of the server
And the last test was to Using Mysql WorkBench and testing if i can connect to my Mysql : Mysql WorkBench
So i dont know why i cant connect to my Mysql BDD remotly from my PHP code.
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";
I can connect to the remote MySQL database from the MySQL Workbench using these credentials but when I try to connect using the php script I keep getting a connection timed out error. The MySQL server has its bind address already changed, it is running on a Linode server so I am not sure if that changes anything.
I'm using the php connect test script for MySQLi from W3 Schools.
<?php
$servername = "remote-db-IP";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
The exact error I get is
Warning: mysqli::mysqli(): (HY000/2002): Connection timed out in
/home/glavxfrw/public_html/db-connect-test2.php on line 7 Connection
failed: Connection timed out