0

I'm seeing an issue where $mysqli = new mysqli($dbhost, $dbuser,'', $db); is giving me Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'user'@'localhost' in /Library/WebServer/Documents/index.php:6 Stack trace: #0 /Library/WebServer/Documents/index.php(6): mysqli->__construct('localhost', 'user', '', 'foo') #1 {main} thrown in /Library/WebServer/Documents/index.php on line 6 although running this file in the command line works just fine. I found a similar thread here PHP: can connect in terminal access denied in browser (Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'root'@'localhost' , but I'm on mariadb installed with homebrew on macos and the thread seemed to have been resolved by having uninstalled XAMMP, which I don't have installed.

I also tried to pass 127.0.0.1 as my host instead of localhost.

the linked duplicate post has nothing to do with my issue in this post as that post is reporting issues with opening the mysql system client to access a database, whereas I'm reporting issues with not being able to access the database through mysqli on a webserver.

reactor
  • 1,722
  • 1
  • 14
  • 34
  • The problem has nothing to do with mysql versions, hosting providers or software collections. It simply means that provided credentials are wrong. Just like when you login to Stack Overflow and it says that password is incorrect, you don't change the browser or PC. You just provide the correct login and password. As simple as that. – Your Common Sense Oct 06 '22 at 08:18
  • @YourCommonSense the credentials I passed would be identical from the webserver as from the command line. I'm not sure why I see success in one but not the other. – reactor Oct 06 '22 at 08:20
  • given both web-server and command line are on the same host, it means that identical credentials should work. So we can conclude that they are not indentical. – Your Common Sense Oct 06 '22 at 08:23
  • what exact command you are using from cli? – Your Common Sense Oct 06 '22 at 08:28
  • @YourCommonSense I'm running `php index.php` with my script looking like this: `query("select * from users"); foreach($result as $r){ echo $r['ip']."\n"; } ?>` – reactor Oct 06 '22 at 08:30
  • what exact command you are using from **cli**? CLI means **command line interface** you mentioned earlier – Your Common Sense Oct 06 '22 at 08:37
  • When CLI works and php do not probably is due to mysql configuration. On CLI even if you specify the host as localhost or 127.0.0.1 it goes by socket, but in php it goes by host. Check mysql configuration if it allows netwrok connection (skip-networking should be commented) and bind address (bind-address = 127.0.0.1) – Roberto Braga Oct 06 '22 at 08:52
  • @RobertoBraga that's not true. Both can connect both ways – Your Common Sense Oct 06 '22 at 08:54
  • @YourCommonSense from mysql manual: On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs: the client connects using a Unix socket file. The --socket option or the MYSQL_UNIX_PORT environment variable may be used to specify the socket name. https://dev.mysql.com/doc/refman/8.0/en/connecting.html mysql programs means mysql mysqldump etc etc, php has its own internal implementation that use any hostname, included localhost, as hostname – Roberto Braga Oct 06 '22 at 09:00
  • @RobertoBraga that's your [wrong] conjecture. PHP doesn't have anything own, it uses the same API from mysql. That follows everything you written about Unix – Your Common Sense Oct 06 '22 at 09:13
  • @YourCommonSense from PHP manual: MySQL Native Driver **is a replacement for the MySQL Client Library (libmysqlclient)**. MySQL Native Driver is part of the official PHP sources as of PHP 5.3.0. https://www.php.net/manual/en/intro.mysqlnd.php and from PDO_mysql page: **Mysqlnd is the default library**. For details about choosing a library, see Choosing a MySQL library. https://www.php.net/manual/en/ref.pdo-mysql.php – Roberto Braga Oct 06 '22 at 13:42
  • @YourCommonSense MysqlND has been developed for better integration in PHP. One of the issue using mysql client library use double amount of memory: php execute the query, libmysql retrieve the data, php copy all the data from libmysql. Therefore, during the script execution, you had the recordset stored twice once in php and one in libmysql without considering the time and cpu work for the copy. MysqlND solve this issue plus other. From mysql site https://dev.mysql.com/downloads/connector/php-mysqlnd/ – Roberto Braga Oct 06 '22 at 14:03
  • @RobertoBraga sorry, I am lost here. Is it related to the problem in the Opening Post? – Your Common Sense Oct 06 '22 at 14:24
  • @RobertoBraga by the way, where I can read about that double memory issue? I had no idea it existed. – Your Common Sense Oct 06 '22 at 14:26
  • doing this fixed it for me: https://stackoverflow.com/questions/8035939/write-privileges-localhost-mac-osx – reactor Oct 07 '22 at 01:40
  • @YourCommonSense >Is it related to the problem in the Opening Post? Yes read my first post >by the way, where I can read about that double memory issue https://blogs.oracle.com/solaris/post/mysql-native-driver-for-php-mysqlnd reduced memory footprint -- keeps every row only once in memory, where as with libmysql you have it twice in memory – Roberto Braga Oct 10 '22 at 06:55
  • @RobertoBraga your initial post mention the non-existent difference between cli and cgi environments. None of your later comments support this claim. Either way, it seems the issue has been resolved already. Hardly surprising, the alleged "solution" is also irrelevant to the problem. – Your Common Sense Oct 10 '22 at 07:12

0 Answers0