在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

dqzow3859
dqzow3859 “使用密码:否”。您是否在root帐户上设置了密码?MySQL帐户是username@hostname,BOTH组件必须匹配。如果您在恰好匹配的用户表条目上设置密码,那么这就是您被拒绝的原因
大约 4 年之前 回复

3个回答



这是为什么一个人永远不应该使用'root'的第二个原因,除非你确切知道自己在做什么。 只有少数几个过期的真的</ strong>知道他们每时每刻都在做什么...... </ p>

在您的localhost上添加一个特定的useraccount,例如'data'并授予 它是您数据库所需的特权。</ p>
</ div>

展开原文

原文

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.



我实际上使用用户名“login”和一个高度复杂的自动生成的密码。 然而,这也不起作用。 我使用没有密码的“root”用户来突出显示甚至管理员帐户都无法连接的事实。</ p>
</ div>

展开原文

原文

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.



 打开phpmyadmin目录中的config.inc.php文件
查找第21行:$ cfg ['Servers'] [$ i] ['password'] ='';
更改为:$ cfg ['Servers'] [$ i] ['password'] ='your_password';
Restart XAMPP
</ code> </ pre>

</ div>

展开原文

原文

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

dpxw7293
dpxw7293 我试过这个解决方案,但它没有用。 更改密码停止了对phpMyAdmin的所有访问。 谢谢你的尝试。
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐