How to Remove, Reinstall, and Set Up MySQL on Ubuntu: Step-by-Step Guide

How to Remove, Reinstall, and Set Up MySQL on Ubuntu: Step-by-Step Guide

Java


How to Remove, Reinstall, and Set Up MySQL on Ubuntu: Step-by-Step Guide

Introduction:

MySQL is one of the most widely used open-source relational database management systems. Over time, you might need to remove an existing MySQL installation, reinstall it from scratch, and create new databases and users for specific applications. In this guide, we will walk you through these steps, including user management and logging into MySQL.

Table of Contents:

  1. Removing MySQL from Ubuntu
  2. Installing MySQL on Ubuntu
  3. Configuring MySQL
  4. Creating a New Database and User
  5. Logging In as a Specific User
  6. Removing a MySQL User

1. Removing MySQL from Ubuntu

  1. Stop the MySQL Service: First, ensure that MySQL is not running:
    sudo systemctl stop mysql
  2. Uninstall MySQL: To remove MySQL and related packages:
    sudo apt-get remove --purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-*
  3. Remove Residual Config Files: Clean up any residual configuration files:
    sudo rm -rf /etc/mysql /var/lib/mysql
    sudo apt-get autoremove
    sudo apt-get autoclean

2. Installing MySQL on Ubuntu

  1. Update Package Repository:
    sudo apt-get update
  2. Install MySQL Server:
    sudo apt-get install mysql-server
  3. Secure MySQL Installation: Run the security script:
    sudo mysql_secure_installation

3. Configuring MySQL

  1. Check MySQL Service Status:
    sudo systemctl status mysql
  2. Log in to MySQL as Root:
    sudo mysql -u root -p

4. Creating a New Database and User

  1. Create a New Database:
    CREATE DATABASE your_database_name;
  2. Create a New User and Grant Privileges:
    CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON your_database_name.* TO 'new_user'@'localhost';
    FLUSH PRIVILEGES;
  3. Verify the User:
    SELECT user, host FROM mysql.user;

5. Logging in as a Specific User

Once the new user is created, you can log in as that user to manage the database:

mysql -u new_user -p your_database_name

6. Removing a MySQL User

  1. Log in as Root:
    sudo mysql -u root -p
  2. Remove the User:
    DROP USER 'new_user'@'localhost';
  3. Clean Up:
    FLUSH PRIVILEGES;

Conclusion:

Managing MySQL on Ubuntu is straightforward once you’re familiar with the basic commands for installing, removing, and managing databases and users. Whether you're troubleshooting an existing installation or setting up MySQL from scratch, these steps will guide you through the process smoothly. Now you know how to remove MySQL, install it afresh, create databases, manage users, and clean up user accounts when necessary.

How to Remove, Reinstall, and Set Up MySQL on Ubuntu: Step-by-Step Guide

“Creativity is intelligence having fun.” Albert Einstein

October 02, 2024

0
0

Comments

+

© 2024 Inc. All rights reserved. mulikevs