How to Install MySQL on Ubuntu

  • Post category:Ubuntu

MySQL is one of the most widely used open-source relational database management systems (RDBMS). It’s popular for web-based applications and is an essential tool for developers working on databases. This guide walks you through installing MySQL on Ubuntu 22.04 and similar versions.

Step 1: Update the Package Repository

Before installing MySQL, it’s important to ensure that your package repository is up to date.

Step 1.1: Open Terminal

Press Ctrl + Alt + T to open your terminal.

Step 1.2: Update the Package List

Run the following command to update your package list:

sudo apt update

Step 1.3: Upgrade Installed Packages (Optional)

You can also upgrade all currently installed packages with this command:

sudo apt upgrade

This step is optional but recommended for optimal system performance.

Step 2: Install MySQL Server

Step 2.1: Install MySQL

To install MySQL server, execute this command:

sudo apt install mysql-server

The installation will start, and all necessary dependencies will be downloaded.

Step 2.2: Verify Installation

Once installed, you can verify the MySQL version by running:

mysqld --version

You should see the MySQL version installed on your system.

Step 3: Secure MySQL Installation

After installation, it’s important to secure your MySQL server.

Step 3.1: Run the Security Script

Run the following security script:

sudo mysql_secure_installation

This script will prompt you to:

  • Set a root password (if you haven’t already).
  • Remove anonymous users.
  • Disallow remote root login.
  • Remove test databases.

Step 3.2: Follow Prompts

Follow the on-screen prompts to configure the security settings. It’s highly recommended to enforce strong security measures during this step.

Step 4: Check MySQL Service Status

Step 4.1: Verify MySQL Service Status

To check if MySQL is running, use the following command:

sudo systemctl status mysql

If the output shows “active (running),” MySQL is successfully installed and running.

Step 5: Log in to MySQL

Step 5.1: Access MySQL Command Line

To access the MySQL command line interface (CLI), use the following command:

sudo mysql -u root -p

You’ll be prompted to enter the password you set during the MySQL secure installation.

Step 5.2: Start Managing Databases

Once logged in, you can begin creating and managing databases, users, and tables.

Conclusion

By following this guide, you now have a working installation of MySQL on your Ubuntu system. With the MySQL server up and running, you can start building databases for your applications or websites. Be sure to keep your MySQL installation secure by following best practices and frequently updating it.

For more tutorials and guides, check out CodeAllow for the latest on web development and database management.