Are you are interested in running SQL on Linux, but don’t know how to get started? In this presentation, we’ll share the software and hardware you need to get started. We’ll also cover these steps:
- Installing and configuring VirtualBox, Ubuntu Server, PUTTY, SQL Server 2019 on Ubuntu.
- Review the basic administration steps such as start and stop the SQL Server services on Linux
- Backup and restore a database on Linux and checking CPU usage, disk i/o, and disk space.
By the end of the presentation, you will have the required knowledge to setup your own lab and continue your journey on further learning of SQL on Linux.
2. www.datavail.com 2
JP Chen
Senior Director and Practice Leader, SQL
Server Practice, Datavail
Enterprise SQL Support
Both as a DBA and DBA Team Manager and Director, JP
brings a wealth of technical certifications and hands-on
experience to every project.
SQL Performance Monitoring & Tuning
Design and develop comprehensive solutions on database
documentation, monitoring and alerting, stabilization, and
continuous improvements with performance monitoring and
tuning.
SQL Server Blogs & Whitepapers
Eager to share his knowledge with the larger SQL Server
community, JP is an avid blogger and author, posting regular
content on Datavail.com.
3. www.datavail.com 3
Datavail
Overview
Databases, Analytics, and
Application Data Integration
13+ years delivering
data services
300+ customers with
average client
retention of 7 years
Managed services,
projects, and staffing
We are data specialists
All major platforms: SQL,
Oracle, DB2, MySQL, Mongo
Comprehensive
development & operational
services
US & Global models
Reactive and proactive
services
Database
Operations
EBS
Hyperion/Essbase
Microsoft Dynamics
SharePoint
Custom development
• Microsoft .NET
• Oracle APEX
Application
Management
Microsoft SSRS, SSIS, SSAS
PowerBI
Oracle OBIEE, BICS, Golden Gate
Analytics, Visualization
& Integration
Informatica
Oracle Data Integrator
ETL development and
operational run
4. Complete Our
Evaluation &
Win Prizes!
Complete our survey
and you will be entered
to win a pair of noise
cancelling headphones
or Datavailopoly!
5. www.datavail.com 5www.datavail.com 5
Building the SQL on Linux Lab
Connecting to SQL on Linux
Configuring SQL on Linux with the mssql-conf tool
Creating, Backing up, and Restoring a Database
Exploring the System Monitoring Tools on Linux
Summary and Next Steps
Agenda
6. www.datavail.com 6www.datavail.com 6
Building the SQL on
Linux Lab
Downloading the software required – all free!
Installing the software required
Installing the SQL Server command-line tools
10. www.datavail.com 10www.datavail.com 10
Change the VirtualBox Network
Adapter to “Host-only Adapter” Before
Continuing
Get the IP Address of the
Ubuntu Server
Update the hosts file in the
Windows PC
Connect from SQL Server
Management Studio (SSMS)
Connect from Azure Data Studio
Connecting to SQL on Linux
11. www.datavail.com 11
Getting the
SQL on
Linux Lab
Ready
Change the VirtualBox Network Adapter to
“Host-only Adapter” Before Continuing
Get the IP Address of the Ubuntu Server
Update the hosts file in the Windows PC
DEMO
Getting the SQL on
Linux lab ready
14. www.datavail.com 14www.datavail.com 14
mssql-conf tool: configuration script equivalent of SQL Server
Configuration Manager (SSCM) and sp_configure stored proc
Parameters configuration for: agent, collation, default data, log,
dump, error log, and backup directories, memory limit, TCP port,
traceflags, and many others
Configuring SQL on Linux with the
mssql-conf tool
DEMO
Configuring memory
limit and TCP port
17. www.datavail.com 17
Using htop Monitors all process running on the
system
Allows you to kill a process, configure
nice, sort columns, search for
processes, and a lot more
DEMO
Using htop
18. www.datavail.com 18
Using
glances
Monitors CPU, load, memory, network rate,
disk i/o, and file system
Shows 4 different types of color
• Green – ok
• Blue – attention
Adjust the thresholds by modifying the configure
file located in /etc/glances/glances.conf
• Violet – alert
• Red – critical
DEMO
Using glances
19. www.datavail.com 19
Using
iostat
Collects and shows operating system
storage input and output statistics
Identifies storage devices performance
issues
• Local disk
• Remote disk
DEMO
Using iostat
Why SQL on Linux?
When we are unclear on the reason or why, what can we do? When we travel to a new country or city and we bring a GPS to help get us where we want to go.
Don’t you wish we have a similar GPS for life? I have great news for you. It turns out, we do. It’s called the universal motivators: Time, Money, and Feeling. Again, the 3 universal motivators are: Time, Money, and Feeling. They are universal and also timeless. They work regardless of time or place and never go out of fashion.
Let’s apply them here for SQL on Linux.
Money: Cost saving for Linux.
Price for Windows Server 2019:
Datacenter Core-based $6,155
Red Hat Enterprise Linux Server:
Premium $1,299
According to W3Cook, Linux powers the servers that run 96.5 percent of the top one million domains in the world.
Time:
If you were thinking about shifting your databases from SQL Server to MariaDB or PostgreSQL to take advantage of Linux hardware savings, you wouldn't have to concern about the conversion costs. Sure – you still pay for SQL Server licenses. But, you could save on the costs for conversion and migration to make up the difference. Not to mention the headaches or loss of sleep due to conversion problems.
<- Save time, money, and make you feel good.
With everyone talking about cloud migrations, what do you think the Operating System of choice for the people in charge of finances.
Hi. I am JP Chen from Datavail. I am the Senior Director and Practice Leader of SQL Server. I am very fortunate to have the privilege to work closely with 200+ SQL DBAs in the US and India supporting 250+ customers SQL Server environments ranging from small to enterprise sizes. We can help you to plan, design, and setup high availability solutions, migrate your environment from on-premises to the cloud, and resolve complex performance issues.
Even if you have 0 or 25 SQL DBAs or if you have 1 or 1000+ SQL Server instances, my team and I can work with you in supporting your SQL Server environment 24x7 maximizing uptime and minimizing call-outs.
What's in it for them?:
In this presentation, we will share with you the software and hardware you need. We will walk you through the steps on:
Building the SQL on Linux Lab
Connecting to SQL on Linux
Configuring SQL on Linux with the mssql-conf tool
Creating, Backing up, and Restoring a Database
Exploring the System Monitoring Tools on Linux
We will cover the basics and absolute essentials to help get you started.
By the end of the presentation, you will have the required knowledge to setup your own SQL on Linux lab for further learning and practice.
When to ask questions: Please hold your questions after the demo for each section. Keep it short as we have a lot to cover. We can further discuss after the presentation if needed.
Before you begin: Check your Windows PC to make sure you have at least 4 GB of RAM and 8 GB of free disk space before continuing. Most modern Windows PC will meet the minimum requirements.
System Requirements Before You Begin
As per the official Ubuntu documentation for system requirements, https://help.ubuntu.com/community/Installation/SystemRequirements, Ubuntu Server (CLI) installation requires the following:
300 MHz x86 processor
256 MiB of system memory (RAM)
1.5 GB of disk space
Graphics card and monitor capable of 640x480
The Microsoft installation guide for SQL Server 2017 on Linux, https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup?view=sql-server-2017#sqlvnext, specifies the following system requirements:
Memory: 2 GB
File System: XFS or EXT4 (other file systems, such as BTRFS, are unsupported)
Disk space: 6 GB
Processor speed: 2 GHz
Processor cores: 2 cores
Processor type: x64-compatible only
All steps are documented here: https://www.datavail.com/blog/sql-on-linux-building-your-own-sql-on-linux-lab
Question: Anyone here heard of or worked with Partition Magic and LILO?
Software Required
To create our SQL on Linux lab, we will need the following software:
VirtualBox – a free and open-source hosted hypervisor for x86 virtualization.
Ubuntu Server – a popular distribution of Linux Server.
PUTTY – a free SSH/telnet client for Windows that allows you to connect to a Linux Server remotely.
SQL Server 2017.
* You need to download SQL Server 2017 on Linux after we had installed Ubuntu Server. Remember, we are building our own SQL on Linux lab on a Windows PC here.
Install VirtualBox
Straight forward. Just run the installation file and accept the defaults.
After the installation completes, then:
Create the new VM “DvSQLOnLinux”.
Increase the memory to 2048MB.
Assign 8 GB for the hard disk.
Increase the CPUs to 2.
Increase to 128 MB for video memory.
Mount the ios file for DvSQLOnLinux Server.
Install Ubuntu Server
Start the installation.
Accept the defaults.
Choose a server name.
Pick a username.
Choose a password. Make sure to remember it.
Install OpenSSH.
Reboot.
Key in your username and password.
Update your system with the latest packages.
Upgrade them.
Install PuTTY Remote Terminal
Install and accept the defaults to complete the setup.
Go to Ubuntu Server to get the ip.
Configure port forwarding in VirtualBox for the Ubuntu Server.
Open PuTTY.
Type “localhost” in the host name textbox. Port 22 in the port textbox. Save the session as “DvSQLOnLinux”.
Key in your username and password to log in just as if you are sitting in front of your Linux Server.
Enter pwd to see the current working directory.
Create the SQLPASS2019.txt.
Go to your Linux Server VM.
Enter pwd to see the current working directory.
Enter ls to see the file also exist.
Install SQL Server 2017
Connect from PuTTY.
Import the public repository GPG keys.
Key in your password.
Register the Microsoft SQL Server Ubuntu repository for SQL Server 2017.
Enter “sudo apt-get update” to update the repository.
Enter “sudo apt-get install -y mssql-server” to install SQL Server.
Enter “sudo /opt/mssql/bin/mssql-conf setup” to run the setup.
Enter “2” to select the “Developer” edition of SQL Server.
Enter “y” to accept the license terms.
Enter the password you wish to use for the sa account.
Enter “systemctl status mssql-server –-no-pager” to verify the SQL Server service is running once the setup is completed.
In the Windows environment, we have the SQLCMD and BCP command-line tools to connect to SQL Server, run SQL statements, and bulk-copy data in and out of the SQL Server instances. These two utilities also exist for SQL on Linux. It is called mssql-tools.
Install the SQL Server command-line tools by following the steps outlined on the webpage.
Connecting to SQL Server locally
sqlcmd -S localhost -U SA -P '<YourPassword>'
select name from sys.databases
Type “go” on the command prompt.
Change the Network Adapter for the Linux virtual machine to “Host-only Adapter” Right-click on “DvSQLOnLinux” on the left-pane of Oracle VM VirtualBox Manager.
Note: After this change, you will lose connection to the Internet. If you need to connect to the Internet, you will need to change the Network Adapter to attach to “NAT” and configure port-forwarding as described back in the “Installing and Configuring PuTTY Remote Terminal” section.
Get the IP Address of the Ubuntu Server
Type “ifconfig” on the command line of your Ubuntu virtual machine.
Make a note of the ip address. In my case, it is 192.168.56.102.
From PuTTY, connect to your Ubuntu Server using the ip.
Update the hosts file in the Windows PC
Search for “notepad” in your Windows PC.
Right-click on “notepad”.
Select the option “Run as Administrator”.
Open the hosts file in your Windows PC. My Windows PC is running Windows 10 Professional, the location for the hosts file is C:\Windows\System32\drivers\etc\hosts.
Add the ip address and hostname of your Ubuntu Server to the last line of the hosts file.
Connecting from SQL Server Management Studio (SSMS):
Connect using the hostname.
Type “select @@VERSION”.
Press the F5 key on your keyboard to execute the query.
Azure Data Studio is a cross-platform database tool for data professionals to work with SQL Server and other database platforms. To simulate a production environment in our SQL on Linux lab, we will install it on our Windows PC and remote connect to our SQL Server instance installed on Ubuntu Server.
Download and install it if not already done.
To connect and query SQL on Linux via Azure Data Studio, perform the following steps:
Open Azure Data Studio.
Key in your server ip. In this case, I had keyed 192.168.56.102.
Select “SQL Login” in the “Authentication type” drop-down box.
Type in “sa” in the “User name” textbox.
Type in your password for the sa account.
Click the “Connect” button to connect to your SQL Server instance installed on the Ubuntu Server.
Click the “New Query” box to open a new query window.
Type “select * from sys.databases” in the new query window.
Press F5 on your keyboard to execute the query.
Try connecting using the server name and it will also connect successfully just like we did in SSMS.
The mssql-conf configuration script for SQL on Linux is the equivalent of SQL Server Configuration Manager (SSCM) and sp_configure stored procedure for SQL on Windows. You can use it to set the following parameters:
Agent
Collation
Default data directory
Default log directory
Default dump directory
Default error log directory
Default backup directory
Memory limit
TCP port
Traceflags
And many others
For a full list of parameters and explanations of each, please go to https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-configure-mssql-conf?view=sql-server-2017.
In this demo, we will walk-through the steps on how to view the mssql-conf configuration script, set the memory limit, and change the default TCP port for SQL, restart SQL Server services for the setting to take effect, connect to the SQL Server instance using the new port, revert the changes:
1. View the contents of the mssql.conf file:
sudo cat /var/opt/mssql/mssql.conf
2. Set the memory limit to 2048 MB:
sudo /opt/mssql/bin/mssql-conf set memory.memorylimitmb 2048
3. Change the default TCP port for SQL to 1455:
sudo /opt/mssql/bin/mssql-conf set network.tcpport 1455
By default, SQL Server listens for connections in port 1433. As a test, we had set it to 1455. When connect to SQL Server, you will need to specify the custom port as in the following:
sqlcmd -S localhost, <port #> -U UserName -P Password
4. Restart SQL Server services for the settings to take effect:
sudo systemctl restart mssql-server
5. Try connecting to SQL Server without the custom port 1455 you had set back in step 13, you will see the corresponding error message:
sqlcmd -S localhost -U USERNAME -P ‘PASSWORD’
6. Connect using the custom port will succeed:
sqlcmd -S localhost,1455 -U USERNAME -P ‘PASSWORD’
7. Enter the edit mode of the mssql.conf file to modify the file:
sudo nano /var/opt/mssql/mssql.conf
8. Move the down arrow key to go down to the tcpport = 1455 line.
9. Move the left arrow key to go the space after 1455.
10. Erase 55 and replace with 33.
11. Press Ctrl + O to write the file.
12. Press Enter to accept the file name as mssql.conf.
13. Press Ctrl + X to exit.
14. Restart the SQL Server service for the setting to take effect:
systemctl restart mssql-server
15. Key in your password.
16. Try connecting using the default port of SQL and it will connect successfully:
sqlcmd -S localhost -U USERNAME -P ‘PASSWORD’
17. Unset the memory limit using the unset option:
sudo /opt/mssql/bin/mssql-conf unset memory.memorylimitmb
You can unset all other parameters exist in the mssql.conf file using this method.
18. Key in your password.
19. Restart SQL Server service for the setting to take effect:
systemctl restart mssql-server
20. Key in your password.
21. View the contents of the mssql.conf file to confirm the memory limit is unset:
sudo cat /var/opt/mssql/mssql.conf
Creating, backing up, and restoring a database are critical databases administration tasks that we must perform regardless of Operating System platform. Good news! If you had done these steps in Windows, the same steps apply in Linux. You just need to be aware of the file path structure.
Creating a database and verifying its data and log files
To create a database and verify its data and log files, perform the following steps:
1. Connect to your Ubuntu Server from your PuTTY remote terminal if not already connected.
2. Connect to your SQL Server:
sqlcmd -S localhost -U USERNAME -P ‘PASSWORD’
3. Create the database, SQLPASS2019:
create database SQLPASS2019
go
4. Specify the database to use:
use SQLPASS2019
go
you will see the message “Changed database context to ‘SQLPASS2019’.
5. Create a table, major_conference, and insert two rows of data:
create table major_conference (id int, name nvarchar(50), start_date datetime)
go
insert into major_conference values (1, ‘SQL Saturday LA’, ‘2019-06-15’)
insert into major_conference values (2, ‘SQL PASS Summit’, ‘2019-11-05’)
insert into major_conference values (3, ‘AWS Re:Invent’, ‘2019-12-02’)
go
6. Find the location of the data and log files for the newly created databases:
select DB_NAME(database_id), physical_name from sys.master_files
where DB_NAME(database_id) = ‘SQLPASS2019’
go
Backing up a database
To backup a database, perform the following steps:
1. Connect to your SQL Server if not already connected:
sqlcmd -S localhost -U USERNAME -P ‘PASSWORD’
2. Backup our sample database, SQLPASS2019:
backup database [SQLPASS2019] to disk = N’/var/opt/mssql/data/SQLPASS2019_20191106.bak’
go
Restoring up a database
To restore a database, perform the following steps:
1. Find the location of the backup file. In our example, it is /var/opt/mssql/data/SQLPASS2019_20191106.bak.
2. Connect to your SQL Server if not already connected:
sqlcmd -S localhost -U USERNAME -P ‘PASSWORD’
3. Change the database to master:
use master
go
Just as in SQL on Windows, you can’t restore a database if it is in use.
4. Restore the database. In this example, we will restore and replace the existing SQLPASS2019 databases:
use master
go
restore database [SQLPASS2019] from disk = N’/var/opt/mssql/data/SQLPASS2019_20191106.bak’ with replace
In Windows, we use the Task Manager to view the apps and processes and their CPU, Memory, Disk, and other utilizations details. We use Performance Monitor to view the live stats and the performance of the system counters. What are the similar utilities for us to do the same on Linux? While there are plentiful of tools you can use, we will explore 3 of the most popular ones: htop, glances, and iostat.
Htop is an enhancement of another system monitoring tool called top. It allows you to monitor all processes running on the system. It is an interactive tool and allows you to kill a process, configure nice, sort columns, search for processes, and a lot more.
To use explore htop, perform the following steps:
1. Download and install htop if not already installed:
sudo apt-get update
sudo apt-get install htop
Key in your password when prompted.
2. Open htop:
htop
3. Look at the upper left corner, you will see the CPU, Mem, and Swp details.
4. Look at the upper right corner, you will see the Tasks, Load average, and the system Uptime.
5. Click on the “CPU%” column to sort the processes by the percentage CPU time currently using.
6. Look at the bottom of the screen, you will see different functions and there F-keys to invoke them.
7. Press F10 to quit.
Glances is a cross-platform system monitoring tool. Yes – you can run it on Linux and on Window. You can use glances to monitor CPU, load, memory, network rate, disk i/o, and file system. Glances shows 4 different types of colors for various statistics: Green - OK, Blue - attention, Violet – alert, Red – critical. You can adjust the thresholds by modifying the configure file located in /etc/glances/glances.conf.
To use explore glances, perform the following steps:
1. Download and install glances if not already installed:
sudo apt-get update
sudo apt-get install glances
Key in your password when prompted.
2. Open glances:
glances
3. Press c to sort process by CPU.
4. Press m to sort process by MEM.
5. Press i to sort processes by I/O rate.
6. Notice that you can view the File System space usage in the “File SYS” section.
7. Press q to quit.
IOSTAT is a system monitor tool that can be used to collect and show operating system storage input and output statistics. It is often used to identify storage devices performance issues. The storage devices can be local disk or remote disks.
To use explore iostat, perform the following steps:
1. Download and install iostat if not already installed:
sudo apt-get update
sudo apt-get install sysstat
Key in your password when prompted.
2. Open iostat:
iostat
3. Notice that the first section contains the CPU report.
4. Look at the device utilization report in the second section. Tps is the number of transfers per second.
5. Convert the iostat to show report in megabytes unit:
iostat -m
6. Create a delay to view the trend:
iostat -m 5 3
iostat with MB unit, 5 seconds interval with 3 times reports
We had went over the software required and how to download and configure them to build your SQL on Linux for further practice and learning. We had reviewed the steps how to connect to SQL on Linux using SSMS and Azure Data Studio with ip and also hostname. We had explored the steps on how to configure SQL on Linux with the mssql-conf tool and walked through examples on how to set the memory limit and tcp port number. Then, we examined the steps for creating, backing up, and restoring a database. The same steps we are all familiar with also apply in Linux. We just need to be aware of the file path and structure differences. Finally, we explore the system monitoring tools on Linux. We have looked into 3 different system monitoring tools: htop, glances, and iostat and explored the basic steps on how to use them.
Now that you know how to build your SQL on Linux lab, you can further move onto testing for optimizing performance for SQL on Linux. In addition, once you have a good foundation on SQL on Linux and basic Linux knowledge, you can then look into running SQL Server container images with Docker.
Learning SQL on Linux will expand your knowledge on running SQL on different Operating Systems and enables further learning on new trending technologies. Charles Darwin once said, “It is not the strongest of the species that survives, nor the most intelligent that survives. It is the one that is the most adaptable to change.” Data technologies are constantly evolving, can you keep pace with change and ensure you adapt? Learning and running SQL on Linux is one of the ways to make sure you do.