Linux mysql command

The mysql command in Linux is used to interact with MySQL database servers from the command line. It allows you to execute SQL queries, manage databases, tables, and users, and perform various administrative tasks.

Here’s an overview of how to use the mysql command:

  1. Connecting to a MySQL Server:
    To connect to a MySQL server, use the following command:
   mysql -u username -p

Replace username with your MySQL username. After executing the command, you will be prompted to enter your MySQL password. If the MySQL server is running on the local machine, you can omit the -h option. Otherwise, you can specify the hostname or IP address of the MySQL server using the -h option.

  1. Executing SQL Queries:
    Once connected to the MySQL server, you can execute SQL queries directly in the command prompt. For example, to retrieve data from a table, you can use the SELECT statement:
   SELECT * FROM table_name;
  1. Managing Databases:
  • Create a new database: CREATE DATABASE database_name;
  • Switch to a specific database: USE database_name;
  • List all databases: SHOW DATABASES;
  • Delete a database:
    DROP DATABASE database_name;
  1. Managing Tables:
  • Create a new table: CREATE TABLE table_name ( column1 datatype, column2 datatype, ... );
  • Show the structure of a table: DESC table_name;
  • Insert data into a table: INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
  • Update records in a table: UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
  • Delete records from a table:
    DELETE FROM table_name WHERE condition;
  1. Managing Users and Privileges:
  • Create a new user: CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
  • Grant privileges to a user: GRANT privileges ON database_name.* TO 'username'@'localhost';
  • Remove privileges from a user: REVOKE privileges ON database_name.* FROM 'username'@'localhost';
  • Delete a user:
    DROP USER 'username'@'localhost';
  1. Examples:
  • Connect to a local MySQL server: mysql -u root -p
  • Create a new database: CREATE DATABASE mydatabase;
  • Switch to a specific database: USE mydatabase;
  • Create a new table: CREATE TABLE mytable ( id INT PRIMARY KEY, name VARCHAR(50) );
  • Insert data into a table: INSERT INTO mytable (id, name) VALUES (1, 'John');
  • Retrieve data from a table: SELECT * FROM mytable;
  • Delete a database:
    DROP DATABASE mydatabase;

The mysql command provides a powerful interface for managing MySQL databases and executing SQL queries. It enables you to perform a wide range of database-related tasks directly from the command line.

For more information about the mysql command and its options, you can