PHP on OSX: Can't connect to local MySQL server through socket '/var/mysql/mysql.sock'
When installing PHP and MySQL on OSX you may get the error Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’. Or you may also get “No such file or directory” when calling mysql_connect from a PHP page. This occurs because PHP is looking for the file mysql.sock in it’s typical installation location of /var/mysql/mysql.sock. However the MySQL OSX installer actually puts the file in /tmp/mysql.sock. There are two easy ways to solve the problem.
Solution 1: Create a symbolic link
Open terminal and do the following:
sudo su
mkdir /var/mysql
ln -s /tmp/mysql.sock /var/mysql/mysql.sock
You just created a symbolic link in the place where PHP expects the socket file to be located so it should be happy.
Solution 2: Edit php.ini
If you don’t like the idea of creating a symbolic link, you can also simply alter your php.ini file to point PHP to the real location of mysql.sock.
Locate /etc/php.ini. (If php.ini doesn’t exist on your system, copy /etc/php.ini.default to /etc/php.ini). You will likely have to do this from the terminal unless you have Finder configured to show hidden files. Open the file and update the setting mysql.default_socket so it looks like this:
mysql.default_socket = /tmp/mysql.sock
To commit the change you need to restart Apache. You can do that in System Settings -> Sharing, then uncheck, then recheck Web Sharing.
注解:这个问题,在php和Mysql一台及其的时候特别需要注意,否则mysql没法连接上,而php -m|grep mysql 是有的,无法排查,而在链接其他IP机器是没有该问题的,注意下。
来源:http://verysimple.com/2009/01/07/php-on-os-cant-connect-to-local-mysql-server-through-socket-varmysqlmysqlsock/
Solution 1: Create a symbolic link
Open terminal and do the following:
sudo su
mkdir /var/mysql
ln -s /tmp/mysql.sock /var/mysql/mysql.sock
You just created a symbolic link in the place where PHP expects the socket file to be located so it should be happy.
Solution 2: Edit php.ini
If you don’t like the idea of creating a symbolic link, you can also simply alter your php.ini file to point PHP to the real location of mysql.sock.
Locate /etc/php.ini. (If php.ini doesn’t exist on your system, copy /etc/php.ini.default to /etc/php.ini). You will likely have to do this from the terminal unless you have Finder configured to show hidden files. Open the file and update the setting mysql.default_socket so it looks like this:
mysql.default_socket = /tmp/mysql.sock
To commit the change you need to restart Apache. You can do that in System Settings -> Sharing, then uncheck, then recheck Web Sharing.
注解:这个问题,在php和Mysql一台及其的时候特别需要注意,否则mysql没法连接上,而php -m|grep mysql 是有的,无法排查,而在链接其他IP机器是没有该问题的,注意下。
来源:http://verysimple.com/2009/01/07/php-on-os-cant-connect-to-local-mysql-server-through-socket-varmysqlmysqlsock/
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/3914/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
评论列表