8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

7.8.4 在多服务器环境中使用客户端程序7.8.4 Using Client Programs in a Multiple-Server Environment

要将客户端程序连接到监听的 MySQL 服务器,而该服务器的网络接口与编译到客户端中的接口不同,您可以使用以下方法之一:To connect with a client program to a MySQL server that is listening to different network interfaces from those compiled into your client, you can use one of the following methods:

  • 使用 --host=host_name --port=port_number 启动客户端,以通过 TCP/IP 连接到远程服务器;使用 --host=127.0.0.1 --port=port_number 连接到本地服务器,或者使用 --host=localhost --socket=file_name 通过 Unix 套接字文件或 Windows 命名管道连接到本地服务器。Start the client with --host=host_name --port=port_number to connect using TCP/IP to a remote server, with --host=127.0.0.1 --port=port_number to connect using TCP/IP to a local server, or with --host=localhost --socket=file_name to connect to a local server using a Unix socket file or a Windows named pipe.

  • 使用 --protocol=TCP 启动客户端以使用 TCP/IP 连接,使用 --protocol=SOCKET 通过 Unix 套接字文件连接,使用 --protocol=PIPE 通过命名管道连接,或使用 --protocol=MEMORY 通过共享内存连接。对于 TCP/IP 连接,你可能还需要指定 --host 和 --port 选项。对于其他类型的连接,你可能需要指定 --socket 选项以指定 Unix 套接字文件或 Windows 命名管道名称,或 --shared-memory-base-name 选项以指定共享内存名称。共享内存连接仅在 Windows 上支持。Start the client with --protocol=TCP to connect using TCP/IP, --protocol=SOCKET to connect using a Unix socket file, --protocol=PIPE to connect using a named pipe, or --protocol=MEMORY to connect using shared memory. For TCP/IP connections, you may also need to specify --host and --port options. For the other types of connections, you may need to specify a --socket option to specify a Unix socket file or Windows named-pipe name, or a --shared-memory-base-name option to specify the shared-memory name. Shared-memory connections are supported only on Windows.

  • 在 Unix 系统上,在启动客户端之前,设置 MYSQL_UNIX_PORT 和 MYSQL_TCP_PORT 环境变量以指向 Unix 套接字文件和 TCP/IP 端口号。如果您通常使用特定的套接字文件或端口号,可以将设置这些环境变量的命令放入您的 .login 文件中,这样每次登录时都会应用它们。参见 第 6.9 节,“环境变量”。 On Unix, set the MYSQL_UNIX_PORT and MYSQL_TCP_PORT environment variables to point to the Unix socket file and TCP/IP port number before you start your clients. If you normally use a specific socket file or port number, you can place commands to set these environment variables in your .login file so that they apply each time you log in. See Section 6.9, “Environment Variables”.

  • 在选项文件的 [client] 组中指定默认的 Unix 套接字文件和 TCP/IP 端口号。例如,你可以在 Windows 上使用 C:\my.cnf,或者在 Unix 的主目录中使用 .my.cnf 文件。参见 第 6.2.2.2 节,“使用选项文件”。Specify the default Unix socket file and TCP/IP port number in the [client] group of an option file. For example, you can use C:\my.cnf on Windows, or the .my.cnf file in your home directory on Unix. See Section 6.2.2.2, “Using Option Files”.

  • 在 C 程序中,您可以在mysql_real_connect()函数调用中指定套接字文件或端口号参数。您也可以通过调用mysql_options()让程序读取选项文件。参见C API 基本函数说明。In a C program, you can specify the socket file or port number arguments in the mysql_real_connect() call. You can also have the program read option files by calling mysql_options(). See C API Basic Function Descriptions.

  • 如果您使用 Perl 的DBD::mysql模块,您可以从 MySQL 选项文件中读取选项。例如:If you are using the Perl DBD::mysql module, you can read options from MySQL option files. For example:

    $dsn = "DBI:mysql:test;mysql_read_default_group=client;"
            . "mysql_read_default_file=/usr/local/mysql/data/my.cnf";
    $dbh = DBI->connect($dsn, $user, $password);
    

    参见第 31.9 节,“MySQL Perl API”。See Section 31.9, “MySQL Perl API”.

    其他编程接口可能提供类似的功能来读取选项文件。Other programming interfaces may provide similar capabilities for reading option files.