How to Manage MySQL Users, Databases, and Tables from the command line

MySQL is probably the most popular relational database management system (RDBMS). A relational database management system organizes data as a relationship between columns and rows in a bunch of tables. 

The columns represent the different attributes of the data, while each row records its values. This article can guide you on how to manage MySQL users and databases remotely using the command line. 

It details how to:

  • Create a MySQL users
  • Delete a MySQL user
  • Create a MySQL database
  • Delete a MySQL database
  • Delete a MySQL table
  • Export a MySQL Database
  • Import an SQL file into a MySQL database

Follow the steps below to set them up.

Creating a MySQL User 

Securely log in to your server through SSH access to open the command line.

Type this command to log in to MySQL as the root user:

mysql -u root -p 

Enter the MySQL root password and press Enter. Now, this will enable you to run MySQL commands as the user.

How to Manage MySQL Users, Databases, and Tables from the command line, Creating a MySQL User 

Now, to create a new user and assign privileges, then run this command from the mysql> prompt:

GRANT ALL PRIVILEGES ON *.* TO ‘username’@’localhost’ IDENTIFIED BY ‘password’;

Replace username with the name of the user you are creating and password with the user’s password.

The above command will assign all privileges to the user. But you can restrict the privileges with the grant command. And here’s how:

GRANT PRIVILEGE ON *.* TO ‘username’@’localhost’;

Replace PRIVILEGE with the permission you want to grant the user. MySQL has over 50 privileges you can assign a user. You can view the full list here.

Now, to log in to MySQL as the new user, type /q to exit the program, then run this command:

mysql -u username -p

Of course, replace username with the name of the user you just created. Type the user’s password and press Enter to log in.

Deleting a MySQL User

You can delete a MySQL user by running this command:

DELETE FROM mysql.user WHERE user = ‘username’;

Replace username with the name of the user you intend to remove and press Enter

You can view all the MySQL users with this command:

SELECT user FROM mysql.user GROUP BY user;

Creating MySQL Database

A user owns a MySQL database. So to create one, you’d need to log in first with the user account that holds the database.

Now run this command to create the database:

CREATE DATABASE databname;

Replace databname with the name of the database you want to create.

You’d need to run the USE command to access or work with the database you just created or any other databases. To do this, type the below command and press Enter.

USE databname;

Replace databname with the name of the database you want to open or work with.

Deleting a MySQL Database

Deleting a MySQL database from the command line is irreversible. So, always ensure to delete only the correct ones.

It’s advisable to list all the databases on your server and then copy the name of the database you intend to delete. Run the SHOW DATABASES command to list all your databases.

SHOW DATABASES;

Use the DROP DATABASES command to delete a MySQL database. Here’s how it should look like after the mysql> prompt:

DROP DATABASE databname;

Replace databname in the code with the name of the database you want to delete. Use the SHOW DATABASE command to confirm if the program has removed the database from the server.

Deleting MySQL Tables

You can delete a MySQL table with the DROP TABLE command, but first, you’d need to select the database where the table resides. Of course, you can do this with the USE command.

USE databname;

Replace databname with the database’s name you intend to select. Now run this command to delete a table:

DROP TABLE tablename;

Replace tablename in the code with the name of the table you want to delete.

Exporting MySQL Database

Exporting a MySQL database is essential if you intend to backup your database or transfer it to another server. You can perform this task using the mysqldump command. 

To export a MySQL database into an SQL file, type this command

mysqldump -u username -p databname > ExportDatabase.sql

Replace username with the name of the database owner and databname with the name of the database you intend to export. 

Also, replace ExportDatabase.sql with the name you’d want to export the MySQL file to.

Enter the user’s password, and MySQL will create the SQL file of your database.

Importing MySQL Database

You can restore a MySQL database by importing the backup file. To import an SQL file, for example, ExportDatabase.sql to your database, then run this command:

mysql -u username -p databname < ExportDatabase.sql

Replace username with the name of the database owner and databname with the name of the database you intend to import the file to. 

Enter the user’s password to complete the import.

Wrapping It Up

You can manage your MySQL databases remotely with a handful of commands. This guide has described how to go about them. Follow the steps to get started.

Was this helpful?

What’s your goal today?

1. Find the right Managed VPS solution

If you’re looking for industry-leading speed, ease of use and reliability Try ScalaHosting with an unconditional money-back guarantee.

2. Make your website lighting fast

We guarantee to make your WordPress site load in less than 2 seconds on a managed VPS with ScalaHosting or give your money back. Fill out the form, and we’ll be in touch.

Please enter a valid name
Please enter a valid website
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

3. Streamline your clients’ hosting experience

If you’re a web studio or development agency hosting more than 30 websites, schedule a call with Vlad, our co-founder and CTO, and see how we can deliver unmatched value to both your business and your clients.

Photo

Need a custom cluster or professional advice?

Book a meeting and get a free 30-minute consultation with Vlad, co-founder & CTO of Scala Hosting, who will help you select, design and build the right solution - from a single data center cluster to a multi-region & multi-datacenter high availability cluster with hundreds of servers.

Book a free consultation

4. Learn how to grow your website in 2024

An all-star team of SEO and web influencers are sharing their secret knowledge for the first time in years. Learn about the future of SEO, Web Design best practices and the secrets to getting the foundation for your website to thrive. Watch the exclusive webinar.

An Exclusive Insiders Look Behind The SEO and Web Development Curtain

Rado
Author

Working in the web hosting industry for over 13 years, Rado has inevitably got some insight into the industry. A digital marketer by education, Rado is always putting himself in the client's shoes, trying to see what's best for THEM first. A man of the fine detail, you can often find him spending 10+ minutes wondering over a missing comma or slightly skewed design.