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

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

    点赞 评论
  • dongzhong5573
    dongzhong5573 2016-08-25 20:09

    I am actually using the username 'login' with an auto generated password that is of high complexity. This however doesn't work either. I have used the 'root' user with no password to highlight the fact that not even the admin accounts can connect.

    点赞 评论
  • dongluolie3487
    dongluolie3487 2016-08-25 22:01
    Open config.inc.php file in the phpmyadmin directory
    Find line 21: $cfg['Servers'][$i]['password'] = '';
    Change it to: $cfg['Servers'][$i]['password'] = 'your_password';
    Restart XAMPP
    
    点赞 评论

相关推荐