How to Send SQL Queries to MySQL from the Command Line

Download ArticleDownload Article

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.

  1. How.com.vn English: Step 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. How.com.vn English: Step 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.
    Advertisement
  3. How.com.vn English: Step 3 Enter your password when prompted.
  4. How.com.vn English: Step 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.
    • Get out of MySQL, type quit at the prompt and press the Enter key.
      How.com.vn English: Step 4 Type your SQL command followed by a semi-colon (;) and press the Enter key.
  5. Advertisement
Method 1
Method 1 of 1:

Running without the console.

Download Article
  1. How.com.vn English: Step 1 WH.performance.clearMarks('image1_rendered'); WH.performance.mark('image1_rendered');...
    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. How.com.vn English: Step 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. How.com.vn English: Step 3 Enter your password when prompted.
    • MySQL should return the result of your query.
      How.com.vn English: Step 3 Enter your password when prompted.
  4. Advertisement


Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Tips

      • 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 can 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.
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      Warnings

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

      About This Article

      Tested by:
      How.com.vn Technology Team
      How.com.vn is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 10 people, some anonymous, worked to edit and improve it over time. This article has been viewed 187,585 times.
      How helpful is this?
      Co-authors: 10
      Updated: July 12, 2023
      Views: 187,585
      Categories: Software
      Thanks to all authors for creating a page that has been read 187,585 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement