当前位置:网站首页>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 :
- Docker engine 1.8 And above
- Minimum 2G Disk space and 2G Of memory space
- Have superuser privileges
- 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 :
- Container name ,shackdemo1, Immediately following the colon is the path of the file to be copied in the container
- 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 :
- The path to the host
- 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
边栏推荐
- [log service CLS] a taste of Tencent cloud log service CLS
- Multimeter resistance measurement diagram and precautions
- Bosun query
- When installing Wireshark, npcap cannot be installed successfully. It is recommended to use WinPcap first
- Security Analysis on mining trend of dogecoin, a public cloud
- How to generate assembly code using clang in Intel syntax- How to generate assembly code with clang in Intel syntax?
- Kubernetes practical tips: using ksniff to capture packets
- MySQL 开发规范
- Is industrial securities reliable? Is it safe to open a securities account?
- The industrial control security of roaming the Intranet
猜你喜欢

运营商5G用户渗透远远比4G慢,5G的普及还得看中国广电

Multimeter resistance measurement diagram and precautions
Step by step introduction to sqlsugar based development framework (9) -- Realizing field permission control with WinForm control

He is also a junior test engineer. Why is his salary high? The interview must be brilliant at these points

熬夜整理出的软件测试【高频】面试题大全(2022最新)

高速公路服务区智能一体机解决方案

VNC Viewer方式的远程连接树莓派

MongoDB入门实战教程:学习总结目录

国产芯片的赶超,让美国手机芯片龙头高通害怕了,出招应对竞争

Intelij 中的 Database Tools可以连接但是无法显示SCHEMA, TABLES
随机推荐
Phpcms upgrade editor method -- simple and effective
"Industry foresight" future development trend of intelligent security monitoring industry
Bitmap of redis data structure
He is also a junior test engineer. Why is his salary high? The interview must be brilliant at these points
Typescript raw data type
practice
Sequential representation and implementation of linear table (refer to YanWeiMin version)
设备通过国标GB28181接入EasyCVR平台,出现断流情况该如何解决?
10 hands-free idea plug-ins. These codes do not need to be written (the second bullet)
Step by step introduction to sqlsugar based development framework (9) -- Realizing field permission control with WinForm control
Is financial management of securities account safe??
PHP export data as excel table
[log service CLS] a taste of Tencent cloud log service CLS
Domestic payment system and payment background construction
How do individuals open accounts for stock speculation? Is it safe to open accounts for stock speculation
Junit5中的参数化测试(Parameterized Tests)指南
The industrial control security of roaming the Intranet
The first in China! Tencent cloud key management system passes password application verification test
国产芯片的赶超,让美国手机芯片龙头高通害怕了,出招应对竞争
Use list