Skip to main content

How to Install MySQL on CentOS

MySQL is a popular and widely used open-source relational database management system (RDBMS). It provides a powerful and flexible way to store and manage large amounts of data. This step-by-step tutorial will guide you through the process of installing MySQL on CentOS, a popular Linux distribution.

Prerequisites

Before we begin, make sure you have the following:

  • A CentOS server with root access or a user account with sudo privileges.
  • A stable internet connection.

Step 1: Update System Packages

To ensure that your system is up to date, it's a good practice to update the system packages before installing any new software. Open a terminal and run the following command:

sudo yum update

This command will update all the installed packages on your CentOS system.

Step 2: Install the MySQL Repository

By default, CentOS provides an older version of MySQL in its official repository. To install the latest version, we need to add the official MySQL repository. Execute the following command to download and install the repository package:

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

This command will download and install the MySQL repository package.

Step 3: Install MySQL Server

Now that we have added the MySQL repository, we can proceed to install the MySQL server. Run the following command:

sudo yum install mysql-server

This command will install the MySQL server and its dependencies.

Step 4: Start MySQL Service

Once the installation is complete, start the MySQL service by running the following command:

sudo systemctl start mysqld

This command will start the MySQL service on your CentOS system.

Step 5: Secure MySQL Installation

MySQL comes with a script that helps to secure the installation by setting a root password and removing some insecure default settings. Run the following command to start the script:

sudo mysql_secure_installation

The script will prompt you to set a root password, remove anonymous users, disallow root login remotely, remove the test database, and reload the privilege tables. Follow the prompts and answer the questions accordingly.

Step 6: Verify MySQL Installation

To confirm that MySQL is installed correctly and running, execute the following command:

sudo systemctl status mysqld

This command will display the status of the MySQL service. If it is running, you will see a message indicating that the service is active.

Step 7: Access MySQL Command Line

To access the MySQL command line, use the following command:

mysql -u root -p

You will be prompted to enter the root password you set during the installation. After entering the password, you will be logged in to the MySQL command line interface.

Usage Examples

Here are a few examples of common MySQL commands that you can try:

  • List all databases: SHOW DATABASES;
  • Create a new database: CREATE DATABASE mydatabase;
  • Switch to a database: USE mydatabase;
  • Create a table:
  CREATE TABLE mytable (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);

What to Watch Out For

During the installation process, there are a few things you should keep in mind:

  • Make sure you have a stable internet connection to download the necessary packages.
  • Double-check the repository URL to ensure that you are installing the correct version of MySQL.
  • When setting the root password, choose a strong and secure password to protect your MySQL installation.

Conclusion

Congratulations! You have successfully installed MySQL on your CentOS system. You can now start using MySQL to store and manage your data. Remember to refer to the official MySQL documentation for more advanced usage and configurations.