首頁 > 軟體

教你使用MySQL Shell連線資料庫的方法

2022-04-12 13:01:30

在有些情況下我們需要使用命令列方式連線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 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連線資料庫內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com