mysql_connect(): [2002] No such file or directory

在mac中搭建php的開發環境 

1. apach ---- 推薦用MAMP。你只要把你的php文件/項目放入到htdocs(/Applications/MAMP/htdocs)目錄下,啓動mamp,輸入localhost就能夠直接看見你這個目錄下的全部文件了。--因爲這種相對簡單,這裏再也不贅述,若是任何問題,歡迎聯繫交流。 php

2. nginx ---- 因爲沒有已經集成的工具,nginx相對apach的配置相對複雜。好比你須要下載mysql,下載php-fpm,甚至還須要用homebrew來安裝。(我下面的說明都是基於homebrew安裝的狀況)在這裏我簡單的說明幾點須要特別注意的狀況: html

2.1 nginx的配置 java

須要說明的是/usr/local/etc/nginx下的conf文件server的配置,fastcgi_param選項,注意當加上$document_root,以下面所示:實際上,下面的$document_root$fastcgi_script_name;是和fastcgi.conf裏面的值一致。 mysql

server {
        listen       80;
        server_name  localhost; //指定你的域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /Users/megan/www/; // 指定你項目的路徑
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

2.2 MySQL的配置 nginx

本文是有mysql引起的,這裏就簡單的說說mysql的簡單命令。 sql

啓動 -- mysql.server start  macos

關閉 -- mysql.server stop app

重啓 -- mysql.server restart less

鏈接 -- mysql -uroot 或者 mysql -uroot -p (這裏是密碼) eclipse

導入數據 -- source ../*.sql

2.3 php-fpm的配置

這玩意兒最鬱悶的是我沒有找到很好的重啓和關閉的命令。好比nginx的啓動用sudo nginx,關閉用sudo nginx -s -stop重啓用sudo nginx -s reload. 可php-fpm我只會用sudo php-fpm來啓動。關閉的方式簡直是粗暴。

我首先用ps aux|grep php來查看有那些php進程在運行,而後執行kill php-fpm相關進程。好比下面這樣的結果執行(sudo)kill 3870 3871 3872.再查看一次,你會發現這些進程沒有了。(若是哪位同仁有更好的辦法並能指點迷津,比女子將不勝感激)

[megan@megan:~]$ps aux|grep php
megan            1338   4.6  7.4  1824760 389504   ??  S    10:51AM  23:40.56 /Applications/zend-eclipse-php/zend-eclipse-php.app/Contents/MacOS/zend-eclipse-php -os macosx -ws cocoa -arch x86 -launcher /Applications/zend-eclipse-php/zend-eclipse-php.app/Contents/MacOS/zend-eclipse-php -name Zend Eclipse for PHP Developers --launcher.library /Applications/zend-eclipse-php/zend-eclipse-php.app/Contents/MacOS//../../../plugins/org.eclipse.equinox.launcher.cocoa.macosx_1.1.200.v20120522-1813/eclipse_1503.so -startup /Applications/zend-eclipse-php/zend-eclipse-php.app/Contents/MacOS//../../../plugins/org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar --launcher.overrideVmargs -showlocation -launcher /Applications/zend-eclipse-php/zend-eclipse-php.app/Contents/MacOS/zend-eclipse-php -showlocation -data /Users/megan/www -launcher /Applications/zend-eclipse-php/zend-eclipse-php.app/Contents/MacOS/zend-eclipse-php -showlocation -keyring /Users/megan/.eclipse_keyring -showlocation -vm /System/Library/Frameworks/JavaVM.framework -vmargs -Xms128M -Xmx512M -Djava.awt.headless=true -XstartOnFirstThread -Dorg.eclipse.swt.internal.carbon.smallFonts -Xdock:icon=../Resources/Eclipse.icns -XX:MaxPermSize=256m
megan          3981   0.4  0.0  2442000    628 s001  S+    6:48PM   0:00.01 grep php
nobody         3872   0.0  0.2  2481552  12008   ??  S     5:57PM   0:05.74 php-fpm
nobody         3871   0.0  0.2  2481552  12748   ??  S     5:57PM   0:01.09 php-fpm
root           3870   0.0  0.0  2464912    592   ??  Ss    5:57PM   0:00.23 php-fpm

回到問題

其實這個問題很簡單,只須要找到你的mysql.sock 而後回到你的php.ini修改一個配置就行了。主要是下面三行代碼。

pdo_mysql.default_socket=/tmp/mysql.sock //若是使用的是zendframework,這個須要配置
// 若是使用的是opencart等並無使用pdo的須要配置下面兩項。
mysql.default_socket = /tmp/mysql.sock 

mysqli.default_socket =/tmp/mysql.sock

實際上只作了一件事,那就是將mysql.sock配置給default_socket。

其實這裏最主要的問題是你怎麼找到你的mysql.sock, 怎麼找你的php.ini。玩了這麼就的mac,除了作iOS開發,不管是玩java仍是php仍是其餘,最鬱悶的一件事情是找相關的配置文件。什麼whereis,locate,which等都用了個遍,每每仍是找不到文件,那種痛苦,不想多說。

找到php.ini最簡單的辦法--建一個文件,phpinfo()會讓世界變得異常美好。

找到mysql.sock -- 你可能會使用mysqladmin viriables 查看mysql全部的參數,你或許發現mysql.sock的路徑指向了/tmp/mysql.sock,但是你找不到那個文件,儘管你現實全部隱藏文件你仍是看不到這個文件。

那麼你能夠執行sudo find / -name mysql.sock 它會告訴你,其實你的mysql.sock在private/tmp/mysql.sock這裏。那麼你可能會想用下面這行代碼來建一個快捷方式。

ln -s /private/tmp/mysql.sock /tmp/mysql.sock

ok,是能夠,或許它會提醒你這個文件已經存在了。不要緊,不用理會它,儘管你在/tmp裏面仍是看不見mysql.sock。你只管用就好了。我也不知掉多到哪一個我看不見的角落了。總之,有個mysql.sock的快捷方式在/tmp下面給你使用。

相關文章
相關標籤/搜索