How To Install Mysql In Ubuntu

How To Install Mysql In Ubuntu

Reading time1 min
#Database#OpenSource#Linux#MySQL#Ubuntu#DatabaseAdministration

Step-by-Step Guide to Installing MySQL on Ubuntu for Robust Database Management

MySQL remains a foundational database technology powering countless applications, from small personal projects to large-scale enterprise systems. For developers and system administrators, mastering its installation on Ubuntu is essential to build, secure, and maintain scalable, high-performance database solutions.

Forget one-click installers—understanding the manual MySQL installation process on Ubuntu demystifies what’s happening under the hood, empowering you to troubleshoot, optimize, and fine-tune your database environment like a true pro.

In this step-by-step guide, we'll walk through installing MySQL on Ubuntu, configuring the server, securing the installation, and performing some basic checks to ensure your database is ready for action.


Why Install MySQL Manually on Ubuntu?

While many tools offer easy installation scripts, manually installing MySQL gives you:

  • Full control over configuration options
  • Insight into the service management and underlying directories
  • A foundation for advanced database administration
  • The ability to customize security settings from the start

Prerequisites

  • A machine running Ubuntu 20.04 or later (commands may vary slightly depending on the version)
  • sudo privileges on the system
  • An active internet connection for package downloads

Step 1: Update Ubuntu Package Repository

Before installing any new software, it’s best practice to update your package repositories and upgrade existing packages to the latest versions:

sudo apt update && sudo apt upgrade -y

Step 2: Install MySQL Server Package

Ubuntu’s default repositories include MySQL packages. To install MySQL server, run:

sudo apt install mysql-server -y

This command installs the MySQL server along with dependencies.


Step 3: Verify MySQL Service Status

Once installed, check whether the MySQL service is running:

sudo systemctl status mysql

You should see an active (running) status. If it’s not running, start the service:

sudo systemctl start mysql

Enable MySQL to start on boot:

sudo systemctl enable mysql

Step 4: Run the MySQL Secure Installation Script

MySQL ships with a security script to improve installation defaults. Execute it with:

sudo mysql_secure_installation

You’ll be prompted to configure options such as:

  • VALIDATE PASSWORD PLUGIN (enables password strength validation)
  • Setting a root password
  • Removing anonymous users
  • Disallowing remote root login
  • Removing test database
  • Reloading privilege tables

For a secure setup, answer yes to each prompt where applicable. This script is critical to locking down the server from unauthorized access.


Step 5: Log Into MySQL Shell

Once securing MySQL, test logging into the MySQL shell as root:

sudo mysql -u root -p

Enter the root password you set earlier. You should see a prompt like:

mysql>

Step 6: Creating a New MySQL User and Database (Optional)

It’s best practice to avoid using the root account for applications. Let’s create a new user and a database:

Inside MySQL shell, run:

CREATE DATABASE myappdb;
CREATE USER 'myappuser'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON myappdb.* TO 'myappuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

This creates a database myappdb, a user myappuser, and grants necessary permissions.


Step 7: Test Access with the New User

Try logging in as the new user:

mysql -u myappuser -p

Enter the password defined above and verify access:

SHOW DATABASES;

You should see the myappdb database listed.


Troubleshooting Common Issues

  • MySQL service not starting

    Check logs for errors:

    sudo journalctl -xeu mysql
    
  • Authentication errors

    Ensure you are using the correct authentication plugin compatible with your MySQL version; Ubuntu sometimes ships with auth_socket for root by default.


Wrapping Up

You now have a fully functional MySQL server running on your Ubuntu system, configured and secured. From here, you can:

  • Integrate your databases with applications
  • Back up and restore data
  • Scale your server configurations for larger workloads
  • Explore performance tuning options

Mastering this manual installation process demystifies the MySQL backend environment and equips you with essential skills to build and maintain robust database systems.


Useful Commands Summary

CommandPurpose
sudo apt update && sudo apt upgrade -yUpdate system packages
sudo apt install mysql-server -yInstall MySQL server
sudo systemctl status mysqlCheck MySQL service status
sudo systemctl start mysqlStart MySQL service
sudo systemctl enable mysqlEnable MySQL to start on boot
sudo mysql_secure_installationSecure MySQL installation
sudo mysql -u root -pLog in to MySQL shell as root

If this guide helped you get started with MySQL on Ubuntu, consider subscribing for more practical tutorials on database management and system administration!