


TITLE: Solution to the problem that PHP cannot connect to the MySQL database server
In the process of website development, PHP and MySQL are an indispensable pair. However, during the development process, we may encounter various problems, one of which is that PHP cannot connect to the MySQL database server. In this article, we will look at the causes and solutions to this problem.
- Cause
There may be many reasons why you cannot connect to the MySQL database server. The following are some common reasons:
1.1 MySQL database server is not running
Before connecting to the MySQL database server, you must ensure that it is running. If the MySQL service is not running, PHP cannot connect to the database server.
1.2 MySQL database connection configuration error
The correct host name, port, user name and password are required to connect to the MySQL database server. If these settings are incorrect, you will not be able to connect to the database server.
1.3 MySQL extension is not installed in PHP
Using MySQL in PHP requires the MySQL extension to be installed. If PHP does not have the MySQL extension installed, it cannot connect to the MySQL database server.
- Solution
The following are some solutions to solve the problem that PHP cannot connect to the MySQL database server.
2.1 Confirm that the MySQL database server is running
First, confirm that the MySQL server is running. To check if the MySQL server is running, open the command line interface and run the following command:
$ service mysql status
If the MySQL server is running, you should see output like this:
● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2021-06-01 12:30:00 UTC; 1min 30s ago Main PID: 1234 (mysqld) Status: "Server is operational" CGroup: /system.slice/mysql.service └─1234 /usr/sbin/mysqld
If you look If the output you receive is different from the above output, it means that the MySQL server is not running. You can start the MySQL server with the following command:
$ service mysql start
2.2 Confirm whether the MySQL configuration is correct
Confirm whether the MySQL connection configuration is correct. Please check the following configuration items:
- Host name: the host name or IP address of the MySQL server
- Port: the port number of the MySQL server, the default is 3306
- user Name: Username to connect to the MySQL database server
- Password: Password to connect to the MySQL database server
Please make sure these configuration items are correct. If you are unsure about the values of these configuration items, please contact your MySQL administrator.
2.3 Confirm that PHP has installed the MySQL extension
Confirm that PHP has installed the MySQL extension. If you are using Debian or Ubuntu, you can run the following command to install the MySQL extension:
$ sudo apt install php-mysql
If you are using Red Hat or Fedora, you can run the following command to install the MySQL extension:
$ sudo yum install php-mysql
If you are using macOS, you can enable the MySQL extension by editing the php.ini file. Find the following line and uncomment it:
;extension=mysqli
Change it to:
extension=mysqli
Please note that you may need to use an appropriate editor to edit the php.ini file and you must restart the web server for the changes to take effect.
- Conclusion
It is a common problem that PHP cannot connect to the MySQL database server. In this article, we cover some common causes and provide some solutions. If you encounter this problem, please first check whether the MySQL server is running, then check whether the MySQL connection configuration is correct and make sure PHP has the MySQL extension installed.
The above is the detailed content of What should I do if php cannot connect to the mysql database server?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
