简体   繁体   English

访问被拒绝使用 PHP 连接到 MySQL

[英]Access Denied Connecting to MySQL with PHP

I know there are a lot of threads asking this question already, but I have been at this for hours and am at wits end.我知道已经有很多线程在问这个问题,但我已经在这个问题上呆了几个小时并且不知所措。 I am attempting to connect to a MySQL database that I am running of my MacBook with the following PHP:我正在尝试使用以下 PHP 连接到我在 MacBook 上运行的 MySQL 数据库:

<?php
DEFINE('DB_USER', 'webuser');
DEFINE('DB_PASSWORD', 'thispassword');
DEFINE('DB_HOST', '127.0.0.1:3306');
DEFINE('DB_NAME', 'learning_accounts');

$dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die('Error connecting: ' .
mysqli_connect_error()
);

?>

That returns the following error:这将返回以下错误:

Error connecting: Access denied for user 'webuser'@'localhost' (using password: YES)

I can connect fine through the terminal, even trying to connect with PHP and root does not work.我可以通过终端正常连接,即使尝试连接 PHP 和 root 也不起作用。 I am new to both PHP and MySQL so any guidance and insight would be appreciated!我是 PHP 和 MySQL 的新手,所以任何指导和见解将不胜感激!

I have tried tons of solutions such as granting privileges, flushing privileges, creating new users, using PDO, nothing has been working - please help!我已经尝试了大量的解决方案,例如授予权限、刷新权限、创建新用户、使用 PDO,但没有任何效果 - 请帮助!

Thanks so much, I hate learning new languages because then I'm a lost fish when it comes to debugging at first!非常感谢,我讨厌学习新语言,因为一开始调试时我就像一条迷路的鱼!

Update更新

I have a password set for my root account, but changing the DB_PASSWORD variable to '' changes the error to: Error connecting: Unknown database 'learning_accounts'我为我的 root 帐户设置了密码,但是将 DB_PASSWORD 变量更改为 '' 将错误更改为:错误连接:未知数据库 'learning_accounts'

Update 2 I have fixed the issue, check my answer to see how, if you are having this problem then I hope you found this thread quickly!更新 2我已经解决了这个问题,请查看我的回答以了解如何解决,如果您遇到此问题,那么我希望您能尽快找到此主题!

  • Zach L扎克

您可能有一个匿名用户 ''@'localhost' 或 ''@'127.0.0.1'

if you tried privilleges, and so on tons of solutions, then you may make another account, and try it.如果您尝试了特权等大量解决方案,那么您可以创建另一个帐户并尝试一下。

If same problem occurs, then it is mysql service/daemon problem.如果出现同样的问题,则是 mysql 服务/守护进程问题。

I have same experience before time.我以前也有同样的经历。

So at that time, I reinstalled mysql server, and problem was fixed.所以当时,我重新安装了mysql服务器,问题解决了。

Try changing "DB_HOST" to "localhost".尝试将“DB_HOST”更改为“localhost”。 If it did no solve the problem, then reinstall your mysql server如果没有解决问题,那么重新安装你的mysql服务器

As @DontPanic suggested in his comment get rid off the 3306 from 127.0.0.1:3306 .正如@DontPanic 在他的评论中所建议的那样,从127.0.0.1:3306删除3306

If you're using XAMPP, WAMP or EasyPHP the MySQL password isn't set by-default.如果您使用 XAMPP、WAMP 或 EasyPHP,默认情况下不会设置 MySQL 密码。 Hence you should use因此你应该使用

DEFINE('DB_USER', 'root'); // default user name
DEFINE('DB_PASSWORD', ''); // empty password just open single quote and close

If you are working on the local server then no need to require the password just make it `blank'.如果您在本地服务器上工作,则不需要密码,只需将其设为“空白”即可。

    define('DB_HOST', 'localhost');    
    define('DB_USER', 'root');
    define('DB_PASSWORD', '');
    define('DB_NAME', 'learning_accounts');

$conn = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
if (!$link) {
    die("Database connection failed: " . mysqli_error());
}

If you are working on the server the just change the DB_USER , DB_PASSWORD and DB_NAME .如果您在服务器上工作,只需更改DB_USERDB_PASSWORDDB_NAME If still not able to connect then check the database name is correct or not.如果仍然无法连接,请检查数据库名称是否正确。

If everything is perfect you have to check the below link如果一切正常,您必须检查以下链接

How to install MySQLi 如何安装 MySQLi

The issue was that I had installed php separately, and made my database, then installed XAMPP and tried to connect to that database.问题是我单独安装了 php,并制作了我的数据库,然后安装了 XAMPP 并尝试连接到该数据库。 The proper way is to create the database using php my admin, which comes with XAMPP, and has it's own php, MySQL, and apache that you must use for everything to work.正确的方法是使用 php my admin 创建数据库,它与 XAMPP 一起提供,并且拥有自己的 php、MySQL 和 apache,您必须使用它们才能正常工作。

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

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