-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_databases.sh
303 lines (260 loc) · 6.87 KB
/
setup_databases.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/bin/bash
# Exit immediately if a command exits with a non-zero status
set -e
# Function to check if a command exists
command_exists () {
command -v "$1" >/dev/null 2>&1
}
# Check for Docker
if ! command_exists docker ; then
echo "Docker is not installed. Please install Docker and try again."
exit 1
fi
# Check for Docker Compose
if ! command_exists docker-compose ; then
echo "Docker Compose is not installed. Please install Docker Compose and try again."
exit 1
fi
# Define project directory
PROJECT_DIR=$(pwd)
# Create necessary directories
echo "Creating directory structure..."
mkdir -p mssql/init
mkdir -p mysql/init
mkdir -p postgres/init
mkdir -p mongodb/init
mkdir -p redis/init
# Set fixed passwords
echo "Setting fixed passwords..."
FIXED_PASSWORD="YourStrong!Password"
MSSQL_SA_PASSWORD="${FIXED_PASSWORD}"
MYSQL_ROOT_PASSWORD="${FIXED_PASSWORD}"
MYSQL_USER_PASSWORD="${FIXED_PASSWORD}"
POSTGRES_PASSWORD="${FIXED_PASSWORD}"
MONGODB_ROOT_PASSWORD="${FIXED_PASSWORD}"
REDIS_PASSWORD="${FIXED_PASSWORD}"
# Create .env file
echo "Creating .env file..."
cat > .env <<EOL
MSSQL_SA_PASSWORD=${MSSQL_SA_PASSWORD}
MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
MYSQL_USER_PASSWORD=${MYSQL_USER_PASSWORD}
POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
MONGODB_ROOT_PASSWORD=${MONGODB_ROOT_PASSWORD}
REDIS_PASSWORD=${REDIS_PASSWORD}
EOL
# Create docker-compose.yml
echo "Creating docker-compose.yml..."
cat > docker-compose.yml <<'EOF'
version: '3.8'
services:
mssql:
image: mcr.microsoft.com/mssql/server:2019-latest
environment:
SA_PASSWORD: "${MSSQL_SA_PASSWORD}"
ACCEPT_EULA: "Y"
ports:
- "1433:1433"
volumes:
- mssql-data:/var/opt/mssql
- ./mssql/init:/init
networks:
- db-network
restart: unless-stopped
mysql:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
MYSQL_DATABASE: todo_db
MYSQL_USER: admin
MYSQL_PASSWORD: "${MYSQL_USER_PASSWORD}"
ports:
- "3306:3306"
volumes:
- mysql-data:/var/lib/mysql
- ./mysql/init:/docker-entrypoint-initdb.d
networks:
- db-network
restart: unless-stopped
postgres:
image: postgres:16
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
POSTGRES_DB: todo_db
ports:
- "5432:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
- ./postgres/init:/docker-entrypoint-initdb.d
networks:
- db-network
restart: unless-stopped
mongodb:
image: mongo:6.0
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: "${MONGODB_ROOT_PASSWORD}"
MONGO_INITDB_DATABASE: todo_db
ports:
- "27017:27017"
volumes:
- mongodb-data:/data/db
- ./mongodb/init:/docker-entrypoint-initdb.d
networks:
- db-network
restart: unless-stopped
redis:
image: redis:7.0
command: ["redis-server", "--requirepass", "${REDIS_PASSWORD}"]
ports:
- "6379:6379"
volumes:
- redis-data:/data
- ./redis/init:/docker-entrypoint-initdb.d
networks:
- db-network
restart: unless-stopped
networks:
db-network:
driver: bridge
volumes:
mssql-data:
mysql-data:
postgres-data:
mongodb-data:
redis-data:
EOF
# Create MSSQL initialization script
echo "Creating MSSQL initialization script..."
cat > mssql/init/init.sql <<EOL
-- init.sql for MSSQL
-- Wait for the SQL Server to be ready
WAITFOR DELAY '00:00:10';
-- Create Database
IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = N'todo_db')
BEGIN
CREATE DATABASE todo_db;
END
GO
USE todo_db;
GO
-- Create Table
IF OBJECT_ID('dbo.todo_table', 'U') IS NULL
BEGIN
CREATE TABLE dbo.todo_table (
id INT IDENTITY(1,1) PRIMARY KEY,
task NVARCHAR(255) NOT NULL,
completed BIT DEFAULT 0,
created_at DATETIME DEFAULT GETDATE()
);
END
GO
-- Insert Dummy Data
INSERT INTO dbo.todo_table (task, completed)
VALUES
('Buy groceries', 0),
('Complete project report', 1),
('Call the bank', 0),
('Schedule dentist appointment', 0);
GO
EOL
# Create MySQL initialization script
echo "Creating MySQL initialization script..."
cat > mysql/init/init.sql <<EOL
-- init.sql for MySQL
-- Create Database
CREATE DATABASE IF NOT EXISTS todo_db;
USE todo_db;
-- Create Table
CREATE TABLE IF NOT EXISTS todo_table (
id INT AUTO_INCREMENT PRIMARY KEY,
task VARCHAR(255) NOT NULL,
completed BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Insert Dummy Data
INSERT INTO todo_table (task, completed) VALUES
('Buy groceries', FALSE),
('Complete project report', TRUE),
('Call the bank', FALSE),
('Schedule dentist appointment', FALSE);
EOL
# Create PostgreSQL initialization script
echo "Creating PostgreSQL initialization script..."
cat > postgres/init/init.sql <<'EOL'
-- init.sql for PostgreSQL
-- Create Table
CREATE TABLE IF NOT EXISTS todo_table (
id SERIAL PRIMARY KEY,
task VARCHAR(255) NOT NULL,
completed BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Insert Dummy Data
INSERT INTO todo_table (task, completed) VALUES
('Buy groceries', FALSE),
('Complete project report', TRUE),
('Call the bank', FALSE),
('Schedule dentist appointment', FALSE);
EOL
# Create MongoDB initialization script
echo "Creating MongoDB initialization script..."
cat > mongodb/init/init.js <<EOL
// init.js for MongoDB
db = db.getSiblingDB('todo_db');
// Create Collection
db.createCollection('todo_table');
// Insert Dummy Data
db.todo_table.insertMany([
{
task: 'Buy groceries',
completed: false,
created_at: new Date()
},
{
task: 'Complete project report',
completed: true,
created_at: new Date()
},
{
task: 'Call the bank',
completed: false,
created_at: new Date()
},
{
task: 'Schedule dentist appointment',
completed: false,
created_at: new Date()
}
]);
EOL
# Create Redis initialization script (optional)
echo "Creating Redis initialization script..."
cat > redis/init/init.sh <<EOL
#!/bin/bash
# init.sh for Redis
# Wait for Redis to be ready
sleep 10
# Set initial keys (optional)
redis-cli -a "${REDIS_PASSWORD}" set welcome "Welcome to Redis!"
redis-cli -a "${REDIS_PASSWORD}" set task1 "Buy groceries"
redis-cli -a "${REDIS_PASSWORD}" set task2 "Complete project report"
redis-cli -a "${REDIS_PASSWORD}" set task3 "Call the bank"
redis-cli -a "${REDIS_PASSWORD}" set task4 "Schedule dentist appointment"
EOL
# Make the Redis initialization script executable
chmod +x redis/init/init.sh
# Start Docker Compose
echo "Starting Docker Compose services..."
docker-compose up -d
echo "All services are up and running!"
# Display .env file content (optional)
echo
echo "=== .env File ==="
cat .env
echo "=================="
echo
# Display container statuses
docker-compose ps
echo "Setup complete!"