Skip to content

Commit fba6570

Browse files
committed
Merge branch 'master' into wwi-app
2 parents f665c5b + 810e7c3 commit fba6570

620 files changed

Lines changed: 89851 additions & 309 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

media/auto-tuning.png

17 KB
Loading

samples/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ Samples that help with the management of SQL Server and Azure SQL Database.
2323
__[tutorials](tutorials/)__
2424

2525
Samples showing how to connect to SQL databases using various programming languages, including Python, C#, Java, Ruby, Node.js, and PHP.
26+
27+
__[containers](containers/)__
28+
29+
Samples showing various SQL Server in container scenarios.

samples/containers/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__[replication](replication/)__
2+
3+
Example of SQL Server Replication in containers. This demo uses docker-compose to start two SQL Server containers; one that acts as the publisher and distributor, and the other as the subscriber in a push snapshot configuration.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## SQL Server Replication with Containers
2+
3+
This demo uses docker-compose to start two SQL Server containers; one that acts as the publisher and distributor, and the other as the subscriber in a push snapshot configuration.
4+
5+
6+
### How to Use
7+
8+
1. Run the following command in this directory:
9+
10+
```
11+
docker-compose up
12+
```
13+
note: this will take approx. 2 min.
14+
15+
In your terminal, you should see something like this
16+
```
17+
db1 | Job 'DB1-Sales-SnapshotRepl-1' started successfully.
18+
db1 | Creating Snapshot...
19+
db1 | Job 'db1-Sales-SnapshotRepl-DB2-1' started successfully.
20+
```
21+
22+
2. Connect to the subscriber listening on localhost,2600 and see that the Sales Database has a Customer table with data in it.
23+
note: credentials are listed in the **docker-compose.yml**
24+
25+
3. when you are done, clean up by running the following command
26+
```
27+
docker-compose down
28+
```
29+
30+
31+
32+
### How it Works
33+
34+
1. Both SQL Server containers start with the environment variables specified in the docker-compose file. In this example, **db1** is the publisher/distributor and **db2** is the subscriber.
35+
2. *db1/db-init.sh* and *db2/db-init.sh* waits for SQL Server to start up and run the *db-init.sql* scripts
36+
3. *db1/db-init.sql* creates a *Sales* Database with *Customer* table and sample data, and proceeds by setting up snapshot replication.
37+
4. *db2/db-init.sql* creates a *Sales* Database.
38+
5. db1 starts replication jobs to push the snapshot to db2
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu
2+
3+
COPY . /
4+
5+
RUN chmod +x /db-init.sh
6+
CMD /bin/bash ./entrypoint.sh
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#wait for the SQL Server to come up
2+
sleep 25s
3+
4+
mkdir /var/opt/mssql/ReplData/
5+
chown mssql /var/opt/mssql/ReplData/
6+
chgrp mssql /var/opt/mssql/ReplData/
7+
8+
9+
echo "running set up script"
10+
#run the setup script to create the DB and the schema in the DB
11+
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P MssqlPass123 -d master -i db-init.sql
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
CREATE DATABASE Sales
2+
GO
3+
USE [SALES]
4+
GO
5+
CREATE TABLE CUSTOMER([CustomerID] [int] NOT NULL, [SalesAmount] [decimal] NOT NULL)
6+
GO
7+
INSERT INTO CUSTOMER (CustomerID, SalesAmount) VALUES (1,100),(2,200),(3,300)
8+
9+
10+
11+
DECLARE @distributor AS sysname
12+
DECLARE @distributorlogin AS sysname
13+
DECLARE @distributorpassword AS sysname
14+
-- Specify the Distributor name. Use 'hostname' command on in terminal to find the hostname
15+
SET @distributor = @@SERVERNAME--in this example, it will be the name of the publisher
16+
SET @distributorlogin = N'sa'
17+
SET @distributorpassword = N'MssqlPass123'
18+
-- Specify the distribution database.
19+
20+
use master
21+
exec sp_adddistributor @distributor = @distributor -- this should be the hostname
22+
23+
-- Log into Distributor and create Distribution Database. In this example, our Publisher and Distributor is on the same host
24+
exec sp_adddistributiondb @database = N'distribution', @log_file_size = 2, @deletebatchsize_xact = 5000, @deletebatchsize_cmd = 2000, @security_mode = 0, @login = @distributorlogin, @password = @distributorpassword
25+
GO
26+
27+
DECLARE @snapshotdirectory AS nvarchar(500)
28+
SET @snapshotdirectory = N'/var/opt/mssql/ReplData/'
29+
30+
-- Log into Distributor and create Distribution Database. In this example, our Publisher and Distributor is on the same host
31+
use [distribution]
32+
if (not exists (select * from sysobjects where name = 'UIProperties' and type = 'U '))
33+
create table UIProperties(id int)
34+
if (exists (select * from ::fn_listextendedproperty('SnapshotFolder', 'user', 'dbo', 'table', 'UIProperties', null, null)))
35+
EXEC sp_updateextendedproperty N'SnapshotFolder', @snapshotdirectory, 'user', dbo, 'table', 'UIProperties'
36+
else
37+
EXEC sp_addextendedproperty N'SnapshotFolder', @snapshotdirectory, 'user', dbo, 'table', 'UIProperties'
38+
GO
39+
40+
41+
DECLARE @publisher AS sysname
42+
DECLARE @distributorlogin AS sysname
43+
DECLARE @distributorpassword AS sysname
44+
-- Specify the Distributor name. Use 'hostname' command on in terminal to find the hostname
45+
SET @publisher = @@SERVERNAME
46+
SET @distributorlogin = N'sa'
47+
SET @distributorpassword = N'MssqlPass123'
48+
-- Specify the distribution database.
49+
50+
-- Adding the distribution publishers
51+
exec sp_adddistpublisher @publisher = @publisher, @distribution_db = N'distribution', @security_mode = 0, @login = @distributorlogin, @password = @distributorpassword, @working_directory = N'/var/opt/mssql/ReplData', @trusted = N'false', @thirdparty_flag = 0, @publisher_type = N'MSSQLSERVER'
52+
GO
53+
54+
55+
DECLARE @replicationdb AS sysname
56+
DECLARE @publisherlogin AS sysname
57+
DECLARE @publisherpassword AS sysname
58+
SET @replicationdb = N'Sales'
59+
SET @publisherlogin = N'sa'
60+
SET @publisherpassword = N'MssqlPass123'
61+
62+
use [Sales]
63+
exec sp_replicationdboption @dbname = N'Sales', @optname = N'publish', @value = N'true'
64+
65+
-- Addi the snapshot publication
66+
exec sp_addpublication
67+
@publication = N'SnapshotRepl',
68+
@description = N'Snapshot publication of database ''Sales'' from Publisher ''<PUBLISHER HOSTNAME>''.',
69+
@retention = 0,
70+
@allow_push = N'true',
71+
@repl_freq = N'snapshot',
72+
@status = N'active',
73+
@independent_agent = N'true'
74+
75+
exec sp_addpublication_snapshot @publication = N'SnapshotRepl',
76+
@frequency_type = 128,
77+
@frequency_interval = 8,
78+
@frequency_relative_interval = 1,
79+
@frequency_recurrence_factor = 0,
80+
@frequency_subday = 4,
81+
@frequency_subday_interval = 2,
82+
@active_start_time_of_day = 0,
83+
@active_end_time_of_day = 235959,
84+
@active_start_date = 0,
85+
@active_end_date = 0,
86+
@publisher_security_mode = 0,
87+
@publisher_login = @publisherlogin,
88+
@publisher_password = @publisherpassword
89+
90+
91+
use [Sales]
92+
exec sp_addarticle
93+
@publication = N'SnapshotRepl',
94+
@article = N'customer',
95+
@source_owner = N'dbo',
96+
@source_object = N'customer',
97+
@type = N'logbased',
98+
@description = null,
99+
@creation_script = null,
100+
@pre_creation_cmd = N'drop',
101+
@schema_option = 0x000000000803509D,
102+
@identityrangemanagementoption = N'manual',
103+
@destination_table = N'customer',
104+
@destination_owner = N'dbo',
105+
@vertical_partition = N'false'
106+
107+
108+
DECLARE @subscriber AS sysname
109+
DECLARE @subscriber_db AS sysname
110+
DECLARE @subscriberLogin AS sysname
111+
DECLARE @subscriberPassword AS sysname
112+
SET @subscriber = N'db2' -- for example, MSSQLSERVER
113+
SET @subscriber_db = N'Sales'
114+
SET @subscriberLogin = N'sa'
115+
SET @subscriberPassword = N'MssqlPass123'
116+
117+
use [Sales]
118+
exec sp_addsubscription
119+
@publication = N'SnapshotRepl',
120+
@subscriber = @subscriber,
121+
@destination_db = @subscriber_db,
122+
@subscription_type = N'Push',
123+
@sync_type = N'automatic',
124+
@article = N'all',
125+
@update_mode = N'read only',
126+
@subscriber_type = 0
127+
128+
129+
exec sp_addpushsubscription_agent
130+
@publication = N'SnapshotRepl',
131+
@subscriber = @subscriber,
132+
@subscriber_db = @subscriber_db,
133+
@subscriber_security_mode = 0,
134+
@subscriber_login = @subscriberLogin,
135+
@subscriber_password = @subscriberPassword,
136+
@frequency_type = 128,
137+
@frequency_interval = 8,
138+
@frequency_relative_interval = 0,
139+
@frequency_recurrence_factor = 0,
140+
@frequency_subday = 4,
141+
@frequency_subday_interval = 2,
142+
@active_start_time_of_day = 0,
143+
@active_end_time_of_day = 0,
144+
@active_start_date = 0,
145+
@active_end_date = 19950101
146+
GO
147+
148+
exec sp_startpublication_snapshot
149+
@publication = N'SnapshotRepl',
150+
@publisher = NULL
151+
GO
152+
PRINT 'Creating Snapshot...'
153+
154+
WAITFOR DELAY '00:00:17'
155+
156+
DECLARE @jobname NVARCHAR(max)
157+
158+
--use the following query to query for the jobname of replication job
159+
select @jobname=s.name
160+
from msdb.dbo.sysjobs s inner join msdb.dbo.syscategories c on s.category_id = c.category_id
161+
where c.name in ('REPL-Distribution')
162+
163+
--select @jobname
164+
165+
exec msdb.dbo.sp_start_job @jobname
166+
167+
-- SELECT name, date_modified FROM msdb.dbo.sysjobs order by date_modified desc
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#start SQL Server, start the script to create/setup the DB
2+
#You need a non-terminating process to keep the container alive.
3+
#In a series of commands separated by single ampersands the commands to the left of the right-most ampersand are run in the background.
4+
#So - if you are executing a series of commands simultaneously using single ampersands, the command at the right-most position needs to be non-terminating
5+
/db-init.sh & /opt/mssql/bin/sqlservr
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu
2+
3+
COPY . /
4+
5+
RUN chmod +x /db-init.sh
6+
CMD /bin/bash ./entrypoint.sh
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#wait for the SQL Server to come up
2+
sleep 20s
3+
4+
echo "running set up script"
5+
#run the setup script to create the DB and the schema in the DB
6+
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P MssqlPass123 -d master -i db-init.sql

0 commit comments

Comments
 (0)