[英]Error: “SQLSTATE[28000] [1045] Access denied for user” on cPanel SQL database
This seems to be a highly covered issue. 这似乎是一个高度覆盖的问题。 However majority of the threads I have seen have been local connection issues, or people saying make sure the user name and password are correct, which I have done.
但是,我见过的大多数线程都是本地连接问题,或者有人说要确保用户名和密码正确,这已经完成了。
I am hosting my first website with a database on name cheap and have imported the database with cPanel and myPHPAdmin. 我使用名称便宜的数据库托管我的第一个网站,并使用cPanel和myPHPAdmin导入了数据库。 However when I load my website I get a PHP error of " SQLSTATE[28000] [1045] Access denied for user 'cPanelUsername_dbUsername'@'server.web-hosting.com' (using password: YES)".
但是,当我加载网站时,出现PHP错误“ SQLSTATE [28000] [1045]用户'cPanelUsername_dbUsername'@'server.web-hosting.com'拒绝访问(使用密码:是)”。 I am using this code to connect.
我正在使用此代码进行连接。
private $DB;
public function __construct() {
$db = 'mysql:dbname=cPanelUsername_Database; charset=utf8; host=server_ip';
$user = "cPanelUsername_DatabaseUsername";
$password = "password";
try {
$this->DB = new PDO ( $db, $user, $password );
$this->DB->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
echo ('Error establishing Connection: '.$e->getMessage());
exit ();
}
}
All permissions are also granted to the user and added to the database. 所有权限也都授予用户并添加到数据库中。 I have created multiple users and all result in the error.
我创建了多个用户,所有这些都导致错误。
If there's no error in login/password, you should ask the question to your hosting provider. 如果登录名/密码没有错误,则应向托管服务提供商询问该问题。 Because it's absolutely unambiguous connection error.
因为这是绝对明确的连接错误。
Try to re-check a host. 尝试重新检查主机。 In your case the host is 'server_ip', but in error is 'server.web-hosting.com'.
在您的情况下,主机是“ server_ip”,但错误的是“ server.web-hosting.com”。
If you are running your code from your localhost (your computer). 如果您从本地主机(您的计算机)运行代码。 Make sure you are not not putting "localhost" or "127.0.0.1" on the "host" field.
确保没有在“主机”字段上放置“ localhost”或“ 127.0.0.1”。
Instead write the actual public ip address of your remote server. 而是写出您的远程服务器的实际公共IP地址。
$db = 'mysql:dbname=cPanelUsername_Database; charset=utf8; host=remote_server_public_ip';
Then go to CPanel and find "Remote MySQL" option that is under the "DATABASES" section and add your public ip address (public ip address of your home/office) to the list. 然后转到CPanel并在“数据库”部分下找到“远程MySQL”选项,然后将您的公共IP地址(家庭/办公室的公共IP地址)添加到列表中。
Once you upload your code to your remote server then you can change the "host" field to "127.0.0.1" or "localhost". 将代码上传到远程服务器后,可以将“主机”字段更改为“ 127.0.0.1”或“本地主机”。
I know it sounds obvious but that just gave me a hard time for about 10 mins. 我知道这听起来很明显,但这给了我大约10分钟的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.