当前位置:网站首页>How to implement SQLSERVER database migration in container

How to implement SQLSERVER database migration in container

2022-06-24 15:38:00 Exclusive rainy days

Understand how to back up and restore in Docker Medium SqlServer Medium database.

Preprocessing :

  1. Docker engine 1.8 And above
  2. Minimum 2G Disk space and 2G Of memory space
  3. Have superuser privileges
  4. Yes Docker Some basic concepts and familiar with basic operations

How to be in Docker Backing up and restoring databases in containers

Install and run the database

Specify according to your own needs SqlServer edition

docker pull microsoft/mssql-server-linux:2017-latest

And then in Docker in backstage function SqlServer Containers .

docker run -e ‘ACCEPT_EULA=Y’ -e ‘MSSQL_SA_PASSWORD=SQLShack$2018’ –name shackdemo1 -p 1401:1433 -d microsoft/mssql-server-linux:latest

Connect to database

After the container starts , Get into SqlServer In container .

docker exec –it shackdemo1 bash

Once in the container , seek sqlcmd command . stay Docker Starting up sqlserver Server , The script is stored in /opt/mssql-tools/bin/sqlcmd in . The following is the main translation and explanation sqlcmd Important parameters of the command .

for example , Connect local SqlServer In the server SQLTestDB database . The input command is

sqlcmd -S 127.0.0.1 -U sa -d SQLTestDB -P Password123

The main command :

  • -S Express SqlServer The address of the server
  • -U Express SqlServer The user name of the database
  • -d Indicates the database used for this operation
  • -P Indicates the password of the user connecting to the database
  • -Q It means that it can be directly connected to the needs SQL Operation statement

And then create a SQLShackDemo database , And insert some table Data is used to test .(select name from sys.databases;go You can see all databases in the system )

1> create database SQLShackDemo;
2> go
1> use SQLShackDemo;
2> go
Changed database context to 'SQLShackDemo'.
1> create table SQLAuthor (id int,name char(20));
2> go
1> insert into SQLAuthor values(1,'Prashanth Jayaram');
2> go

(1 rows affected)
1>

Backup database

then , Use the following command to backup the database .

BACKUP DATABASE [SQLShackDemo] TO DISK = N'/var/opt/mssql/backup/SQLShackDemo.bak' WITH FORMAT, INIT, COMPRESSION,STATS = 10

Then we left the container to test that the backup file was not container Container erase . adopt docker cp It is very convenient to realize the mutual transmission between the data in the container and the data of the host .

Simply look at docker cp command , It is mainly divided into two parameters :

  1. Container name ,shackdemo1, Immediately following the colon is the path of the file to be copied in the container
  2. The path to the host .

for example ,docker ps shackdemo1:/var/opt/mssql/backup/SQLShackDemo.bak /tmp/, After executing the order , You can view it on the host ls -l /tmp/SQLShackDemo.bak.

Recover database

The main content is how to do other things SqlServer Container based backup File recovery database ?

Create a new container ,shackdemo2.

docker run -e ‘ACCEPT_EULA=Y’ -e ‘MSSQL_SA_PASSWORD=SQLShack$2018’ –name shackdemo2 -p 1402:1433 -d microsoft/mssql-server-linux:latest

Then enter the new container

docker exec -it shackdemo2 bash

Enter the specified command line

/opt/mssql-tools/bin/sqlcmd -S localhost -U SA Password:

Enter the following select name from sys.databases;go command , Query the existing system database information of the newly created container .

Exit the container , Copy the previously backed up files to the container shackdemo2 in . Similarly, there are two parameters :

  1. The path to the host
  2. Container name +:+ The directory in the container
Docker cp /tmp/SQLShackDemo.bak sqldemo2:var/opt/mssql/data/

Next , Sign in shackdemo2 Containers , And enter sqlcmd Console after , Run the following recovery operations .

RESTORE DATABASE [SQLShackDemo] FROM DISK = N’/var/opt/mssql/data/SQLShackDemo.bak’ with REPLACE

When the recovery is complete , You can go through the front select name from sys.databases;go Property to query .

utilize Docker The data volume inside realizes database migration

usually , Data information can be stored in a data volume container , such , When stopped or destroyed SqlServer When the container , The information stored in the database can still be maintained . And be able to It is convenient and efficient to transfer data in different containers .

Start a new container , And mount and create data volumes sqlservervolume

docker run -e'ACCEPT_EULA=Y' -e'[email protected]' --name sqldemo  –v sqlservervolume:/var/opt/mssql -d microsoft/mssql-server-linux:2017-latest

then , Enter the inside of the container , Perform some database operations .

$ docker exec -it sqldemo bash
# /opt/mssql-tools/bin.sqlcmd -U SA -P [email protected]
1> create database sqlvolumeetestDB;
2>go

After creation , Leave SQL shell And quit . Stop and remove sqldemo Containers .

docker stop sqldemo
docker rm sqldemo

Stop and remove sqldemo after , Previously created Data volumes still exist .

docker volume ls

And then again , Start a new container , And still mount the previous data volume . After the mount is successful , You'll find that , The container still exists .

Part of supplementary learning

Security

Backup for database , It is suggested that TRUSTWORTHY Set to OFF. Please refer to the official website for details ALTER DATABASE SET Options (Transact-SQL)

ALTER DATABASE database_name SET TRUSTWORTHY OFF

jurisdiction

By default ,sysadmin、db_owner and db_backoperator And other roles need to be granted BACKUP DATABASE and BACKUP LOG jurisdiction .

How to use Transact-SQL

Through execution BACKUP DATABASE Statement to create a complete database backup , At the same time make

  • The name of the database to back up
  • A backup device that writes a full database backup .

The basic syntax is defined as follows :

BACKUP DATABASE database TO backup_device [ , ...n ] [ WITH with_options [ , ...o ] ] ;

Assume , Want to back up the database ‘SQLTestDB’ Backup the database contents in to disk .

USE SQLTestDB;
GO
BACKUP DATABASE SQLTestDB
TO DISK = 'c:\tmp\SQLTestDB.bak'
   WITH FORMAT,
      MEDIANAME = 'SQLServerBackups',
      NAME = 'Full Backup of SQLTestDB';
GO

Reference documents


  1. Understanding Backup and Restore operations in SQL Server Docker Containers
  2. Restore a SQL Server database in Docker - SQL Server | Microsoft Docs
原网站

版权声明
本文为[Exclusive rainy days]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241321404433.html