Open In App

MySQL in Kali Linux Command Line

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

2024-01-12_12-38

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.

2024-01-12_15-15

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.

2024-01-12_15-17

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:

2024-01-12_15-18

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:

2024-01-12_15-21

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:

2024-01-12_15-37

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');

2024-01-12_15-40

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:

2024-01-12_15-57

7. How to Update and Delete Records in MySQL in Kali Linux:

Update Records:

UPDATE users SET email = 'newemail@example.com' 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 ‘newemail@example.com’.
  • 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 = ‘newemail@example.com’ WHERE id = 1; would update the email of the user with ID 1 to ‘newemail@example.com’. The updated table would look like this:

+—-+———-+————————+——+

| id | username | email | role |

+—-+———-+————————+——+

| 1 | John | newemail@example.com | 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 | newemail@example.com | 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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads