简体   繁体   中英

PHP: can connect in terminal access denied in browser (Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'root'@'localhost'

I have a PHP script that connects to a database, selects rows from a table:

db.php :

    function connect()  {
        try {
            $this->connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

            if ($this->connection->connect_error) {
                die("\nConnection failed: " . $this->connection->connect_error);
            } else {
                print "\nConnected successfully\n";
            }
        } catch (Exception $e)  {
            echo 'Exception: ' . $e -> getMessage();
            die("\nConnection failed: " . $this->connection->connect_error);
        }
    }

It works without fail when I run it in terminal - connects and retrieves all the data from the table.

However when I copy the file to a /opt/lampp/htdocs/myapp and open it, I get the error:

Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in /opt/lampp/htdocs/myapp/db.php on line 15
Connection failed: Access denied for user 'root'@'localhost' (using password: YES) 
Warning: mysqli::close(): Couldn't fetch mysqli in /opt/lampp/htdocs/myapp/db.php on line 61

I don't know how to fix it. I tried to add 192.168.1.1 to /etc/hosts file. Now it looks like this:

127.0.0.1   localhost
127.0.1.1   parsecer
192.168.1.1     parsecer

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

I tried commenting out the bind-address line in /etc/mysql/mysql.conf.d/mysqld.cnf :

...
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address           = 127.0.0.1
#mysqlx-bind-address    = 127.0.0.1
...

However I still get that error...

EDIT : I managed to replicate the access denied error inside Terminal (IDE) by using localhost:3306 instead of just localhost as a host name...

EDIT : I tried running like this:

 $this->connection = new mysqli("localhost", "root", "correctpassword", "stuff", 3306);
  1. In Terminal (IDE) it WORKS
  2. In browser - access denied error

I also have this in the mysqld.cnf file:

[mysqld]
skip-grant-tables

EDIT :

select user, host from mysql.user

gives this:

在此处输入图像描述

EDIT :

I created a new user in MySQL, gave it all privileges, the code works with the new user in Terminal doesn't in browser.

EDIT :

I turned off MySQL in XAMPP panel, but MySQL still works in terminal, so I think there are two different MySQLs on my PC.

Now the code works in terminal but in browser gives

Warning: mysqli::__construct(): (HY000/2002): No such file or directory in /opt/lampp/htdocs/stuff/db.php on line 16
Connection failed: No such file or directory
Warning: mysqli::close(): Couldn't fetch mysqli in /opt/lampp/htdocs/stuff/db.php on line 67

I also added

bind-address = 0.0.0.0

to /etc/mysql/mysql.conf.d/mysqld.cnf

UPDATE WITH ANSWER :

A user @Shadow guessed correctly that I had two versions of Postgres running simultaneously. One I installed manually and the other was installed with a XAMPP package.

I removed the whole XAMPP package, manually installed Apache server (which was part of the XAMPP as well), and then, having only one Postgres and one Apache manually (using terminal) run both and the code worked!

A user @Shadow guessed correctly that I had two versions of Postgres running simultaneously. One I installed manually and the other was installed with a XAMPP package.

I removed the whole XAMPP package, manually installed Apache server (which was part of the XAMPP as well), and then, having only one Postgres and one Apache manually (using terminal) run both and the code worked!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM