Saturday, December 10, 2016

Database connection failed...Error: 2003 Can't connect to MySQL server on '127.0.0.1' (13 "Permission denied")


Database connection failed...Error: 2003 Can't connect to MySQL server

When trying to connect through my web application in php, I encountered the above error message.

The solution seems obvious. You just need to assign permissions properly to the user.

But, I have already assigned permissions to the user and it works when I connect through the mysql command line.

When I use the command
/usr/bin/mysql -u alkrie -p mydb
I get prompted for the password and when I key it in, I get the MySQL prompt promptly.

mysql>

So, the MySQL connection is established when connecting through the command line, but generates "Database connection failed...Error: 2003 Can't connect to MySQL server" error, when connecting through the browser.

I looked around for a solution and homed in on a solution. This issue seems specific to a version of Linux. My server is a Centos 7

Find out your Redhat version using the command
cat /etc/redhat-release

Solution to Database connection failed...Error: 2003 Can't connect to MySQL server

It would seem that we have use the command setsebool for the permissions you want the server to allow or deny.

Use either of the following commands to allow http to connect to the database. Use the second command to make the setting permanent. Making it permanent means the setting will be remembered after a restart.

setsebool httpd_can_network_connect_db=1
allows network to connect to the database temporarily


setsebool -P httpd_can_network_connect_db on 
will allow network to connect to the database and enable it permanently

Note:
You may use 
/usr/sbin/getsebool -a|grep httpd|less
to find out the status of the network requests that are allowed [on/off]

No comments:

Knee Jerk Governance ?!

A Country of RULES I do not know how many of you have noticed.  We are a country of RULES, RULES & more RULES. Probably the only...

Copyrighted.com Registered & Protected DWYE-NHTO-NBNH-7FFM