PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 1, 2022

[FIXED] Connection failed: Connection refused mysql (LAMP)

 March 01, 2022     connection, lamp, mysql, php     No comments   

Issue

When I try to connect to mysql it refuses my script. I have mysql installed on ubuntu 16.10 (LAMP).

When I run this:

<?php
$servername = "ip";
$username = "user";
$password = "pw";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

It shows:

Connection failed: Connection refused mysql (LAMP)

I tried to change the bind-address to 0.0.0.0, then it showed:

Connection failed: Host 'webserver1' is not allowed to connect to this MySQL server

I tried to comment (#) the bind-address. That didn't work either.

I am running the scripts on a client with another ip-range/subnet then the mysql-server. When I run phpmyadmin on the client I have no problem.


Solution

Its possible to have a security precaution. You could try adding a new administrator account in mysql.

mysql> CREATE USER 'yourUser'@'localhost' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'yourUser'@'localhost'
    ->     WITH GRANT OPTION;
mysql> CREATE USER 'yourUser'@'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'yourUser'@'%'
    ->     WITH GRANT OPTION;

Try newly created user in your code.

Hope it helps!



Answered By - Shashank Shah
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing