Database by Doug
Tuesday, May 28, 2024
Find NULL in any column
Thursday, May 16, 2024
Azure SQL Edge Instance on a Macbook Pro M1 with Docker
This is an update to this post from 2020.
I work on a MacBook Pro M1 with a 16" display that has been a great workhorse for me for years. Using a containerized image of Microsoft SQL Server makes it possible to run an entire SQL Server on my aging MacBook.
As in 2020, it's very handy to have a SQL instance on your local machine. I do this kind of stuff with a local sql database instance:
- data modeling
- demonstrations
- development
- quick proof of concept
- sandbox development
- disconnected work
Instructions
- Get Docker
- Open Terminal
- Execute a long-ish docker command
- Verify via Docker Desktop
- Connect to the instance from a client in the host machine
1. Docker Installation
2. Open a terminal window
3. Run Docker Command
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=ABCabc123" -p 1433:1433 --name sqledge --hostname sqledge -d mcr.microsoft.com/azure-sql-edge
docker run mcr.microsoft.com/azure-sql-edge
docker run /
-e "ACCEPT_EULA=Y"
-e "MSSQL_SA_PASSWORD=ABCabc123"
-p 1433:1433
-name sqledge
--hostname sqledge
-d
4. Verify via Docker Desktop
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES79e76f16d903 mcr.microsoft.com/azure-sql-edge "/opt/mssql/bin/perm…" 4 hours ago Up 4 hours 1401/tcp, 0.0.0.0:1433->1433/tcp sqledge
CONTAINER ID 79e76f16d903IMAGE mcr.microsoft.com/azure-sql-edgeCOMMAND "/opt/mssql/bin/perm…" CREATED 4 hours agoSTATUS Up 4 hours PORTS 1401/tcp, 0.0.0.0:1433->1433/tcp NAMES sqledge
5. Connect using a Database Client
- the Server has a comma between the IP and the port, i.e., localhost, 1433
- the Authentication type is SQL login, which would not be very secure for a production server
- the User name is sa
- the Password will need to be the password from the docker command. in our case "ABCabc123"
- the Server Group is a convenience for ADS, which allows you to put connections in groups
- the other entries have been left at the defaults
Just to be clear about what is going on:
- Azure Data Studio (ADS) is running on the host, my Macbook
- ADS is connected to port 1433 on the localhost IP address, localhost is my Macbook (localhost IP is typically 127.0.0.1, which works also)
- Two-way communication is mapped from localhost:1433 to/from sqledge:1433
- The SQL Server in the container is listening to/sending on port 1433
6. Shut it down
docker stop sqledgedocker stop sqledge
docker rm sqledge
Azure SQL Edge local vs SQL Server local vs Azure SQL Server
- simple authentication
- can create, start, stop, remove, clone containers easily
- can do these things:
- all data definition language commands (create, alter, drop)
- all data manipulation language commands (SELECT, INSERT, UPDATE, etc.)
- all basic T-SQL commands
- data stream
- special data types such as HierarchyID, Spatial, Full Text, FileStreams, In Memory OLTP, JSON (limitations), etc.
- special features such as Replication Polybase, Snapshots, Linked Servers, CLR, High Availability, etc.
- auxiliary server product capabilities: Analysis Services, Reporting Services, etc.
Summary
Tuesday, September 29, 2020
Installing the Northwind Sample Database on your SQL Server Instance
This post shows how to run a SQL Server Instance on about any computer using Docker Containers. Your next step might be to get a sample database into that SQL Server Instance.
Thanks to Microsoft, you can get their sample databases as T-SQL scripts. You can use these to install these databases on whatever server you are connected to, including your "containerized" SQL Server instance.
Let's do the Northwind database. We'll use Azure Data Studio, since it works on Win/Linux/Mac.
Here are the steps:
- Click on this link, which will open up the text of the instnwnd.sql file.
- Copy all that code - I used ctrl-a to select it all, then ctrl-c to copy all of that into the clipboard.
- Open up Azure Data Studio (ADS)
- Right-click on your database server, and choose New Query
- Paste all the code into the query window - I used ctrl-v to do the paste from the clipboard
- Now click Run
Depending on your machine, this may take several minutes to run, and will give many output messages. Depending on the version of SQL Server that you are running, you are likely to get a few harmless error messages and warnings like "Tokenization is skipped for long lines ...".
Thursday, September 24, 2020
Running a SQL Server Instance in Docker
- quick tests
- performance testing
- demonstrations
- development
- testing
- work around limitations of a cloud instance
- Download, install, and run Docker
- Get to an (elevated?) command prompt
- Run a long-ish docker command
- Verify via Docker
- Connect from Host with client
- Shut it down
1. Docker Installation
Follow the installation instructions for your platform. This varies slightly for the platform you are on. If you can install a program on your machine, you can do this.
2. Command Prompt
3. Run Docker Command
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=123ABCabc" -p 1401:1433 --name sqlsrvr1 -h -sqlsrvr1 -d mcr.microsoft.com/mssql/server:2019-latest
Here's the code again, broken into multiple lines. Note the Windows continuation character ^ (caret) at the end of each line:
docker run -e "ACCEPT_EULA=Y" ^ -e "SA_PASSWORD=123ABCabc" ^ -p 1401:1433 ^ --name sqlsrvr1 ^ -h sqlsrvr1 ^ -d ^ mcr.microsoft.com/mssql/server:2019-latest
Either of the above should work via cut-and-paste, but I find it easier to read and visually verify the one-parameter-per-line version.
The basic command (without options, which won't work) is:
docker run mcr.microsoft.com/mssql/server:2019-latest
mcr.microsoft.com/mssql/server:2019-latest
. If that image happens to be on the local machine, great. If not, it will download the image from mcr.microsoft.com
.2019-latest
. Microsoft releases many container images from which you can choose. Find them all on DockerHub. With this tag, are basically saying, get the latest stable release of SQL Server 2019. That basic command will not create a usable sql server container. You need some additional pieces:
-e "ACCEPT_EULA=Y"
- passes "Y" to SQL Server, indicating acceptance of the End User License Agreement (EULA)
-e "SA_PASSWORD=123ABCabc"
- passes the "sa" user password to SQL Server
- you will log in to this server using SQL Authentication, with "sa" as the username, and "123ABCabc" as the password
- I'm using "123ABCabc" because it satisfies the password policy requirements (9 chars, upper and lower, digits"
- "sa" is short for system administrator, and is basically the root user on the server instance that you are running
-p 1401:1433
- maps the container's port 1433 to your machine's port 1401
- SQL Server "listens" for connections on this port by default
- So if you connect to your host machine's port 1401, you will be connected to the container's port 1433
- in my situation, I am running a SQL instance on the host that is already using the default port 1433.
--name sqlsrvr1
- this gives the container your desired name, rather than a docker-generated name
- when issuing commands on the host, you can refer to the container by this name
-h sqlsrvr1
- this sets the HOSTNAME environment variable inside the container, rather than the docker-generated name
- when issuing commands inside the container, you can refer the system inside the container by this name
-d
- this runs the container "detached"
- this means that the container will run in the background rather than the foreground
4. Verify via Docker
mcr.microsoft.com/mssql/server:2019-latest
image. Run this at the command line to see it:docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d325650811b1 mcr.microsoft.com/mssql/server:2019-latest "/opt/mssql/bin/perm…" 43 minutes ago Up 43 minutes 0.0.0.0:1401->1433/tcp sqlsrvr1
Verify via Connection
- ADS is running on the host
- it is connecting to the host IP address (localhost)
- it is connecting to the host Port 1401
- Docker has mapped host port 1401 to the container's port 1433, which is the default port for SQL Server
- So ADS will be connected to the container's port 1433
- Note the Server text box has "url, port" - there's a comma between the url and the port
docker stop sqlsrvr1
docker start sqlsrvr1
docker rm sqlsrvr1