November 9, 2023
This guide provides step-by-step instructions for installing and using MySQL on macOS. It's tailored for engineers looking to get MySQL up and running quickly and efficiently.
1. Installing MySQL
Using Homebrew:
If you don't have Homebrew installed, you'll want to start there:
/bin/bash -c "$(curl -fsSL <https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh>)"
Once Homebrew is installed:
brew update brew install mysql
Manual Installation:
- Go to the official MySQL website's download page and download the macOS version of MySQL Community Server.
- Open the downloaded DMG file.
- Follow the installation instructions.
2. Starting and Stopping the MySQL Server
Using Homebrew:
# To start MySQL brew services start mysql # To stop MySQL brew services stop mysql
Manually:
sudo /usr/local/mysql/support-files/mysql.server start sudo /usr/local/mysql/support-files/mysql.server stop
3. Connecting to the MySQL Server
By default, MySQL creates a user root
with no password. You can connect to the MySQL server with the following command:
mysql -uroot
If you've set a password for root
(recommended):
mysql -uroot -p
It will then prompt you for the password.
4. Basic MySQL Commands
Once connected, here are a few basic commands to get started:
- Show all databases:
SHOW DATABASES;
- Create a new database:
CREATE DATABASE dbname;
- Switch to a database:
USE dbname;
- Show all tables in the current database:
SHOW TABLES;
- Display structure of a table:
DESCRIBE tablename;
Remember to end each command with a semicolon (;
).
5. Uninstalling MySQL
Using Homebrew:
brew remove mysql brew cleanup
Manually:
Stop the MySQL server:
sudo /usr/local/mysql/support-files/mysql.server stop
Remove MySQL files:
sudo rm -rf /usr/local/mysql sudo rm -rf /usr/local/mysql-* sudo rm -rf /Library/StartupItems/MySQLCOM
Remove MySQL preferences:
sudo rm -rf /Library/PreferencePanes/My* rm -rf ~/Library/PreferencePanes/My* sudo rm -rf /Library/Receipts/mysql* sudo rm -rf /Library/Receipts/MySQL* sudo rm -rf /private/var/db/receipts/*mysql*
Remove the MySQL user and group (be cautious with this step):
sudo dscl . -delete /Users/mysql sudo dscl . -delete /Groups/mysql
That's it! You should now have a solid foundation for using MySQL on your Mac. As always, consult the official MySQL documentation for deeper dives into specific topics and advanced configurations.