-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysql.ps1
70 lines (65 loc) · 2.08 KB
/
mysql.ps1
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
# sudo pwsh -Command "iex '& ([scriptblock]::Create((iwr https://raw.githubusercontent.com/abdullahfarook/kube/main/mysql.ps1))) -mysql_root_password <password> '"
Param(
[Parameter(Mandatory)][string]$mysql_root_password,
[string]$mysql_path = "/shared/mysql",
[string]$mysql_version = "latest",
[string]$join_network,
[bool]$existing = $true,
[string]$new_user,
[string]$new_password
)
Write-Host "Starting MySQL setup script..."
$compose = @"
version: '3'
services:
mysql:
image: mysql/mysql-server:$mysql_version
container_name: mysql
command: --default-authentication-plugin=mysql_native_password
volumes:
- $mysql_path/data:/var/lib/mysql
- $mysql_path/conf.d:/etc/mysql/conf.d
restart: always
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: '$mysql_root_password'
"@
$file = "mysql-compose.yml"
# remove existing file
if (Test-Path $file) {
Remove-Item $file
}
$compose | Out-File -FilePath $file -Force
Write-Host "Docker Compose file created at $file"
try {
Write-Host "Starting MySQL container..."
nerdctl compose -f $file up -d
Write-Host "MySQL container started successfully."
}
catch {
Write-Error "Failed to start MySQL container:`n$_"
exit 1
}
rm $file
# join network
if ($null -ne $joinNetwork) {
Write-Host "Creating and connecting to network $joinNetwork..."
nerdctl network create $joinNetwork
nerdctl network connect $joinNetwork mysql
Write-Host "Network $joinNetwork created and connected successfully."
}
# create user and database
if ($existing -eq $false) {
# check for arguments new_user and new_password
if ($null -eq $new_user -or $null -eq $new_password) {
Write-Error "new_user and new_password must be provided if provided argument 'existing' is false"
exit 1
}
Write-Host "Creating new MySQL user $new_user..."
nerdctl exec -i mysql bash -c @"
mysql -h localhost -u root -p$mysql_root_password <<< "show databases;"
"@
Write-Host "New MySQL user $new_user created successfully."
}
Write-Host "MySQL setup script completed."