Introduction

Microsoft SQL Server is a powerful relational database management system (RDBMS) that has traditionally been associated with Windows. However, Microsoft has expanded its support to Linux, allowing users to run SQL Server on a variety of platforms. This guide will walk you through the process of installing Microsoft SQL Server on Linux, creating a database, opening ports for remote connections, setting up backups, and creating scripts for automation.

Step 1: Install Microsoft SQL Server on Linux

To install Microsoft SQL Server on a Linux system, follow these steps:

  1. Update your system:
    sudo apt-get update
  2. Install the SQL Server package:
    sudo apt-get install -y mssql-server
  3. Run the setup:
    sudo /opt/mssql/bin/mssql-conf setup

    During the setup, you will be prompted to accept the license terms and set the SA (System Administrator) password.

  4. Verify the installation:
    systemctl status mssql-server

    This command will show the status of the SQL Server service. Ensure that it is running.

Step 2: Create a Database

Once SQL Server is installed, you can create a database using the following steps:

  1. Connect to SQL Server:
    sqlcmd -S localhost -U SA -P 'YourPassword'
  2. Create a new database:
    CREATE DATABASE TestDB;
  3. Verify the database creation:
    SELECT Name FROM sys.databases;

Step 3: Open Ports for Remote Connections

To allow remote connections to your SQL Server instance, you need to open the appropriate port (default is 1433) on your Linux firewall.

  1. Open the port:
    sudo ufw allow 1433/tcp
  2. Enable the firewall:
    sudo ufw enable
  3. Verify the firewall status:
    sudo ufw status

Step 4: Set Up Backups

Regular backups are essential for data protection. Here’s how to set up automated backups:

  1. Create a backup directory:
    sudo mkdir /var/opt/mssql/backup
  2. Set permissions:
    sudo chown mssql:mssql /var/opt/mssql/backup
  3. Create a backup script:
    sudo nano /usr/local/bin/backup_db.sh

    Add the following content to the script:

    #!/bin/bash
    sqlcmd -S localhost -U SA -P 'YourPassword' -Q "BACKUP DATABASE TestDB TO DISK = '/var/opt/mssql/backup/TestDB.bak'"
                
  4. Make the script executable:
    sudo chmod +x /usr/local/bin/backup_db.sh
  5. Schedule the backup using cron:
    crontab -e

    Add the following line to schedule a daily backup at 2 AM:

    0 2 * * * /usr/local/bin/backup_db.sh

Step 5: Automate Tasks with Scripts

You can automate various tasks using scripts. For example, to automate database maintenance tasks, you can create a script and schedule it using cron.

  1. Create a maintenance script:
    sudo nano /usr/local/bin/db_maintenance.sh

    Add the following content to the script:

    #!/bin/bash
    sqlcmd -S localhost -U SA -P 'YourPassword' -Q "DBCC CHECKDB('TestDB') WITH NO_INFOMSGS, ALL_ERRORMSGS"
                
  2. Make the script executable:
    sudo chmod +x /usr/local/bin/db_maintenance.sh
  3. Schedule the script using cron:
    crontab -e

    Add the following line to schedule the script to run weekly:

    0 3 * * 1 /usr/local/bin/db_maintenance.sh

Conclusion

By following this guide, you have successfully installed Microsoft SQL Server on a Linux system, created a database, opened ports for remote connections, set up automated backups, and created scripts for automation. These steps will help you manage your SQL Server instance efficiently and ensure that your data is secure and accessible.

For more advanced configurations and optimizations, refer to the official Microsoft SQL Server on Linux documentation.