How to Query with the mysql Client

Retrieving data from MySQL can be accomplished in several ways. One of the easiest and most convenient is through the MySQL client, also call the command line interface (CLI). Before you can send SQL queries to MySQL using the CLI, you have to locate and start it. To send SQL queries to MySQL from the CLI, follow these steps:

  1. Locate the mysql client.

    By default, the mysql client program is installed in the subdirectory bin, under the directory where MySQL is installed. In Unix and Linux, the default is /usr/local/mysql/bin or /usr/local/bin. In Windows, the default is c:\Program Files\MySQL\MySQL Server 5.0\bin. However, the client might be installed in a different directory. Or, if you aren't the MySQL administrator, you might not have access to the mysql client.

    If you don’t know where MySQL is installed or can’t run the client, ask the MySQL administrator to put the client somewhere where you can run it or to give you a copy that you can put on your own computer.

  2. Start the client.

    In Unix and Linux, type the path/filename (for example, /usr/local/mysql/bin/mysql). In Windows, open a command prompt window and then type the path\filename (for example, c:\ Program Files\MySQL\MySQL Server 5.0\bin\mysql). This command starts the client if you don't need to use an account name or a password. If you need to enter an account or a password or both, use the following parameters:

    • -u user: user is your MySQL account name.

    • -p: This parameter prompts you for the password for your MySQL account.

    For instance, if you’re in the directory where the mysql client is located, the command might look like this:

    mysql -u root -p
  3. If you’re starting the mysql client to access a database across the network, use the following parameter after the mysql command:

    -h host, where host is the name of the machine where MySQL is located.

    For instance, if you’re in the directory where the mysql client is located, the command might look like this:

    mysql -h mysqlhost.mycompany.com -u root -p

    Press Enter after typing the command.

  4. Enter your password when prompted for it.

    The mysql client starts, and you see something similar to this:

    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 459 to server version: 5.0.15
    Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
    mysql>
  5. Select the database that you want to use.

    At the mysql prompt, type the following:

    use databasename

    Use the name of the database that you want to query.

    Some SQL statements, such as SHOW DATABASES, don’t require that you select a database. For those statements, you can skip Step 5.

    image0.jpg
  6. At the mysql prompt, type your SQL statement followed by a semicolon (;) and then press Enter.

    If you forget to type the semicolon (;) at the end of the query, the mysql client doesn’t execute the statement. Instead, it continues to display the prompt (mysq>) until you enter a semicolon.

    The response to the statement is displayed onscreen.

  7. To leave the mysql client, type quit at the prompt and then press Enter.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.