drhs13583567608
2015-02-18 04:12
浏览 177
已采纳

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

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. Under my user privileges in Workbench there's a GUI which allows you to enable all grants and permissions.

When I run the script 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. 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:

$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.

图片转代码服务由CSDN问答提供 功能建议

我见过几个类似的帖子,但没有一个真正有助于解决我的问题。 我正在尝试连接到我使用MySQL workbench创建的数据库。 根据我在Workbench中的用户权限,有一个GUI,允许您启用所有授权和权限。

当我运行脚本 show grants for phpTester @ localhost; 时,它显示:

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

所以我知道我拥有所有权限,WorkBench也有一个插件在哪里 可以将SQL复制到剪贴板,它将自动提取您的数据库设置,以便您可以复制并粘贴到PHP脚本中。 所以我的脚本如下所示:

  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('无法连接:'。mysql_error()); 
} 
 
 $ db_selected = mysql_select_db(DB_NAME,$ link); 
 
if(!$ db_selected  ){
 die('Not Working'.Dc_NAME。':'。mysql_error());  
} 
   
 
 

失败并显示错误:

  警告:mysql_connect():第10行用户“phpTester”@“localhost”(使用密码:YES)拒绝访问
无法连接:用户“phpTester”@“localhost”拒绝访问(使用密码:YES)
    
 
 

当我使用这行PHP时:

  $ link = mysql_connect(DB_HOST,DB_USER); 
    
 
 

我收到此错误:

  Not Working forms1:拒绝访问 对于用户'''localhost'到数据库'forms1'。
   
 
 

但我确实为此用户设置了密码,我使用xampp作为我的localhost 。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drzk21632 2015-02-21 04:03
    已采纳

    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. Make sure that in your phpmyadmin that user has the privileges you need. And there you go, your workbench is connected to your phpmyadmin.

    点赞 打赏 评论

相关推荐 更多相似问题