• 如何使用MySQL Shell连接数据库


    在有些情况下我们需要使用命令行方式连接MySQL数据库,这时可以使用MySQL官方提供的命令行工具MySQL Shell。

    一、下载MySQL Shell

    下载地址:https://dev.mysql.com/downloads/shell/

    MySQL Shell下载地址

    注意下载地址为下面的Other Downloads,上面的Recommended Download是MySQL数据库的下载地址。

    二、配置运行环境

    下载后得到一个名为mysql-shell-8.0.19-windows-x86-64bit.zip的压缩包,将该压缩包解压后执行其bin目录下的mysqlsh.exe程序即可,为了方便也可以直接将程序路径添加到环境变量Path中,如下图所示。

    MySQL Shell环境变量配置

    打开Windows PowerShell或CMD窗口,输入mysqlsh --version,如果出现版本提示则表明环境变量配置成功。

    PS C:Userszhbao> mysqlsh --version
    D:Program FilesMySQLmysql-shell-8.0.19-windows-x86-64bitinmysqlsh.exe   Ver 8.0.19 for Win64 on 
    x86_64 - for MySQL 8.0.19 (MySQL Community Server (GPL))
    
    • 1
    • 2
    • 3

    三、连接MySQL数据库

    连接数据库的方法有两种,第一种是直接在命令行中输入mysqlsh命令和数据库地址等信息,然后根据提示输入密码即可。

    PS C:Userszhbao> mysqlsh admin48@10.53.122.98:3360
    Please provide the password for 'admin48@10.53.122.98:3360': ******
    Save password for 'admin48@10.53.122.98:3360'? [Y]es/[N]o/Ne[v]er (default No): n
    MySQL Shell 8.0.19
    
    Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
    Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
    Other names may be trademarks of their respective owners.
    
    Type 'help' or '?' for help; 'quit' to exit.
    Creating a session to 'admin48@10.53.122.98:3360'
    Fetching schema names for autocompletion... Press ^C to stop.
    Your MySQL connection id is 958
    Server version: 5.7.20 MySQL Community Server (GPL)
    No default schema selected; type use  to set one.
     MySQL  10.53.122.98:3360  JS >
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    另一种方法是先输入mysqlsh启动程序,然后再通过connect命令连接数据库。

    PS C:Userszhbao> mysqlsh
    MySQL Shell 8.0.19
    
    Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
    Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
    Other names may be trademarks of their respective owners.
    
    Type 'help' or '?' for help; 'quit' to exit.
     MySQL  JS > connect admin48@10.53.122.98:3360
    Creating a session to 'admin48@10.53.122.98:3360'
    Please provide the password for 'admin48@10.53.122.98:3360': ******
    Save password for 'admin48@10.53.122.98:3360'? [Y]es/[N]o/Ne[v]er (default No): n
    Fetching schema names for autocompletion... Press ^C to stop.
    Your MySQL connection id is 960
    Server version: 5.7.20 MySQL Community Server (GPL)
    No default schema selected; type use  to set one.
     MySQL  10.53.122.98:3360  JS >
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    MySQL Shell默认是JS语言交互模式,可以通过sql命令切换到SQL语言交互模式。

     MySQL  10.53.122.98:3360  JS > sql
    Switching to SQL mode... Commands end with ;
     MySQL  10.53.122.98:3360  SQL >
    
    • 1
    • 2
    • 3

    接下来就可以通过SQL语言执行数据库命令了。

  • 相关阅读:
    【第四天]C++高级类和对象:运算符重载、string类和智能指针的深度解析
    SpringCloud微服务之sentinel实现限流详细流程
    查找并删除指定文件夹下包含有指定的关键词的文件(GUI界面)
    国产操作系统上安装软件包及环境管理系统Conda _ 统信 _ 麒麟
    Linux:环境变量
    【3D 图像分割】基于 Pytorch 的 VNet 3D 图像分割6(数据预处理)
    客户管理系统(SSM版):查看交易明细
    MySql跨库跨表触发器
    本周 AI 新闻报道:多个大厂发布了重大更新
    Android系统编程入门系列之硬件交互——无线通信WLAN
  • 原文地址:https://blog.csdn.net/m0_67390969/article/details/126565673