Category: Linux

Linux Blog Archives, by Peter Whyte (SQL Database Administrator). Includes random Linux posts, and Windows Subsystem for Linux (WSL).

  • Enable Windows Subsystem for Linux (with Ubuntu 18.04)

    Enable Windows Subsystem for Linux (with Ubuntu 18.04)

    WSL allows Windows users to develop and run applications within a GNU/Linux environment. Previous to this feature release we needed VM’s or a dual boot setup.

    For more information, as always have a look at the Microsoft Docs. There’s a huge amount of tutorials and documentation on this area.

    This post is a simple guide and includes:
    # Enabling Windows Subsystem for Linux (WSL)
    # Downloading a Linux Distro
    # Installing a Linux Distro


    Enabling Windows Subsystem for Linux (WSL)

    Open PowerShell as Administrator and run the following to enable WSL:

    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
    PowerShell Enable WSL

    You’ll then be prompted to restart your local computer.

    PowerShell Enable WSL Restart Prompt

    Downloading a Linux Distro

    You can find all the Linux Distributions on the Microsoft Store and download from there. This may be restricted by Group Policy if you’re on a Domain, so you can download manually from this list on the documentation page.

    For this guide I’m downloading using Invoke-WebRequest:

    cd c:\your_download_folder
    Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1804 -OutFile Ubuntu1804.appx -UseBasicParsing
    PowerShell Invoke-WebRequest Ubuntu 18.04

    Installing a Linux Distro

    We have to rename the .appx file I downloaded above to .zip, decompress and then run the .exe file to install Ubuntu.

    Rename-Item .\ubuntu1804.appx .\ubuntu1804.zip
    Expand-Archive .\ubuntu1804.zip .\ubuntu1804
    .\ubuntu1804\ubuntu1804.exe
    WSL Ubuntu Install

    The terminal should scope into the Linux instance. Type ‘exit’ to get back to PowerShell.


  • Check & Restart SQL Server Services on Linux

    Check & Restart SQL Server Services on Linux

    As of the 2017 Edition of SQL Server we have been able to install SQL Server on Linux. Linux will likely be more of an unfamiliar environment for traditional Microsoft SQL DBAs. This guide should hopefully help you when you need to check the status of SQL Services and Stop/Start services on a Linux host.

    Microsoft Docs have a page on this – Start, stop, and restart SQL Server services on Linux

    The examples provided in this post utilise the systemctl command. Many Linux distributions use systemctl as a tool for managing services including CentOS, Ubuntu, Debian & RedHat.

    Note that the systemctl command is not available in some Linux distributions, including if you are running Linux on WSL (Windows Subsystem for Linux). This is because Ubuntu WSL does not use the systemd init system, which is what systemctl is designed to manage.

    The following should help you check and restart SQL Services on a Linux host:
    # Show Enabled Services on Linux
    # Check Status of SQL Server Service Linux
    # Stop, Start and Restart SQL Server Services on Linux

    Show Enabled Services on Linux

    To show all enabled services on a Linux system, use the following command:

    systemctl list-unit-files | grep enabled
    
    systemctl list unit files enabled

    If your service is not on the list of enabled services, it will not start automatically on the next system reboot. To enable the SQL Server service with systemctl, use the following command : sudo systemctl enable mssql-server

    Check Status of SQL Server Service on Linux

    The systemctl status mssql-server command is used to check the current status of the SQL Server service on your system. This command will display information about the service, including whether its currently active or not.

    sudo systemctl status mssql-server
    
    systemctl SQL Server Status

    If the SQL Service is active, the output of the command will include the text “active (running)” in green. This indicates that the service is currently running and is available to process requests.

    If the service is not active, the output will include the text “inactive (dead)” in red. This indicates that the service is not currently running, and may need to be started or restarted in order to be used.

    Stop, Start or Restart the SQL Server Services on Linux

    To stop, start, or restart the SQL Server service on Linux, you can use the systemctl command.

    sudo systemctl stop mssql-server
    

    To start the service again, use the following command:

    sudo systemctl start mssql-server
    

    To restart the service, use the following command:

    sudo systemctl restart mssql-server
    systemctl Restart SQL Server

    After running any of these commands, it is always a good idea to check the status of the service to make sure that the desired action was completed successfully. You can do this using the systemctl status mssql-server command as shown in the screenshot.

  • Installing SQL Server on Linux

    Installing SQL Server on Linux

    A post on installing SQL Server on Linux, following the Microsoft SQL Server 2019 Guide (Red Hat).

    # Install SQL Server.
    # Install the SQL Server Command-Line Tools.
    # Connect to SQL Server Locally.
    # Connect to SQL Server Remotely.


    Install SQL Server

    1. Get connected & download the Microsoft SQL Server 2019 Red Hat repository configuration file.

    sudo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/8/mssql-server-2019.repo
    Linux EC2 Connect Curl MSSQL 2019

    2. Install SQL Server.

    sudo yum install -y mssql-server
    Linux Yum Install SQL Server 2019

    3. Run mssql-conf setup to choose the SQL Server Edition, SA password and accepting of the Licence Agreement.

    sudo /opt/mssql/bin/mssql-conf setup
    Linux MSSQL Configuration

    4. Check the SQL Server service by running systemctl status.

    systemctl status mssql-server
    Linux Systemctl Status MSSQL

    5. Allow in remote 1433 connections.

    I’m using Firewalld as per Microsoft’s guide – here’s my quick install notes.

    sudo firewall-cmd --zone=public --add-port=1433/tcp --permanent
    sudo firewall-cmd --reload
    Linux Open 1433 Firewall

    Install the SQL Server Command-Line Tools

    1. Download the Microsoft Red Hat repo config file.

    sudo curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/8/prod.repo
    Linux Curl MS RedHat Repo

    2. Install ODBC driver for SQL Server (Linux) – allows BCP & SQLCMD.

    sudo yum install -y mssql-tools unixODBC-devel
    Linux Install SQL Server ODBC Driver

    3. Add mssql-tools to PATH environment variable – allows tools to be run without full path.

    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
    source ~/.bashrc
    Linux Add MSSQL-Tools to PATH

    Connect to SQL Server Locally

    Using the PATH variable above, call sqlcmd:

    Linux SQLCMD Select Sys.Tables

    Connect to SQL Server Remotely

    I’m connecting to the EC2 Amazon Linux SQL Server instance via SSMS on my local machine (Windows 10).

    For this to work we had to allow in remote 1433 connections (step 5 during install above), and I added my IP into the AWS Security Group.

    SSMS Connect to AWS EC2 Linux SQL Server

  • Installing Firewalld on Linux

    Installing Firewalld on Linux

    This post contains installation notes for installing Firewalld on an Amazon Linux 2 EC2 instance.

    Firewalld is a Linux firewall tool that makes it easy to create custom firewall rules. It acts as a frontend for iptables, the packet filtering system, and lets you apply rules dynamically. This makes it a great tool for keeping your Linux system’s network secure. You can use the firewall-cmd command-line tool to manage your firewall rules and configurations. Check out the Firewalld documentation for more info.

    Install Firewalld on Linux

    To install Firewalld on an Amazon Linux 2 EC2 instance, you might need to first ensure that the epel-release package is installed on your system. This package provides access to the Extra Packages for Enterprise Linux (EPEL) repository, which contains the Firewalld package. You can install the epel-release package using the following command:

    sudo yum install epel-release
    

    To get started with the Firewalld install, log in to your EC2 instance and run the following commands:

    sudo yum install firewalld
    
    Linux Yum Install Firewalld

    The command sudo systemctl start firewalld is used to start the Firewalld service on a Linux system. Once the above install is complete start the Firewalld service:

    sudo systemctl start firewalld
    sudo systemctl enable firewalld
    sudo systemctl status firewalld
    
    Linux SystemCtl Start Firewalld

    Firewalld should now be up and running on your Amazon Linux 2 EC2 instance. You can then use the firewall-cmd command-line tool to manage your firewall rules and configurations. For example, you can use the --list-all option to view all of the currently active firewall rules:

    firewall-cmd --list-all
    

    This will show you all of the rules that are currently in effect, along with information about which zones they apply to and the sources and destinations they apply to. This can be useful for troubleshooting and verifying that your firewall is configured as you expect.

  • How to Create MySQL Databases & Tables

    How to Create MySQL Databases & Tables

    This blog post is a guide on how to create databases and tables in MySQL, from a Windows and Linux computer. The information included here should help beginners who are new to MySQL.

    To create a new database in MySQL, and many other relational database systems we use the CREATE DATABASE SQL Statement.

    The meaning behind the word ‘database’ can vary, and often be referred to the same thing as a Schema. In MySQL, Databases and Schemas are both one of the same things.

    The following MySQL tips are covered in this post:
    # Create a MySQL Database
    # The USE DATABASE Command
    # Close MySQL Queries with Semicolons
    # How to Exit from MySQL CLI

    Create a MySQL Database

    We can create a MySQL Database by running CREATE DATABASE databaseName

    The create database example below is being done using the GUI option, MySQL WorkBench. For more information on SQL syntax and parameters of the CREATE DATABASE statement in MySQL, we can refer to the MySQL Dev Docs.

    Create Database MySQL WorkBench

    After running the above, refresh Schemas within the Navigator to the left and open the new database.

    If you’re instance running on Linux, database names are case-sensitive, whereas on Windows this isn’t an issue. Below, I’m creating a database on a local MySQL install (Windows 10) and then creating a MySQL table within it using a Capital letter on the schema/database name.

    MySQL Create Table and Insert Data

    Below is an example of doing the same on Linux using the MySQL Command-Line Client.

    SSH Connect to Amazon Linux

    The above is us logging in with the ec2-user to an Amazon Linux EC2 instance. Then we create a new table using a Capital letter on the database schema name.

    MySQL Create Table and Insert Data

    A common best practice is to stick to the one case for object naming in SQL for this reason. For example, use finance_database instead of Finance_Database, or use FINANCE_DATABASE.

    The USE DATABASE Command

    The USE DATABASE command in SQL is used to set your statements to use the specified database as your default (current) database.

    To show an example of USE in SQL, I create a new database and table MySQL Workbench below. The CREATE TABLE and INSERT INTO statements include the schema name (butter), and then I attempt to run a SELECT statement on the table without including the schema name.

    MySQL WorkBench Create Table and Insert Data (without schema)

    The returns an error – Error Code: 1046. No database selected. Select the default DB to be used by double-clicking its name in the SCHEMAS list in the sidebar.

    As described in this error message, we can set our default schema in the MySQL Workbench Navigator.

    MySQL WorkBench Set Default Database

    That’s one solution. The other is to use the USE DATABASE statement. I’m adding USE butter; to the TSQL in the MySQL Workbench query window.

    All queries executed after running the USE command will assume the database specified when running queries without a schema name.

    MySQL WorkBench Create Table and Insert Data (with USE)

    Close MySQL Queries with Semicolons

    Semicolons are a way to determine when your SQL statement ends in MySQL.

    A query does not run by hitting return in a MySQL terminal, it requires a semicolon to know when the SQL statement is complete.

    The screenshot below shows a Linux client connected to MySQL. The first command includes a semicolon and returns a list of databases. The second command (DROP DATABASE) displays the example of a command not being executed.

    MySQL Drop Database Delayed Exit

    The command does not run until we add a semicolon and hit return, as marked with a red cross on the screenshot.

    To further help understand semicolons in MySQL I also will show an example in MySQL Workbench. I’m creating a new table and inserting a row of data into the table on the next row.

    MySQL WorkBench Create Table and Insert Data (no exit)

    The CREATE TABLE statement returns an error message – Error Code: 1064: You have an error in your SQL syntax; check the manual that the corresponds to your MySQL...

    The table did not get created here. If I add the semicolon at the end of the CREATE TABLE line the table will get created and a row will be inserted.

    MySQL WorkBench Create Table and Insert Data (with exit)

    If you’re running queries line-by-line then the semicolon isn’t as much required. For more information, see this MySQL documentation link.

    How to Exit MySQL CLI

    This part shows you how to bug out of MySQL CLI.

    To exit MySQL CLI, type QUIT and hit return in your terminal window.

    How to exit out of MySQL in Terminal