Below is a comprehensive MySQL Command Line tutorial for Kali Linux, starting from beginner to advanced levels. MySQL is a popular open-source relational database management system, and Kali Linux is a Debian-based Linux distribution often used for penetration testing and ethical hacking. Make sure you have MySQL installed on your Kali Linux system before starting.
1. How to install MySQL in Kali Linux:
Update the Package Index:
sudo apt-get update
Here,
- sudo: Execute the command with administrative privileges.
- apt-get: Debian package management command-line tool.
- update: Refresh the local package index with the latest information from the repositories.

Explanation: Before installing software, it's crucial to update the local package index to ensure that the system has the latest information about available packages and their versions.
Install MySQL Server:
sudo apt-get install mysql-server
Here,
- sudo: Execute the command with administrative privileges.
- apt-get: Debian package management command-line tool.
- install: Download and install new software packages.
- mysql-server: Package name for the MySQL server software.
Explanation: This command installs the MySQL server on Kali Linux. The package manager fetches necessary files from the repositories, resolves dependencies, and installs MySQL server along with any required components.
2. How to Start and Stop MySQL Server in Kali linux:
Start MySQL Service:
sudo service mysql start
Here,
- sudo: Execute the command with administrative privileges.
- service: A command to interact with system services.
- mysql: The name of the MySQL service.
- start: Command to start the MySQL service.

Explanation: This command initiates the MySQL service on Kali Linux. It is used to bring the MySQL server online and make it accessible for database connections.
Stop MySQL Service:
sudo service mysql stop
Here,
- sudo: Execute the command with administrative privileges.
- service: A command to interact with system services.
- mysql: The name of the MySQL service.
- stop: Command to stop the MySQL service.
Explanation: This command halts the MySQL service on Kali Linux. It is used when you want to temporarily shut down the MySQL server, perhaps for maintenance or other operational reasons.
3. How to Login to MySQL in Kali Linux:
Access MySQL Command Line:
mysql -u root -p
- mysql: Command to access the MySQL command-line interface.
- -u root: Specify the MySQL user to log in as (in this case, 'root').
- -p: Prompt for the MySQL user's password.
Explanation: This command initiates a connection to the MySQL server's command-line interface. It specifies the user 'root' with the -u flag and prompts for the user's password with the -p flag.
Entering MySQL Root Password:
After executing the command, you will be prompted to enter the MySQL root password.

Explanation: This step ensures that only authenticated users with the correct password can access the MySQL command-line interface. The root user typically has elevated privileges, allowing for various administrative tasks on the MySQL server.
4. How to Create and Delete Databases in MySQL in Kali Linux:
Create a Database:
To create a database named "mydatabase" in MySQL, you can use the following SQL command:
CREATE DATABASE mydatabase;
After executing this command, the MySQL server will create a new database named "mydatabase." If the operation is successful, you won't see any output. You can then proceed to use this database for storing tables and data.
Delete a Database:
To delete or drop the "mydatabase" database in MySQL, you can use the following SQL command:
DROP DATABASE mydatabase;
This command permanently removes the specified database and all its associated tables and data. Use this command with caution, as it cannot be undone.
5. Basic Commands in MySQL in Kali Linux:
a) Show Databases:
SHOW DATABASES;
Explanation: This SQL command is used to display a list of all databases available on the MySQL server.
Result:

In this example, the command shows a list of default databases that are usually present in a MySQL installation.
b) Use a Specific Database:
USE database_name;
Explanation: This SQL command is used to select and activate a specific database for further operations. Replace database_name with the name of the database you want to use.
Example:
USE mydatabase;
Result:

After executing this command, any subsequent SQL queries will be applied to the selected database (mydatabase in this example).
c) Create a Table
Now you can create a table. here's a basic example:
CREATE TABLE mytable (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);
This example create a table named "mytable" with columns for `id`, `name`, and `age`. The `id` column is set as the primary key.
d) Show Tables in a Database:
SHOW TABLES;
Explanation: This command lists all the tables in the currently selected database.
Result:

e) Insert Data into a Table
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
Explanation: This command is used to insert data into a specific table. Replace table_name, column1, column2, etc., and value1, value2, etc., with the appropriate table and values.
Example:
INSERT INTO table1 (id, name) VALUES (1, 'John');

Result: The command inserts a new record into `table1` with an `id` of 1 and a `name` of 'John'.
f) Show Table Structure:
DESCRIBE table_name;
Explanation: This command provides information about the structure of a specific table.
Example:
DESCRIBE mytable;
Result:

In this example, the command shows the fields, types, and other attributes of the `mytable`.
6. How to Retrieve Data in MySQL in Kali Linux:
SQL Statement:
SELECT * FROM users;
Explanation:
- SELECT: This keyword is used to retrieve data from a database.
- * (Asterisk): The asterisk is a wildcard character that represents all columns in the table. In this context, it instructs the database to return all columns for each row in the "users" table.
- FROM: Specifies the table from which to retrieve the data. In this case, it's the "users" table.
Example:
Assume you have a table named "mytable" with the following structure:

7. How to Update and Delete Records in MySQL in Kali Linux:
Update Records:
UPDATE users SET email = '[email protected]' WHERE id = 1;
Explanation:
- UPDATE: This keyword is used to modify existing records in a table.
- users: Specifies the table to be updated, in this case, the "users" table.
- SET: Specifies the column to be updated and the new value. Here, it updates the "email" column with the value '[email protected]'.
- WHERE: Specifies the condition that determines which records to update. In this case, it updates the record where the "id" is equal to 1.
Example:
Assume you have a table named "users" with the following structure:
+----+----------+-----------------+------+
| id | username | email | role |
+----+----------+-----------------+------+
| 1 | John | john@email | admin|
| 2 | Alice | alice@email | user |
| 3 | Bob | bob@email | user |
+----+----------+-----------------+------+
Executing the SQL statement UPDATE users SET email = '[email protected]' WHERE id = 1; would update the email of the user with ID 1 to '[email protected]'. The updated table would look like this:
+----+----------+------------------------+------+
| id | username | email | role |
+----+----------+------------------------+------+
| 1 | John | [email protected] | admin|
| 2 | Alice | alice@email | user |
| 3 | Bob | bob@email | user |
+----+----------+------------------------+------+
Delete Records:
SQL Statement:
DELETE FROM users WHERE id = 1;
Explanation:
- DELETE FROM: This syntax is used to remove records from a table.
- users: Specifies the table from which to delete records, in this case, the "users" table.
- WHERE: Specifies the condition to identify which records to delete. Here, it deletes records where the "id" is equal to 1.
Example:
Assuming the original "users" table:
+----+----------+------------------------+------+
| id | username | email | role |
+----+----------+------------------------+------+
| 1 | John | [email protected] | admin|
| 2 | Alice | alice@email | user |
| 3 | Bob | bob@email | user |
+----+----------+------------------------+------+
Executing the SQL statement `DELETE FROM users WHERE id = 1;` would remove the record where the "id" is 1. The updated table would be:
+----+----------+------------------------+------+
| id | username | email | role |
+----+----------+------------------------+------+
| 2 | Alice | alice@email | user |
| 3 | Bob | bob@email | user |
+----+----------+------------------------+------+
8. Indexes and Constraints in MySQL in Kali Linux
Add an Index
SQL Statement:
ALTER TABLE users ADD INDEX idx_username (username);
Explanation:
- ALTER TABLE: This statement is used to modify an existing table structure.
- users: Specifies the name of the table to be altered.
- ADD INDEX: Instructs the database to add an index to a specific column.
- idx_username: Specifies the name of the index. In this case, it's named "idx_username."
- (username): Specifies the column for which the index is being added, in this case, the "username" column.
Example:
Assume you have a table named "users" with the following structure:
+----+----------+-----------------+------+
| id | username | email | role |
+----+----------+-----------------+------+
| 1 | John | john@email | admin|
| 2 | Alice | alice@email | user |
| 3 | Bob | bob@email | user |
+----+----------+-----------------+------+
Executing the SQL statement `ALTER TABLE users ADD INDEX idx_username (username)`; would add an index to the "username" column. This index improves query performance when searching or sorting based on the "username" column.
Add a Foreign Key Constraint:
SQL Statement:
ALTER TABLE orders ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id);
Explanation:
- ALTER TABLE: This statement is again used to modify an existing table structure.
- orders: Specifies the name of the table to be altered, in this case, the "orders" table.
- ADD CONSTRAINT: Instructs the database to add a constraint to the table.
- fk_user: Specifies the name of the constraint. Here, it's named "fk_user."
- FOREIGN KEY (user_id): Specifies the column that will act as a foreign key in the "orders" table, in this case, the "user_id" column.
- REFERENCES users(id): Specifies the referenced column in the "users" table, in this case, the "id" column.
Example:
Assume you have a table named "orders" with the following structure:
+---------+---------+------------+
| order_id| user_id | order_date |
+---------+---------+------------+
| 101 | 1 | 2022-01-01 |
| 102 | 2 | 2022-01-02 |
| 103 | 3 | 2022-01-03 |
+---------+---------+------------+
Executing the SQL statement `ALTER TABLE orders ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id);` would add a foreign key constraint to the "user_id" column in the "orders" table. This constraint ensures that values in the "user_id" column must exist in the "id" column of the "users" table, establishing a relationship between the two tables.
Conclusion
In this article we discussed This MySQL Command Line tutorial for Kali Linux provides a step-by-step guide from beginner to advanced levels. Starting with the installation of MySQL on Kali Linux, it covers fundamental commands like starting and stopping the MySQL server, logging in, and creating/deleting databases and tables. The tutorial progresses to advanced topics including updating and deleting records, working with indexes and constraints, and executing complex queries. It also touches on essential administrative tasks such as backup and restore procedures, user management, and security considerations. This concise guide equips users with practical knowledge for efficient MySQL command line operations in a Kali Linux environment, particularly beneficial for those focused on security and ethical hacking.
Similar Reads
Kali Linux - Command Line Essentials Command-line plays a vital role while working with Kali Linux as most of its tools don't have a Graphical User Interface and if you are performing ethical hacking or penetration testing then most of the time you will have to work with command Line Interface itself. While executing a command in Kali
5 min read
How to Install MySQL on Linux? MySQL is one of the most widely used relational database management systems (RDBMS) Known for its reliability, speed, and scalability. MySQL is used for data operations like querying, filtering, sorting, grouping, modifying, and joining the tables present in the database. It is the backbone of many
5 min read
Hashcat Tool in Kali Linux Hashcat is famous as the fastest password cracker and password recovery utility. Hashcat is designed to break or crack even the most complex passwords in a very less amount of time. Features of hashcat: The 90+ Algorithm can be implemented with performance and optimization in mind.The number of thre
2 min read
File System Navigation Commands in Linux Linux offers an alternative to the usual Windows and icons. The terminal might seem different at first, relying on text commands instead of a mouse. Unlike Windows, which relies heavily on a graphical interface, Linux gives you direct access to the core of your computer through text-based commands.A
7 min read
atq command in linux with examples atq displays the list of pending jobs which are scheduled by the user. If the user is the superuser then the pending jobs of all the users will be displayed. The output lines display Job number, date, hour, queue, and username for each job. Syntax: atq [-V] [-q queue] Options: -V: It will display th
2 min read
apt command in linux with examples apt provides a high-level Command Line Interface (CLI) for the APT package management system, offering a user-friendly interface intended for interactive use. It simplifies common tasks like installation, upgrades, and removal, with better defaults than more specialized tools like apt-get and apt-ca
5 min read
hash command in Linux with examples The `hash` is a command in Linux that helps in managing the hash table. It facilitates with mapping the names of executable files to their locations on disk. It helps in speeds uping the commands execution by remembering paths of frequently used commands. It uses the `hash` to view hashed commands (
5 min read
whereis command in Linux with Examples 'whereis' command is used to find the location of source/binary file of a command and manuals sections for a specified file in Linux system. If we compare 'whereis' command with 'find' command they will appear similar to each other as both can be used for the same purposes but 'whereis' command prod
4 min read
username Command in Linux With Examples Linux as an operating system holds the capabilities of handling multiple users each with a username and a display name (Full Name). So it is important to keep a check on the users and their related information in order to maintain the integrity and security of the system. Whenever a user is added it
4 min read
batch command in Linux with Examples batch command is used to read commands from standard input or a specified file and execute them when system load levels permit i.e. when the load average drops below 1.5. Syntax: batch It is important to note that batch does not accepts any parameters. Other Similar Commands: atq: Used to display th
1 min read