How Do You Send SQL Queries to MySQL From the Command Line?

The primary option for executing a MySQL query from the command line is by using the MySQL command line tool. This program is typically located in the directory that MySQL has installed. You must have a username and password in order to connect to the MySQL server.

  1. Load the command line interface

    If you are using Windows, the interface used to access the command line is the Command Prompt. Access the Start Menu or Start Screen and search for “cmd” or “Command Prompt” without the quotes. If you are using Linux, the interface is called the Terminal. This application is launched by searching for “Terminal” from the application menu. Since many distributions of Linux exist, the exact method to finding the Terminal may vary.

  2. Run the command line tool

    Type “mysql –user=[username] –password=[password] [database]” into the command line. Replace the [username], [password] and [database] with the appropriate information without the brackets. If you do not have a database setup, leave this section blank. A database can be set up from within the tool.

  3. Execute the queries

    From the MySQL command shell, type the query you wish to run. If you have not created a database, you may run a query to do so. Type “CREATE DATABASE [database name];” into the prompt. Replace [database name] with the name of your desired database. Type “USE [database name];” after you create a database or if you did not specify a database when you ran the tool. This determines which database to affect with queries by default.