当前位置:网站首页>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
边栏推荐
- New de debugging
- Differential privacy
- Design of vga/lcd display controller system based on FPGA (Part 2)
- Poor remote code execution in Alien Swarm
- How to build a high-performance go cache Library
- 高速公路服务区智能一体机解决方案
- Bitmap of redis data structure
- Do you really know the difference between H5 and applet?
- Is it safe for futures companies to open accounts
- Very exciting! 12000 words summarized the theory of network technology, reviewing the old and learning the new
猜你喜欢
Step by step introduction to sqlsugar based development framework (9) -- Realizing field permission control with WinForm control

Left hand code, right hand open source, part of the open source road

Two way combination of business and technology to build a bank data security management system
![clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]](/img/f0/42f394dbc989d381387c7b953d2a39.jpg)
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]

推荐几款超级实用的数据分析利器

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

【我的OpenGL学习进阶之旅】OpenGL的坐标系的学习笔记

为什么企业实施WMS仓储管理系统很容易失败
Record the range of data that MySQL update will lock

Intelij 中的 Database Tools可以连接但是无法显示SCHEMA, TABLES
随机推荐
MongoDB入门实战教程:学习总结目录
MySQL toolset: the official performance testing tool mysqlslap
MySQL replication series 6- tables related to replication information
Teach you how to view version information with mongodb
Sequential representation and implementation of linear table (refer to YanWeiMin version)
如何在Thymeleaf3标签中使用嵌套标签
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]
HMM to CRF understanding and learning notes
刚刚阿里面软件测试回来,3+1面任职阿里P7,年薪28*15薪
Which securities company is better and safer for great wisdom to choose when opening an account
Remember: never use UTF-8 in MySQL
Easynvr has been connected to the third-party supervision platform. How to achieve local Internet access
Istio practical skill: hide the automatically added server header
Cloud + community [play with Tencent cloud] essay solicitation activity winners announced
An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!
How about stock online account opening and account opening process? Is it safe to open an account online?
PHP export data as excel table
SF express: please sign for MySQL soul ten
VNC Viewer方式的远程连接树莓派
Huangchuping presided over the video conference on fixed-point contact with Zhuhai, resolutely implemented the deployment requirements of the provincial Party committee, and ensured positive results i