Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. #!/bin/bash -e
  2.  
  3. # Use the following variables to control your install:
  4.  
  5. # Password for the SA user (required)
  6. MSSQL_SA_PASSWORD='SmartRep@2020'
  7.  
  8. # Product ID of the version of SQL server you're installing
  9. # Must be evaluation, developer, express, web, standard, enterprise, or your 25 digit product key
  10. # Defaults to developer
  11. MSSQL_PID='express'
  12.  
  13. # Install SQL Server Agent (recommended)
  14. SQL_INSTALL_AGENT='y'
  15.  
  16. # Install SQL Server Full Text Search (optional)
  17. # SQL_INSTALL_FULLTEXT='y'
  18.  
  19. # Create an additional user with sysadmin privileges (optional)
  20. SQL_INSTALL_USER='smartrep'
  21. SQL_INSTALL_USER_PASSWORD='smartrep@2020'
  22.  
  23. if [ -z $MSSQL_SA_PASSWORD ]
  24. then
  25. echo Environment variable MSSQL_SA_PASSWORD must be set for unattended install
  26. exit 1
  27. fi
  28.  
  29. echo Adding Microsoft repositories...
  30. sudo curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
  31. repoargs="$(curl https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2017.list)"
  32. sudo add-apt-repository "${repoargs}"
  33. repoargs="$(curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list)"
  34. sudo add-apt-repository "${repoargs}"
  35.  
  36. echo Running apt-get update -y...
  37. sudo apt-get update -y
  38.  
  39. echo Installing SQL Server...
  40. sudo apt-get install -y mssql-server
  41.  
  42. echo Running mssql-conf setup...
  43. sudo MSSQL_SA_PASSWORD=$MSSQL_SA_PASSWORD \
  44. MSSQL_PID=$MSSQL_PID \
  45. /opt/mssql/bin/mssql-conf -n setup accept-eula
  46.  
  47. echo Installing mssql-tools and unixODBC developer...
  48. sudo ACCEPT_EULA=Y apt-get install -y mssql-tools unixodbc-dev
  49.  
  50. # Add SQL Server tools to the path by default:
  51. echo Adding SQL Server tools to your path...
  52. echo PATH="$PATH:/opt/mssql-tools/bin" >> ~/.bash_profile
  53. echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
  54. source ~/.bashrc
  55.  
  56. # Optional SQL Server Agent installation:
  57. if [ ! -z $SQL_INSTALL_AGENT ]
  58. then
  59. echo Installing SQL Server Agent...
  60. sudo apt-get install -y mssql-server-agent
  61. fi
  62.  
  63. # Optional SQL Server Full Text Search installation:
  64. if [ ! -z $SQL_INSTALL_FULLTEXT ]
  65. then
  66. echo Installing SQL Server Full-Text Search...
  67. sudo apt-get install -y mssql-server-fts
  68. fi
  69.  
  70. # Configure firewall to allow TCP port 1433:
  71. echo Configuring UFW to allow traffic on port 1433...
  72. sudo ufw allow 1433/tcp
  73. sudo ufw reload
  74.  
  75. # Optional example of post-installation configuration.
  76. # Trace flags 1204 and 1222 are for deadlock tracing.
  77. # echo Setting trace flags...
  78. # sudo /opt/mssql/bin/mssql-conf traceflag 1204 1222 on
  79.  
  80. # Restart SQL Server after installing:
  81. echo Restarting SQL Server...
  82. sudo systemctl restart mssql-server
  83.  
  84. # Connect to server and get the version:
  85. counter=1
  86. errstatus=1
  87. while [ $counter -le 5 ] && [ $errstatus = 1 ]
  88. do
  89. echo Waiting for SQL Server to start...
  90. sleep 3s
  91. /opt/mssql-tools/bin/sqlcmd \
  92. -S localhost \
  93. -U SA \
  94. -P $MSSQL_SA_PASSWORD \
  95. -Q "SELECT @@VERSION" 2>/dev/null
  96. errstatus=$?
  97. ((counter++))
  98. done
  99.  
  100. # Display error if connection failed:
  101. if [ $errstatus = 1 ]
  102. then
  103. echo Cannot connect to SQL Server, installation aborted
  104. exit $errstatus
  105. fi
  106.  
  107. # Optional new user creation:
  108. if [ ! -z $SQL_INSTALL_USER ] && [ ! -z $SQL_INSTALL_USER_PASSWORD ]
  109. then
  110. echo Creating user $SQL_INSTALL_USER
  111. /opt/mssql-tools/bin/sqlcmd \
  112. -S localhost \
  113. -U SA \
  114. -P $MSSQL_SA_PASSWORD \
  115. -Q "CREATE LOGIN [$SQL_INSTALL_USER] WITH PASSWORD=N'$SQL_INSTALL_USER_PASSWORD', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=ON, CHECK_POLICY=ON; ALTER SERVER ROLE [sysadmin] ADD MEMBER [$SQL_INSTALL_USER]"
  116. fi
  117.  
  118. echo Done!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement