Академический Документы
Профессиональный Документы
Культура Документы
manexware
CHAPTER 1
This topic explains how to pull and run the mssql-server Docker image. This image consists
of SQL Server running on Linux and can be used with the Docker Engine 1.8+ on Linux or
on Docker for Mac/Windows. We are currently tracking all issues with the Docker image in
our mssql-docker GitHub repository.
Note
This image is running SQL Server on an Ubuntu Linux base image. To run the SQL Server on Windows
Containers Docker image, check out the mssql-server-windows Docker Hub page.
1
sudo docker pull microsoft/mssql-server-linux
2
Tip
If you using Docker for Windows, remove the word sudo from the command-line in this step and
step three.
3 To run the Docker image, you can use the following commands:
Copy
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=<YourStrong!Passw0rd>' -p
1433:1433 -d microsoft/mssql-server-linux
4
5 To persist the data generated from your Docker container, you must map volume to
the host machine. To do that, use the run command with the -v <host
directory>:/var/opt/mssql flag. This will allow the data to be restored between con-
tainer executions.
Copy
sudo docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=<-
YourStrong!Passw0rd>' -p 1433:1433 -v <host directory>:/var/opt/mssql -d
microsoft/mssql-server-linux
6
Note
The ACCEPT_EULA and SA_PASSWORD environment variables are required to run the image.
Important
Volume mapping for Docker-machine on Mac with the SQL Server on Linux image is not supported
at this time.
7
Upgrading the Docker image
Upgrading the Docker image will require just pulling the latest version from the registry. In
order to do so, you just need to execute the pull command:
Copy
bash
sudo docker pull microsoft/mssql-server-linux:latest
You can now create new containers that will have the latest version of SQL Server in Linux
on Docker.
2
Alternatively, you can connect to the SQL Server instance on your Docker machine from
any external Windows or Linux tool that supports SQL connections, such as SQL Server
Management Studio (SSMS) on Windows.
+
Next steps
After installing SQL Server on Linux, next see how to connect to the server and run basic
Transact-SQL queries. Also, check out the mssql-docker GitHub repository for resources
and feedback.
3
CHAPTER 2
The following steps install the command-line tools, Microsoft ODBC drivers, and their de-
pendencies. The mssql-tools package contains:
4 If you had a previous version of mssql-tools installed, remove any older unixODBC
packages.
sudo yum update
sudo yum remove unixODBC-utf16 unixODBC-utf16-devel
4
5 Run the following commands to install mssql-tools with the unixODBC developer pack-
age.
sudo yum update
sudo yum install mssql-tools unixODBC-devel
Note
# # To update to the latest version of mssql-tools run the following commands:
To make sqlcmd/bcp accessible from the bash shell for interactive/non-login sessions, #
modify the PATH in the ~/.bashrc file with the following command:
3 Update the sources list and run the installation command with the unixODBC devel-
oper package.
sudo apt-get update
sudo apt-get install mssql-tools unixodbc-dev
Note
5
# # To update to the latest version of mssql-tools run the following commands:
## sudo apt-get update
sudo apt-get install mssql-tools
To make sqlcmd/bcp accessible from the bash shell for interactive/non-login sessions, #
modify the PATH in the ~/.bashrc file with the following command:
Note
To update to the latest version of mssql-tools run the following commands:
To make sqlcmd/bcp accessible from the bash shell for interactive/non-login sessions, mod-
ify the PATH in the ~/.bashrc file with the following command:
6
Install tools on macOS
Sqlcmd and bcp are not available on macOS.
Docker
Starting with SQL Server vNext CTP 1.4, the SQL Server command-line tools are included
in the Docker image. If you attach to the image with an interactive command-prompt, you
can run the tools locally.
Tip
If you successfully installed the tools with apt-get, yum, or zypper, you do not need to download or manu-
ally install any of the packages below.
The following table provides the location for the latest tools packages. To install the RPM
packages, use rpm -ivh packagename.rpm. To install the Debian packages, use dpkg -i
packagename.deb.
These packages depend on msodbcsql, which must be installed first. The msodbcsql pacak-
age also has a dependency on either unixODBC-devel (RPM) or unixodbc-dev (Debian).
The location of the msodbcsql packages are listed in the following table.
7
msodbcsql Version Download
package
In some cases, you might have to manually install the dependencies for the mssql-tools
and msodbcsql packages. For Debian packages, you can inspect the dependencies with
the following commands:
Next steps
After installation, connect to the SQL Server instance to create and manage databases. To
get started, see Connect and query SQL Server on Linux.
8
CHAPTER 3
This topic provides connection requirements and guidance for SQL Server vNext CTP 1.4
running on Linux. In most cases, the connection requirements and processes do not differ
across platforms. This topic approaches the subject in the context of Linux and then points
to other resources.
This topic is a walk-through. In this walk-through, you will use sqlcmd to connect to SQL
Server vNext on Linux. After successfully connecting, you will use Transact-SQL (T-SQL)
queries to create and populate a small database.
Tip
Sqlcmd is just one tool for connecting to SQL Server to run queries and perform management and devel-
opment tasks. For other tools such as SQL Server Management Studio and Visual Studio Code, see the
Develop and Manage areas.
Tip
9
On macOS, use sql-cli because sqlcmd and bcp are not available.
Tip
You can omit the password on the command-line to be prompted to enter it.
To connect to a remote instance, specify the machine name or IP address for the -S pa-
rameter.
Tip
If you get a connection failure, first attempt to diagnose the problem from the error message. Then re-
view the connection troubleshooting recommendations.
To to complete each of these tasks, copy the Transact-SQL from the examples below into
the sqlcmd session that you created in the previous step.
For example, this query returns the name of all of the databases.
USE testdb;
GO
10
Create a table in the current database:
QUIT
In this walk-through you connected to SQL Server with sqlcmd, and created and populated
a database. For more information on how to use sqlcmd.exe, see sqlcmd Utility.
Next Steps
If you're new to T-SQL, see Tutorial: Writing Transact-SQL Statements and the Transact-
SQL Reference (Database Engine).
11
CHAPTER 4
SQL Server's backup and restore feature is the recommended way to migrate a database
from SQL Server on Windows to SQL Server vNext CTP 1.4 on Linux. This topic provides
step-by-step instructions for this technique. In this tutorial, you will:
Note
To restore a database to SQL Server on Linux, the source backup must be taken from SQL Server 2014
or SQL Server 2016. The backup SQL Server build number must not be greater than the restore SQL
Server build number.
2 Open the zip file, and extract the AdventureWorks2014.bak file to a folder on your ma-
chine.
3
Transfer the backup file to Linux
To restore the database, you must first transfer the backup file from the Windows machine
to the target Linux machine.
12
1 For Windows, install a Bash shell. There are several options, including the following:
Download an open source Bash shell, such as PuTTY.
Or, on Windows 10, use the new built-in Bash shell (beta).
Or, if you work with Git, use the Git Bash shell.
2 Open a Bash shell (terminal) and navigate to the directory containing
AdventureWorks2014.bak.
3 Use the scp (secure copy) command to transfer the file to the target Linux machine.
The following example transfers AdventureWorks2014.bak to the home directory of us-
er1 on the server named linuxserver1.
Copy
bash
sudo scp AdventureWorks2014.bak user1@linuxserver1:./
4
In the previous example, you could instead provide the IP address in place of the
server name.
5
There are several alternatives to using scp. One is to use Samba to setup an SMB network
share between Windows and Linux. For a walkthrough on Ubuntu, see How to Create a Net-
work Share Via Samba. Once established, you can access it as a network file share from
Windows, such as \\machinenameorip\share.
1 Open a Terminal on the target Linux machine that contains the backup.
2 Enter super user mode.
sudo su
3 Create a new backup directory. The -p parameter does nothing if the directory already
exists.
mkdir -p /var/opt/mssql/backup
4 Move the backup file to that directory. In the following example, the backup file resides
in the home directory of user1. Change the command to match the location of
AdventureWorks2014.bak on your machine.
mv /home/user1/AdventureWorks2014.bak /var/opt/mssql/backup/
13
Restore the database backup
To restore the backup, you can use the RESTORE DATABASE Transact-SQL (TQL) com-
mand.
Note
The following steps use the sqlcmd tool. If you havent install SQL Server Tools, see Install SQL Server
on Linux.
1 In the same terminal, launch sqlcmd. The following example connects to the local SQL
Server instance with the SA user. Enter the password when prompted or specify the
password with the -P parameter.
sqlcmd -S localhost -U SA
2 After connecting, enter the following RESTORE DATABSE command, pressing EN-
TER after each line. The example below restores the AdventureWorks2014.bak file
from the /var/opt/mssql/backup directory.
RESTORE DATABASE AdventureWorks
FROM DISK = '/var/opt/mssql/backup/AdventureWorks2014.bak'
WITH MOVE 'AdventureWorks2014_Data' TO
'/var/opt/mssql/data/AdventureWorks2014_Data.mdf',
MOVE 'AdventureWorks2014_Log' TO
'/var/opt/mssql/data/AdventureWorks2014_Log.ldf'
GO
4 Run the following query that lists the top 10 products in the Production.Products table.
SELECT TOP 10 Name, ProductNumber FROM Production.Product ORDER BY Name
5 GO
6
7 +
14
15
CHAPTER 5
TIPS
Overview
The RESTORE FILELISTONLY option allows you to see a list of the files that were backed
up. So for example if you have a full backup you will see all of the data files (mdf) and the
log file (ldf).
Explanation
This information can only be returned using T-SQL there is not a way to get this informa-
tion from SQL Server Management Studio. Although if you do a restore and select options,
you will see some of this information in SSMS.
The RESTORE FILELISTONLY option can be simply issued as follows for a backup that ex-
ists on disk. If there are multiple backups in one file and you do not specify "WITH FILE =
X" you will only get information for the first backup in the file. To get the FILE number use
RESTORE HEADERONLY and use the "Position" column.
16
When I had this problem, it's because the restore command was taking long enough for mssql to
time out (with a totally unhelpful error message). Specifying a long timeout when connecting al-
lowed the restore to complete. eg
mssql -s localhost -p "< sa_password >" -t 36000000 -T 36000000
17