How to Send a SQL Query to MySQL from the Command Line

Table of contents:

How to Send a SQL Query to MySQL from the Command Line
How to Send a SQL Query to MySQL from the Command Line
Anonim

A simple text program called "mysql" should have been installed along with MySQL on your PC. It allows you to send SQL queries directly to the MySQL server, and to export the results as text. It's a quick and easy way to test your MySQL installation.

Steps

Send Sql Queries to Mysql from the Command Line Step 1
Send Sql Queries to Mysql from the Command Line Step 1

Step 1. Find the mysql program (it should be in a subfolder called "bin" under the folder where MySQL was installed)

  • Example for Windows users: C: / mysql / bin / mysql.exe
  • Example for Linux / Unix users: / usr / local / mysql / bin / mysql
Send Sql Queries to Mysql from the Command Line Step 2
Send Sql Queries to Mysql from the Command Line Step 2

Step 2. Start mysql - When prompted, type:

mysql -h hostname -u username –p,

  • in which

    • host is the machine where the MySQL server is used;
    • username is the MySQL account you want to use;
    • -p is used to enter the password of the MySQL account.
    Send Sql Queries to Mysql from the Command Line Step 3
    Send Sql Queries to Mysql from the Command Line Step 3

    Step 3. Enter your password when prompted

    Send Sql Queries to Mysql from the Command Line Step 4
    Send Sql Queries to Mysql from the Command Line Step 4

    Step 4. Type your SQL command followed by a semicolon (;) and press Enter

    The response from the server should appear on the screen.

    Send Sql Queries to Mysql from the Command Line Step 5
    Send Sql Queries to Mysql from the Command Line Step 5

    Step 5. To quit mysql, type “quit” when prompted and press Enter

    Method 1 of 1: Working without a console

    Send Sql Queries to Mysql from the Command Line Step 6
    Send Sql Queries to Mysql from the Command Line Step 6

    Step 1. Find the mysql program (it should be in a subfolder called "bin" under the folder where MySQL was installed)

    • Example for Windows users: C: / mysql / bin / mysql.exe
    • Example for Linux / Unix users: / usr / local / mysql / bin / mysql
    Send Sql Queries to Mysql from the Command Line Step 7
    Send Sql Queries to Mysql from the Command Line Step 7

    Step 2. Start mysql - When prompted, type:

    mysql -h hostname -u username -p db_name -e "query"

    • in which

      • host is the machine where the MySQL server is used;
      • username is the MySQL account you want to use;
      • -p is used to enter the password of the MySQL account;
      • "Db_name" is the name of the database to query, and …
      • … “Query” is the query (required) you want to make.
      Send Sql Queries to Mysql from the Command Line Step 8
      Send Sql Queries to Mysql from the Command Line Step 8

      Step 3. Enter your password when prompted

      Send Sql Queries to Mysql from the Command Line Step 9
      Send Sql Queries to Mysql from the Command Line Step 9

      Step 4. MySQL should give you the result of the query

      Advice

      • Be sure to include ";" at the end of your query if you are using the console, to indicate that you are done.
      • You can specify the password on the command line by putting it directly after the –p, for example “mysql -u username -h host –p password”. Note the absence of spaces between the -p and the password.
      • If you are using the command line, you can use the -B label (for example: mysql -u username '-h host -p db_name -Be "query") to get the result in batch mode, rather than the default tabular mode of MySQL, for a more in-depth process.

Recommended: