How to connect sql server using docker on mac? - sql-server

I am trying to connect to sql server using docker. I have successfully enabled the container using this command
sudo docker run -d --name aakash -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Maximus6hero#" -p 1434:1434 microsoft/mssql-server-linux
docker ps
3e41723b93d5 microsoft/mssql-server-linux "/opt/mssql/bin/sqls…" About an hour ago Up About an hour 1433/tcp, 0.0.0.0:1434->1434/tcp aakash
It shows that my container is running.
But when i try to connect using db visualizer it throws an error.
The TCP/IP connection to the host localhost, port 1434 has failed. Error: "The driver received an unexpected pre-login response. Verify the connection properties and check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. This driver can be used only with SQL Server 2000 or later.". ClientConnectionId:6a802ae0-7203-411d-a599-4c1de997d843.
I also have windows pc with me, and i can connect easily using sql server management studio. Even i can connect others pc using that windows pc. But when i try to connect my mac to windows pc using its ip it also gives the same error.
I have enabled tcp/ip connection on windows using sql server configuration manager. But there is no such thing on mac.

You might be getting this error on your MAC because when you run the docker in the background, it uses only 2GB of memory by default which is insufficient to run the SQL server as it needs minimum of 3.25 GB and ideally, we should point it to 4GB of memory. Update the preferences section with above details on your docker. Save and restart the docker and you may check the below steps to see if that might help you for your MAC. I did this using Azure Data Studio and Docker.
Once you have Saved and restart the docker with 4GB of Memory Allocation for docker to run, all you'd need to do is pull the docker image of the sql server and download it. this can be done by below commands on your terminal . FYI, I am using bash commands below:
Command 1:
sudo docker pull mcr.microsoft.com/mssql/server:2017-latest
This will pull the latest vesion docker image and download. Once done, you need to set your SQL authentication on the server for your database. Follow below commands:
Command 2:
sudo docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=<SetYourPasswordHere>' \
-p 1433:1433 --name sql1 \
-d mcr.microsoft.com/mssql/server:2017-latest
This sets your password and uses the port 1433 for SQL server (which is the default port). To confirm if the image has been created and the SQL server is running on docker, execute the below command to check log(s).
Command 3:
docker ps
To check all instances in your history of dockers( i.e. if you already had dockers installed before you are attempting this SQL connection/execution), run the below command and it will give you all the logs of all instances you have created
Command 4:
docker ps -a
or
docker ps -all
Once, you have completed above steps and see that the docker has created SQL instance, you need to go to Azure Data Studio and set the below credentials to access the server that you just created above using Docker.
Server: localhost
Authentication Type: SQL Authentication
Username: sa
Password: <Check Command 2 to see what you entered in the password where it says SetYourPasswordHere>
Hope this helps in your tryst with running SQL server on your MAC. All the Best!

Related

Is it possible to connect with an Oracle Docker container with an ODBC?

I am using docker images from this github: https://github.com/oracle/docker-images
I installed oracle db version 19c in my environment with the following command:
docker run --name oracle19c --network host -p 1521:1521 -p 5500:5500
-v /opt/oracle:/u01/oracle oracle/database:19.3.0-ee
Then I connect to it to run sqlplus with:
docker exec -ti oracle19c sqlplus system/oracle#orclpdb1
SQL>
Then I managed to successfully load some data into it. However, I would like to connect to the database with an ODBC connection. However, I am confused on going about and connecting to the database inside docker, I have not been able to find any resources or guides. The server runs Ubuntu and I am connected over an ssh connection if that helps.
Edit:I tried with unixodbc but I would be willing to switch over to any other available options.

Can't Connect "SSMS on Windows" With "SQLServer on Linux" Container On Docker

I've pulled latest SQL Server docker image
docker pull mcr.microsoft.com/mssql/server
Then started with port 1400.
(I'm not using default port 1434 because I've a local instance of SQLServer running outside of docker which my SSMS can quickly connect)
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=pa$$word#123" -e "MSSQL_PID=Express" --name "SQLServer" -p 1400:1400 -d mcr.microsoft.com/mssql/server
This is my container info
When I check the logs everything looks normal. But when I try to connect from SSMS, I'm getting the following error.
What could be the reason? I'm also planning to build a .NET Core application to connect to this docker image but now I've concerns regarding if it's some TLS version mismatch.
I found the solution. I need to specify the instance name in SSMS in order to connect to it

Connecting SQL Server in a Docker image from SSMS (Win 10) throws an error 'An existing connection was forcibly closed by the host'

The environment is a single machine trying to connect to SQL Server 2019 in a Docker image (mcr.microsoft.com/mssql/server:2019-latest). The first time I tried this was with this tutorial:
tomssl and the connection using SSMS worked. But I need SQL Server 2019, so I tried the 2019-latest image, and the connection closed issue is now permanent.
If I remove the port number then the password error appears.
The docker image:
docker pull mcr.microsoft.com/mssql/server:2019-CU8-ubuntu-16.04
The run command from docker:
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Simp$on9432' -p 1405:1433 --name sql19-25 -v D:\Docker:/sql -d mcr.microsoft.com/mssql/server:2019-CU8-ubuntu-16.04 sleep infinity
The sleep infinity was added to keep the image running.
I have tried to do the 2017 version again, but the same error appears.
The command was run in PowerShell 7.1.0.
OS Name Microsoft Windows 10 Pro, version 10.0.19042 Build 19042
Docker version 20.10.0, build 7287ab3
WSL 2 installed
Storage Driver: overlay2
Through SSMS (v18.8), I can connect to SQLEXPRESS. Also I have tried stopping the SQLEXPRESS, but still get the same error message.
Suggestions?

How to connect to a SQL Server database inside a docker container?

I've created a docker container using the following command:
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=MyStr#ngPassw0rd' -e 'MSSQL_PID=Express' -p 1433:1433 -d microsoft/mssql-server-linux:latest
I'd like to use SSMS to connect to this database from my host machine.
Which parameters should I use in the SSMS login dialog?
My current entries:
Server type: Database Engine
Server name: [machine-name]
Authentication: SQL Server authentication
Login: sa
Password: MyStr#ngPassw0rd
But I'm getting this error:
TITLE: Connect to Server
Cannot connect to localhost:.
ADDITIONAL INFORMATION:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 53)
network path not found
Seems I'm not able to 'see' the database.
Is there something wrong with my connection string or is it not possible to connect this way?
If you are using Docker CE on Windows with Linux containers, specify double-quotes around the environment variables so that they are recognized:
docker run -e "ACCEPT_EULA=Y" -e 'SA_PASSWORD=MyStr#ngPassw0rd" -e "MSSQL_PID=Express" -p 1433:1433 -d microsoft/mssql-server-linux:latest
You will then be able to connect using SSMS. With the default 1433 port in this case, you can specify . or localhost as the server name. You can also connect to the container instance from a remote host specifying only your machine name (assuming firewall rules allow incoming port 1433 traffic).
With single-quotes, the values are not recognized and the container will immediately stop. You can use docker logs <container-name> to view messages from the SQL Server error log for troubleshooting. Below is an example using an explict container name (which I recommend to facilitate tasks like this) using your original problem command:
docker run --name sql1 -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=MyStr#ngPassw0rd' -e 'MSSQL_PID=Express' -p 1433:1433 -d microsoft/mssql-server-linux:latest
c3de002fc22106e53c9b165c7f444905b5ef1bb1596eddf1b097cdd6d60e6c75
docker logs sql1
The SQL Server End-User License Agreement (EULA) must be accepted
before SQL Server can start. The license terms for this product can be
downloaded from http://go.microsoft.com/fwlink/?LinkId=746388.
You can accept the EULA by specifying the --accept-eula command line
option, setting the ACCEPT_EULA environment variable, or using the
mssql-conf tool.
As you can see from the logged message, the SQL instance stopped (stopping the container) because the SQL Server did not recognize the EULA was accepted due to the single quotes.

How to connect Docker with Azure Data Studio?

I install docker container on mac(OS X) and install Microsoft SQL 2017 image file on docker.So, I try to connect docker with Azure Data Studio but didn't connect it. Can I connect docker with Azure Data Studio and How to configure it? Please help me, thank a lot.
Use 127.0.0.1,1433 instead of 127.0.0.1:1433
This syntax is what my ASP.NET Core app uses as syntax so I figured MS liked that format for connection strings and such.
This worked for me. Hope it helps.
I was able to run SQL server on MAC using Docker by running it along with the Azure Data Studio.
In order to connect to a server, you need to go to preferences of your Docker settings and increase the Memory allocation from the default of 2GB to minimum 4GB (as SQL server needs min 3.25GB space). Save and restart the docker.
Once restarted, all you need to do is pull the docker image of the sql server and download it. this can be done by below commands on your terminal . FYI, I am using bash commands below:
Command 1:
sudo docker pull mcr.microsoft.com/mssql/server:2017-latest
This will pull the latest vesion docker image and download. Once done, you need to set your SQL authentication on the server for your database. Follow below commands:
Command 2:
sudo docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=<SetYourPasswordHere>' \
-p 1433:1433 --name sql1 \
-d mcr.microsoft.com/mssql/server:2017-latest
This sets your password and uses the port 1433 for SQL server (which is the default port). To confirm if the image has been created and the SQL server is running on docker, execute the below command to check log(s).
Command 3:
docker ps
To check all instances in your history of dockers( i.e. if you already had dockers installed before you are attempting this SQL connection/execution), run the below command and it will give you all the logs of all instances you have created
Command 4:
docker ps -a
or
docker ps -all
Once, you have completed above steps and see that the docker has created SQL instance, you need to go to Azure Data Studio and set the below credentials to access the server that you just created above using Docker.
Server: localhost
Authentication Type: SQL Authentication
Username: sa
Password: <Check Command 2 to see what you entered in the password where it says SetYourPasswordHere>
Hope this helps in your tryst with running SQL server on your MAC. All the Best!
You certainly can connect to a sql server image running in a docker container through azure data studio,
Based on the details mentioned in the question, I'm assuming that you have followed the steps on Microsoft docs for configuring sql server with docker,
The following command is needed to configure and run the SQL Server image docker container:
sudo docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=your-strong-password’ -p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-latest;
To quickly verify
check that the image is running by running:
docker ps -a
And checking the status column (with the correct instance name) to be 'UP',
Then launch Azure Data Studio and fill the connection details:
If you have followed all the default settings in setting up the image, this should work for you,
Hope this helps,
I hope first you have installed sql-cli(make sure you have node.js installed in your system),
Then connect to Mssql with command -> mssql -u -p
try to connect/create a database with docker first then connect from Azure Data Studio

Resources