Hosting + Ai Website Builder + Free Domain (3 Month Free Credit)
Shop Today

How to Create MySQL on Linux Server (Installation & Management Guide)

MySQL is one of the most popular open-source relational database management systems (RDBMS), trusted by developers and businesses worldwide. Running MySQL on Linux provides reliability, scalability, and high performance, while allowing users to easily create MySQL databases for managing and organizing data efficiently.

How to Fix MySQL on Linux Server
How to Fix MySQL on Linux Server

In this guide, we’ll explain how to create MySQL on Linux server, configure it for security, manage services effectively, and troubleshoot common issues.

Prerequisites

Before beginning the installation, you need to make sure your system is ready. Having the right prerequisites avoids errors during setup.

  • A Linux distribution (Ubuntu, Debian, CentOS, or RHEL)
  • Root or sudo user privileges
  • Stable internet connection
  • Basic knowledge of Linux commands

Step-by-Step Guide to Install MySQL on Linux Server

Installing MySQL on a Linux server provides a reliable foundation for managing data, powering applications, and supporting dynamic websites.

  • Updating the System

Updating packages ensures you’re installing MySQL in a secure and stable environment.

sudo apt update && sudo apt upgrade -y   # For Ubuntu/Debian  
sudo yum update -y                      # For CentOS/RHEL 
  • Installing MySQL Server

MySQL installation is straightforward using the default package managers.

  • On Ubuntu/Debian:
sudo apt install mysql-server -y
  • On CentOS/RHEL:
sudo yum install @mysql -y
  • Starting and Enabling MySQL Service

Once installed, you must start the MySQL service and ensure it launches on system boot.

sudo systemctl start mysql    # or mysqld on CentOS  
sudo systemctl enable mysql
  • Verifying Installation

It’s important to confirm MySQL is running correctly.

sudo systemctl status mysql

You can also log in to the MySQL shell to test:

mysql -u root -p

Configuring MySQL on Linux

After installing MySQL, proper configuration is necessary to optimize performance, enhance security, and ensure efficient database management. Configuration helps tailor MySQL settings to match your application and server requirements.

Securing MySQL Installation

MySQL ships with a built-in script to strengthen its security. Running it removes weak defaults and improves safety.

sudo mysql_secure_installation

This script allows you to:

  • Set a strong root password
  • Remove anonymous users
  • Disable remote root login
  • Remove test databases
  • Creating Databases and Users

To start using MySQL, you need databases and users with proper privileges. Inside the MySQL shell:

CREATE DATABASE mydb;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;

Securing MySQL on Linux Server

Security is crucial to protect your data from unauthorized access. MySQL offers several ways to improve protection.

  • Firewall Rules: Allow only necessary access to port 3306. sudo ufw allow 3306/tcp
  • Enable SSL/TLS: Encrypt client-server communication.
  • Limit Remote Access: Only allow connections from trusted IP addresses.

Managing MySQL Services on Linux

After installation, you’ll need to manage MySQL regularly. Linux provides system commands to control its operation easily.

  • Starting and Stopping MySQL

You may need to start or stop MySQL during maintenance or updates.

sudo systemctl start mysql  
sudo systemctl stop mysql  
  • Restarting and Reloading MySQL

Restarting is used after major configuration changes, while reloading applies small adjustments without downtime.

sudo systemctl restart mysql  
sudo systemctl reload mysql 
  • Checking Service Status

Knowing whether MySQL is active or failed is important for stability.

sudo systemctl status mysql 
  • Enabling MySQL on Boot

Ensure MySQL launches automatically after server restarts.

sudo systemctl enable mysql 
  • Monitoring Logs

Logs provide insights into errors, warnings, and performance. On Ubuntu, check /var/log/mysql/, while CentOS users should check /var/log/mysqld.log. Regular log monitoring helps prevent downtime.

Common Issues and Fixes

Even with a proper setup, you may face issues. Here are some common problems and their fixes.

  • MySQL Failed to Start
    • Check service errors with systemctl status mysql.
    • Fix incorrect configurations in /etc/mysql/.
  • Access Denied for User
    • Review user permissions:
    SHOW GRANTS FOR 'myuser'@'localhost';
  • Port Conflict on 3306
    • Check if another service is using the port:
    sudo lsof -i :3306
    • Change the port in my.cnf if required.

For advanced fixes, community documentation often provides solutions to fix MySQL issues in Linux effectively.

FAQs to Create MySQL on Linux Server

How do I check the MySQL version on Linux?

You can run:

mysql --version

to display the installed version.

How do I reset the MySQL root password in Linux?

Stop the MySQL service, restart it with --skip-grant-tables, log in without a password, and update the root password with SQL commands. Restart normally afterward.

Can I allow remote MySQL access on Linux?

Yes, by editing the mysqld.cnf file, updating firewall rules, and granting remote privileges to the user. However, always secure remote access with strong authentication and SSL/TLS.

Conclusion

MySQL on Linux is a powerful and stable database solution that can support everything from small applications to enterprise systems. By following this guide, you’ve learned how to create MySQL, install, configure it securely, manage services, and handle common issues. With proper management and monitoring, your MySQL database will remain reliable and efficient. For more, visit, MySQL offical documentation.

Himanshu Joshi

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top