Edit Article

Edited by Sharimo1, Maluniu, Harri, Gary Warman and 3 others

One Methods:Running without the console.

A simple text-based program called mysql should have been part of your MySQL installation. It lets you send SQL queries directly to the MySQL server and output the results in text format. It is a quick and easy way to test your MySQL installation.

Ad

EditSteps

  1. 1
    Find the mysql program (Should be in a subdirectory called bin under the directory where MySQL was installed)
    • E.g. Windows users: C:\mysql\bin\mysql.exe
    • E.g. Linux/Unix users: /usr/local/mysql/bin/mysql
    Ad
  2. Send Sql Queries to Mysql from the Command Line Step 2.jpg
    2
    Start mysql - At the command prompt, type: mysql -h hostname -u username -p,
    • where
      • host is the machine where the MySQL server is running
      • username is the MySQL account you want to use
      • -p will make mysql prompt you for the MySQL account password.
  3. Send Sql Queries to Mysql from the Command Line Step 3.jpg
    3
    Enter your password when prompted.
  4. Send Sql Queries to Mysql from the Command Line Step 4.jpg
    4
    Type your SQL command followed by a semi-colon (;) and press the Enter key. The response from the server should be displayed on your screen.
  5. Send Sql Queries to Mysql from the Command Line Step 5.jpg
    5
    To get out of mysql, type quit at the prompt and press the Enter key.

EditRunning without the console.

  1. 1
    Find the mysql program (Should be in a subdirectory called bin under the directory where MySQL was installed)
    • E.g. Windows users: C:\mysql\bin\mysql.exe
    • E.g. Linux/Unix users: /usr/local/mysql/bin/mysql
  2. Send Sql Queries to Mysql from the Command Line Step 7.jpg
    2
    Start mysql - At the command prompt, type: mysql -h hostname -u username -p db_name -e "query"
    • where
      • host is the machine where the MySQL server is running
      • username is the MySQL account you want to use
      • -p will make mysql prompt you for the MySQL account password.
      • db_name is the name of the database to run the query in, and,
      • query is the query that you want to run.
  3. Send Sql Queries to Mysql from the Command Line Step 8.jpg
    3
    Enter your password when prompted.
  4. Send Sql Queries to Mysql from the Command Line Step 9.jpg
    4
    MySQL should return the result of your query.
    Ad


Add your own method
Save

EditTips

  • Be sure to include ; at the end of your query if you are using the console so it knows you are done with the query.
  • You cans specify the password on the command line by putting it directly after the -p, e.g. mysql -u username -h host -ppassword. Notice there is no space between the -p and the password.
  • If you are running it from the command line and not using the shell, you can use the -B flag (e.g., mysql -u username' -h host -p db_name -Be "query") to get the output in batch mode, instead of in the default MySQL tabular mode, for further processing.
Ad

EditWarnings

  • Be sure you check what queries you run before you run them, as you don't want to accidentally drop a whole database!

Article Info

Categories: MySQL

Recent edits by: Lutherus, Teresa, Gary Warman

In other languages:

Español: Cómo mandar consultas sql a la línea de comandos de MySQL

Ad

Thanks to all authors for creating a page that has been read 42,720 times.

Was this article accurate?

YesNo

Become
an Author!

Write an Article