Home > Backend Development > PHP Problem > What should I do if php and mysql cannot connect?

What should I do if php and mysql cannot connect?

coldplay.xixi
Release: 2023-03-07 08:30:01
Original
6132 people have browsed it

php与mysql连接不上的解决办法:1、获取当前配置信息;2、获取MySQL socket路径;3、重新配置PHP,打开【php.ini】修改【mysql.default_socket】;4、重启nginx或者apache。

What should I do if php and mysql cannot connect?

php与mysql连接不上的解决办法:

1、获取当前 mysql.default_socket、mysqli.default_socket、pdo_mysql.default_socket 配置信息

建立一个 PHP 文件, 显示 phpinfo():

用浏览器打开:

What should I do if php and mysql cannot connect?

在此页面,找到mysql.default_socket、mysqli.default_socket、pdo_mysql.default_socket信息:

What should I do if php and mysql cannot connect?

2、获取 MySQL socket 路径

通过控制台进入 MySQL, 输入命令: STATUS, 查找 UNIX socket 值

#mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 8
Server version: 5.7.11 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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.7.11, for osx10.9 (x86_64) using  EditLine wrapper
Connection id:        8
Current database:    
Current user:        root@localhost
SSL:            Not in use
Current pager:        less
Using outfile:        ‘‘
Using delimiter:    ;
Server version:        5.7.11 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:            8 days 15 hours 1 min 17 sec
Threads: 2  Questions: 21  Slow queries: 0  Opens: 114  Flush tables: 1  Open tables: 0  Queries per second avg: 0.000
--------------
Copy after login

3、这时候, 你对比 mysql.default_socket、mysqli.default_socket、pdo_mysql.default_socket 和 通过 MySQL UNIX socket 发现不一样, 这就是 mysql_connect(); 警告的原因: PHP 配置 mysql 有问题.

4、重新配置 PHP, 打开php.ini修改mysql.default_socket、mysqli.default_socket、pdo_mysql.default_socket 的值为: /tmp/mysql.sock

pdo_mysql.default_socket=/tmp/mysql.sock  
mysql.default_socket=/tmp/mysql.sock  
mysqli.default_socket=/tmp/mysql.sock
Copy after login

或者修改my.cnf的socket:

[client]  
socket=/tmp/mysql.sock  
  
[mysqld]  
socket=/tmp/mysql.sock
Copy after login

5、重启nginx或者apache 

相关免费学习推荐:php编程(视频)

The above is the detailed content of What should I do if php and mysql cannot connect?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template