PHP mysqli_connect() Error on "localhost" on macOS

This section provides a tutorial on the PHP mysqli_connect() 'No such file or directory' error, and the workaround of replacing 'localhost' with '127.0.0.1'.

If you try to connect the MySQL server on the local macOS system with "localhost" as the server name, using PHP mysqli_connect() function, you may get the "No such file or directory" error.

Here is the my PHP test script, MySQL-Localhost.php:

%lt;?php
#  MySQL-Localhost.php
#  Copyright (c) 2005 HerongYang.com. All Rights Reserved.
#
   $con = mysqli_connect('localhost','guest','guest');
   print "MySQL server info = ".mysqli_get_server_info($con)."\n";
   print "MySQL status = ".mysqli_stat($con)."\n";
   mysqli_close($con);
?>

Here is the error message I got, when running it to reach the MySQL server on my local macOS computer:

herong$ php MySQL-Localhost.php
PHP Warning:  mysqli_connect(): (HY000/2002): No such file or directory in \
   /Users/herong/MySQL-Localhost.php on line 6
...

One way to avoid this error is to use the IP address representing the "localhost". You can find the IP address of "localhost" with the "ping" command:

herong$ ping localhost

PING localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.046 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.065 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.071 ms
^C

Update the PHP test script with the IP address:

<?php
#  MySQL-IP.php
#  Copyright (c) 2005 HerongYang.com. All Rights Reserved.
#
   $con = mysqli_connect('127.0.0.1','guest','guest');
   print "MySQL server info = ".mysqli_get_server_info($con)."\n";
   print "MySQL status = ".mysqli_stat($con)."\n";
   mysqli_close($con);
?>

The updated script should work now:

herong$ php MySQL-IP.php

MySQL server info = 8.0.17
MySQL status = Uptime: 180642  Threads: 11  Questions: 299593
   Slow queries: 0  Opens: 728  Flush tables: 3  Open tables: 609
   Queries per second avg: 1.658

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 PHP Programs and MySQL Server

 Perl Programs and MySQL Servers

 Java Programs and MySQL Servers

 Datatypes and Data Literals

 Operations and Expressions

 Character Strings and Bit Strings

 Commonly Used Functions

 Table Column Types for Different Types of Values

 Using DDL to Create Tables and Indexes

 Using DML to Insert, Update and Delete Records

 Using SELECT to Query Database

 Window Functions for Statistical Analysis

 Use Index for Better Performance

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 System, User-Defined and Stored Procedure Variables

 MySQL Server Administration

 Storage Engines in MySQL Server

 InnoDB Storage Engine - Primary and Secondary Indexes

 Performance Tuning and Optimization

 Bulk Changes on Large Tables

MySQL Server on macOS

 Install MySQL Database Server on macOS

PHP mysqli_connect() Error on "localhost" on macOS

 Installing MySQL Server on Linux

 Connection, Performance and Second Instance on Linux

 Archived Tutorials

 References

 Full Version in PDF/EPUB