Accessing Laravel Herd's MySQL Service (or Any Other Service) from Your CLI

Laravel Herd is revolutionizing the way developers approach Laravel and PHP development on macOS. It's designed to be a comprehensive, native development environment that simplifies the initial setup process, allowing you to dive directly into what matters most: building your applications. Among its numerous features, Herd includes out-of-the-box (for PRO users) support for key services like MySQL or Redis, and others, making it easier than ever to integrate databases into your projects.

This guide will walk you through setting up MySQL to work seamlessly with Laravel Herd from the command line interface (CLI), enhancing your development workflow.

Getting Started with Laravel Herd

Before diving into MySQL integration, ensure that you have Laravel Herd installed on your macOS system. Herd bundles essential tools like PHP and nginx within a fast, native application environment, streamlining the Laravel development setup.

Configuring MySQL for CLI Usage

To efficiently use MySQL from the CLI, follow these steps:

1. Setting the Path to MySQL Binaries

To begin, you'll need to make the MySQL service binaries accessible from your CLI. This involves adding the MySQL binary path to your system's PATH variable. The process varies slightly depending on your shell (e.g., zsh, bash, fish), but here's how you can do it using the popular zsh shell:

Open your terminal and edit your .zshrc file, typically found in the home directory, by running:

nano ~/.zshrc

Then, append the following line:

export PATH="/Users/Shared/Herd/services/mysql/8.0.36/bin:$PATH"

Replace 8.0.36 with the version of MySQL bundled with your Laravel Herd installation. This command ensures that your system recognizes the MySQL CLI commands.

After adding this line, save and exit the editor. Then, apply the changes by running:

source ~/.zshrc

For other shells like bash or fish, the process involves editing the respective shell's configuration file (e.g., .bash_profile for bash) with a similar PATH modification.

2. Configure MySQL Client

Next, you'll need to inform MySQL about the location of the socket file created by Laravel Herd, as it uses a custom name for this file. To do so:

Create a new file called .my.cnf in your home directory:

nano ~/.my.cnf

Paste the following configuration, adjusting the socket path as needed:

[client]
user=root
password=
socket=/tmp/mysql-<UNIQUE_ID>.sock

The UNIQUE_ID segment corresponds to a generated identifier specific to your Herd MySQL service instance. Locate the exact *.sock file in the /tmp directory to find your unique socket path.

Conclusion

Your macOS system is now set up to use MySQL from the CLI with Laravel Herd. These steps not only simplify database management for your Laravel projects but also empower you to harness the full potential of Laravel Herd's streamlined development environment. Remember, the socket file name may vary, requiring a quick verification in the /tmp directory.

By following this guide, you can enhance your Laravel development workflow on macOS, embracing the efficiency and convenience offered by Laravel Herd.