无法使用 MAMP 或社区服务器连接到 mysql 服务器
我使用 MySQL Workbench 创建了一个数据库,现在我需要访问它。所以我编写了一个 php 脚本来访问它:
<?
$db = mysql_connect("127.0.0.1:3306","root", "");
if (!$db){
echo "Could not connect to database";
exit();
}
$db_name = "pfc_db";
if (!mysql_select_db($db_name, $db)){
die ("Could not select database");
}
$sql=mysql_query("select * from CAPAS");
while($row=mysql_fetch_assoc($sql)){
$output[]=$row;
if (isset($output)){
echo "yes";
echo $output[0];
}
else{echo "no";}
}
mysql_close();
?>
我对 MAMP 和 MySQL 社区服务器有疑问,我需要在正确的路径上进行推送。 我已经在我的 mac 上安装了 MAMP,并将端口切换到默认的 mysql 端口 3306。我已将 PHP 测试脚本放在我在 htdocs 下创建的文件夹“Api”中。因此,要尝试一下,我在浏览器上输入 http://127.0.0.1/api/test.php 结果是“无法连接到数据库”。我做对了吗?
那么社区服务器呢?它比 MAMP 更好吗?是否应该运行 MAMP 才能工作?我可以只使用社区服务器吗?如果是这样,我应该将 PHP 脚本放在哪里?哪个文件夹?因为当我尝试使用社区服务器进行相同的测试时,浏览器不会显示错误消息,而是开始将 php 脚本下载到我的下载文件夹。为什么会出现这样的情况呢?
正如你所看到的,我对这些服务器的东西有点混乱,我需要一些帮助来解决它。
I created a database with MySQL Workbench, and now I need to access it. So I've written a php script to access it:
<?
$db = mysql_connect("127.0.0.1:3306","root", "");
if (!$db){
echo "Could not connect to database";
exit();
}
$db_name = "pfc_db";
if (!mysql_select_db($db_name, $db)){
die ("Could not select database");
}
$sql=mysql_query("select * from CAPAS");
while($row=mysql_fetch_assoc($sql)){
$output[]=$row;
if (isset($output)){
echo "yes";
echo $output[0];
}
else{echo "no";}
}
mysql_close();
?>
I have doubts about MAMP and MySQL Community Server and I need a push on the right path.
I've installed MAMP on my mac and switched the ports to the default mysql ones 3306. I've placed the PHP test script in a folder "Api" I've created under htdocs. So to try it out i type on my browser http://127.0.0.1/api/test.php and the result is "Could not connect to databse". Am I doing it right?
And what about Community Server? Is it better than MAMP? Should it be running for MAMP to work? Can I just use Community Server? If so, where should I place the PHP scripts? Which folder? Because when I try to do the same test with Community Server, instead of showing the error message, the browser starts downloading the php script to my downloads folder. Why happens that?
As you can see I have a bit of a mess in my mind with these server stuff and I need some help to figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不需要安装 MySQL 社区服务器。 MAMP 已随 MySQL 一起提供。
您需要使用
mysql_error()
函数输出代码中更具体的错误。mysql_error 将从 MySQL 返回错误消息。
You do not need to install MySQL Community Server. MAMP already comes with MySQL.
You need to ouput more specific errors within your code by using the
mysql_error()
function.mysql_error will return the error message(s) from MySQL.
按照我给 user775263 的 asnwer,我得到了这个用户表:
+
如您所见,任何用户都设置了任何密码。但后来我发现 bin/phpmyadmin 下有一个 config.inc.php 脚本,其中有这些句子:
I attempts again to connect with
$db = mysql_connect("127.0.0.1:3306","root", " root");
并且连接成功。为什么mysql命令行中的users表没有反映这一点?正常吗?我应该改变它吗?然后我遇到了另一个问题。它无法选择我在 MySQL Workbench 上创建的数据库“pfc_db”。我收到错误消息未知数据库“pfc_db”。但是,当我输入
mysql> 时;显示数据库;
提到的数据库确实出现在表格上:那么这里可能有什么问题呢?
Following the asnwer I was giving to user775263, i get this table of users:
+
As you can see, any of the users has any password set. But then I found out there's a config.inc.php script under bin/phpmyadmin where there are these sentences:
I tried again to connect with
$db = mysql_connect("127.0.0.1:3306","root", "root");
and the connexion was successful. Why the users table in mysql command line doesn't reflect this? Is it normal? Should I change it?And then I run into another problem. It can't select the database 'pfc_db' I created on MySQL Workbench. I get the error message Unknown database 'pfc_db'. However, when I type
mysql> show databases;
the mentioned db does appear on the table:So what might be the problem here?
好吧,我会回答自己,以便如果有人遇到同样的问题。
发生密码错误的原因是我在同一台计算机上安装了两个mysql,因此我正在检查其中一个的密码,而浏览器正在查看另一个。
因此,如果您在密码访问方面遇到问题,请检查您是否安装了两个 mysql,并检查文件 config.inc.php
我还通过套接字连接更改了 TCP/IP,这会提高您使用的速度您的本地主机作为服务器。
Well, I'm gonna answer myself so that if someone runs into the same problem.
The error of password happened because I had two installations of mysql in the same computer, so that I was checking the passwords in one of them and the browser was looking at the other.
Therefore, if you have problems with password access, check that you don't have two mysql installed and also check the file config.inc.php
I also changed TCP/IP by a socket connexion, which improves the speed if you're using your localhost as server.
这对我有用
sudo mkdir /var/mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock
This worked for me
sudo mkdir /var/mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock
我遇到了这个问题,并通过使用默认的 TCP/IP 端口而不是套接字端口来修复它。
因此,在我的例子中,我使用了 8889 而不是 3306
I ran into this problem and fixed it by using the default TCP/IP port instead of the socket port.
So in my case instead of 3306, I used 8889