In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "the connection process between MySQL and client". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the connection process between MySQL and client".
Establish and abort a connection to the server
1. How to use the 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 command 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; the other is "#". Under Windows, the prompt looks like "c: >".
2. The most commonly used options for clients: host, user, and password
To connect to the server, when you call mysql, you will usually need to provide a MySQL user name and, most likely, a password. If the server is running on a machine that you are not logged in to, you will also need to specify a hostname. 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: *
* represents your password; enter it when mysql displays Enter password: prompt.
When you first learn MySQL, you will probably worry about its security system, because it makes it difficult for you to do what you want to do. You must have permission to create and access the database, and you must give your name and password whenever you connect to the database. However, after you enter and use your own records through the database, the view will change immediately. You will appreciate that MySQL prevents others from snooping (or worse, sabotage! (your information.
The following describes the meaning of the option:
-h host_name (optional form:-- host=host_name)
The server host to which you want to connect. If this server is running on the same machine as mysql, this option is generally omitted.
-u user_name (optional form:-- user=user_name)
Your MySQL user name. If you use UNIX and your MySQL user name is the same as the registration name, you can omit this option; mysql will use your registration name as your MySQL name.
Under Windows, the default user name is ODBC. This may not 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. For example, use the following set command to specify a user name for paul:
-p (optional form:-- password)
This option tells mysql to prompt for your MySQL password. Note: type your password on the command line in the form of-pyour_password (optional:-- password=your_password). However, for security reasons, it is best not to do so. Choose-p not to follow the password and tell mysql to prompt you to type the password at startup. For example:
When you see Enter password:, type the password. (the password will not be displayed on the screen so as not to be seen by others. ) Note that the MySQL password does not have to be the same as the UNIX or Windows password.
If the-p option is omitted completely, mysql assumes that you don't need a password and don't prompt.
Note that the-h and-u options are related to the words that follow them, regardless of whether there is a space between the options and the words that follow them. This is not the case with-p. If you give an exit order on the command line, there must be no space between-p and the password.
For example, suppose my MySQL username and password are tom and secret, respectively, and I want to connect to the server running on the same machine that I registered. The following mysql command does this:
Shell > mysql-u tom-p
After I type the command, mysql displays Enter password: prompt for a password. Then I type the password (* indicates that I typed secret).
If all goes well, mysql displays a string of messages and a "mysql >" prompt indicating that it is waiting for me to publish the query. The complete boot sequence is as follows:
In order to connect to a server running on some other machine, you need to specify the hostname with-h. If the host is mysql.domain.net, the corresponding command is as follows:
Shell > mysql-h mysql.domain.net-u tom-p
In most of the examples that illustrate the mysql command line later, we intend to omit the-h,-u, and-p options for simplicity. And assume that you will provide any options you need.
There are many ways to set up an account so that you don't have to type in the connection parameters every time you run mysql. This problem has been introduced earlier, you only need to provide parameters in the options file, see 3.2.2 for details. You may want to skip to this section now to find ways to connect to the server more easily.
3. End the session
After you have established a connection to the server, you can end the session at any time by typing the following command:
Quit exit
You can also type Control-D to exit, at least on UNIX.
Simplify connections with options files
When activating mysql, you may need to specify connection parameters such as hostname, user name, or password. Running a program requires a lot of typing, which can soon be boring. The options file can be used to store connection parameters and reduce input work.
For example, for using a mysqladmin client to manage a database, you will soon get tired of using such a long command line:
Shell > mysql-u root-p varialbles
Enter password: *
You may choose to use a global options file to store your parameters:
[mysqladmin]
# you can also use [client] to store parameters for all clients
User=root
Password=yourpassword
In this way, executing mysqladmin variables will not show any storage rejection errors, and you can maintain the database as a root user.
Wait a minute, you will immediately find that this is a great security, because anyone who can read the options file can get your password! The workaround is to provide only the password option, not the password:
[admin]
User=root
Password
In this way, when you execute on the command line, you will be prompted for your data password:
Shell > mysql varialbles
Enter password: *
However, you have to provide the password option, or you will still have to provide the-p option on the command line.
Using the input line editor of mysql
Mysql has a built-in GNU Readline library that allows editing of input lines. You can process the currently entered rows, or call up previously entered rows and re-execute them (as is or with further modifications). This is convenient when typing a line and finding an error; you can backspace within the line and fix it before pressing the Enter key. If you enter a query with an error, you can call the query and edit it to resolve the problem, and then resubmit it. This is the easiest way if you type the entire query on one line. )
Some very useful editing sequences are listed in Table 1, and there are many input editing commands in addition to those given in this table. Using the Internet search engine, you should be able to find an online version of the Readline manual. This manual is also included in the Readline distribution and can be found in. Get the Gnu Web site of org/.
Table 1 mysql input editing commands
Key sequence specification
Up Arrow, Ctrl-p
Down Arrow, Ctrl-N
Left Arrow, Ctrl-B
Right Arrow, Ctrl-F
Escape Ctrl-B
Escape Ctrl-F
Ctrl-A
Ctrl-E
Ctrl-D
Delete
Escape D
Escape Backspace
Ctrl-K
Ctrl-_ adjusts the previous line
Adjust to the next line
Move the cursor to the left (backward)
Move the cursor to the right (forward)
Move back one word
Move forward one word
Move the cursor to the line header
Move the cursor to the end of the line
Delete the characters under the cursor
Delete the character to the left of the cursor
Deletion of words
Delete the word to the left of the cursor
Delete all characters from the cursor to the end of the line
Undo the last change; can be repeated
The following example describes a simple use of input editing. Suppose you entered the following query using mysql:
If you have noticed that "president" is misspelled as "persident" before pressing Enter, press the left arrow or Ctrl-B to move the cursor to the left of "s" multiple times. Then press Delete to delete "er" twice, type "re" to correct the error, and press Enter to publish the query. If you don't notice the misspelling and press Enter, there will be no problem. After mysql displays the error message, press the up arrow or Ctrl-P to bring up the row, and then edit it.
Input line editing does not work in the Windows version of mysql, but free cygwin_32 client distributions are available from the MySQL Web site. The mysqlc program in this distribution is the same as mysql, but it supports input line editing commands.
Batch mode connection
In the previous section, you interactively used mysql to enter the query and view the results. You can also run mysql in batch mode. To do this, put the command you want to run in a file and tell mysql to read its input from the file:
Shell > mysql
< batch-file 如果你需要在命令行上指定连接参数,命令可能看起来像这样: shell>Mysql-h host-u user-p
< batch-file Enter password: ******** 当你这样使用mysql时,你正在创建一个脚本文件,然后执行脚本。 为什么要使用一个脚本?有很多原因: 如果你重复地运行查询(比如说,每天或每周),把它做成一个脚本使得你在每次执行它时避免重新键入。 你能通过拷贝并编辑脚本文件从类似的现有的查询生成一个新查询。 当你正在开发查询时,批模式也是很有用的,特别对多行命令或多行语句序列。如果你犯了一个错误,你不必重新打入所有一切,只要编辑你的脚本来改正错误,然后告诉mysql再次执行它。 如果你有一个产生很多输出的查询,你可以通过一个分页器而不是盯着它翻屏到你屏幕的顶端来运行输出: $ mysql < batch-file | more 你能捕捉输出到一个文件中进行更一步的处理: shell>Mysql
< batch-file >Mysql
Thank you for reading, the above is the content of "the connection process between MySQL and client". After the study of this article, I believe you have a deeper understanding of the connection process between MySQL and client, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.