15
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP の MySQL で "No such file or directory" と出た時の対処方法

Last updated at Posted at 2013-08-04

症状

  • MacOS X の MAMP で WebからのDB接続は問題ない
  • けど、コマンドラインからやると接続エラー
  • エラー内容は "No such file or directory" となる

原因

mysql.sock の場所の指定がおかしいから

対策

以下で治った

ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock

調べ方

以下のコマンドで、設定しているsockファイルの場所を調べる

% php -i | grep socket
mysql.default_socket => /var/mysql/mysql.sock => /var/mysql/mysql.sock
mysqli.default_socket => /var/mysql/mysql.sock => /var/mysql/mysql.sock
pdo_mysql.default_socket => /var/mysql/mysql.sock => /var/mysql/mysql.sock

コード上で、mysql_connect 当の後に

mysql_error();

でエラー内容見られるよ

参考

php - Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in - Stack Overflow
http://stackoverflow.com/questions/4219970/warning-mysql-connect-2002-no-such-file-or-directory-trying-to-connect-vi

15
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?