Your Gateway To Digital Success
Wesbytes Blue Logo

Wesbytes Knowledge Base

Search our articles or browse by category below

How to Manage MySQL

Last modified: July 2, 2022
You are here:
Estimated reading time: 1 min

This guide will explains how to use the mysql programme and the command line to perform standard MySQL database management tasks. These responsibilities include creating and deleting databases, users and tables.

Add Users and Databases

    1. Firstly, log in to MySQL as the root user at the command line:
      mysql -u root -p
    2. Then, type the MySQL root password, and press Enter.
    3. To create a database user, type the following command.
      *Replace username and password with the user you want to create and the user’s password:
      GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password';

      This command grants the user all permissions. However, you can specify permissions to maintain precise control over database access. For instance, to explicitly grant the SELECT permission, you can use the following command:

      GRANT SELECT ON *.* TO 'username'@'localhost';

      For more information about setting MySQL database permissions, kindly visit https://dev.mysql.com/doc/refman/5.5/en/grant.html.

    4. After that, type \q to exit the mysql program.
    5. To log in to MySQL as the user you just created, type the following command. Replace username with the name of the user you created in step 3:

      mysql -u username -p
    6. Next, type the user’s password, and then press Enter.

    7. To create a database, type the following command.
      Replace dbname with the name of the database that you want to create:

      CREATE DATABASE dbname;
    8. To work with the new database, use the following command.
      Replace dbname with the name of the database you created in step 7:

      USE dbname;
    9. You can now work with the database. For example, the following commands show how to create a basic table named table, and how to insert some data into it:

      CREATE TABLE table( id smallint unsigned not null auto_increment, name varchar(20) not null, constraint pk_table primary key (id) );
      INSERT INTO table( id, name ) VALUES ( null, 'Sample data' );

Removing Tables and Databases

To delete a table, use the following command from the mysql> prompt. Replace tablename with the name of the table that you want to delete:

DROP TABLE tablename;
This command assumes that you have already selected a database by using the USE statement.

Similarly, to delete an entire database, use the following command from the mysql> prompt. Replace dbname with the name of the database that you want to delete:

DROP DATABASE dbname;

Removing Database Users

To view a list of all users, type the following command from the mysql> prompt:

SELECT user FROM mysql.user GROUP BY user;

To delete a specific user, type the following command from the mysql> prompt. Replace username with the name of the user that you want to delete:

DELETE FROM mysql.user WHERE user = 'username';
Was this article helpful?
Dislike 0
Views: 13