DEV Community

Gaurav Singh
Gaurav Singh

Posted on

How to setup MySql on MacOS using Brew.

Setting up MySql with MacOs

Step1: Installation from brew.

brew install mysql
Enter fullscreen mode Exit fullscreen mode

Step2: Run this command if we don't want to run MySql in background.

/opt/homebrew/opt/mysql/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql
Enter fullscreen mode Exit fullscreen mode

or for running it in background or at login run this command.

brew services start mysql
Enter fullscreen mode Exit fullscreen mode

I prefer not to run it in background or when i login into my mac.

Step3: Let's check it is working correctly.

Connect to MySQL.

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

Run the Query.

SHOW DATABASES;
Enter fullscreen mode Exit fullscreen mode

Step4: For turning it off we can use this command (Only if you not running it in background).

/opt/homebrew/opt/mysql/bin/mysqladmin -u root -p shutdown
Enter fullscreen mode Exit fullscreen mode

Step5:(Optional): We can add alias for it for starting it and stopping it.

add alias in ~/.zshrc file or if you bash add it in ~/.bashrc file.

sql-start

alias sql-start='/opt/homebrew/opt/mysql/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql'
Enter fullscreen mode Exit fullscreen mode

sql-stop

alias sql-stop="/opt/homebrew/opt/mysql/bin/mysqladmin -u root -p shutdown"
Enter fullscreen mode Exit fullscreen mode

By doing this you can start and stop it when need.

Top comments (0)