2015-02-18 04:12

访问被拒绝使用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:


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);

  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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • drzk21632 drzk21632 6年前

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

    点赞 评论 复制链接分享