I'm installing a drupal site on a cloud, which has a remote database cloud service.
But I found that drupal seems ignore the host
I give and use unix_socket
to connect the remote database.
Here is the Set up database screen during installation. I'm sure database name, username and password are correct. I have try ip address and domain name in the host
field. The socket
field leaves empty.
And I got:
Failed to connect to your database server. The server reports the following message: SQLSTATE[HY000] [2002] Invalid argument.
I'm not familiar with PHP and mysql, I don't know a better way to find out what Invalid argument is. I So I do some grep
and add some print_r()
in [drupaldir]/includes/database/database.inc
:
$driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
print_r($dsn);print_r($username);print_r($password);print_r($driver_options);
// Call PDO::__construct and PDO::setAttribute.
parent::__construct($dsn, $username, $password, $driver_options);
This is what I got (add a few line break, and replace the real name and password):
mysql:unix_socket=;dbname=demo_db
demo_username
demo_password
Array ( [1000] => 1 [20] => 1 [3] => 2 )
Is this mean, Drupal installation ignores the host
what ever I give and just use unix_socket
even it's empty?
##SOVLVED
I think I have located this issue. includes/database/mysql/database.inc, line 32:
// The DSN should use either a socket or a host/port.
if (isset($connection_options['unix_socket'])) {// will be TRUE if unix_socket is ''
$dsn = 'mysql:unix_socket=' . $connection_options['unix_socket'];
}
else {
// Default to TCP connection on port 3306.
$dsn = 'mysql:host=' . $connection_options['host'] . ';port=' . (empty($connection_options['port']) ? 3306 : $connection_options['port']);
}
$connection_options['unix_socket']
will be an empty string if we leave it empty on the page. So this test will be TRUE
. Replace isset
with !empty
solve this issue:)