From 58a1166b1cd7abc03357d69ed72c1fc86d36ea36 Mon Sep 17 00:00:00 2001 From: vin yu Date: Tue, 2 Oct 2018 15:44:25 -0700 Subject: [PATCH] Adding container section to samples and replication --- samples/containers/README.md | 3 + samples/containers/replication/README.md | 38 ++++ samples/containers/replication/db1/Dockerfile | 6 + samples/containers/replication/db1/db-init.sh | 11 ++ .../containers/replication/db1/db-init.sql | 167 ++++++++++++++++++ .../containers/replication/db1/entrypoint.sh | 5 + samples/containers/replication/db2/Dockerfile | 6 + samples/containers/replication/db2/db-init.sh | 6 + .../containers/replication/db2/db-init.sql | 2 + .../containers/replication/db2/entrypoint.sh | 5 + .../containers/replication/docker-compose.yml | 23 +++ 11 files changed, 272 insertions(+) create mode 100644 samples/containers/README.md create mode 100644 samples/containers/replication/README.md create mode 100644 samples/containers/replication/db1/Dockerfile create mode 100644 samples/containers/replication/db1/db-init.sh create mode 100644 samples/containers/replication/db1/db-init.sql create mode 100644 samples/containers/replication/db1/entrypoint.sh create mode 100644 samples/containers/replication/db2/Dockerfile create mode 100644 samples/containers/replication/db2/db-init.sh create mode 100644 samples/containers/replication/db2/db-init.sql create mode 100644 samples/containers/replication/db2/entrypoint.sh create mode 100644 samples/containers/replication/docker-compose.yml diff --git a/samples/containers/README.md b/samples/containers/README.md new file mode 100644 index 00000000..aa37b09f --- /dev/null +++ b/samples/containers/README.md @@ -0,0 +1,3 @@ +__[replication](replication/)__ + +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. \ No newline at end of file diff --git a/samples/containers/replication/README.md b/samples/containers/replication/README.md new file mode 100644 index 00000000..2364b81d --- /dev/null +++ b/samples/containers/replication/README.md @@ -0,0 +1,38 @@ +## SQL Server Replication with 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. + + +### How to Use + +1. Run the following command in this directory: + +``` +docker-compose up +``` +note: this will take approx. 2 min. + +In your terminal, you should see something like this +``` +db1 | Job 'DB1-Sales-SnapshotRepl-1' started successfully. +db1 | Creating Snapshot... +db1 | Job 'db1-Sales-SnapshotRepl-DB2-1' started successfully. +``` + +2. Connect to the subscriber listening on localhost,2600 and see that the Sales Database has a Customer table with data in it. +note: credentials are listed in the **docker-compose.yml** + +3. when you are done, clean up by running the following command +``` +docker-compose down +``` + + + +### How it Works + +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. +2. *db1/db-init.sh* and *db2/db-init.sh* waits for SQL Server to start up and run the *db-init.sql* scripts +3. *db1/db-init.sql* creates a *Sales* Database with *Customer* table and sample data, and proceeds by setting up snapshot replication. +4. *db2/db-init.sql* creates a *Sales* Database. +5. db1 starts replication jobs to push the snapshot to db2 \ No newline at end of file diff --git a/samples/containers/replication/db1/Dockerfile b/samples/containers/replication/db1/Dockerfile new file mode 100644 index 00000000..60965d2b --- /dev/null +++ b/samples/containers/replication/db1/Dockerfile @@ -0,0 +1,6 @@ +FROM mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu + +COPY . / + +RUN chmod +x /db-init.sh +CMD /bin/bash ./entrypoint.sh \ No newline at end of file diff --git a/samples/containers/replication/db1/db-init.sh b/samples/containers/replication/db1/db-init.sh new file mode 100644 index 00000000..aa83da97 --- /dev/null +++ b/samples/containers/replication/db1/db-init.sh @@ -0,0 +1,11 @@ +#wait for the SQL Server to come up +sleep 25s + +mkdir /var/opt/mssql/ReplData/ +chown mssql /var/opt/mssql/ReplData/ +chgrp mssql /var/opt/mssql/ReplData/ + + +echo "running set up script" +#run the setup script to create the DB and the schema in the DB +/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P MssqlPass123 -d master -i db-init.sql \ No newline at end of file diff --git a/samples/containers/replication/db1/db-init.sql b/samples/containers/replication/db1/db-init.sql new file mode 100644 index 00000000..d1d92143 --- /dev/null +++ b/samples/containers/replication/db1/db-init.sql @@ -0,0 +1,167 @@ +CREATE DATABASE Sales +GO +USE [SALES] +GO +CREATE TABLE CUSTOMER([CustomerID] [int] NOT NULL, [SalesAmount] [decimal] NOT NULL) +GO +INSERT INTO CUSTOMER (CustomerID, SalesAmount) VALUES (1,100),(2,200),(3,300) + + + +DECLARE @distributor AS sysname +DECLARE @distributorlogin AS sysname +DECLARE @distributorpassword AS sysname +-- Specify the Distributor name. Use 'hostname' command on in terminal to find the hostname +SET @distributor = @@SERVERNAME--in this example, it will be the name of the publisher +SET @distributorlogin = N'sa' +SET @distributorpassword = N'MssqlPass123' +-- Specify the distribution database. + +use master +exec sp_adddistributor @distributor = @distributor -- this should be the hostname + +-- Log into Distributor and create Distribution Database. In this example, our Publisher and Distributor is on the same host +exec sp_adddistributiondb @database = N'distribution', @log_file_size = 2, @deletebatchsize_xact = 5000, @deletebatchsize_cmd = 2000, @security_mode = 0, @login = @distributorlogin, @password = @distributorpassword +GO + +DECLARE @snapshotdirectory AS nvarchar(500) +SET @snapshotdirectory = N'/var/opt/mssql/ReplData/' + +-- Log into Distributor and create Distribution Database. In this example, our Publisher and Distributor is on the same host +use [distribution] +if (not exists (select * from sysobjects where name = 'UIProperties' and type = 'U ')) + create table UIProperties(id int) +if (exists (select * from ::fn_listextendedproperty('SnapshotFolder', 'user', 'dbo', 'table', 'UIProperties', null, null))) + EXEC sp_updateextendedproperty N'SnapshotFolder', @snapshotdirectory, 'user', dbo, 'table', 'UIProperties' +else + EXEC sp_addextendedproperty N'SnapshotFolder', @snapshotdirectory, 'user', dbo, 'table', 'UIProperties' +GO + + +DECLARE @publisher AS sysname +DECLARE @distributorlogin AS sysname +DECLARE @distributorpassword AS sysname +-- Specify the Distributor name. Use 'hostname' command on in terminal to find the hostname +SET @publisher = @@SERVERNAME +SET @distributorlogin = N'sa' +SET @distributorpassword = N'MssqlPass123' +-- Specify the distribution database. + +-- Adding the distribution publishers +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' +GO + + +DECLARE @replicationdb AS sysname +DECLARE @publisherlogin AS sysname +DECLARE @publisherpassword AS sysname +SET @replicationdb = N'Sales' +SET @publisherlogin = N'sa' +SET @publisherpassword = N'MssqlPass123' + +use [Sales] +exec sp_replicationdboption @dbname = N'Sales', @optname = N'publish', @value = N'true' + +-- Addi the snapshot publication +exec sp_addpublication +@publication = N'SnapshotRepl', +@description = N'Snapshot publication of database ''Sales'' from Publisher ''''.', +@retention = 0, +@allow_push = N'true', +@repl_freq = N'snapshot', +@status = N'active', +@independent_agent = N'true' + +exec sp_addpublication_snapshot @publication = N'SnapshotRepl', +@frequency_type = 128, +@frequency_interval = 8, +@frequency_relative_interval = 1, +@frequency_recurrence_factor = 0, +@frequency_subday = 4, +@frequency_subday_interval = 2, +@active_start_time_of_day = 0, +@active_end_time_of_day = 235959, +@active_start_date = 0, +@active_end_date = 0, +@publisher_security_mode = 0, +@publisher_login = @publisherlogin, +@publisher_password = @publisherpassword + + +use [Sales] +exec sp_addarticle +@publication = N'SnapshotRepl', +@article = N'customer', +@source_owner = N'dbo', +@source_object = N'customer', +@type = N'logbased', +@description = null, +@creation_script = null, +@pre_creation_cmd = N'drop', +@schema_option = 0x000000000803509D, +@identityrangemanagementoption = N'manual', +@destination_table = N'customer', +@destination_owner = N'dbo', +@vertical_partition = N'false' + + +DECLARE @subscriber AS sysname +DECLARE @subscriber_db AS sysname +DECLARE @subscriberLogin AS sysname +DECLARE @subscriberPassword AS sysname +SET @subscriber = N'db2' -- for example, MSSQLSERVER +SET @subscriber_db = N'Sales' +SET @subscriberLogin = N'sa' +SET @subscriberPassword = N'MssqlPass123' + +use [Sales] +exec sp_addsubscription +@publication = N'SnapshotRepl', +@subscriber = @subscriber, +@destination_db = @subscriber_db, +@subscription_type = N'Push', +@sync_type = N'automatic', +@article = N'all', +@update_mode = N'read only', +@subscriber_type = 0 + + +exec sp_addpushsubscription_agent +@publication = N'SnapshotRepl', +@subscriber = @subscriber, +@subscriber_db = @subscriber_db, +@subscriber_security_mode = 0, +@subscriber_login = @subscriberLogin, +@subscriber_password = @subscriberPassword, +@frequency_type = 128, +@frequency_interval = 8, +@frequency_relative_interval = 0, +@frequency_recurrence_factor = 0, +@frequency_subday = 4, +@frequency_subday_interval = 2, +@active_start_time_of_day = 0, +@active_end_time_of_day = 0, +@active_start_date = 0, +@active_end_date = 19950101 +GO + +exec sp_startpublication_snapshot +@publication = N'SnapshotRepl', +@publisher = NULL +GO +PRINT 'Creating Snapshot...' + +WAITFOR DELAY '00:00:17' + +DECLARE @jobname NVARCHAR(max) + +--use the following query to query for the jobname of replication job +select @jobname=s.name +from msdb.dbo.sysjobs s inner join msdb.dbo.syscategories c on s.category_id = c.category_id +where c.name in ('REPL-Distribution') + +--select @jobname + +exec msdb.dbo.sp_start_job @jobname + +-- SELECT name, date_modified FROM msdb.dbo.sysjobs order by date_modified desc \ No newline at end of file diff --git a/samples/containers/replication/db1/entrypoint.sh b/samples/containers/replication/db1/entrypoint.sh new file mode 100644 index 00000000..4a6e4fe6 --- /dev/null +++ b/samples/containers/replication/db1/entrypoint.sh @@ -0,0 +1,5 @@ +#start SQL Server, start the script to create/setup the DB +#You need a non-terminating process to keep the container alive. +#In a series of commands separated by single ampersands the commands to the left of the right-most ampersand are run in the background. +#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 + /db-init.sh & /opt/mssql/bin/sqlservr \ No newline at end of file diff --git a/samples/containers/replication/db2/Dockerfile b/samples/containers/replication/db2/Dockerfile new file mode 100644 index 00000000..60965d2b --- /dev/null +++ b/samples/containers/replication/db2/Dockerfile @@ -0,0 +1,6 @@ +FROM mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu + +COPY . / + +RUN chmod +x /db-init.sh +CMD /bin/bash ./entrypoint.sh \ No newline at end of file diff --git a/samples/containers/replication/db2/db-init.sh b/samples/containers/replication/db2/db-init.sh new file mode 100644 index 00000000..eed9da31 --- /dev/null +++ b/samples/containers/replication/db2/db-init.sh @@ -0,0 +1,6 @@ +#wait for the SQL Server to come up +sleep 20s + +echo "running set up script" +#run the setup script to create the DB and the schema in the DB +/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P MssqlPass123 -d master -i db-init.sql \ No newline at end of file diff --git a/samples/containers/replication/db2/db-init.sql b/samples/containers/replication/db2/db-init.sql new file mode 100644 index 00000000..49c3beff --- /dev/null +++ b/samples/containers/replication/db2/db-init.sql @@ -0,0 +1,2 @@ +CREATE DATABASE Sales +GO diff --git a/samples/containers/replication/db2/entrypoint.sh b/samples/containers/replication/db2/entrypoint.sh new file mode 100644 index 00000000..4a6e4fe6 --- /dev/null +++ b/samples/containers/replication/db2/entrypoint.sh @@ -0,0 +1,5 @@ +#start SQL Server, start the script to create/setup the DB +#You need a non-terminating process to keep the container alive. +#In a series of commands separated by single ampersands the commands to the left of the right-most ampersand are run in the background. +#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 + /db-init.sh & /opt/mssql/bin/sqlservr \ No newline at end of file diff --git a/samples/containers/replication/docker-compose.yml b/samples/containers/replication/docker-compose.yml new file mode 100644 index 00000000..f932b02a --- /dev/null +++ b/samples/containers/replication/docker-compose.yml @@ -0,0 +1,23 @@ +version: "3" +services: + db1: + build: ./db1 + environment: + SA_PASSWORD: "MssqlPass123" + ACCEPT_EULA: "Y" + MSSQL_AGENT_ENABLED: "true" + ports: + - "2500:1433" + container_name: db1 + hostname: db1 + db2: + build: ./db2 + environment: + SA_PASSWORD: "MssqlPass123" + ACCEPT_EULA: "Y" + MSSQL_AGENT_ENABLED: "true" + ports: + - "2600:1433" + container_name: db2 + hostname: db2 +