龚哥哥 - 山里男儿 爱生活、做自己!
PHP使用localhost无法连接MySQL 127.0.0.1可以
发表于 2015-10-9 | PHP

As we know,在UNIX/LINUX中,使用localhost进行连接默认会使用Unix socket,使用127.0.0.1会使用tcp socket.

sh-3.2# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 85
Server version: 5.6.21 MySQL Community Server (GPL)
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.21, for osx10.8 (x86_64) using  EditLine wrapper
Connection id:  85
Current database:
Current user:   root@localhost
SSL:    Not in use
Current pager:  stdout
Using outfile:  ''
Using delimiter:    ;
Server version: 5.6.21 MySQL Community Server (GPL)
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: 4 min 22 sec
Threads: 4  Questions: 178  Slow queries: 0  Opens: 118  Flush tables: 1  Open tables: 111  Queries per second avg: 0.679
--------------
mysql> 

编辑php.ini文件(常见位置 /etc/php.ini)
    mysql.default_socket=/tmp/mysql.sock
    pdo_mysql.default_socket=/tmp/mysql.sock
保存重启PHP即可

发表评论:

TOP