简体   繁体   English

PHP 无法连接到 MySQL 数据库访问被拒绝

[英]PHP can't connect to the MySQL Database Access denied

I get this error message我收到此错误消息

Access denied for user 'user'@'localhost' (using password: YES)用户 'user'@'localhost' 访问被拒绝(使用密码:YES)

I tried to access with the root, the admin account and some user account I made for the web visitors with a few privileges.我尝试使用 root、管理员帐户和我为具有一些权限的网络访问者创建的一些用户帐户进行访问。 Those users (admin and web user) were created with cPanel.这些用户(管理员和网络用户)是使用 cPanel 创建的。

I'm testing the connection with this simple code to avoid making a mess with functional code.我正在测试与这个简单代码的连接,以避免弄乱功能代码。

<?php
$con = mysql_connect("localhost","root","");
  
if (!$con)
{

die('Could not connect: ' . mysql_error());

}

 ?>

I stumbled with this explanation but it doesn't work either我偶然发现了这个解释,但它也不起作用

Your cPanel username and password can be used to connect to your databases (as well as your cPanel).您的 cPanel 用户名和密码可用于连接到您的数据库(以及您的 cPanel)。 If you're connecting to your database using your cPanel username and password, you can reset your cPanel password to ensure you are using the correct username and password.如果您使用 cPanel 用户名和密码连接到数据库,则可以重置 cPanel 密码以确保使用正确的用户名和密码。

If you setup a MySQL username and password specifically for accessing a database, you'll want to ensure you are using the correct username in your php scripts.如果您专门为访问数据库设置了 MySQL 用户名和密码,您需要确保在 php 脚本中使用正确的用户名。 For example, MySQL usernames are always in this format:例如,MySQL 用户名始终采用以下格式:

cpanel-username_mysql-username cpanel-用户名_mysql-用户名

If your cPanel username is userna5 and you created a database username of dbuser1, then the actual database username would be:如果您的 cPanel 用户名是 userna5 并且您创建的数据库用户名是 dbuser1,那么实际的数据库用户名将是:

userna5_dbuser1 userna5_dbuser1

Edit: i already assigned the users to a database and their privileges.编辑:我已经将用户分配给数据库及其权限。 I'm working on a remote server我在远程服务器上工作

You need to grant permissions to the user@host in mysql 您需要向mysql中的user @ host授予权限

grant all privileges on YOURDB.* to
'YOURUSER'@'localhost' identified by 'YOURPASSWORD';

I faced the exact same issue on one of my employees' Windows PC.我在我的一名员工的 Windows PC 上遇到了完全相同的问题。 The issue was caused duo the fact that his private repositories folders including those who were used by Docker were located in C:\\Users{user_name} folder.造成这个问题的原因是他的私有存储库文件夹(包括 Docker 使用的那些文件夹)位于 C:\\Users{user_name} 文件夹中。 At some point in time, Windows prevented Docker's access to these folders.在某个时间点,Windows 阻止了 Docker 访问这些文件夹。

Solution: relocation the folders outside the Users folder solved the issue.解决方案:重新定位用户文件夹之外的文件夹解决了该问题。

Are you running the code on your local machine or on the remote server.您是在本地机器上还是在远程服务器上运行代码。 If it's running on your local machine you'll have to replace 'localhost' with the ip address of the server eg.如果它在您的本地机器上运行,您必须将“localhost”替换为服务器的 IP 地址,例如。 '123.123.123.123' '123.123.123.123'

如果您在创建用户和数据库后使用在线服务器,请确保您授予用户访问数据库的权限

If you are accessing MySQL database from the remote server, you will have to use server IP address for MySQL host.如果您从远程服务器访问 MySQL 数据库,则必须使用 MySQL 主机的服务器 IP 地址。 Also you will have to allow your remote server IP address in "Remote MySQL" under your cPanel otherwise you will not be able to access your database remotely.此外,您必须在 cPanel 下的“远程 MySQL”中允许您的远程服务器 IP 地址,否则您将无法远程访问您的数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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