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