Home > Backend Development > PHP Problem > php7 cannot connect to mysq

php7 cannot connect to mysq

王林
Release: 2023-05-07 09:46:06
Original
757 people have browsed it

Solution to the problem that PHP7 cannot connect to the MySQL database

PHP language is one of the most widely used server-side programming languages, and MySQL is a very popular relational database management system. During PHP development, we often use MySQL to store and manage data. However, sometimes when using the PHP7 version to connect to MySQL, the database cannot be connected. So how to solve it? This article will introduce to you the solution to the problem that PHP7 cannot connect to the MySQL database.

  1. Check whether the MySQL driver is installed

PHP7 is different from previous versions. It does not have the MySQL extension installed by default and needs to be installed manually. Therefore, the first step is to check whether the MySQL driver is installed on the system. You can use the following command in the terminal to check:

php -m | grep mysql
Copy after login

If there is no output, it means that the MySQL driver is not installed on the system. You can use the following command to install:

sudo apt-get install php7.0-mysql
Copy after login

After the installation is completed, you can use the above command in the terminal to check again whether the installation is successful.

  1. Change the MySQL configuration file

In PHP7, the default encryption method of MySQL is changed from the original mysql_native_password to caching_sha2_password. Therefore, modifications need to be made when connecting to MySQL, otherwise the database connection will fail. You can add the following content to the MySQL configuration file my.cnf:

[mysqld]
default-authentication-plugin=mysql_native_password
Copy after login

After the addition is completed, restart the MySQL service to take effect.

  1. Check whether the MySQL service is turned on

When connecting to the database, if the MySQL service is not turned on, the connection will fail. You can use the following command to check whether the service is turned on:

sudo service mysql status
Copy after login

If the service is not turned on, you can use the following command to start it:

sudo service mysql start
Copy after login

If the service is turned on but still cannot be connected, you can try again Start the MySQL service:

sudo service mysql restart
Copy after login
  1. Check whether the database connection information is correct

If none of the above methods solve the problem, then you need to check whether the relevant information about connecting to the database is correct. Including user name, password, host address, port number, etc. It can be checked in the php code. Code example:

$servername = "localhost";
$username = "username";
$password = "password";

// 创建连接
$conn = new mysqli($servername, $username, $password);

// 检查连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
echo "连接成功";
Copy after login

The variables $servername in the code are the host address, $username is the username, and $password is the password. By checking whether this information is correct, you can determine the reason why you cannot connect to the database.

Conclusion

The above is the solution to the problem that PHP7 cannot connect to the MySQL database. By checking the MySQL driver, changing the MySQL configuration file, checking whether the MySQL service is turned on, and checking whether the database connection information is correct, you can generally solve the problem of not being able to connect to the database. If none of the above methods can resolve it, then other issues need to be considered and more detailed troubleshooting will be required. During the PHP development process, if you encounter problems as long as you patiently find and solve them, you can successfully complete the project.

The above is the detailed content of php7 cannot connect to mysq. For more information, please follow other related articles on the PHP Chinese website!

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