sql docker容器_如何将Microsoft SQL Server Docker容器与Azure Data Studio连接
sql docker容器
This guide shows you how to use Docker to pull a MSSQL Server image and run it. Azure Data Studio is a cross-platform database tool that will be using to connect our Docker container with MSSQL and execute SQL statements.
本指南向您展示如何使用Docker提取MSSQL Server映像并运行它。 Azure Data Studio是一个跨平台的数据库工具,将用于将Docker容器与MSSQL连接并执行SQL语句。
At the end, I will show you how to import a database to the Docker file system so that you can access it through Azure Data Studio.
最后,我将向您展示如何将数据库导入Docker文件系统,以便您可以通过Azure Data Studio访问它。
Check out other related guides here:
在此处查看其他相关指南:
-
How to Connect your AWS RDS Microsoft SQL Server using Azure Data Studio
-
How to Import a Sample Database to your AWS RDS Microsoft SQL Server using S3
We will be touching on the technologies shown below:
我们将涉及以下技术:
- Database: Microsoft SQL Server 数据库:Microsoft SQL Server
- Container to pull mssql-server-demo: Docker 提取mssql-server-demo的容器:Docker
- Installer for mssql-cli: Node.js (Run-time Environment) / Node Package Manager (NPM) mssql-cli的安装程序:Node.js(运行时环境)/ Node Package Manager(NPM)
- Database tool and GUI: Azure Data Studio 数据库工具和GUI:Azure Data Studio
使用Docker构建环境 (Building our Environment with Docker )
安装Docker (Installing Docker)
Full guide for this portion here:
这部分的完整指南在这里 :
-
Download Docker CE (Community Edition) for Mac here.
在此处下载适用于Mac的Docker CE(社区版)。
- To install, double-click on the .dmg file and then drag the Docker application icon to your Application folder. 要安装,请双击.dmg文件,然后将Docker应用程序图标拖到您的Application文件夹中。
什么是Docker? (What is Docker?)
Docker is a platform that enables software to run in its own isolated environment. SQL Server (from 2017) can be run on Docker in its own isolated container.
Docker是一个平台,使软件可以在自己的隔离环境中运行。 SQL Server(自2017年起)可以在Docker的隔离容器中运行。
Once Docker is installed, you simply download — or “pull” — the SQL Server on Linux Docker Image to your Mac, then run it as a Docker container. This container is an isolated environment that contains everything SQL Server needs to run.
安装Docker后,您只需将Linux Docker Image上SQL Server下载(或“拉”)到Mac,然后将其作为Docker容器运行。 此容器是一个隔离的环境,其中包含SQL Server需要运行的所有内容。
启动Docker (Launch Docker)
Open your Docker application, it should be located in the Applications folder.
打开您的Docker应用程序,它应该位于Applications文件夹中。
增加记忆 (Increase the Memory)
By default, Docker will have 2GB of memory allocated to it. SQL Server needs at least 3.25GB. To be safe, increase it to 4GB if you can. Since this is just a playground, 2GB should be enough.
默认情况下,Docker将为其分配2GB的内存。 SQL Server至少需要3.25GB。 为了安全起见,请尽可能将其增加到4GB。 由于这只是一个游乐场,因此2GB应该足够了。
可选-如果您要增加内存大小: (Optional - in case you want to increase memory size:)
- Select Preferences from the little Docker icon in the top menu 从顶部菜单中的Docker小图标中选择偏好设置
- Slide the memory slider up to at least 2GB 将内存滑块向上滑动至少2GB
- Click Apply & Restart 单击“应用并重新启动”
下载SQL Server (Download SQL Server)
Open a Terminal window and run the following command.
打开一个终端窗口并运行以下命令。
sudo docker pull mcr.microsoft.com/mssql/server:2019-latest
This downloads the latest SQL Server 2019 for Linux Docker image to your computer.
这会将最新SQL Server 2019 for Linux Docker映像下载到您的计算机。
You can also check for the latest container version on the Docker website if you wish.
如果需要,您还可以在Docker网站上检查最新的容器版本 。
启动Docker映像 (Launch Docker Image)
Run the following command to launch an instance of the Docker image you just downloaded:
运行以下命令以启动您刚刚下载的Docker映像的实例:
docker run -d --name sql_server_demo -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=really
Example output:
输出示例:
检查Docker容器(可选) (Check the Docker container (optional))
You can type the following command to check that the Docker container is running.
您可以键入以下命令来检查Docker容器是否正在运行。
docker ps
If it’s up and running, it should return something like this:
如果启动并运行,它将返回以下内容:
If you accidentally closed your Docker App, open your terminal and type
如果您不小心关闭了Docker App,请打开终端并输入
docker start sql_server_demo
安装Node.js和NPM (Install the Node.js and NPM)
Check if you have Node.js and NPM. Run the following commands in your terminal.
检查您是否具有Node.js和NPM。 在终端中运行以下命令。
node -v
npm -v
If you get an output with a version number, skip the rest of this section.
如果您获得带有版本号的输出,请跳过本节的其余部分。
Then visit the Node.js website by clicking the following link:
然后通过单击以下链接访问Node.js网站:
Click the LTS version (the version number may be various) download button to download the Node.js package:
单击LTS版本(版本号可能不同)下载按钮以下载Node.js软件包:
Next click and run the package after downloading. MacOS and Windows will have different installation process. Please follow the instruction to install the Node.js.
下载后,单击下一步并运行包。 MacOS和Windows将具有不同的安装过程。 请按照说明安装Node.js。
Then test again if Node.js and NPM were installed successfully by running the following commands in the terminal:
然后通过在终端中运行以下命令来再次测试Node.js和NPM是否已成功安装:
node -v
npm -v
An output should look like this:
输出应如下所示:
安装sql-cli (Install sql-cli)
Run the following command to install the sql-cli command line tool. This tool allows you to run queries and other commands against your SQL Server instance.
运行以下命令以安装sql-cli命令行工具。 使用此工具,您可以对SQL Server实例运行查询和其他命令。
npm install -g sql-cli
If you get a permission error, use the sudo
command:
如果出现权限错误,请使用sudo
命令:
sudo npm install -g sql-cli
连接到MSSQL Server (Connect to MSSQL Server)
Connect to your SQL Server using the mssql command, followed by the username and password parameters. Syntax: -u <username> -p <password>
使用mssql命令以及用户名和密码参数连接到SQL Server。 语法:-u <用户名> -p <密码>
mssql -u sa -p reallyStrongPwd123
Your output should look like this if you successfully connected:
如果成功连接,您的输出应如下所示:
运行快速测试 (Run a Quick Test)
Run a quick test to check if you can connect to your SQL Server. Use the following SQL statement to check your SQL Server version:
运行快速测试以检查是否可以连接到SQL Server。 使用以下SQL语句检查您SQL Server版本:
SELECT @@VERSION;
If it’s running, you should see something like this:
如果正在运行,您应该会看到类似以下内容:
下载SQL Server GUI-Azure Data Studio (Download an SQL Server GUI - Azure Data Studio)
Azure Data Studio (formerly SQL Operations Studio) is a free GUI management tool that you can use to manage SQL Server on your computer. You can use it to create and manage databases, write queries, backup and restore databases, and more.
Azure Data Studio (以前称为SQL Operations Studio)是一个免费的GUI管理工具,可用于管理计算机上SQL Server。 您可以使用它来创建和管理数据库,编写查询,备份和还原数据库等等。
Azure Data Studio is available on Windows, Mac and Linux.
Windows,Mac和Linux上都可以使用Azure Data Studio。
安装Azure Data Studio (Install Azure Data Studio)
To install Azure Data Studio onto your Mac:
要将Azure Data Studio安装到Mac上,请执行以下操作:
-
Visit the Azure Data Studio download page, and click the .zip file for macOS
访问Azure Data Studio下载页面 ,然后单击macOS的.zip文件
- Once the .zip file has finished downloading, double click it to expand its contents .zip文件下载完成后,双击它以展开其内容
-
Drag the .app file to the Applications folder (the file will probably be called Azure Data Studio.app)
将.app文件拖到 Applications文件夹中(该文件可能称为Azure Data Studio.app )
连接到SQL Server (Connect to SQL Server)
Now that Azure Data Studio is installed, you can use it to connect to SQL Server.
现在已经安装了Azure Data Studio,您可以使用它来连接到SQL Server。
- Launch Azure Data Studio. It is located in your Applications folder. 启动Azure Data Studio。 它位于您的应用程序文件夹中。
- Enter the login credentials and other information for the SQL Server instance that you’d like to connect to: 输入您要连接SQL Server实例的登录凭据和其他信息:
It should look similar to this:
它看起来应该类似于:
It should look similar to this:
它看起来应该类似于:
-
Server Name: localhost, [port number]
服务器名称 :本地主机,[端口号]
Server Name: localhost, [port number] Example: localhost, 1433
服务器名称 :本地主机,[端口号] 示例 :本地主机,1433
-
Authentication Type: SQL Login
身份验证类型 :SQL登录
-
User name: [your SQL Server username] or sa
用户名 :[您SQL Server用户名]或sa
-
Password: [your SQL Server password] or reallyStrongPwd123
密码 :[您SQL Server密码]或trueStrongPwd123
-
Database Name: <default>
数据库名称 :<默认>
-
Server Group: <default>
服务器组 :<默认>
If you use a port other than the default 1433, click Advanced and enter it in the Port field.
如果使用默认端口1433以外的端口,请单击“高级”,然后在“端口”字段中输入该端口。
Alternatively, you can append it to your server name with a comma in between. For example, if you used port 1400, type in localhost,1400.
或者,您可以将其附加到服务器名称,中间用逗号隔开。 例如,如果您使用端口1400,则键入localhost,1400。
You can now go ahead and create databases, run scripts, and perform other SQL Server management tasks.
现在,您可以继续创建数据库,运行脚本并执行其他SQL Server管理任务。
-
Click New Query
点击新查询
2. Type SELECT @@VERSION, then Click Run Query.
2.键入SELECT @@ VERSION ,然后单击“运行查询” 。
You should be able to see: Microsoft SQL Server in the Results.
您应该能够看到:结果中的Microsoft SQL Server 。
使用Azure Data Studio将示例数据库导入到SQL Server (Importing a sample database to your SQL Server using Azure Data Studio)
下载示例数据库文件AdventureWorks (Download the sample database file AdventureWorks)
To get the OLTP downloads of AdventureWorks, go to this link and choose any sample database. In my example, I choose AdventureWorks2017.bak
. We will upload this to the S3 Bucket.
要获得AdventureWorks的OLTP下载,请转到此链接并选择任何示例数据库。 在我的示例中,我选择AdventureWorks2017.bak
。 我们将其上传到S3存储桶。
将文件复制到您的Docker (Copying the file to your docker)
Type the following command in the terminal following this syntax:
使用以下语法在终端中键入以下命令:
docker cp <location_of_file> <container_id>:/var/opt/mssql/data
It should look like this:
它看起来应该像这样:
If you forgot your container id, use the docker ps
command.
如果您忘记了容器ID,请使用docker ps
命令。
在Docker中导入示例数据库 (Importing the sample database in Docker)
Go to Azure Data Studio, and click the localhost, 1443, then choose Restore.
转到Azure Data Studio,然后单击localhost 1443 ,然后选择“还原” 。
Then choose Backup file as the selection for Restore from. Next, click the blue button on the right of Backup file path.
然后选择“ 备份文件”作为“从中还原”的选择。 接下来,单击“ 备份文件路径 ”右侧的蓝色按钮。
Look for the sample database file. It should be located in
查找示例数据库文件。 它应该位于
/var/opt/mssql/data/AdventureWorks2017.bak
Choose Restore.
选择还原 。
Check your localhost, 1443. It should generated a Database named AdventureWorks2017 and have contents such as Tables and Views. If not, right-click on localhost, 1443 and choose Refresh. You can also restart your Azure Data Studio application.
检查您的本地主机1443。它应该生成一个名为AdventureWorks2017的数据库,并具有诸如表和视图之类的内容。 如果不是,请右键单击localhost 1443,然后选择“刷新”。 您还可以重新启动Azure Data Studio应用程序。
测试样本数据库 (Testing the sample database)
-
Choose AdventureWorks2017 from the dropdown menu.
从下拉菜单中选择AdventureWorks2017 。
- Write a SQL query: 编写一个SQL查询:
SELECT * FROM HumanResources.Department;
3. Click Run to run the query.
3.单击运行以运行查询。
You should have an output like this:
您应该具有以下输出:
Congratulations! ????????????
恭喜你! ????????????
Resources:
资源:
Connect with me on LinkedIn here
在此处通过LinkedIn与我联系
sql docker容器