3.1 Connecting to and Disconnecting from the Server连接到服务器和断开与服务器的连接

To connect to the server, you usually need to provide a MySQL user name when you invoke mysql and, most likely, a password. 要连接到服务器,通常需要在调用mysql时提供MySQL用户名,最有可能的是提供密码。If the server runs on a machine other than the one where you log in, you must also specify a host name. 如果服务器在您登录的计算机以外的计算机上运行,则还必须指定主机名。Contact your administrator to find out what connection parameters you should use to connect (that is, what host, user name, and password to use). 请与管理员联系,以了解应使用哪些连接参数进行连接(即,要使用的主机、用户名和密码)。Once you know the proper parameters, you should be able to connect like this:一旦您知道正确的参数,您应该能够像这样连接:

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

host and user represent the host name where your MySQL server is running and the user name of your MySQL account. hostuser表示运行MySQL服务器的主机名和MySQL帐户的用户名。Substitute appropriate values for your setup. 为您的设置替换适当的值。The ******** represents your password; enter it when mysql displays the Enter password: prompt.*******代表您的密码;当mysql显示Enter password:提示时输入它。

If that works, you should see some introductory information followed by a mysql> prompt:如果这样做有效,您应该看到一些介绍性信息,后面是mysql>提示:

shell> mysql -h host -u user -p
Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 25338 to server version: 8.0.25-standard

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

The mysql> prompt tells you that mysql is ready for you to enter SQL statements.mysql>提示符告诉您mysql已准备好输入SQL语句。

If you are logging in on the same machine that MySQL is running on, you can omit the host, and simply use the following:如果您在运行MySQL的同一台计算机上登录,则可以省略主机,只需使用以下命令:

shell> mysql -u user -p

If, when you attempt to log in, you get an error message such as ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2), it means that the MySQL server daemon (Unix) or service (Windows) is not running. 如果在尝试登录时收到错误消息,例如ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2),则表示MySQL服务器守护程序(Unix)或服务(Windows)未运行。Consult the administrator or see the section of Chapter 2, Installing and Upgrading MySQL that is appropriate to your operating system.请咨询管理员或参阅第2章,“安装和升级适合您的操作系统的MySQL”一节。

For help with other problems often encountered when trying to log in, see Section B.3.2, “Common Errors When Using MySQL Programs”.有关尝试登录时经常遇到的其他问题的帮助,请参阅第B.3.2节,“使用MySQL程序时的常见错误”

Some MySQL installations permit users to connect as the anonymous (unnamed) user to the server running on the local host. 一些MySQL安装允许用户以匿名(未命名)用户身份连接到本地主机上运行的服务器。If this is the case on your machine, you should be able to connect to that server by invoking mysql without any options:如果您的计算机上出现这种情况,您应该能够通过调用mysql连接到该服务器,而无需任何选项:

shell> mysql

After you have connected successfully, you can disconnect any time by typing QUIT (or \q) at the mysql> prompt:成功连接后,您可以在mysql>提示上键入QUIT(或\q)随时断开连接:

mysql> QUIT
Bye

On Unix, you can also disconnect by pressing Control+D.在Unix上,也可以按Control+D断开连接。

Most examples in the following sections assume that you are connected to the server. 以下部分中的大多数示例都假定您已连接到服务器。They indicate this by the mysql> prompt.他们通过mysql>提示来表示这。