Connection between MySQL and clients (1)

Source: Internet
Author: User

This article demonstrates how to use the mysql client program to connect to the database server. Mysql is sometimes called "Terminal Monitor" or "monitoring"). It is an interactive program that allows you to connect to a MySQL server, run the query, and view the results. Mysql can be used in the batch mode: You put the query in a file in advance and then tell the mysql execution file content. Both methods of using mysql are involved here.

To view a selection project table provided by mysql, call it using the -- help option:

Shell> mysql -- help

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

Establish and stop connections with the server

1. How to use a client to establish a connection

To connect to the server, activate the mysql program from the shell program, namely from a UNIX prompt or from the DOS console in Windows. The command is as follows:

Shell> mysql

Another example is to directly connect to a database:

Shell> mysql db_name

"$" Represents the shell prompt in this book. This is one of the standard UNIX prompts; the other is "#". In Windows, the prompt is similar to "c: \> ".

2. The most common options for a client: Host, user, and password

To connect to the server, when you call mysql, you usually need to provide a MySQL user name and probably a password. If the server runs on a machine that you are not logged on to, you 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, username and password used ). Once you know the correct parameters, you should be able to connect like this:

shell> mysql -h host -u user -pEnter password: ********

* ******* Indicates your password. when mysql displays Enter password: prompt, Enter it.

At the beginning of learning MySQL, you may be worried about its security system, because it makes it difficult for you to do what you want. You must obtain the permission to create and access the database. You must give your own name and password to connect to the database at any time .) However, after you enter and use your own records through the database, your views will change immediately. At this time, you will appreciate that MySQL has prevented others from looking at it or getting worse !) Your information.

The following describes the meaning of the options:

-H host_name: -- host = host_name)

The server host that you want to connect. This option can be omitted if the server runs on the same machine as mysql.

-U user_name: -- user = user_name)

Your MySQL user name. If you use UNIX and your MySQL user name is the same as your registration name, you can skip this option. mysql uses your registration name as your MySQL name.

In Windows, the default user name is ODBC. This may not be very useful. You can specify a name on the command line or set a default name in the environment variable by setting the USER variable. Use the following set command to specify a user name of paul:

-P selectable format: -- password)

This option tells mysql to prompt you to enter your MySQL password. Note: You can select the form of-pyour_password: -- password = your_password) and enter your password on the command line. However, for security reasons, it is best not to do so. Select-p to tell mysql that you are prompted to enter the password at startup. For example:

Enter password. The password is not displayed on the screen to avoid being visible to others .) Note that the MySQL password is not necessarily the same as the UNIX or Windows Password.

If the-p option is omitted, mysql considers that you do not need a password and do not prompt.

Note: The-h and-u options are related to the words behind them, regardless of whether there is a space between the options and the followed words. -P is not like this. If a password is provided on the command line, no space is required between the-p and the password.

For example, assume that my MySQL user name and password are tom and secret respectively, and you want to connect to the server running on the same machine that I registered. The following mysql command can complete this task:

Shell> mysql-u tom-p

After I type a command, mysql displays "Enter password", prompting you to type a password. Then, I typed the password ***** to indicate that I typed secret ).

If everything goes well, mysql displays a message string and a "mysql>" prompt, indicating that it is waiting for my release query. The complete startup sequence is as follows:

To connect to a server running on another machine, use-h to specify the host name. If the host is mysql.domain.net, the corresponding command is as follows:

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

In most examples of mysql command lines described later, we plan to save the-h,-u, and-p options for simplicity. It is assumed that you will provide any required options.

There are many ways to set accounts, so you do not have to type in connection parameters every time you run mysql. This problem has been discussed earlier. You only need to provide parameters in the option file. For details, see 3.2.2. You may want to skip to this section to find ways to connect to the server more easily.

3. End the session

After establishing a connection to the server, you can enter the following command at any time to end the session:

Quit exit

You can also type Control-D to exit, at least on UNIX.


Related Article

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.