MySQL connection not working: 2002 No such file or

2019-01-01 11:06发布

I'm trying to set up WordPress. I have Apache and MySQL running, and the accounts and database are all set up. I tried to make a simple connection:

<?php
    $conn = mysql_connect('localhost', 'USER', 'PASSWORD');
    if(!$conn) {
        echo 'Error: ' . mysql_errno() . ' - ' . mysql_error();
    }
?>

And I always get this:

Error: 2002 - No such file or directory

What file or directory could it be talking about?

I'm on a OS X Snow Leopard, using the built-in Apache. I installed MySQL using the x86_64 dmg.

UPDATE: I found that the socket is at /tmp/mysql.sock, so In php.ini, I replaced all occurrences of the wrong path with that.

18条回答
泛滥B
2楼-- · 2019-01-01 11:49

Restarting the mysql server might help. In my case, restarting the server saved a lot of time.

service mysql restart

P.S.- use sudo service mysql restart for non-root user.

查看更多
长期被迫恋爱
3楼-- · 2019-01-01 11:50

Replacing 'localhost' to '127.0.0.1' in config file (db connection) helped!

查看更多
萌妹纸的霸气范
4楼-- · 2019-01-01 11:51

I had a similar problem and was able to solve it by addressing my mysql with 127.0.0.1 instead of localhost.

This probably means I've got something wrong in my hosts setup, but this quick fix get's me going for right now.

查看更多
伤终究还是伤i
5楼-- · 2019-01-01 11:52

I encountered this problem too, then i modified 'localhost' to '127.0.0.1',it works.

查看更多
泛滥B
6楼-- · 2019-01-01 11:52

I've installed MySQL using installer. In fact, there was no data directory alongside 'bin' directory.

So, I manually created the 'data' directory under "C:\Program Files\MySQL\MySQL Server 8.0". And it worked (changing the root password following steps suggested on https://dev.mysql.com/doc/mysql-windows-excerpt/5.7/en/resetting-permissions-windows.html.

查看更多
柔情千种
7楼-- · 2019-01-01 11:59

First, ensure MySQL is running. Command: mysqld start

If you still cannot connect then: What does your /etc/my.cnf look like?

The other 2 posts are correct in that you need to check your socket because 2002 is a socket error.

A great tutorial on setting up LAMP is: http://library.linode.com/lamp-guides/centos-5.3/index-print

查看更多
登录 后发表回答