0

My php page and mysql server are in same server. I can connect database using local IP , it works. but, when trying to connect with localhost it show the following error

Connection failed: Access denied for user 'root'@'localhost'

changed mysqld.cnf file as BIND ADDRESS = 0.0.0.0

I gave the following permission GRANT ALL PRIVILEGES ON . TO root@localhost IDENTIFIED BY 'password' WITH GRANT OPTION; GRANT ALL PRIVILEGES ON . TO root@my local IP IDENTIFIED BY 'password' WITH GRANT OPTION;

I don't know what is problem in my code and configuration ...

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database="db";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error){
    die("Connection failed: "  . mysqli_connect_error());
} else {
echo 'connected successfully';die;
}
mysqli_close($conn);
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Arun Kumar
  • 495
  • 9
  • 29
  • 1
    maybe this can help you : https://stackoverflow.com/questions/1708826/how-to-get-all-privileges-back-to-the-root-user-in-mysql – Niv Apo Jan 11 '18 at 14:57
  • @NivApo when i give **/usr/sbin/mysqld** this command no respose I get, and when using this command **grant all privileges on *.* to 'root'@'localhost' with grant option** the error no grand command appears – Arun Kumar Jan 11 '18 at 15:11

1 Answers1

2

Have you tried servername 127.0.0.1?

From the mysql manual:

On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server.1

Jürgen K.
  • 21
  • 2