dtqysxw4659 2016-08-25 17:48
浏览 268
已采纳

在Mac上的XAMPP中连接到MySQL数据库

I am using cakePHP to connect to a database in XAMPP phpMyAdmin. I have added a new user, set the password and set the privilleges. I cannot for the life me create a connection to the database on my localhost. What I have done so far.... I have a live web server with a database. I can connect to this database no problem. I have a database running in MySQL workbench on localhost. I can connect to this no problem. This tells me that there is nothing wrong with my connection code and the problem must sit with phpMyAdmin in XAMPP. I have created a simple connection PHP script to try and connect to the database and it doesn't work. The same script can connect to my live web server and my localhost MySQL workbench database but not my XAMPP database. My connection script is as follows:

<?php

   $con= mysqli_connect("localhost","root","","test");
   // Check connection
   if (mysqli_connect_errno())
   {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

   // Perform a query, check for error
   if (!mysqli_query($con,"INSERT INTO Persons (FirstName) VALUES ('Glenn')"))
   {
        echo("Error description: " . mysqli_error($con));
   }

   mysqli_close($con);

?>

The error message I get is;

Failed to connect to MySQL: Access denied for user 'root'@'localhost' (using password: NO)Error description:

If i go to phpMyAdmin in XAMPP and go to the test database I can clearly see that this user has full access. I have tried using the host name as 127.0.0.1, localhost and ::1. Nothing seems to work. I just dont understand why XAMPP will not let me connect to the database. I hope I'm not missing something obvious so any help will really be appreciated. here is a screen shot of the users and their privilleges for the database test

  • 写回答

3条回答

  • duandai3964 2016-08-25 19:45
    关注

    That's the second reason why one should never use 'root' unless one knows exactly what one is doing. Only a few expirenced ones really know what they're doing each and every moment...

    Add a specific useraccount like 'data' on your localhost and grant it the needed priviledges on your DB.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?