0

I have setup the ubuntu server and my php code is establishing a database connection with mysql running on the same server. My credentials for the database are correct, crossed checked it thrice but i am unable to figure out what i a missing. So what i have done so far.

Make sure the mysql server is running.

  1. mysql -u root -h 127.0.0.1 -p // I can login into mysql server on ubuntu server.
  2. Run this command 'netstat -tulpen' and get this entry tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 107 20280 -

Error on apache when php is trying to access mysql:

PHP Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES)

Lastly my database credentials look like this:

// here I have tried 127.0.0.1:3306, localhost as well but it does not seem to work
$dbhost2 = 'localhost:3306'; 
$dbuser2 = 'root';
$dbpass2 = 'somepassword';
$dbname2 = 'my_db_name';

Updated: This is how i am connecting my php code is using the above variables to connect to the database

$DBCONN2 = @mysqli_connect($dbhost2, $dbuser2, $dbpass2, $dbname2) or die('Failed');

Updated 2: There is also another connection being used like this

$DBCONNi = new mysqli($dbhost2, $dbuser2, $dbpass2, $dbname2) or die('Failed');
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
Hassan Abbas
  • 1,166
  • 20
  • 47
  • try removing the port number from the credentials – Lelio Faieta Nov 08 '17 at 12:25
  • already checked with that, also mentioned this into my question. – Hassan Abbas Nov 08 '17 at 12:25
  • Sadly I can't help you, don't know where this is coming from, BUT this question should be an example on how-to-ask. Thats exactly what we want to see here. Nice question, showed us what you've tried so far, little code snipped. – Twinfriends Nov 08 '17 at 12:26
  • 1
    Can you show us the code that actually connects to the database? – M. Eriksson Nov 08 '17 at 12:26
  • 1
    The error message **does not** match the code provided. you are getting this error somewhere else. Voting to close as off topic. – Your Common Sense Nov 08 '17 at 12:35
  • I'm guessing that you're having more than one db connection in your code? One that uses the OOP-version of mysqli? That's the one that's failing. The above code simply can't produce that error message (wrong API and you're actually suppressing error messages and is using an `or die()`). If this logged an error, it would be _"**Warning:** mysqli_connect(): (HY000/1045)"_ instead of _"mysqli::__construct()_" – M. Eriksson Nov 08 '17 at 12:37
  • @YourCommonSense I didnt follow you? What kind of error are you expecting ?, This is the only error i am getting in apache logs. – Hassan Abbas Nov 08 '17 at 12:37
  • @MagnusEriksson, Nope this is the only piece of code that is establishing a db connection. – Hassan Abbas Nov 08 '17 at 12:42
  • 1
    Don't know what to tell you. I can't reproduce that error message using `mysqli_connect()`, only with `new Mysqli()`, so unless you have some own built version of PHP, I can't see how it's possible. – M. Eriksson Nov 08 '17 at 12:44
  • besides, given @ symbol in front of the function call, this code will give you only one word "Failed". – Your Common Sense Nov 08 '17 at 12:47
  • @MagnusEriksson, my bad sorry for the previous response. Yes there is one point where new Mysqli is used. And i think your right !. But even in that case what should be the solution ? – Hassan Abbas Nov 08 '17 at 12:50
  • How would we be able to answer that without seeing all the relevant code for that connection? It can literally be anything. – M. Eriksson Nov 08 '17 at 12:51
  • @MagnusEriksson, updated the question. – Hassan Abbas Nov 08 '17 at 12:53
  • Why do you have more than one connection? Have you tried using 127.0.0.1 on that one (_without_ the port number)? Have you checked that this connection have the correct credentials? echo the credentials to make sure you do. Also, remove the `or die()` from the OOP version. – M. Eriksson Nov 08 '17 at 12:59
  • Yes i got the problem, that connection was not using the right credentials. Thanks @MagnusEriksson – Hassan Abbas Nov 08 '17 at 13:02

3 Answers3

-3

Try with PDO https://github.com/fxstar/PhpJsCss/blob/master/PDOapi/_PDO_Class.php

    // Mysql connect    
function Conn(){
    try{
        // data from config file globals variables
        global $mysqlhost, $mysqluser, $mysqlpass, $mysqlport, $mysqldb;
        // pdo
        $conn = new PDO('mysql:host='.$mysqlhost.';port='.$mysqlport.';dbname='.$mysqldb.';charset=utf8', $mysqluser, $mysqlpass);
        // don't cache query
        $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        // show warning text
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
        // throw error exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        // don't colose connecion on script end
        $conn->setAttribute(PDO::ATTR_PERSISTENT, false);
        // set utf for connection utf8_general_ci or utf8_unicode_ci 
        $conn->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci'");
        // Buffered query
        // $conn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true);

        // PDO SSL
        // $conn = new PDO('mysql:host='.$mysqlhost.';port='.$mysqlport.';dbname='.$mysqldb.';charset=utf8', $mysqluser, $mysqlpass,array( PDO::MYSQL_ATTR_SSL_KEY    =>'/path/to/client-key.pem', PDO::MYSQL_ATTR_SSL_CERT=>'/path/to/client-cert.pem', PDO::MYSQL_ATTR_SSL_CA    =>'/path/to/ca-cert.pem'));
        // Or
        // $conn->setAttribute(PDO::MYSQL_ATTR_SSL_KEY => '/path/to/client-key.pem');
        // $conn->setAttribute(PDO::MYSQL_ATTR_SSL_CERT => '/path/to/client-cert.pem');
        // $conn->setAttribute(PDO::MYSQL_ATTR_SSL_CA => '/path/to/ca-cert.pem');           
        return $conn;
    }catch(Exception $e){
        echo "Mysql connection error!!!";
        return 0;
    }
    // $rows = $res->fetchAll(PDO::FETCH_ASSOC);
    // $cnt = $res->rowCount();
    // $id = $this->db->lastInsertId();
    // buffered query
    // $stmt = $db->prepare('select * from foo', array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true)
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alise
  • 1
  • 2
  • Thank you for your contribution. Unfortunately, this post is off topic. On a side note, your class has some issues. You may find my [article on the common database wrapper mistakes](https://phpdelusions.net/pdo/common_mistakes) interesting. – Your Common Sense Nov 08 '17 at 12:46
  • You don't know about you writing. – Alise Nov 08 '17 at 12:49
  • You're writing the same thing as everyone on the web from php help. – Alise Nov 08 '17 at 12:50
  • You probably do not understand the subject. – Alise Nov 08 '17 at 12:51
  • Please read the article. You will see I understands much more than you think. Honestly, that monster class you linked here should never exist. It violates all OOP principles at once – Your Common Sense Nov 08 '17 at 13:14
-3

mysql -u root -h 127.0.0.1 -p

...

$dbhost2 = 'localhost:3306';

In MySQL, 'localhost' is not a network address. 'localhost' is interpreted as meaning 'use the defined (AF_UNIX) filesystem socket, not a network socket'. In addition, the authentication for 'localhost' connections is handled separately from network connections.

The path to the filesystem socket is defined in 'my.cnf'. The location of my.cnf is defined when the relevant servers and clients are compiled, but is usually '/etc'

While I've not tried explicitly with mysqlnd, I would expect its behaviour to be consistent with clients using libmysql.

Change your PHP code to use '127.0.0.1' (specifying the default port number 3306 should be redundant)

Community
  • 1
  • 1
symcbean
  • 47,736
  • 6
  • 59
  • 94
-3

Try without password or try to add password to mysql root user. ANd see here Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES)

Alise
  • 1
  • 2