How to Use the mysql Client

When MySQL is installed, a simple, text-based program called mysql (or sometimes the command line interface or the CLI) is also installed. Programs that communicate with servers are client software; because this program communicates with the MySQL server, it's a client. When you enter SQL queries in this client, the response is returned to the client and displayed onscreen.

The monitor program can send queries across a network; it doesn’t have to be running on the machine where the database is stored.

This client is always installed when MySQL is installed, so it's always available. It’s quite simple and quick if you know SQL and can type your queries without mistakes.

To send SQL queries to MySQL from the mysql client, 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.

  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.

You can use the mysql client to send a SQL statement that you type yourself, and it returns the response to the statement.

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

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.