|  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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: marques at displague dot com
New email:
PHP Version: OS:


 [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: Wed Apr 17 00:01:28 2024 UTC