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.
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.
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:
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.
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!