Jump to content

MySQL not using hostname, user, and password from my.cnf (MySQL configuration file)


Twinbird

Recommended Posts

Hello,

 

The title says it all: MySQL is not using the hostname, user, and password that I have specified in my MySQL configuration file (my.cnf).

 

I have a my.cnf file located at /etc/mysql and a .my.cnf file at ~/.

The file in my ~ direcotry contains the following:

[client]
host = localhost
user = root
password = *****

 

In my php script, I have the following:

try
{
$pdo = new PDO('mysql:dbname=grad');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
echo $e;
exit();
}

 

When I run mysql --print-defaults I can see that MySQL has indeed loaded the host, user and password from my config file, but it does not seem to take effect as my php script gives me the following error:

exception 'PDOException' with message 'SQLSTATE[42000] [1044] Access denied for user ''@'localhost' to database 'grad'' in /var/www/ ...

 

MySQL version: 5.5.29-0ubuntu0.12.04.1

Web server: Apache 2.2.22 (Ubuntu)

 

Any suggestions?

 

Thanks!

Maybe, it depends on what implementation is in use. If it's using the mysql client libraries then it may work. If it's using PHP's mysql native driver (mysqlnd) though it won't, as indicated by the note:

This option is not available if mysqlnd is used, because mysqlnd does not read the mysql configuration files.

I would advise you just include the details within the script rather than try and rely on some feature that may or may not work.

 

If you really want to keep the details out of the script, you can use PDO's aliasing feature and save the details into the php.ini file. The manual page for PDO's constructor has the details and an example.

 

edit: On second look the aliasing doesn't let you define the user/pass in php.ini, only the dsn details like host, port, dbname.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.