How to Install MySQL on Ubuntu

MySQL is a widely used open-source database management system. It is one of the most popular databases used globally. In this article, we will discuss how to install MySQL on Ubuntu.

Ubuntu is one of the most popular Linux-based operating systems. It is widely used in the server environment. MySQL is the default database server for Ubuntu. Therefore, it is easy to install and set up MySQL on Ubuntu.

Before we start, let’s make sure that we have the latest updates installed on our Ubuntu system. To do this, open a terminal and type the following command:

Sudo apt update && sudo apt upgrade

This will update the package lists and install any available updates.

Now, let’s move on to the installation of MySQL on Ubuntu.

Step 1: Install MySQL

To install MySQL on Ubuntu, we need to use the apt package manager. Open a terminal and type the following command:

Sudo apt install mysql-server

This command will install the MySQL server and client packages on your Ubuntu system.

Step 2: Secure MySQL

Once the installation is complete, we need to secure MySQL by running the following command:

Sudo mysql_secure_installation

This command will ask you a series of questions that will help you to secure your MySQL installation. You will be prompted to set a root password, remove anonymous users, disable root login remotely, and remove the test database.

Step 3: Start and Enable MySQL

To start the MySQL service, run the following command:

Sudo systemctl start mysql

To enable the MySQL service to start automatically at boot time, run the following command:

Sudo systemctl enable mysql

Step 4: Verify MySQL

To verify that MySQL is running and listening on the default port, run the following command:

Sudo netstat -plunt | grep mysqld

This command will show you the MySQL process and the port it is listening on.

Step 5: Connect to MySQL

To connect to MySQL, you can use the MySQL client command-line tool. To do this, run the following command:

Mysql -u root -p

You will be prompted to enter the root password you set during the installation.

Step 6: Create a Database

To create a new database, run the following command:

CREATE DATABASE dbname;

Replace dbname with the name of the database you want to create.

Step 7: Create a User

To create a new user, run the following command:

CREATE USER ‘username’@’localhost’ IDENTIFIED BY ‘password’;

Replace username and password with the username and password you want to use for the new user.

Step 8: Grant Privileges

To grant privileges to the new user, run the following command:

GRANT ALL PRIVILEGES ON dbname.* TO ‘username’@’localhost’;

Replace dbname, username, and localhost with the name of the database, username, and hostname you want to grant privileges for.

Step 9: Flush Privileges

To apply the changes you made to the privileges, run the following command:

FLUSH PRIVILEGES;

Step 10: Exit MySQL

To exit MySQL, run the following command:

Exit

In this article, we discussed how to install MySQL on Ubuntu. We covered the steps involved in installing, securing, starting, and verifying MySQL on Ubuntu. We also covered how to create a database, user, and grant privileges to the user. With this knowledge, you can now get started with using MySQL on Ubuntu.