|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #41435 my.cnf database and host values are ignored in pdo_mysql
Submitted: 2007-05-18 14:50 UTC Modified: 2016-11-23 13:38 UTC
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: marques at displague dot com Assigned:
Status: Open Package: PDO MySQL
PHP Version: 5.2.2 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
40 + 28 = ?
Subscribe to this entry?

 [2007-05-18 14:50 UTC] marques at displague dot com
When creating a pdo_mysql connection, database and hostname are required even when they are supplied in the my.cnf file.

Reproduce code:
contents of ~/.my.cnf:
user = myusername
password = mypassword
database = mydb
host =
default-character-set = utf8

PHP code to test usage of ~/.my.cnf:
$pdo = new PDO('mysql:dbname=ppv',null,null,array(PDO::MYSQL_ATTR_READ_DEFAULT_GROUP=> 'php');

$st = $pdo->prepare("select user()");
$rs = $st->fetchAll();

Expected result:

Actual result:
Fatal error: Uncaught exception 'PDOException' with message 'invalid data source name'

In the above example, "dbname" is still required even though a "database" was given in ~/.my.cnf.

The host given in ~/.my.cnf is also ignored, and PHP/mysql defaults to "localhost".

Interestingly enough, default_character_set is applied.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-18 14:52 UTC] marques at displague dot com
The example should have read: 
$ppv = PDO('mysql',null,null,array(PDO::MYSQL_ATTR_READ_DEFAULT_GROU
P=> 'php');
 [2007-05-18 15:10 UTC] marques at displague dot com
It looks like you can get either host or dbname to come from my.cnf, but not both, by leaving the value blank in the DSN as the final parameter.

'mysql:dbname=' will use the database parameter in my.cnf
'mysql:host=' will use the host parameter in my.cnf
SQLSTATE[42000] [1044] Access denied for user 'myusername'@'localhost' to database ':host='

SQLSTATE[HY000] [2005] Unknown MySQL server host ':dbname=
 [2011-01-01 23:53 UTC]
-Package: Feature/Change Request +Package: MySQL related
 [2016-11-23 13:38 UTC]
-Package: MySQL related +Package: PDO MySQL
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 26 07:01:30 2024 UTC