5.2.2 连接 MySQL 命令行客户端

本主题介绍如何使用 MySQL 命令行客户端连接到 MySQL 数据库系统。

先决条件:

  • 具有互联网连接的机器或计算实例,用于连接到 MySQL 数据库系统。

  • 安装在机器或计算实例上的 MySQL 命令行客户端。

  • 要连接的 MySQL 数据库系统上的 MySQL 用户帐户。您可以使用在创建数据库系统时指定的 MySQL 管理员用户或使用在数据库系统上创建的 MySQL 用户帐户 CREATE USER

  • 允许连接到数据库系统的机器或计算实例的面向公众的 IP 地址。 MySQL 数据库系统详细信息页面上提供了允许的客户端地址信息。请参阅 第 3.4 节,“查看数据库系统详细信息”

  • MySQL 数据库系统详细信息页面 上定义的 MySQL 数据库系统的主机名 。请参阅 第 3.4 节,“查看数据库系统详细信息”

要连接到数据库系统:

  1. 启动 MySQL 客户端并使用以下命令连接到 MySQL 数据库系统:

    $> mysql --host HostNameOfMySQLDBSystem -u Username -p

    建立连接并显示类似于以下内容的消息:

    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 40
    Server version: 8.0.30-u1-cloud MySQL Enterprise - Cloud
    
    Copyright (c) 2000, 2022, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql>