8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 5 教程 / 5.1 连接和断开服务器
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

5.1 连接和断开服务器5.1 Connecting to and Disconnecting from the Server

要连接到服务器,通常在调用 mysql 时,需要提供 MySQL 用户名,并且很可能还需要提供密码。如果服务器运行在与你登录的机器不同的机器上,你还必须指定主机名。请联系管理员以了解你应该使用哪些连接参数(即使用哪个主机、用户名和密码)。一旦知道正确的参数,你应该可以像下面这样连接:To connect to the server, you usually need to provide a MySQL user name when you invoke mysql and, most likely, a password. 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:

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

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

如果成功,你应该会看到一些介绍性信息,随后是 mysql> 提示符:If that works, you should see some introductory information followed by a mysql> prompt:

$> 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.4.9-standard

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

mysql>

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

如果您在运行 MySQL 的同一台机器上登录,可以省略主机,并直接使用以下内容: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 -u user -p

如果在尝试登录时,您收到诸如 ERROR 2002 (HY000):无法通过套接字 '/tmp/mysql.sock' 连接到本地 MySQL 服务器 (2) 的错误信息,这意味着 MySQL 服务器守护进程(Unix)或服务(Windows)未运行。请咨询管理员或参阅适用于您的操作系统的 第 2 章,安装 MySQL 部分。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. Consult the administrator or see the section of Chapter 2, Installing MySQL that is appropriate to your operating system.

有关尝试登录时经常遇到的其他问题的帮助,请参见 B.3.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”.

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

$> mysql

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

mysql> QUIT
Bye

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

以下各节中的大多数示例假设您已连接到服务器。它们通过 mysql> 提示符来表示这一点。Most examples in the following sections assume that you are connected to the server. They indicate this by the mysql> prompt.