SQLSTATE [28000] [1045]访问被拒绝用户'www-data'@'localhost'(使用密码:NO)

I am receiving the following error:

SQLSTATE[28000] [1045] Access denied for user 'www-data'@'localhost' (using password: NO)

I only get this on my production server. On my localhost I do not receive the error. The logins for the user is the same on both. The code is just pushed to the server. I have the username and password both supplied. Same databases and both trying to connect to 'localhost'. Both servers run PHP 5.6

There is no difference between the production and test server, besides the fact I am only getting the error on my production server. I have restarted and re-added the database and user to the MySQL server.

I would please like help on this as I have not found someone have this same issue, usually it is someone who forgot something or wrote 'bad code'. Well, this is different since mine 'sometimes' works, just not on my production server.

Code in my class file:

public function connect($hostname, $database, $username, $password, $port = 3306)
    {
        try {
            $this->db = new PDO("mysql:dbname=$database;host=$hostname;port=$port;charset=utf8", $username, $password);
        } catch (PDOException $e){
            print "MySQL Error:" . $e->getMessage() . "<br>";
            die('');
        }
    }

Code in the php file causing the error

$db = new db;
$db->connect('localhost', 'DATABASE', 'USER', 'PASS');