Here is a quick step-by-step tutorial on how to install MySQL on a Mac using Homebrew. Currently I am using MacOS Sonova 14.4.1.
Before we start, it's a good idea to check if MySQL is already installed on your machine. In the Terminal window, type the following command and press Enter:
mysql --version
If MySQL is installed, you'll see the version number displayed. If you don't see a version number or if you see an error message indicating that MySQL is not found, proceed to the next step to install it using Homebrew.
Install MySQL Using Homebrew
If you don't already have Homebrew installed, you can install it by running the following command in Terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
If you already have Homebrew installed, its a good idea to make sure you have the latest version. In terminal run the following command:
brew update
Once Homebrew is installed and updated, you can proceed to install MySQL by running the following command:
brew install mysql
Start MySQL Server
After the installation is complete, you can start the MySQL server using Homebrew services. Run the following command:
brew services start mysql
You should see:
Verify MySQL Installation
To verify that MySQL is installed and running correctly, you can connect to the MySQL server using the command-line client. Type the following command in Terminal and press Enter:
mysql -u root -p
You'll be prompted to enter the MySQL root password. If you haven't set a password yet, you can press Enter to connect without a password.
If everything is set up correctly, you'll be greeted with the MySQL command-line interface, indicating that MySQL is installed and running on your Mac.
That's it! You've successfully installed MySQL on your Mac using Homebrew. You can now start using MySQL for your database needs.
Once connected to the MySQL server, you can set a password for the root user using the following SQL command:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_new_password';
After setting the password, you need to flush the privileges to ensure that the changes take effect immediately. Run the following command:
FLUSH PRIVILEGES;
Once you've set the root password and flushed privileges, you can exit the MySQL command-line client by typing exit.
That's it! You've successfully set a root password for MySQL running on your local machine. Make sure to use this password when connecting to MySQL as the root user in the future.
Using the root user for routine tasks in a MySQL environment, especially in a development or production environment, is generally considered a security risk. The root user has unrestricted access to all databases and can perform any operation, which increases the potential impact of accidental or malicious actions.
Instead of using the root user for day-to-day tasks, it's a best practice to create a separate user with appropriate permissions for each project or application. Here's a recommended approach:
Create a New User
First Log in to MySQL as the root user:
mysql -u root -p
Once logged in, create a new user with a username and password specific to your project.
CREATE USER 'project_user'@'localhost' IDENTIFIED BY 'your_password';
Replace 'project_user' with your desired username and 'your_password' with a strong password.
Grant Permissions
After creating the user, you need to grant appropriate permissions to the user for the specific databases and operations required by your project. For example, if your project requires access to a database named project_db, you can grant permissions as follows:
GRANT ALL PRIVILEGES ON project_db.* TO 'project_user'@'localhost';
Adjust the permissions according to the specific needs of your project. Avoid granting more privileges than necessary. After granting permissions, flush the privileges to apply the changes immediately.
FLUSH PRIVILEGES;
Exit the MySQL command-line client
exit
By creating a dedicated user for each project with limited privileges, you can minimize the risk of accidental data loss or unauthorized access. Additionally, following the principle of least privilege ensures that users only have access to the resources they need to perform their tasks, enhancing the security of your MySQL environment.
Finally, don't forget to stop the MySQL service on your Mac!
brew services stop mysql
You can verify that the MySQL service has stopped by running:
brew services list
Resources