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

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

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘