2018-07-14 14:26 阅读 177

mysqli_connect error 2002“No such file or directory”Mac OS 10.13.2

I know questions related to this have been asked a zillion times on Stackoverflow and elsewhere, however, I can't seem to zero in on a resolution.

I am using a nearly fresh-out-of-the-box Macbook 13 running OS 10.13.6 High Sierra.

My goal is to create a development environment with MAMP such that I can create dynamic webpages and I'd like to use Tableau for analytics drawing data out of MySQL.

I have successfully installed and run MAMP - I have created a simple database using phpMyAdmin - and I have connected Tableau to this database successfully - this works very well.

With that in operation, I shifted my attention to trying to run php from my browser. Entering "localhost" yielded the "It works!" result.

All good so far but here is where things go off the rails: I cannot connect to MySQL via a php file using the following (****** = my root password) - filename: test_connect0.php:

$link = mysqli_connect("localhost", "root", "*****");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL ."<br>";
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL ."<br>";
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL ."<br>";
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

What returns from running Safari or Chrome - doesn't matter - is the familiar issue: 2002 No such file or directory.

BELIEVE ME, I have searched all over the internet for solutions and I believe I have tried each and everyone to no avail.

Here are a few things I have done:

  1. Running phpInfo from MAMP returns the expected path to php.ini: /Applications/MAMP/bin/php/php7.2.1/conf
  2. In that php.ini file, the mysqli.default_socket is /Applications/MAMP/tmp/mysql/mysql.sock and that file does indeed exist
  3. Running phpInfo from MAMP indicates the php version is 7.2.1
  4. In /etc/apache2, I edited httpd.conf and uncommented LoadModule php7_module libexec/apache2/libphp7.so
  5. I have tried a number of different ways to run the test_connect0.php file: localhost/test_connect0.php localhost:3306/test_connect0.php

An observation: In item 3 above, I noted that phpInfo from MAMP returns 7.2.1 as the version. When I run a phpinfo() script from the browser, however, it returns 7.1.16. I can't explain why that's a different php version and I cannot even FIND version 7.1.16 on my machine AND I'm not sure any of that really matters.

To summarize, I have a functional MAMP and a functional Tableau connection to MAMP's MySQL. What I can't do is connect to MySQL from a php file run in the browser - I get the 2002 error.

What other information can I provide to help zero in on a solution?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongxian6285 dongxian6285 2018-07-15 13:31

    I found the solution:

    PHP script fix: Originally, I had my test_connect0.php script in /Library/WebServer/Documents. It turns out, it needs to be in /Applications/MAMP/htdocs. Once I moved the file to that location, I entered this url into the browser: localhost:8888/test_connect0.php

    That resulted in a successful connection to MAMP's MySQL.

    Tableau notes: As I noted earlier, I was able to connect Tableau to MySQL, however, it wasn't connecting to MAMP's MySQL. To shift Tableau to see MAMP's MySQL, I entered the following into Tableau's dialog:

    Server: localhost Port: 8888 Username: root Password: ******

    Two things are necessary ahead of time:

    1. Install the OBDC drivers per Tableau's instructions
    2. Run this in a terminal window: sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
    点赞 评论 复制链接分享