其实在osx系统下是支持apache和php的,但是自己安装mysql之后,用php的mysql_connect去连接mysql的时候,经常会出现这样的错误:
No such file or directory
这种情况大多数是因为php中配置的mysql.sock路径和mysql自己真实的mysql.sock路径不一致造成的
我们在命令行mysql之后进入mysql,然后status可以看到我们mysql的真实mysql.sock路径
someoneMacBook-Pro:apache2 someone$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.19 Homebrew
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> status
--------------
mysql Ver 14.14 Distrib 5.6.19, for osx10.9 (x86_64) using EditLine wrapper
Connection id: 7
Current database:
Current user: someone@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.6.19 Homebrew
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 1 hour 53 min 17 sec
Threads: 5 Questions: 42 Slow queries: 0 Opens: 68 Flush tables: 1 Open tables: 61 Queries per second avg: 0.006
--------------
mysql>
从上面可以看到我们的mysql.sock地址,然后再看看我们php去哪里找mysql.sock了呢?
查看/private/etc/php.ini
这里很多人找不到php.ini这个文件,没关系,这个目录有一个php.ini.default文件,我们cp php.ini.default php.ini就可以copy一份出来
然后我们编辑这份php.ini文件,找到下面三项:
pdo_mysql.default_socket =
mysql.default_socket =
mysqli.default_socket =
也许这里你看到的都是空项,没关系,我们这里把他们全部换成 /tmp/mysql.sock 然后重启apache : apachectl restart
之后我们的php就可以正常使用mysql_connect连接我们的mysql数据库了~~