Linux Commands Ubuntu

How to Run MySQL in Linux/Ubuntu Terminal?

How to Run MySQL in Linux Ubuntu Terminal

MySQL is a free-to-use Relational Database Management System (RDBMS) that stores data in a well-structured format that can be retrieved when needed. MySQL is easy to set up and provides ease of use. This article will demonstrate how to run MySQL on Linux/Ubuntu Terminal using the following content:

  • How to Log Into MySQL From Terminal?
  • How to Create a MySQL User from the Linux/Ubuntu Terminal?
  • How to Grant Privileges?
  • Basic Operations in MySQL?
  • How to Delete/Remove MySQL User Account?
  • How to Exit MySQL?

How to Log Into MySQL From Terminal?

In order to start working with MySQL, we will first check if MySQL is installed in the system by executing the following command:

$ mysql --version

It can be seen from the above image that MySQL is installed in the system.

If the “–version” command returns “bash: /usr/bin/mysql: No such file or directory”, then it indicates that MySQL is not installed and it can be installed by following the steps discussed in How to Install MySQL on Ubuntu 20.04?

Type the below-stated sudo command to log into MySQL using a Linux terminal:

$ sudo mysql

Additionally, MySQL can be connected to a local or remote server by using the syntax:

$ sudo mysql -u <username> -p

A user name is created in the next section.

How to Create a MySQL User from the Linux/Ubuntu Terminal?

In order to create a MySQL user, use the following syntax:

mysql> CREATE USER 'USERNAME'@'HOST' IDENTIFIED BY 'PASSWORD';

Replace the ‘USERNAME’, ‘HOST’, and ‘PASSWORD’ in the syntax with any valid values of your choice:

$ CREATE USER 'linuxuser'@'localhost' IDENTIFIED BY 'pass#W@123%4%';

From the above image, it can be seen that a user has been created successfully.

How to Grant Privileges?

When a user is created, it doesn’t have any privileges. However, the GRANT command can be used to allow any specific privileges like CREATE, INSERT, SELECT, etc. to the newly created user explicitly:

mysql> GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'linuxuser'@'localhost' WITH GRANT OPTION;

From the above image, it can be seen that CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, and RELOAD privileges are granted to the newly created ‘linuxuser’.

Additionally, all privileges can be granted to a user by executing the following query:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'linuxuser'@'localhost' WITH GRANT OPTION;

From the above image, it can be seen that all privileges are granted to the ‘linuxuser’.

Basic Operations in MySQL

Some of the basic MySQL operations are demonstrated below:

How to View Database From the Command Line?

All existing databases can be viewed by the following query:

mysql> SHOW DATABASES;

From the above image, it can be seen that there are currently four databases.

How to Create a Database Using the Linux/Ubuntu Terminal?

To create a new database, execute the following syntax:

mysql> CREATE DATABASE <dbname>

Where dbname indicates the name of the database to be created.

So by using the above syntax, we can create a database “programming” by executing the following command:

mysql> Create database programming;

From the above image, it can be seen that a new database “programming” is created.

How to Pick/Select Created Databases?

We can select a database by using the following syntax:

mysql> USE dbname;

Where dbname represents the Name of a database to be accessed/selected.

 

Using the above syntax, we can select the “programming” database as follows.

After executing the USE statement, we have successfully picked the programming database. Various operations like creating tables, adding, editing, and deleting records in tables can be performed by executing different MySQL queries.

How to Delete/Drop a MySQL Database From the Linux/Ubuntu Terminal?

An unwanted existing database can be removed/dropped by using the following syntax:

mysql> DROP DATABASE <dbname>

Where dbname represents the name of the database to be dropped.

For instance, we can remove a database named “programming” by executing the following DROP command:

mysql> DROP DATABASE programming;

From the above image, it can be verified that the “programming” database has been successfully dropped.

How to Delete/Remove MySQL User Account?

A user can be deleted in MySQL by executing the following syntax:

mysql> DROP USER ’username’@’host’

Where username represents the username of the user that needs to be dropped and Host indicates the hostname.

For example, by using the above syntax, a user can be dropped in MySQL as follows:

mysql> DROP USER 'linuxuser'@'localhost';

From the above syntax, we can observe that the user ‘linuxuser’@’localhost’ is dropped.

How to Exit MySQL?

Finally, we can exit MySQL by executing the following query:

$ mysql> exit

As the above snippet shows, executing the exit query navigates us back to the Ubuntu terminal.

MySQL Client Options and Query Syntax

A full list of MySQL commands can be viewed by executing the following statement:

mysql> help

This is how you can execute MySQL in a Linux terminal.

Conclusion

To run MySQL in a Linux/Ubuntu terminal, first, you need to log into MySQL, create a user account, and grant privileges to the newly created user. After that, you can execute any MySQL command or query to perform different database operations. This article has demonstrated how to run MySQL on Linux/Ubuntu Terminal.

Similar Posts