Home >Backend Development >PHP Problem >What to do if php connects to mysql error 13

What to do if php connects to mysql error 13

PHPz
PHPzOriginal
2023-03-31 10:08:431392browse

Solution to error 13 when php connects to MySQL

When using php to connect to MySQL, error 13 sometimes occurs. In this article, I will introduce some solutions to this problem.

First of all, error 13 is a MySQL Permission problem. This may be because the user does not have sufficient permissions when connecting to MySQL, resulting in the inability to connect to MySQL. If you want to solve this problem, you need to log in to MySQL and grant sufficient permissions to the user.

In this article, I will provide you with some solutions:

Method One: Check the MySQL permissions of the user you are using

Using MySQL in PHP When connecting to access MySQL, we need to provide the MySQL username and password. By default, MySQL does not allow anonymous users to connect. Therefore, we need to ensure that the MySQL user used has the necessary permissions.

You can check the permissions of the MySQL user you are using with the following command:

SHOW GRANTS FOR 'username'@'localhost';

If your user does not have sufficient permissions, you can grant permissions to it using the following command:

GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost';

Please note that this will grant your user full access to all databases and tables.

Method 2: Check the firewall settings

In some cases, the firewall may also cause connection error 13. If your computer is running a firewall, you should ensure that the MySQL port (default is 3306) is open in the firewall rules.

You can use the following command to check whether your port is open:

sudo ufw status

If the port is not open, you can use the following command to open the port:

sudo ufw allow 3306/tcp

Method Three: Change bind-address in the MySQL configuration file

Another factor is the bind-address in the configuration file of the MySQL server, which defaults to localhost. If you want to connect to MySQL from a remote machine, you should change it to 0.0.0.0.

Please note that you should configure your firewall accordingly when using this method to ensure security.

The steps to change bind-address are as follows:

  1. Open the configuration file using the following command:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
  1. Search for bind-address in the file keyword and then change it to:
bind-address = 0.0.0.0
  1. Save and close the file and then restart the MySQL service:
sudo systemctl restart mysql

These are the solutions to resolve php connection MySQL errors 13. Some common methods. If you have tried the above methods but still cannot resolve the issue, you should check if your PHP and MySQL versions are compatible.

Hope this article is helpful to you!

The above is the detailed content of What to do if php connects to mysql error 13. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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