Twinbird Posted February 25, 2013 Share Posted February 25, 2013 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! Link to comment https://forums.phpfreaks.com/topic/274921-mysql-not-using-hostname-user-and-password-from-mycnf-mysql-configuration-file/ Share on other sites More sharing options...
Jessica Posted February 25, 2013 Share Posted February 25, 2013 I'm pretty sure you still need to tell PDO the host, user and password, using your $_SERVER vars. Link to comment https://forums.phpfreaks.com/topic/274921-mysql-not-using-hostname-user-and-password-from-mycnf-mysql-configuration-file/#findComment-1414844 Share on other sites More sharing options...
kicken Posted February 25, 2013 Share Posted February 25, 2013 PHP does not read my.cnf. That is something specific to mysql's client tools, such as the mysql command line tool. Link to comment https://forums.phpfreaks.com/topic/274921-mysql-not-using-hostname-user-and-password-from-mycnf-mysql-configuration-file/#findComment-1414847 Share on other sites More sharing options...
Jessica Posted February 25, 2013 Share Posted February 25, 2013 I misread and thought it was a .conf file. Link to comment https://forums.phpfreaks.com/topic/274921-mysql-not-using-hostname-user-and-password-from-mycnf-mysql-configuration-file/#findComment-1414854 Share on other sites More sharing options...
Twinbird Posted February 25, 2013 Author Share Posted February 25, 2013 I see. Thanks for the help Jessica and kicken! Link to comment https://forums.phpfreaks.com/topic/274921-mysql-not-using-hostname-user-and-password-from-mycnf-mysql-configuration-file/#findComment-1414878 Share on other sites More sharing options...
Twinbird Posted February 26, 2013 Author Share Posted February 26, 2013 So, I came accross the following: http://php.net/manual/en/ref.pdo-mysql.php PDO::MYSQL_ATTR_READ_DEFAULT_FILE Is it possible to use this constant to read the my.cnf file? Link to comment https://forums.phpfreaks.com/topic/274921-mysql-not-using-hostname-user-and-password-from-mycnf-mysql-configuration-file/#findComment-1415183 Share on other sites More sharing options...
kicken Posted February 26, 2013 Share Posted February 26, 2013 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. Link to comment https://forums.phpfreaks.com/topic/274921-mysql-not-using-hostname-user-and-password-from-mycnf-mysql-configuration-file/#findComment-1415200 Share on other sites More sharing options...
DavidAM Posted February 26, 2013 Share Posted February 26, 2013 Or you could read the values from your my.cnf file in the PHP script and pass them to PDO -- see parse_ini_file. Link to comment https://forums.phpfreaks.com/topic/274921-mysql-not-using-hostname-user-and-password-from-mycnf-mysql-configuration-file/#findComment-1415230 Share on other sites More sharing options...
Jessica Posted February 26, 2013 Share Posted February 26, 2013 It does look like by default PDO should access that file - but I don't know how you'd get the values from it. Link to comment https://forums.phpfreaks.com/topic/274921-mysql-not-using-hostname-user-and-password-from-mycnf-mysql-configuration-file/#findComment-1415238 Share on other sites More sharing options...
Twinbird Posted February 27, 2013 Author Share Posted February 27, 2013 Thanks for the replies guys! PHP/PDO is using mysql client libraries, and not mysqlnd. I tried parse_ini_file('/etc/mysql/my.cnf') but my.cnf is not a valid configuration file. Link to comment https://forums.phpfreaks.com/topic/274921-mysql-not-using-hostname-user-and-password-from-mycnf-mysql-configuration-file/#findComment-1415399 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.