简体   繁体   English

mysql_connect超时

[英]mysql_connect timing out

I am trying to set up a PHP framework on my Mac 10.6 computer, and it keeps erroring during setup due to database connectivity issues. 我试图在Mac 10.6计算机上设置一个PHP框架,由于数据库连接问题,它在安装过程中始终出错。 So I put together a small php script to see if I could connect to the server myself, and it couldn't either. 因此,我整理了一个小的php脚本,看看我自己是否可以连接到服务器,但也不能。 The operation times out. 操作超时。

However, I am able to login to mysql from commandline perfectly fine. 但是,我能够从命令行完美地登录到mysql。 It's only PHP that is having these connection issues. 只有PHP出现了这些连接问题。 Does anyone have any ideas? 有人有什么想法吗?

Here is the script. 这是脚本。

<?php
  $connection = mysql_connect("http://localhost", "root", "");
if( $connection ) {
  mysql_close( $connection );
  die('TRUE');
}else {
  die('Could not connect: ' . mysql_error());
}

?>

EDIT: I tried removing http:// but then I get a "No such file error." 编辑:我尝试删除http://,但随后出现“没有此类文件错误”。

PHP Warning:  mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Users/newuser/Downloads/t.php on line 2
PHP Warning:  mysql_connect(): No such file or directory in /Users/newuser/Downloads/t.php on line 2
Could not connect: No such file or directory

You are using this : 您正在使用此:

mysql_connect("http://localhost", "root", "");

But your MySQL host is not http://localhost : it's probably localhost . 但是您的MySQL主机不是http://localhost :可能是localhost


Try to remove that http:// , using this : 尝试使用以下方法删除该http://

mysql_connect("localhost", "root", "");


As a sidenote : working with the root account is not a good practice -- even on a development platform. 附带说明:即使在开发平台上,使用root帐户也不是一个好习惯。


EDIT after the comment : In that case, try with 127.0.0.1 (the IP address of localhost ) 在评论后编辑:在这种情况下,请尝试使用127.0.0.1 localhost的IP地址)


And if you really want to use Unix sockets, you'll have to find which socket is used by your MySQL server (it should be indicated in its configuration) ; 而且,如果您确实要使用Unix套接字,则必须查找MySQL服务器使用的套接字(应在其配置中指出) for instance, in my /etc/mysql/my.cnf , I have : 例如,在我的/etc/mysql/my.cnf ,我有:

[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock

Then, you'll want PHP to use the same one : 然后,您将希望PHP使用相同的代码:

Take the "http://" out of the mysql_connect parameter. 从mysql_connect参数中删除“ http://”。 It wants a host, not an HTTP URL. 它需要一个主机,而不是HTTP URL。

删除http://并仅使用主机名localhost

Use "127.0.0.1" it will force using TCP4 insted of sockets. 使用“ 127.0.0.1”,它将强制使用由套接字插入的TCP4。

If you want to use sockets, according to this document the default socket on Mac OS X is: 如果要使用套接字,根据本文档 ,Mac OS X上的默认套接字为:

/tmp/mysql.sock

Fot this to be default you should edit your php confiuration or have an .htaccess file with: 如果要将此设置为默认值,则应编辑php配置文件或具有以下文件的.htaccess文件:

php_value mysql.default_socket "/tmp/mysql.sock"

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

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