Home > Article > Backend Development > How to solve the problem that MySQL cannot connect to PHP in CentOS7
When using the CentOS 7 operating system, we may encounter the problem that MySQL cannot connect to PHP. This is because CentOS 7 comes with a conflict between MariaDB and MySQL, resulting in the inability to connect to the database. This article will introduce you how to solve the problem that MySQL cannot connect to PHP in CentOS 7.
1. Disable MariaDB
In order to avoid conflicts with MariaDB, we need to disable it first. You can use the following command:
systemctl stop mariadb.service #Stop the MariaDB service
systemctl disable mariadb.service #Disable the MariaDB service
After disabling, We need to confirm that the MariaDB service has been stopped. You can use the following command:
systemctl status mariadb.service # Check the MariaDB service status
If the status is displayed as inactive, it means the Mariadb service has been stopped.
2. Install MySQL
Install MySQL dependencies:
yum -y install wget
yum -y install net-tools
Download the MySQL installation package:
wget https://dev.mysql.com/get/mysql57-community-release-el7 -11.noarch.rpm
Install MySQL:
rpm -ivh mysql57-community-release-el7-11.noarch.rpm
yum install mysql-community-server
After the installation is complete, start MySQL:
systemctl start mysqld
systemctl enable mysqld
3. Modify MySQL configuration
Connect to MySQL:
mysql -u root -p
Enter password
Modify the MySQL configuration file:
vim /etc/my.cnf
Add the following content under the [mysqld] configuration:
skip-grant-tables
Initialization password:
use mysql;
update user set authentication_string=password('password') where user='root';
flush privileges;
quit;
Modify skip-grant-tables in the configuration file to the following content:
skip- grant-tables
Restart the MySQL service:
systemctl restart mysqld
4. Modify the PHP configuration file
Edit the PHP configuration file:
vim /etc/php .ini
die('Could not connect: ' . mysqli_error());}
echo 'Connected successfully' ;
mysqli_close($con);
?>
The above is the detailed content of How to solve the problem that MySQL cannot connect to PHP in CentOS7. For more information, please follow other related articles on the PHP Chinese website!