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 serverWhen 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.
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
Solution to Database connection failed...Error: 2003 Can't connect to MySQL serverIt 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.
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
You may use
/usr/sbin/getsebool -a|grep httpd|less
to find out the status of the network requests that are allowed [on/off]