0

I'm getting the following error installing drupal 7:

Failed to connect to your database server. The server reports the following message: SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'myservername' (13).

I am running MySQL on one windows server and httpd on another linux. MySQL is running just fine and on the httpd server I can connect just fine to the MySQL, as follows:

mysql -h dbserver -p --port=3001 -u drupal

Similarly I can run mysqli and pdo and it connection just fine too from my httpd:

<?php
$servername = "dbserver";
$username = "drupal";
$password = "xxx";
$dbname = "drupal";
$port = 3001;

$conn = new mysqli($servername, $username, $password, $dbname, $port);

if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$conn->close();

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname;port=$port", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
$conn=null;
 ?>

So there is nothing wrong on my web server or php or mysql. It's just through the installation that it fails. I've been in the advanced and entered all the correct details.

Any idea? I looked and there was no apache log and I couldn't figure out how to increase error reporting during install or find exactly where it does the database connection check..

thanks.

Neil Walker
  • 6,400
  • 14
  • 57
  • 86

2 Answers2

1

Can you confirm you have dbserver's ip mapped. Go to terminal and type : "ping dbserver" and please post result.

EDITED: This situation was caused by a security policy of the linux distro where the apache server is setup that doesn't allow it to connect to remote mysql instances. For solving this situation go to terminal and type sudo setenforce 0, that way linux won't enforce such strict webserver behaviour.

biozes
  • 41
  • 5
  • Yes, that works. As in my mysqli and pdo code I'm using the server name and it's fine. I also tried ip address and that fails. As I said, it works from code on the web server just not from Drupal install. I doubt this is anything but I traced the method 'getConnection' and for key and target variables they are both 'default', I'd have thought they'd be something else... – Neil Walker May 17 '15 at 19:21
  • Simply, I don't think it's getting that far. In my test code it takes about 5 seconds to finish the code, but on the Drupal installer as soon as I click the button it returns with a failure. – Neil Walker May 17 '15 at 19:25
  • ok I would now check for the service. Please use : `service --status-all` on your terminal and show the mysql entries. – biozes May 17 '15 at 20:16
  • sorry, MySQL is running on Windows server, web server is linux. But I can connect from my Linux box with the code above just fine. Any idea how to output debug on the installer? – Neil Walker May 17 '15 at 20:21
  • ok in your linux distro go to terminal and type `sudo setenforce 0`, that way linux won't enforce such strict webserver behaviour. let me know how this works for you. – biozes May 17 '15 at 20:33
  • Thank you @biozes, that was just awesome. I didn't even know SELinux was setup and reading it says httpd is one thing it monitors. I have no idea how you knew :) Please create and answer and I'll mark it as the right one for anyone else. – Neil Walker May 19 '15 at 13:28
  • btw, why is it like this? is there anything that can be done other than turning off selinux to allow http server to contact another server? or is it the way Drupal is doing things? – Neil Walker May 19 '15 at 13:43
  • great, happy it solved your problem, I've edited the question. Thank you. I'm not sure if there is a work around, I might look into it later on. all the best – biozes May 29 '15 at 15:25
0

Assuming you're using RHEL/CentOS, I found the answer from @alexbilbie here:

setsebool -P httpd_can_network_connect_db 1

From the RHEL documentation:

When disabled, this Boolean prevents HTTP scripts and modules from initiating a connection to database servers. Enable this Boolean to allow this access.

Community
  • 1
  • 1
Aidan Feldman
  • 5,205
  • 36
  • 46