Showing posts with label Centos 7. Show all posts
Showing posts with label Centos 7. Show all posts

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]

PHP - Error 500 - Centos 7





Error 500 Internal Server Error in PHP


I have a Google Instance running Centos 7.

I had this strange Error 500 message whenever I upload a piece of php code.

When accessing through the browser, it comes up with the error message, which was loading without issues in my local machine.


 

Internal Server Error


The error was something like this.
HTTP request sent, awaiting response... 500 Internal Server Error
2016-10-18 11:28:33 ERROR 500: Internal Server Error.
I searched around and found a solution probable solution.

Somebody suggested that I execute the following command to make it right.

sudo chcon unconfined_u:object_r:httpd_config_t:s0 myfile.php

It did work though I do not know why. My guess is that it could be something to do with the newline and return values.

Notepad plus, which is the editor that I used for this purpose, has the options, Windows, Unix and Old Mac under "Edit", "EOL Conversion". My guess is that choosing "Unix/OSX format" when saving you code in Notepad plus might be another method to solve the problem.


Here is another instance where the 500 Internal server error may occur. Let us suppose you have a symbolic link to a folder that is present elsewhere in the disk. You are referring to a specific file, it may be a config file which you want to include in your php code. This error is thrown because Centos has a problem accessing the file due to non availability of rights. As you know, every object in Unix is accessible based on rights only.

How to find the exact problem for Internal server error

Our system was Unix/Centos


We can find the exact problem by looking at the httpd server logs that are created in the Apache server or Nginx or any server that you may be running.

So, what is the Centos error log file location?

In Centos, the Centos error log file path would be  /var/log/httpd/-error_log

So, if your domain is google.com and you are running your webserver in a Centos machine, then the path to your error log file would probably be  /var/log/httpd/google.com-error_log

An example error_log file would look something like this.

[Tue Oct 18 11:35:28.950158 2016] [:error] [pid 15013] [client 172.xx.xx.49:33929] PHP Warning:  include(/home/ghost/rider/tajmahal.php): failed to open stream: Permission denied in /home/ghost/rider/opendata.php on line 3
[Tue Oct 18 11:35:28.950188 2016] [:error] [pid 15013] [client 172.68.146.49:33929] PHP Warning:  include(): Failed opening 'tajmahal.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /home/ghost/rider/opendata.php on line 3


If you look in the above log, it becomes obvious where the error lies. 

Hope it helped. 

Visual Ads vs. Text Ads: Why Pictures Usually Steal the Show for Better Ad Performance

Are you ready to read convincing information on this topic? Hey. Let's have a quick chat about something super important for anyone want...

Most Popular

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