PHP和Mysql连接

I use os X yosemite on my mac. I have been facing issues with PHP and Mysql connection lately. I could not connect PHP to mysql using this statement

$link = mysql_connect("localhost",$username,$passwd);
        if($link)
        {
            echo "Connection successful";
        }
        else 
        {
            $msg=mysql_error();
            echo $msg;
        }

I have seen some recommendation over the internet to link some sock files (I am mad at myswl for not understanding and recording what I did). After those sock changes, it worked fine. Now I restarted the machine, and a couple of days later, I am not able to connect to mysql both directly as well as through PHP. I get the following error.

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (2)

I tried the suggestions of modifying the php.ini, but it didn't help.

Thanks for your help.

This is how the PHP.ini looks like:

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/pdo_mysql.default-socket
pdo_mysql.default_socket= tmp/mysql.sock as well as /var/mysql/mysql.soc
-------------------------------------------------------------------------------------------

More information
-----------------
mymac:mysql abc$ cd /var/mysql
mymac:mysql abc$ ls -ltr

/*
lrwxr-xr-x  1 root  wheel  15 Feb  6  2014 mysql.soc -> /tmp/mysql.sock

lrwxr-xr-x  1 root  wheel  15 Nov 30 10:07 mysql.sock -> /tmp/mysql.sock */
-----------------

I noticed that when you connect to localhost the socket connector is used, but when you connect to 127.0.0.1 the TCP/IP connector is used. You could try using 127.0.0.1 if the socket connector is not enabled/working.

OR Configure your my.cnf (usually in the /etc/mysql/ folder) file with

socket=/var/lib/mysql/mysql.sock

check if you are running MySQL or not.

also try changing your permission to mysql folder, you can try:

sudo chmod -R 755 /var/lib/mysql/

that solved it for me