SELinux causes PHP to connect MySQL exception can ' t connect to MySQL server solution _mysql

Source: Internet
Author: User

Colleagues report a strange phenomenon, a simplest test of PHP code, in the test environment is normal, but in the formal environment, regardless of the way (TCP/IP, UNIX sockets) can not connect to MySQL.

I helped to view the next, indeed, whether the specified IP, port TCP/IP mode connection, or the UNIX socket connection, the error message is similar:

Copy Code code as follows:
Could not Connect:can ' t connect to MySQL server on ' MySQL. SERVER ' (13)

In any case, modify the MySQL authorization, or adjust the php.ini on MySQL settings, or modify the MySQL listening network, can not be resolved.

If you use the command line to manually connect to MySQL, then everything is fine.

The problem seems to be that PHP (with Apache) is not allowed to connect to MySQL, but there is no limit to firewalls. To think about it, only selinux factors can lead to this problem.

Copy Code code as follows:
Getsebool-a|grep-i httpd

You can see here that the licensing mode for the HTTPD process is set up, and then take a closer look and have an option:

Copy Code code as follows:
Getsebool-a|grep-i Httpd_can_network_connect
Httpd_can_network_connect--> off

Now I see, it turns out that SELinux restricts httpd's access to the outside world. Turn it on:
Copy Code code as follows:
Setsebool-p httpd_can_network_connect=1

If you are unfamiliar with SELinux, you can also directly modify the system configuration file/etc/sysconfig/selinux, global shutdown:
Copy Code code as follows:

# This file controls the ' state of ' SELinux on the system.
# selinux= can take one of these three values:
# Enforcing-selinux security policy is enforced.
# Permissive-selinux Prints warnings instead of enforcing.
# disabled-no SELinux policy is loaded.
#SELINUX =enforcing
#把设置改为disabled
Selinux=disabled
# selinuxtype= can take one of these two values:
# targeted-targeted processes are protected,
# Mls-multi level security protection.
selinuxtype=targeted

Then restart the operating system to take effect.
Alternatively, direct execution of the order may take effect online:
Copy Code code as follows:

[root@imysql~]# Setenforce 0
Setenforce:selinux is disabled

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.