SQL开发知识:教你使用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))

三、连接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 <schema> to set one.
MySQL 10.53.122.98:3360 JS >

另一种方法是先输入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 <schema> to set one.
MySQL 10.53.122.98:3360 JS >

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 >

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

到此这篇关于使用MySQL Shell连接数据库的方法的文章就介绍到这了,更多相关MySQL Shell连接数据库内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!


数据运维技术 » SQL开发知识:教你使用MySQL Shell连接数据库的方法