


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
skip-character-set-client-handshake
-
Restart the MySQL service:
Restart the MySQL service
systemctl restart mysqld
4. Modify the PHP configuration file
-
Edit the PHP configuration file:
vim /etc/php .ini
- ##Search for mysql.default_socket or mysqli.default_socket and change it to the MySQL socket file address: mysql.default_socket = /var/lib/mysql/ mysql.sockmysqli.default_socket = /var/lib/mysql/mysql.sockpdo_mysql.default_socket = /var/lib/mysql/mysql.sock Save changes and exit.
- Enter the following code in the terminal (the username and password need to be modified yourself):$con = mysqli_connect("localhost","username","password");
if (!$con)
{}die('Could not connect: ' . mysqli_error());
Copy after loginecho 'Connected successfully' ;
mysqli_close($con);
?>
Save the file to the root directory of Apache and access the file through the browser.
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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