How to Install MySQL on Linux (Ubuntu & CentOS) – Step-by-Step Guide for Beginners

  • 2025-08-23
  • OS
OS

1. What is MySQL?

MySQL is an open-source relational database management system (RDBMS) widely used as a backend for web applications. It is often combined with programming languages such as PHP and Python. As a free and open-source software, MySQL has become one of the most popular database systems around the world.

Main Features of MySQL:

  • Free and Open-Source: Available for both personal and commercial use without restrictions.
  • High Performance: Efficiently handles large-scale data processing with strong reliability.
  • Scalability: Flexible enough to scale from small projects to enterprise-level applications.

In this article, we’ll explain how to install MySQL on Linux environments (Ubuntu and CentOS), introduce basic operations, and provide solutions for common errors.

2. Preparation: Linux Environment Setup

Before installing MySQL, it’s important to make sure your Linux system is up to date. Outdated systems may cause dependency or compatibility issues.

2.1 System Requirements

The basic requirements for installing MySQL are as follows:

  • Memory: At least 512MB (1GB or more recommended)
  • Disk Space: Minimum of 500MB
  • OS Version: Ubuntu 20.04 or later, CentOS 7 or later

2.2 Update the System

To ensure a smooth installation, update your system packages before starting. Run the following commands:

  • For Ubuntu:
sudo apt update && sudo apt upgrade -y
  • For CentOS:
sudo yum update -y
Update Linux system before installing MySQL

3. How to Install MySQL on Ubuntu

3.1 Install via APT Repository

Install MySQL from the official APT repository by running:

sudo apt install mysql-server -y

3.2 Strengthen Security Settings

After installation, run the mysql_secure_installation command to configure security options such as setting a root password and disabling unnecessary defaults.

sudo mysql_secure_installation

3.3 Start and Check MySQL Service

Verify that MySQL was installed successfully and start the service with the following commands:

sudo systemctl start mysql
sudo systemctl status mysql

4. How to Install MySQL on CentOS

4.1 Set Up Yum Repository

On CentOS, add the official MySQL repository by running:

sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

4.2 Install MySQL

After setting up the repository, install MySQL with:

sudo yum install mysql-community-server

4.3 Start and Enable MySQL

Once installed, start MySQL and enable it to launch automatically on boot:

sudo systemctl start mysqld
sudo systemctl enable mysqld
Install MySQL on CentOS using Yum

5. Basic Operations

After installing MySQL, you can create databases and manage users with the following basic commands.

5.1 Log in to MySQL

To log in as root, run:

mysql -u root -p

5.2 Create a Database

Create a new database with:

CREATE DATABASE example_db;

Check if the database was created successfully:

SHOW DATABASES;

5.3 Create a User and Allow Remote Access

Create a new user and grant privileges for external access:

CREATE USER 'new_user'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'new_user'@'%';

6. Troubleshooting

6.1 MySQL Error 1045: Access Denied

If you see the error “ERROR 1045: Access denied for user ‘root’@’localhost’” when trying to log in, try the following solutions:

  1. Use the correct password: Make sure the root password is entered correctly.
  2. Reset the root password: If you’ve forgotten the password, restart MySQL in safe mode and reset it:
sudo mysqld_safe --skip-grant-tables &
mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
  1. Check the port settings: Confirm MySQL is listening on the default port 3306 and update if necessary.
SHOW VARIABLES LIKE 'port';

Update the configuration if needed and restart MySQL.

Fix MySQL error 1045 access denied

7. Conclusion

We have covered how to install MySQL on Linux (Ubuntu and CentOS), explained the basic operations, and shared troubleshooting steps for common issues such as Error 1045. Use this guide to install and manage MySQL effectively on your server environment.