Manage Databases and Users using the MySQL CLI

Manage MySQL Databases and Users Using The CLI

Managing MySQL databases and users through the MySQL Command Line is often more efficient than using graphical tools. Administrators gain faster navigation, reduced overhead, and a deeper understanding of core functionality. This post explores how to create databases, build tables, list existing objects, update data, and handle user accounts from the MySQL Command Line.


TL;DR

Using the MySQL Command Line involves creating databases, building tables, editing records, and adjusting user privileges. This approach offers control over database structures and user access without relying on external interfaces. Proper command line usage ensures effective resource management and faster response times.


MySQL Command Line Overview

The MySQL Command Line provides direct access to core features such as database creation, table management, and user administration. Basic familiarity with MySQL’s official documentation is recommended before proceeding. Make sure you have valid credentials and a stable connection to the server.


Creating a Database

Creating a database is straightforward once you are connected:

Replace my_app_db with your chosen name. The command allocates space for a new project or environment.


Listing Databases

To view all existing databases on your server, use:

This command displays a list of available databases, including system-related ones like information_schema or mysql, which contain internal data.


Creating a Table

After switching to your target database:

Define a table with fields:

This creates an employees table with columns for ID, name, role, and hire date.


Listing Tables

To confirm that your new table exists, run:

You should see employees listed as part of your database objects. This is a quick way to verify table creation.


Managing MySQL Databases: Checking Table Details

You can review table structure and confirm column definitions:

This command displays each column, data type, and additional attributes like AUTO_INCREMENT or PRIMARY KEY.


Adding a Record to a aTable

Inserting a new row into a table can be done with an INSERT statement:

Omit the id field if it is set to auto-increment. The row now exists in the employees table.


Deleting a Record

If you need to remove an entry, use DELETE:

Always ensure you provide a WHERE clause to limit deletions to specific records. Otherwise, you risk removing all data in the table.


Updating a Record

To modify existing data, rely on UPDATE:

This updates the role column for the employee whose id is 2. Use filters carefully when updating data to avoid unintended changes.


Managing Users

User management includes creation, privilege assignment, and revocation. For instance, to create a user:

Grant privileges on a specific database:

Revoke ALL PRIVILEGES with granular permission levels if you prefer. To revoke privileges, use:


References

  1. MySQL Official Documentation

Leave a Reply

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