Open MySQL Using Mac Terminal

  1. Open MySQL on Mac Terminal
  2. Add MySQL Path to the System Environment for Easy Access

This article will teach you how to open MySQL using Mac Terminal. We anticipate you might find our method a bit complex, so we’ll teach you a simpler approach.

This approach works for macOS Catalina and later and macOS Mojave and earlier.

Open MySQL on Mac Terminal

First, ensure you have the temporary password created by MySQL when you installed it. Then launch your terminal and type in the following to open MySQL.

/usr/local/mysql/bin/mysql -u root -p

From the command above:

  1. -u is the flag that tells MySQL that the next parameter is a username.
  2. root is the username. If your username is not root, replace it accordingly.
  3. -p is the flag that tells MySQL the username has a password.

MySQL will ask for the temporary password when executing the command. Enter the password, and you’ll have access to MySQL on the terminal.

Now, you’ll notice the command is long, so let’s make it easier.

Add MySQL Path to the System Environment for Easy Access

You can add the MySQL Path to the system environment for easier access. As a result, you won’t have to type /usr/local/mysql/bin/mysql -u root -p every time you want to access MySQL.

Instead, you can type mysql -u root -p and your password. We’ll show you how to add the MySQL path for the following.

  1. macOS Catalina and later
  2. macOS Mojave and earlier

Add MySQL Path on macOS Catalina and Later

You can add a MySQL path to the .zprofile file in your home directory in macOS Catalina and later. Start with the following.

echo 'export PATH="$PATH:/usr/local/mysql/bin"' >> ~/.zprofile

The export command will set the MySQL environment variable. Therefore, the next command will load the ~/.zprofile into the current shell script.

As a result, the MySQL path becomes available in memory.

source ~/.zprofile

Now, you can use the following short command to access MySQL from the terminal.

mysql -u your_mysql_username -p

From the last command above, your_mysql_username is your MySQL username. This could be root or something similar.

Upon executing the previous command, MySQL will ask for the password. Enter the correct password, and you’ll have access to MySQL.

Add MySQL Path on macOS Mojave and Earlier

To add MySQL Path in macOS Mojave and earlier, follow the process for macOS Catalina and later. With the exception that you add the MySQL path to .bash_profile.

Now, open your terminal and do the following. First, export the MySQL path to .bash_profile.

echo 'export PATH="$PATH:/usr/local/mysql/bin"' >> ~/.bash_profile

Next, use the source command to load .bash_profile into the current shell.

source ~/.bash_profile

Then you can log in to MySQL using the following command on the terminal.

mysql -u your_mysql_username -p

Replace your_mysql_username with your MySQL username. Then press the Enter key on the keyboard and type your MySQL password.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.