In CentOS, php cannot connect to mysql using 127.0.0.1. centosmysql
Php code is simple:
Copy codeThe Code is as follows:
$ Server = "127.0.0.1 ";
Println ("Begin ");
$ Link = mysql_connect ($ server, "mysql", "mysql ");
If (! $ Link ){
Die ('could not connect: '. mysql_error (). mysql_errno ());
}
In linux, you can use php mysql. php to view the running result, but an error is reported in my windows browser:
Copy codeThe Code is as follows:
Cocould not connect: Can't connect to MySQL server on '2017. 0.0.1 '(13) 127
Cause:
Copy codeThe Code is as follows:
# Getsebool-a | grep httpd
[Neo @ neo phpMyTest] $ getsebool-a | grep httpd
Httpd_can_network_connect --> off
Solution:
Copy codeThe Code is as follows:
# Setsebool httpd_can_network_connect 1
It turns out to be SELINUX, so I usually disable SELINUX and iptables ip6tables directly.
Copy codeThe Code is as follows:
# Disable SELINUX
Chkconfig -- level 12345 iptables off
Chkconfig -- level 12345 ip6tables off
Service iptables stop
Service ip6tables stop
View SELinux status:
1./usr/sbin/sestatus-v # If The SELinux status parameter is enabled, it is enabled.
SELinux status: enabled
2. getenforce # You can also use this command to check
Disable SELinux:
1. temporarily shut down (no need to restart the machine ):
Setenforce 0 # Set SELinux to permissive Mode
# Setenforce 1 Set SELinux to enforcing Mode
2. to modify the configuration file, restart the machine:
Modify the/etc/selinux/config file
Change SELINUX = enforcing to SELINUX = disabled