简体   繁体   English

访问被拒绝使用MySQL WorkBench生成的PHP连接到数据库

[英]Access denied connecting to database using PHP generated by MySQL WorkBench

I've seen a couple of similar posts, but none really help with my problem. 我见过几个类似的帖子,但没有一个真正有助于解决我的问题。 I'm trying to connect to a database I created by using MySQL workbench. 我正在尝试连接到我使用MySQL workbench创建的数据库。 Under my user privileges in Workbench there's a GUI which allows you to enable all grants and permissions. 根据我在Workbench中的用户权限,有一个GUI,允许您启用所有授权和权限。

When I run the script show grants for phpTester@localhost; 当我运行脚本show grants for phpTester@localhost; it shows: 表明:

GRANT ALL PRIVILEGES ON *.* TO 'phpTester'@'localhost' WITH GRANT OPTION

So I know I have all permissions, and WorkBench also has a plug-in where you can copy your SQL to a clipboard and it will automatically extract your database settings so you can just copy and paste into your PHP script. 所以我知道我拥有所有权限,WorkBench也有一个插件,你可以将SQL复制到剪贴板,它会自动提取你的数据库设置,这样你就可以复制并粘贴到PHP脚本中。 So my script looks like this: 所以我的脚本看起来像这样:

define('DB_NAME', 'forms1');
define('DB_USER', 'phpTester');
define('DB_PASSWORD', '12345');
define('DB_HOST', 'localhost');
define('DB_PORT', 2080);


$link = mysql_connect(DB_HOST, DB_USER,DB_PASSWORD, DB_PORT);

if (!$link){
  die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if(!$db_selected){
  die('Not Working ' . DB_NAME . ':' . mysql_error());  
} 

It fails with the error: 它失败并出现错误:

Warning: mysql_connect(): Access denied for user 'phpTester'@'localhost' (using password: YES) on line 10
Could not connect: Access denied for user 'phpTester'@'localhost' (using password: YES)

And When I use this line of PHP: 当我使用这行PHP时:

$link = mysql_connect(DB_HOST, DB_USER);

I get this error: 我收到此错误:

Not Working forms1:Access denied for user ''@'localhost' to database 'forms1'.

But I do have a password set for this user, and I am using xampp for my localhost. 但我确实为这个用户设置了密码,我使用xampp作为我的localhost。

Turns out I was connecting to the wrong host, its a really simple fix, instead of connecting to 'localhost' you have to connect to the standard 127.0.0.1 loop back ip address, and port 3306. Then you enter your info like the user in your phpmyadmin and the password and you should be connected. 原来我连接到错误的主机,它是一个非常简单的修复,而不是连接到'localhost'你必须连接到标准的127.0.0.1循环回IP地址,以及端口3306.然后你输入你的信息就像用户在你的phpmyadmin和密码,你应该连接。 Make sure that in your phpmyadmin that user has the privileges you need. 确保在您的phpmyadmin中该用户具有您需要的权限。 And there you go, your workbench is connected to your phpmyadmin. 你去了,你的工作台连接到你的phpmyadmin。

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

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