0

I am developing on mac using MAMP I try to connect to my local mysql database from php:

$con=mysqli_connect("localhost","root","root","testDB");

this is the error I get:

Warning: mysqli_connect(): (HY000/2002): No such file or directory in /Users/tshahar/Documents/Dev/server/test.php on line 20 Failed to connect to MySQL: No such file or directory

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
talshahar
  • 528
  • 3
  • 12
  • I've never worked in MAMP before but shouldn't you execute a PHP file from within your web server (MAMP)'s directory instead of your Documents folder? If MAMP gives you that ability then check the path and see if the file `test.php` actually exists on that path. – Basit Apr 09 '14 at 06:11
  • Post all your,code , the error you get doesn't link to the code you post – Poomrokc The 3years Apr 09 '14 at 06:11
  • that is the only line of code in my php file – talshahar Apr 09 '14 at 06:18

2 Answers2

1

To summarize, use:

127.0.0.1

Instead of:

localhost

The reason is that "localhost" is a special name for the mysql driver making it use the unix socket to connect to mysql instead of the a tcp socket.

refer this link:

click here to example

source from:1

Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in

Community
  • 1
  • 1
jmail
  • 5,944
  • 3
  • 21
  • 35
0

Try using IP instead for local host that would be 127.0.0.1 or ::1 if your system does IPV6.

can't really tell you why that is but I guess the SQL won't answer to a domain name because it's configured that way.(had the same problem yesterday)

oh no
  • 46
  • 2
  • when I try IP (127.0.0.1) I get this error: Warning: mysqli_connect(): (HY000/2002): Connection refused in /Users/tshahar/Documents/Dev/server/test.php on line 20 Failed to connect to MySQL: Connection refused[tshahar1-mac:~/Documents/Dev/server] tshahar% – talshahar Apr 09 '14 at 06:35