-2

Just finished installing MySQL on my Macbook in order to do my web dev homework assignment. I'm running my web pages using the mac webserver at Macintosh HD/Library/WebServer/Documents/. When I try to execute my php file from the terminal with the following code to create a table in a database I get the error also below.

Code Executed from the terminal using "php fileName.php":

<?php
    $mysqli = new mysqli(localhost, root, ****, lab7);

    $query = "CREATE TABLE products (ID int primary key)";

    $mysqli->query($query);

    $mysqli->close();
?>

Error:

Warning: mysqli::__construct(): (HY000/2002): No such file or directory in /Library/WebServer/Documents/week7/Lab7/CreateTableTest.php on line 2

Warning: mysqli::query(): Couldn't fetch mysqli in /Library/WebServer/Documents/week7/Lab7/CreateTableTest.php on line 6

Warning: mysqli::close(): Couldn't fetch mysqli in /Library/WebServer/Documents/week7/Lab7/CreateTableTest.php on line 8
Andrew
  • 21
  • 3
  • Possible duplicate of [PDOException SQLSTATE\[HY000\] \[2002\] No such file or directory](https://stackoverflow.com/questions/20723803/pdoexception-sqlstatehy000-2002-no-such-file-or-directory) – Obsidian Age Aug 01 '18 at 22:35
  • `new mysqli('localhost', 'root', '****', 'lab7')` - you need to add quotes to those, if that's what you're really using for syntax. Since unquoted values are considered as constants. – Funk Forty Niner Aug 01 '18 at 22:35
  • `mysqli(localhost, root, ****, lab7)` needs to be `mysqli("localhost", "root", "****", "lab7")` - they're supposed to be string values. – ADyson Aug 01 '18 at 22:36
  • PHP's error reporting would have helped here, btw. – Funk Forty Niner Aug 01 '18 at 22:38
  • This isn't working even with the quotes. Is there other installation things I need to do? I have the server running and I've already created the database lab 7 in it. I was using this as a test to make sure i set everything up correctly. – Andrew Aug 01 '18 at 22:44
  • See if https://stackoverflow.com/questions/20073168/my-database-user-exists-but-i-still-get-an-hy000-2002-no-such-file-or-direct helps - i.e. change "localhost" to "127.0.0.1" instead. It may depend exactly how mysql was set up. – ADyson Aug 01 '18 at 23:08
  • In regards to the suggested duplicate question, have you tried changing 'localhost' to '127.0.0.1'? – khartnett Aug 01 '18 at 23:08
  • I did and got an additional error: Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in /Library/WebServer/Documents/week7/Lab7/CreateTableTest.php on line 2 – Andrew Aug 01 '18 at 23:41
  • Did you create the `lab7` database yet? – RiggsFolly Aug 01 '18 at 23:58

1 Answers1

0

Looks like it's due to a sha2 authentication method in MySQL 8 that php does not recognized. See the link below and thank you all for the help.

Link

Andrew
  • 21
  • 3
  • To add to this, I went to the preference pane and initialized a new database and chose the legacy configuration option and changed "localhost" to "127.0.0.1". The table was then successfully created in this test program. – Andrew Aug 02 '18 at 19:59