简体   繁体   中英

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

I am switching my hosting platform from cPanel to Plesk and seem to be having some issues with a script on one of my sites. The main site is running on the latest version of Joomla and makes it's database connection with no errors. I have an external script that I run on a cron job however that does not seem to connect. I get this error:

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

I have to assume that it has to do with my connection class so I will post it here:

  1. I have a cofig.ini that contains the DB login credentials.

    [database] username = database_user password = ****** dbname = database_name

then my connection class is this:

<?php
//Database connection class
class Db {
  protected static $connection;
  public function connect() {    
    if(!isset(self::$connection)) {
      global $config;
      self::$connection = new mysqli('localhost:3306',$config['username'],$config['password'],$config['dbname']);
    }
    if(self::$connection === false) {
      return false;
    }
    return self::$connection;
  }
  public function query($query) {
    $connection = $this -> connect();
    $result = $connection -> query($query);
    return $result;
  }
  public function select($query) {
    $rows = array();
    $result = $this -> query($query);
    if($result === false) {
        return false;
    }
    while ($row = $result -> fetch_assoc()) {
        $rows[] = $row;
    }
    return $rows;
  }
  public function error() {
    $connection = $this -> connect();
    return $connection -> error;
  }
  public function quote($value) {
    $connection = $this -> connect();
    return "'" . $connection -> real_escape_string($value) . "'";
  }
}
?>

and then finally my script runs the query as such....

<?php
require_once('/var/www/vhosts/user/httpdocs/db.php');
$config = parse_ini_file('/var/www/vhosts/user/httpdocs/config.ini');

$db = new Db();
$rows = $db -> select('
    SELECT * 
    FROM milestones 
    WHERE status != "arrived" 
    AND departure_time BETWEEN (NOW() - INTERVAL 12 HOUR) 
    AND (NOW() + INTERVAL 12 HOUR) 
    AND type = "4"
');

?>

I am guessing that maybe there is a module not enabled that is prohibiting this script from establishing a connection properly, as like I said, the Joomla script seems to connect without issue using the exact same credentials?

What do you mean by "external script"?

By the output you gave I assume it is located on the same machine as the Joomla installation.

The problem most likely doesn't lie with your credentials (if they work for Joomla and you have double-checked them) but with the hostname you provide or a server configuration or MariaDB user (lfc_site) permissions.

You can try to change localhost in your code to 127.0.0.1 and see if it makes any difference.

If not, please SSH into your server and provide output of

  1. plesk db and then SELECT user, host FROM mysql.user WHERE user = 'lfc_site';
  2. and grep bind /etc/mysql/my.cnf

for further investigations.

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