一聚教程网:一个值得你收藏的教程网站

热门教程

使用MySQL Shell连接数据库代码方法

时间:2022-06-29 08:33:36 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下使用MySQL Shell连接数据库代码方法,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

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

一、下载MySQL Shell

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

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

二、配置运行环境

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

打开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 [email protected]:3360
Please provide the password for '[email protected]:3360': ******
Save password for '[email protected]: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 '[email protected]: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 >

另一种方法是先输入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 [email protected]:3360
Creating a session to '[email protected]:3360'
Please provide the password for '[email protected]:3360': ******
Save password for '[email protected]: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 >

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语言执行数据库命令了。

热门栏目