How to install MySQL on Ubuntu

MySQL is an open-source and implements the relational model and uses Structured Query Language (SQL) to manage its data. This post will go over how to install MySQL on Ubuntu 20.04.

Installing MySQL

You should run two commands by following:

$ sudo apt update
$ sudo apt install mysql-server

After installing finished, we go to the next step for making some security configuration before using MySQL.

Configuring MySQL
We use the script mysql_secure_installation which prompts you to define the MySQL root password and other security-related options , including removing remote access to the root user and setting the root password.

$ sudo mysql_secure_installation

If you want to a remote access to your MySQL server, you should do:

$ sudo ufw enable
$ sudo ufw allow mysql

Running MySQL
To run the MySQL service from shell:

$ sudo systemctl start mysql
$ sudo systemctl enable mysql
$ mysql -u root -p

When you’re prompted for a password, enter the one that you set at installation time, the following mysql shell prompt should appear:

mysql>

For now, you can create a new user with a CREATE USER statement. These follow this general syntax:

mysql> CREATE USER 'username'@'host' IDENTIFIED WITH authentication_plugin BY 'password';

After creating your new user, you can grant them the appropriate privileges as follows:

mysql> GRANT PRIVILEGE ON database.table TO 'username'@'host';

You also can grant multiple privileges to the same user in one command by separating each with a comma or grant a user privileges globally by entering asterisks (*) in place of the database and table names for all.

mysql> GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'username'@'localhost' WITH GRANT OPTION;

Finally, you should run the FLUSH PRIVILEGES command. This will free up any memory that the server cached as a result of the preceding CREATE USER and GRANT statements:

mysql> FLUSH PRIVILEGES;

To exit the MySQL client:

mysql> exit;


Conclusion
This post gave you a basic MySQL setup installed on your server. To get more useful commands please go to the MySQL official website.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s