Skip to content

Instantly share code, notes, and snippets.

@swagfin
Last active March 23, 2024 09:45
Show Gist options
  • Save swagfin/b6f636be390fdfa295d9aadc1d5cc76d to your computer and use it in GitHub Desktop.
Save swagfin/b6f636be390fdfa295d9aadc1d5cc76d to your computer and use it in GitHub Desktop.
Mssql-server for Linux (Unattended Install)

Install MSSQL Server 2019 On Linux (Ubuntu)

This script can be used to install Microsoft sql server 2019 on Ubuntu 20.04 versions

# Password for the SA user (required)
MSSQL_SA_PASSWORD='<ENTER SA PASSWORD HERE>'
# Product ID of the version of SQL server you're installing
# Must be evaluation, developer, express, web, standard, enterprise, or your 25 digit product key
# Defaults to developer
MSSQL_PID='developer'
# Enable SQL Server Agent (recommended)
SQL_ENABLE_AGENT='y'
# Create an additional user with sysadmin privileges (optional)
# SQL_INSTALL_USER='<Username>'
# SQL_INSTALL_USER_PASSWORD='<YourStrong!Passw0rd>'

if [ -z $MSSQL_SA_PASSWORD ]
then
  echo Environment variable MSSQL_SA_PASSWORD must be set for unattended install
  exit 1
fi

#Adding Microsoft repositories...
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
# Register the Microsoft SQL Server Ubuntu repository
sudo curl -o /etc/apt/sources.list.d/mssql-server.list https://packages.microsoft.com/config/ubuntu/20.04/mssql-server-2019.list

#update
sudo apt update -y
sudo apt install -y mssql-server

## THEN EXECUTE ##
#setup sqlserver
sudo MSSQL_SA_PASSWORD=$MSSQL_SA_PASSWORD \
     MSSQL_PID=$MSSQL_PID \
     /opt/mssql/bin/mssql-conf -n setup accept-eula

#Installing mssql-tools
sudo ACCEPT_EULA=Y apt install -y mssql-tools unixodbc-dev

# Add SQL Server tools to the path by default:.
echo PATH="$PATH:/opt/mssql-tools/bin" >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
#reload bashrc
source ~/.bashrc

# Optional Enable SQL Server Agent:
if [ ! -z $SQL_ENABLE_AGENT ]
then
  echo Enabling SQL Server Agent...
  sudo /opt/mssql/bin/mssql-conf set sqlagent.enabled true
fi
# Configure firewall to allow TCP port 1433:
sudo ufw allow 1433/tcp comment 'Microsoft SQL Server'
sudo ufw reload
#PLEASE NOTE ufw MAY BE DISABLED AND ENABLING IT COULD KICK YOU OUT OF SSH, SO ADD UFW port 22

# Restart SQL Server after installing:
sudo systemctl restart mssql-server
# Connect to server and get the version:

# Adding Optional new user creation:
if [ ! -z $SQL_INSTALL_USER ] && [ ! -z $SQL_INSTALL_USER_PASSWORD ]
then
  echo Creating user $SQL_INSTALL_USER
  /opt/mssql-tools/bin/sqlcmd \
    -S localhost \
    -U SA \
    -P $MSSQL_SA_PASSWORD \
    -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]"
fi


# Restart SQL Server
sudo systemctl restart mssql-server
# Verify SQL Server status
systemctl status mssql-server

You can save the above script as:

install-mssqlserver.sh

Then make it executable using the following command and run it

chmod +x install-mssqlserver.sh
@GeorgeNjeri-BET
Copy link

Resolving CR characters

sed -i -e 's/\r$//' install-mssqlserver.sh

The command will replace those CR characters with nothing, which will leave these lines with LF (\n) as the ending, and Bash will be able to read and execute the file unattended

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment