Detailed MySQL connection with the client

Source: Internet
Author: User
Tags command line mysql client mysql command line

This article demonstrates how to connect to a database server by using a MySQL client program. MySQL (sometimes called "terminal monitor" or just "watch") is an interactive program that allows you to connect to a MySQL server, run queries, and see the results. MySQL can be used in batch mode: You put the query in a file beforehand and tell MySQL to execute the contents of the file. Two ways to use MySQL are covered here.

To see a list of selected items provided by MySQL, call it with the--HELP option:

shell> MySQL--help

This chapter assumes that MySQL has been installed on your machine and that you have a MySQL server that you can connect to. If this is not true, contact your MySQL administrator. (If you are an administrator, you will need to consult the other chapters of this manual.) )

Establish and abort a connection to the server

1. How to use client to establish a connection

To connect to the server, activate the MySQL program from the shell (that is, from the UNIX prompt, or from the DOS console under Windows). The order is as follows:

Shell>mysql

For example, connect directly to a database:

shell> MySQL db_name

The "$" in this book represents the shell prompt. This is one of the UNIX standard prompts, and the other is "#". Under Windows, a prompt resembles "c:\>".

2, the client most commonly used options: host, user and password

In order to connect to the server, when you call MySQL, you will usually need to provide a MySQL username and very likely, a password. If the server is running on a machine that you are not logged on to, you will also need to specify the host name. Contact your administrator to find out what connection parameters you should use to connect (that is, the host, user name, and password used). Once you know the correct parameters, you should be able to connect like this:

shell> mysql -h host -u user -p
Enter password: ********

On behalf of your password; When MySQL displays enter password: When prompted, enter it.

When you first learn about MySQL, you'll probably be bothered by its security system because it makes it difficult for you to do what you want to do. (You must obtain permission to create and access the database, and you must give your name and password whenever you connect to the database.) However, when you enter and use your own records through the database, the view changes immediately. At this point you will appreciate that MySQL prevents others from peering (or worse, damaging!). ) Your information.

The following describes the meaning of options:

-H HOST_NAME (selectable form:--host=host_name)

The server host you want to connect to. This option can generally be omitted if the server is running on the same machine as MySQL.

-U user_name (selectable form:--user=user_name)

Your MySQL username. If you use UNIX and your MySQL username and registration name are the same, you can omit this option; MySQL will use your registered name as your MySQL username.

Under Windows, the default user name is ODBC. This may not necessarily be very useful. You can specify a name on the command line, or you can set a default name in the environment variable by setting the USER variable. Use the following SET command to specify a user name for Paul:

-P (Selectable form:--password)

This option tells MySQL to prompt you to type your MySQL password. Note: You can type your password on the command line in the form of-pyour_password (optionally:--password=your_password). However, for security reasons, it is best not to do so. Select-p to tell MySQL not to password to prompt you to type the password at startup. For example:

When you see Enter password: Type the password. (The password does not show to the screen so that it is not visible to others.) Note that MySQL passwords do not necessarily have to be the same as UNIX or Windows passwords.

If you omit the-P option completely, MySQL thinks you don't need a password, no prompts.

Note that the-H and-u options are related to the words that follow them, regardless of whether there are spaces between the options and the words followed. And-P is not the case, if a password is given on the command line, there must be no space between-p and the password.

For example, suppose that my MySQL username and password are Tom and secret, and I want to connect to a server running on the same machine that I registered with. The following MySQL command completes this work:

Shell>mysql–u tom-p

After I type the command, MySQL displays Enter password: prompts to type the password. Then I type the password (the secret indicates that I typed it).

If all goes well, MySQL displays a string of messages and a "mysql>" hint, indicating that it is waiting for me to post the query. The complete boot sequence is as follows:

In order to connect to a server running on another machine, you need to specify the hostname with-H. If the host is mysql.domain.net, the corresponding command looks like this:

shell>mysql –h mysql.domain.net –u tom -p

For the sake of simplicity, we intend to omit the-H,-u, and-P options in most of the examples that follow the MySQL command line. And assume that you will provide any options that you want.

There are many ways to set up accounts so that you do not have to type in the connection parameters every time you run MySQL. This question has been introduced in the previous, you only need to provide the parameter in the option file, specifically please see 3.2.2. You might want to jump to that section now to find some ways to be more connected to the server.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.