|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-01-11 03:32 UTC] graced at wingsnw dot com
Description: ------------ my.cnf files allow the username/password/hostname and other connection information to be stored for later use. The Mysqli extension accepts a username from the file (overriding the default of the current login username) but not a password, thus it attempts to connect with no password even when a password is specified. The mysql command line client, when pointed at the file using --defaults-extra-file=path/to/file.cnf, successfully connects. Reproduce code: --------------- my.cnf contents: [client] host = localhost user = "username" password = "topsecret" PHP script: <?php error_reporting(E_ALL); $DB = mysqli_init(); $DB->options(MYSQLI_READ_DEFAULT_FILE, "my.cnf"); $DB->options(MYSQLI_READ_DEFAULT_GROUP, "client"); $DB->real_connect(); Expected result: ---------------- A successful database connection. Actual result: -------------- Warning: mysqli::real_connect(): (28000/1045): Access denied for user 'wadmin'@'localhost' (using password: NO) in /home/.../- on line 6 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 05:00:01 2025 UTC |
<?php mb_internal_encoding('UTF-8'); $mycnf="my.cnf"; $sql = mysqli_init(); mysqli_options($sql,MYSQLI_READ_DEFAULT_FILE,$mycnf); mysqli_real_connect($sql); Run on 5.6 responds with "Warning: mysqli_real_connect(): (HY000/2002): No such file or directory in /Users/ben/test.php on line 6" - SO the error message has changed, but the effect is no connection still. Note that php see the file (using file_exists()).All our databases are remote. Using DNS is obviously correct, but it makes no difference if we use either an IPv4 or DNS for the host. Localhost is a red herring here. "mysql --defaults-extra-file=my.cnf" works fine from commandline, and fails within php. Now tested in both 5.6 and 7. Compare the two following code fragments. <?php //This code works. It's a nasty workaround. mb_internal_encoding('UTF-8'); $mycnf="/Users/ben/my.cnf"; $sqls = parse_ini_file($mycnf); $sql = mysqli_init(); mysqli_real_connect($sql,$sqls['host'],$sqls['user'],$sqls['password'],$sqls['database']); if ($rs = $sql->query("select TABLE_NAME from information_schema.columns group by TABLE_NAME")) { print $rs->num_rows . "\n"; $rs->close(); }; // <?php //This code fails. mb_internal_encoding('UTF-8'); $mycnf="/Users/ben/my.cnf"; $sql = mysqli_init(); mysqli_options($sql,MYSQLI_READ_DEFAULT_FILE,$mycnf); mysqli_real_connect($sql); if ($rs = $sql->query("select TABLE_NAME from information_schema.columns group by TABLE_NAME")) { print $rs->num_rows . "\n"; $rs->close(); }; In my opinion, PHP shouldn't even need read access to the file - as it should pass over the information to the mysqlclient library, which should need read access to the file.This still seems to be broken on Debian 7.0's PHP 5.6.27-0+deb8u1 (with php5-mysqlnd 5.6.27+dfsg-0+deb8u1) and Ubuntu 16.10's PHP 7.0.8-3ubuntu3 (with php7.0-mysql 7.0.8-3ubuntu3). Setup: $ php --version PHP 7.0.8-3ubuntu3 (cli) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.8-3ubuntu3, Copyright (c) 1999-2016, by Zend Technologies $ cat dbtest.php <?php error_reporting(E_ALL); $DB = mysqli_init(); $DB->options(MYSQLI_READ_DEFAULT_FILE, "/xxxxx/.my.cnf") || die(); $DB->options(MYSQLI_READ_DEFAULT_GROUP, "client") || die(); $DB->real_connect(); $ cat ~/.my.cnf [client] host=xxx_some_remote_host user=xxx password=xxx Connect fails: $ php dbtest.php PHP Warning: mysqli::real_connect(): (HY000/2002): No such file or directory in /xxxxx/dbtest.php on line 6 strace shows that the php process doesn't even try opening .my.cnf: $ strace -e trace=file,network php dbtest.php [...] open("dbtest.php", O_RDONLY) = 3 [... several calls to getcwd and lstat ...] socket(AF_LOCAL, SOCK_STREAM, 0) = 3 connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/mysqld/mysqld.sock"}, 29) = -1 ENOENT (No such file or directory) PHP Warning: mysqli::real_connect(): (HY000/2002): No such file or directory in /xxxxx/dbtest.php on line 6 +++ exited with 0 +++ See also: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=835173 http://stackoverflow.com/questions/12241333/mysql-wont-read-password-from-configuration-fileHi everybody, I'm attempting to reproduce this problem with very little luck, I'm running Oracle Linux release 3.10.0-514.10.2.el7.x86_64 with: PHP 7.1.3 (cli) (built: Apr 18 2017 11:05:24) ( NTS DEBUG GCOV ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies And MySQL 5.7.18 configured in secure mode, with the following my.conf file: [client] user="root" password="Pass" host=localhost port=3380 socket=/tmp/mysql.sock database=test default-character-set=utf8 The test script is: error_reporting(E_ALL); $DB = mysqli_init(); $DB->options(MYSQLI_READ_DEFAULT_FILE, "./my.conf"); $DB->options(MYSQLI_READ_DEFAULT_GROUP, "client"); $DB->real_connect(); And the connection works perfectly, the my.conf file is used to fetch the authentication data. From the log snipped from hsd@php.net the error: connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/mysqld/mysqld.sock"}, 29) = -1 ENOENT (No such file or directory) Looks to be related with the missing mysqld.sock file which then cause the connection failure. Was an eventual socket file problem (missing &c) excluded already?