How to install MySQL server on Mac from Terminal?
Here are the minimum steps you need to follow to have your own MySQL server up and running in no time:
Open a terminal and type
> brew install mysql
After successful installation, it is advisable to set a password for the root user.
Let’s first start the server:
> brew services start mysql
Then, run the following to set a root password:
> mysql_secure_installation
Now with this, you can log in to the MySQL server from the terminal:
> mysql -u root -p
Enter the password to connect to the server.
It is not recommended to use the root user for your analysis and you should create users with restricted privileges to protect your database in case of a breach.
Let’s say you want to have a database named crypto and Alice is going to use this database to perform CRUD operations.
Creating a database called crypto. While you are logged into MySQL, type:
mysql> CREATE DATABASE crypto;
Now, let’s create a user named alice and give all privileges to this database.
mysql> CREATE USER 'alice'@'localhost' IDENTIFIED BY 'password';mysql> GRANT ALL PRIVILEGES on crypto.* TO 'alice'@'localhost';
Now Alice can log in to the MySQL server:
> mysql -u alice -p -D crypto
Once you are done, if this is a database you have set up on your laptop to play with, you should shut the server down.
> brew service stop mysql
That’s it. Pretty easy, isn’t it?
Happy SQLing!