Home > Article > Backend Development > php does not connect to the database
php does not connect to the database?
Summary of solutions when PHP cannot connect to MySQL
1. Get the current mysql.default_socket, mysqli.default_socket, pdo_mysql.default_socket configuration information
Create a PHP file, display phpinfo():
Open with browser:
##On this page, find mysql.default_socket, mysqli.default_socket , pdo_mysql.default_socket information: 2. Get the MySQL socket pathEnter MySQL through the console , enter the command: STATUS, find the UNIX socket value#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 --------------3. At this time, you compare mysql.default_socket, mysqli.default_socket, pdo_mysql.default_socket and find the difference through MySQL UNIX socket, This is the reason for the warning of mysql_connect();: There is a problem with PHP configuring mysql. 4. Reconfigure PHP, open php.ini and modify mysql.default_socket, mysqli.default_socket, pdo_mysql.default_socket The value is: /tmp/mysql.sock
pdo_mysql.default_socket=/tmp/mysql.sock mysql.default_socket=/tmp/mysql.sock mysqli.default_socket=/tmp/mysql.sockOr modify the socket of my.cnf:
[client] socket=/tmp/mysql.sock [mysqld] socket=/tmp/mysql.sock5. Restart nginx or apache For more PHP related knowledge, please visit
PHP中文网!
The above is the detailed content of php does not connect to the database. For more information, please follow other related articles on the PHP Chinese website!