简体   繁体   English

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

[英]Connecting to MySQL database in XAMPP on Mac

I am using cakePHP to connect to a database in XAMPP phpMyAdmin. 我正在使用cakePHP连接到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. 到目前为止,我已经完成了...。我有一个带有数据库的实时Web服务器。 I can connect to this database no problem. 我可以连接到这个数据库没问题。 I have a database running in MySQL workbench on localhost. 我有一个在本地主机上的MySQL工作台中运行的数据库。 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. 这告诉我,我的连接代码没有任何问题,问题必须出在XAMPP中的phpMyAdmin上。 I have created a simple connection PHP script to try and connect to the database and it doesn't work. 我创建了一个简单的连接PHP脚本来尝试连接到数据库,但它不起作用。 The same script can connect to my live web server and my localhost MySQL workbench database but not my XAMPP database. 相同的脚本可以连接到我的实时Web服务器和本地MySQL工作台数据库,但不能连接到XAMPP数据库。 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: 无法连接到MySQL:用户'root'@'localhost'的访问被拒绝(使用密码:NO)错误描述:

If i go to phpMyAdmin in XAMPP and go to the test database I can clearly see that this user has full access. 如果我在XAMPP中转到phpMyAdmin并转到测试数据库,则可以清楚地看到该用户具有完全访问权限。 I have tried using the host name as 127.0.0.1, localhost and ::1. 我尝试使用主机名作为127.0.0.1, localhost和:: 1。 Nothing seems to work. 似乎没有任何作用。 I just dont understand why XAMPP will not let me connect to the database. 我只是不明白为什么XAMPP不会让我连接到数据库。 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 这是用于数据库测试的用户及其权限的屏幕快照

That's the second reason why one should never use 'root' unless one knows exactly what one is doing. 这就是为什么除非有人确切知道自己在做什么,否则永远不要使用“ root”的第二个原因。 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. 在本地主机上添加一个特定的用户帐户(例如“数据”),并在数据库上为其授予所需的特权。

I am actually using the username 'login' with an auto generated password that is of high complexity. 我实际上使用的用户名“ login”具有高度复杂的自动生成的密码。 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. 我使用了没有密码的“ root”用户来强调一个事实,即使管理员帐户也无法连接。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM