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.
Answers
If you use Linux: the path to the mysql.sock file is wrong. This is usually because you are using (LAMPP) XAMPP and it isn't in /tmp/mysql.sock
Open the php.ini file and find this line:
mysql.default_socket
And make it
mysql.default_socket = /path/to/mysql.sock
This is for Mac OS X with the native installation of Apache HTTP and custom installation of MySQL.
The answer is based on @alec-gorge's excellent response, but since I had to google some specific changes to have it configured in my configuration, mostly Mac OS X-specific, I thought I'd add it here for the sake of completeness.
Enable PHP5 support for Apache HTTP
Make sure the PHP5 support is enabled in
/etc/apache2/httpd.conf
.
Edit the file with
sudo vi /etc/apache2/httpd.conf
(enter the password when asked) and uncomment (remove ;
from the beginning of) the line to load the php5_module module.LoadModule php5_module libexec/apache2/libphp5.so
Start Apache HTTP with
sudo apachectl start
(or restart
if it's already started and needs to be restarted to re-read the configuration file).
Make sure that
/var/log/apache2/error_log
contains a line that tells you the php5_module is enabled - you should see PHP/5.3.15
(or similar).[notice] Apache/2.2.22 (Unix) DAV/2 PHP/5.3.15 with Suhosin-Patch configured -- resuming normal operations
Looking up Socket file's name
When MySQL is up and running (with
./bin/mysqld_safe
) there should be debug lines printed out to the console that tell you where you can find the log files. Note the hostname in the file name - localhost
in my case - that may be different for your configuration.
The file that comes after
Logging to
is important. That's where MySQL logs its work.130309 12:17:59 mysqld_safe Logging to '/Users/jacek/apps/mysql/data/localhost.err'.
130309 12:17:59 mysqld_safe Starting mysqld daemon with databases from /Users/jacek/apps/mysql/data
Open the
localhost.err
file (again, yours might be named differently), i.e. tail -1 /Users/jacek/apps/mysql/data/localhost.err
to find out the socket file's name - it should be the last line.$ tail -1 /Users/jacek/apps/mysql/data/localhost.err
Version: '5.5.27' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL)
Note the
socket:
part - that's the socket file you should use in php.ini
.
There's another way (some say an easier way) to determine the location of the socket's file name by logging in to MySQL and running:
show variables like '%socket%';
Configuring PHP5 with MySQL support - /etc/php.ini
Speaking of php.ini...
In
/etc
directory there's /etc/php.ini.default file. Copy it to /etc/php.ini.sudo cp /etc/php.ini.default /etc/php.ini
Open
/etc/php.ini
and look for mysql.default_socket.sudo vi /etc/php.ini
The default of
mysql.default_socket
is /var/mysql/mysql.sock
. You should change it to the value you have noted earlier - it was /tmp/mysql.sock
in my case.
Replace the
/etc/php.ini
file to reflect the socket file's name:mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock
Final verification
Restart Apache HTTP.
sudo apachectl restart
Check the logs if there are no error related to PHP5. No errors means you're done and PHP5 with MySQL should work fine. Congrats!
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.
Not that it helps you much, but in the recent versions (and even less recent) of MySQL, error code 2002 means “Can't connect to local MySQL server through socket [name-of-socket]”, so that might tell you a bit more.
I encountered this problem too, then i modified 'localhost' to '127.0.0.1',it works.
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.
Make sure your local server (MAMP, XAMPP, WAMP, etc..) is running.
On a Mac, before doing all the hard work, simply check your settings in
System Preferences > MySQL
. More often than not, I've experienced the team running into this problem since The MySQL Server Instance is stopped
.
Click the
Start MySQL Server
button, and magic will happen.
I had the same problem. My socket was eventually found in /tmp/mysql.sock. Then I added that path to php.ini. I found the socket there from checking the page "Server Status" in MySQL Workbench. If your socket isn't in /tmp/mysql.sock then maybe MySQL Workbench could tell you where it is? (Granted you use MySQL Workbench...)
0 comments:
Post a Comment