SELinux causes a PHP connection to MySQL exception. Can't connect to MySQL server. selinuxmysql

Source: Internet
Author: User

SELinux causes a PHP connection to MySQL exception. Can't connect to MySQL server. selinuxmysql

Colleagues reported a strange phenomenon. A simple PHP code test is normal in the test environment, but in the formal environment, no matter what method (TCP/IP, unix socket) is used) cannot connect to mysql.

I checked with my help. The error message is similar, whether it is a tcp/IP connection of the specified ip address or port or a unix socket connection:
Copy codeThe Code is as follows: cocould not connect: Can't connect to MySQL server on 'mysql. Server' (13)
No matter how you modify the MySQL authorization, modify the MySQL settings in php. ini, or modify the MySQL listening network port, the problem cannot be solved.

If you use the command line to manually connect to MySQL, everything works.

The problem seems to be that php (with apache) is not allowed to connect to MySQL, but there is no limit on the firewall. If you want to solve this problem, only SELinux causes it.
Copy codeThe Code is as follows: getsebool-a | grep-I httpd

We can see that the httpd process license mode is set here. Let's take a closer look at the option:
Copy codeThe Code is as follows: getsebool-a | grep-I httpd_can_network_connect
Httpd_can_network_connect --> off
Now we understand that SELinux restricts httpd's external access permissions. Enable it:
Copy codeThe Code is as follows: setsebool-P httpd_can_network_connect = 1
If you are not familiar with SELinux, you can directly modify the system configuration file/etc/sysconfig/selinux and disable it globally:
Copy codeThe Code is 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
# Change the setting to 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, you can directly execute the command to take effect online:
Copy codeThe Code is as follows:
[Root @ imysql ~] # Setenforce 0
Setenforce: SELinux is disabled


How to solve the problem of "Can't connect to mysql server on 'localhost' (10061)" reported when connecting to MySQL in php )? Now

Windows
Enter the following command line:
> Cd E: \ mysql \ bin (location of your wamp5mysql database)
> Mysqladmin-u root password your password
> Mysql-u root-p
Enter password: Your password
You can

,,,,,,,,,,,,,,,,,
Find the root cause, and try again here:

The root cause of this problem is: Because the password is set for the root of mysql, rather than the password is blank when the initial installation is good, use

Mysqladmin version cannot work like this. It must be like this: mysqladmin-uroot-p version. Press enter and follow the prompts to enter

Run the command with the root password.

The first method is actually a solution without knowing the root password. In this way, you can simply start mysql without a password.

And modify the root password to solve the problem of forgetting the root password.

Enter the "mysqladmin-u root password your password" command to modify the password of the root user.

If you are prompted to enter the original password and the execution is successful, the original password is blank. After that, you can use the modified password to log on successfully.

................................. How to change the password?
First of all, we need to declare that, in most cases, modifying MySQL requires the root permission in mysql, so generally users cannot change the password.

, Unless the Administrator is requested.

Method 1
Phpmyadmin is the simplest. Modify the user table of the mysql database,
But don't forget to use the PASSWORD function.
Method 2
Use mysqladmin, which is a special case stated above.
Mysqladmin-u root-p password mypasswd
After entering this command, you need to enter the original root password, and then the root password will be changed to mypasswd.
Change the root in the command to your username, and you can change your password.
Of course, if your mysqladmin cannot connect to mysql server, or you cannot execute mysqladmin,
This method is invalid.
In addition, mysqladmin cannot clear the password.
The following methods are used at the mysql prompt and must have the root permission of mysql:

Method 3
Mysql> insert into mysql. user (Host, User, Password)
VALUES ('%', 'Jeffrey ', PASSWORD ('biscuit '));
Mysql> FLUSH PRIVILEGES
Specifically, this is adding a user with the username jeffrey and password biscuit.
I wrote this example in mysql Chinese reference manual.
Be sure to use the PASSWORD function, and then use flush privileges.
Method 4
Similar to method Sany, but the REPLACE statement is used.
Mysql> replace into mysql. user (Host, User, Password)
VALUES ('%', 'Jeffrey ', PASSWORD ('biscuit '));
Mysql> FLUSH PRIVILEGES
Method 5
Use the set password statement,
Mysql>... the remaining full text>

Unstable MySQL connection Can't connect to MySQL server on 'localhost' (10048) PHP page flash

You set mysql_connect ("localhost", "root", "password ")
Change to mysql_connect ("127.0.0.1", "root", "password ")
Try connecting
Yes
Open the hosts file.
Add the ing between 127.0.0.1 and localhost.

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.