Skip to main content

Step-by-Step Tutorial: Installing MySQL on Ubuntu

MySQL is a widely-used open-source relational database management system that provides a robust and scalable solution for storing and managing data. In this step-by-step tutorial, we will guide you through the process of installing MySQL on an Ubuntu system. By following these instructions, you will have a fully functional MySQL installation up and running in no time!

Prerequisites

Before we begin, make sure you have the following prerequisites in place:

  • An Ubuntu system (version 18.04 or higher)
  • Access to a terminal or command-line interface
  • Administrative privileges (sudo access)

Now, let's dive into the installation process:

Step 1: Update System Packages

  1. Open a terminal by pressing Ctrl+Alt+T or searching for "Terminal" in the application launcher.
  2. Update the package lists for upgrades and new package installations by running the following command:
sudo apt update
  1. This command will fetch the latest information about available packages from the Ubuntu repositories.

Step 2: Install MySQL Server Package

  1. Once the package lists are updated, you can proceed to install the MySQL server package by executing the following command:
sudo apt install mysql-server
  1. You will be prompted to enter your sudo password. Type it in and press Enter to continue.

  2. Ubuntu will download and install the necessary packages for MySQL. During the installation, you will be asked to confirm by typing Y and pressing Enter.

Step 3: Configure MySQL Server

  1. After the installation is complete, the MySQL server will start automatically. However, you need to run a security script to configure some initial settings.

  2. In the terminal, type the following command:

sudo mysql_secure_installation
  1. You will be prompted to enter the MySQL root password. Since this is a fresh installation, there won't be any password set yet. Press Enter to proceed without entering a password.

  2. Next, you will be asked if you want to set a root password. Type Y and press Enter to set a secure password for the MySQL root user. Follow the instructions to choose a strong password and confirm it.

  3. You will be asked a series of questions regarding the security of your MySQL installation. It is recommended to answer Y to all of them to improve security. These questions generally include removing anonymous users, disallowing root login remotely, removing test databases, and reloading privilege tables.

Step 4: Verifying MySQL Installation

  1. To ensure that MySQL is installed correctly and running, you can use the following command to check the status of the MySQL service:
sudo systemctl status mysql
  1. If MySQL is running properly, you will see an output indicating that the service is active and running.

Step 5: Accessing MySQL

  1. To access the MySQL command-line interface, use the following command:
mysql -u root -p
  1. You will be prompted to enter the MySQL root password you set during the configuration step (Step 3.4).

  2. After entering the password, you will be greeted with the MySQL command prompt, indicating a successful connection.

Congratulations! You have successfully installed MySQL on your Ubuntu system. You can now start using MySQL to create databases, tables, and perform various operations.

Usage Examples

Here are a few examples of common MySQL commands to get you started:

  1. Creating a new database:
CREATE DATABASE mydatabase;
  1. Switching to a specific database:
USE mydatabase;
  1. Creating a new table:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(100)
);
  1. Inserting data into a table:
INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]');
  1. Querying data from a table:
SELECT * FROM users;

Remember to end each SQL statement with a semicolon (;).

What to Watch Out For

  • During the MySQL installation, make sure to set a strong password for the root user to enhance security.
  • When executing SQL statements, pay attention to the syntax and ensure that each statement ends with a semicolon (;).
  • Be cautious when performing operations that modify or delete data, as they can have irreversible consequences.

That's it! You now have MySQL installed on your Ubuntu system and are ready to unleash its power for your database needs. Enjoy exploring the world of MySQL!