drbd65446 2015-02-25 19:54
浏览 32

无法通过PHP连接到MySQL数据库

I'm trying to connect to a MySQL database through a php script. I have tried everything, but always end up with: "Connection failed: Access denied for user 'user'@'localhost' (using password: YES)"

I can connect to MySQL in the command line:

mysql -u user -p -h localhost

This is an example of the php script:

<?php 
  $DB_HOST = "localhost";
  $DB_USER = "user";
  $DB_PASS = "pass";
  $DB_NAME = "dbname";

  // Create connection
  $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

  // Check connection
  if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
  }
?>

These are the privileges set in the DB for the user:

Localhost:

GRANT USAGE ON *.* TO 'user'@'localhost' IDENTIFIED BY PASSWORD '*<numbers>' 
GRANT ALL PRIVILEGES ON `dbName`.* TO 'user'@'localhost' 

'%':

GRANT USAGE ON *.* TO 'user'@'%' IDENTIFIED BY PASSWORD '*<numbers>'
GRANT ALL PRIVILEGES ON `dbName`.* TO 'user'@'%'

I am running MAMP on my system.

Is there something else I'm missing with the privileges?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题