Home >Backend Development >PHP Problem >How to solve the problem that MySQL cannot connect to PHP in CentOS7

How to solve the problem that MySQL cannot connect to PHP in CentOS7

PHPz
PHPzOriginal
2023-04-13 09:20:09800browse

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

  1. 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

  2. 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

  1. Install MySQL dependencies:

    yum -y install wget

    yum -y install net-tools

  2. Download the MySQL installation package:

    wget https://dev.mysql.com/get/mysql57-community-release-el7 -11.noarch.rpm

  3. Install MySQL:

    rpm -ivh mysql57-community-release-el7-11.noarch.rpm

    yum install mysql-community-server

  4. After the installation is complete, start MySQL:

    systemctl start mysqld

    systemctl enable mysqld

3. Modify MySQL configuration

  1. Connect to MySQL:

    mysql -u root -p

    Enter password

  2. Modify the MySQL configuration file:

    vim /etc/my.cnf

  3. 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;

  4. Modify skip-grant-tables in the configuration file to the following content:

    skip- grant-tables

    skip-character-set-client-handshake

  5. Restart the MySQL service:

    Restart the MySQL service

    systemctl restart mysqld

4. Modify the PHP configuration file

  1. Edit the PHP configuration file:

    vim /etc/php .ini

  2. ##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.sock

    mysqli.default_socket = /var/lib/mysql/mysql.sock

    pdo_mysql.default_socket = /var/lib/mysql/mysql.sock

  3. Save changes and exit.
5. Test connection

  1. 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());

    }

    echo 'Connected successfully' ;
    mysqli_close($con);
    ?>

  2. Save the file to the root directory of Apache and access the file through the browser.
If Connected successfully is output, it means that the MySQL database can now be connected through PHP. If the test fails, please check the above steps and find the problem again.

Summary:

The above is the solution to the problem that MySQL cannot connect to PHP in CentOS 7. I hope it can provide help to students in need. If you have other questions, please leave a message to discuss.

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!

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