dongxu1668 2014-11-30 10:31
浏览 79

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 */
-----------------
  • 写回答

1条回答 默认 最新

  • douwei8672 2014-11-30 11:02
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀