I have been trying to connect SQL database (in Azure) to shinyapp deployed in shinyapps.io, but I could connect to the database from local R console. Please let me know what I am doing wrong?
This is the connection string in Azure SQL DB.
And this is what I am trying from local R console and the connection was successful.
con <- dbConnect(
odbc(),
Driver = "SQL Server Native Client 11.0",
Server = "xxxx.database.windows.net",
Database = "hist_data",
UID = "narendra",
PWD = "xxx",
Port = 1433
)
I had also connected the Database to the local SSMS.
When I deploy the app, getting error:
nanodbc/nanodbc.cpp:1021: 00000: [unixODBC][Driver Manager]Can't open lib 'SQL Server Native Client 11.0' : file not found
I tried to set Driver = "FreeTDS" along with TDS_Version, but it didn't work. However, when I removed the TDS_Version, the app started working on shinyapps.io.
I resolved it by removing "tcp:" from the server name prefix. You also need to adjust the driver name to "SQLServer".
Related
I am working on a standalone desktop application. I am Using an mdf file for the database and I have two connection strings
Server =.\SQLExpress;
AttachDbFilename = C:\MyFolder\MyDataFile.mdf;
Database = dbname;
Trusted_Connection = Yes;
I don't want to use this string because its not necessary that the client will install it on same path
Server =.\SQLExpress;
AttachDbFilename =|Directory\MyDataFile.mdf;
Database = dbname;
Trusted_Connection = Yes;
and this shows
error 40: Unable to connect sql server when installed on clients PC
You need to store in variable entire connection string, it'll work.
If it don't, please share your code that acquire connection.
I tried to create a connection from RStudio to Microsoft SQL 2016, using the following code:
con <- dbConnect(odbc(), .connection_string = "Driver={SQL Server};Server= XXX; Database=XXX;UID=domain\\username;PWD=XXX;TrustServerCertificate=yes")
And get this error:
Error: nanodbc/nanodbc.cpp:950: 28000: [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'domain\username'.
RStudio create values but not a connection to SQL because it doesn't show up in the connection pane.
I am working on a remote desktop, have read rights and Windows Authentification for SQL Server Management Studio. DBI and ODBC package are installed.
I also tried the code WITHOUT UID and PW but then i got this error:
...Cannot open database "XXX" requested by the login. The login failed.
Then i tried this:
con <- dbConnect(odbc::odbc(), .connection_string = "Driver={SQL Server}, Server=XXX,Database=XXX,Port=1433")
And get this Error:
...Invalid connection string attribute.
Do i need more than read rights or do i need Windows AND/OR SQL Authentification?
Or do you think there is something wrong with the codes I am using?
So my Shinyapp works great locally connecting to my Azure Server housing my SQL database using this connection:
con <- DBI::dbConnect(odbc::odbc(),
driver="SQL Server",
database="<db>",
UID="<user#db>",
PWD="<password>",
server="tcp:<user>.database.windows.net",
port=1433)
When publishing, I get this error:
nanodbc/nanodbc.cpp:950: 01000: [unixODBC][Driver Manager]
Can't open lib 'SQL Server' : file not found
http://docs.rstudio.com/shinyapps.io/applications.html suggests to use SQLServer instead of SQL Server but the connection breaks down (both from locally and when publishing).
I've also tried the RODBC but this has not worked either.
I'm a bit stumped on what driver to use to make the connection work. Any help would be greatly appreciated!
Using the SQL Server Management Studio (SSMS) Express, I can find the database and connect without problems.
But when I use pyodbc to connect to the same server using:
import pyodbc
Server = r"xxxER\xxxSQLSERV"
db = "xxxDB"
user = "xxx"
password = "xxxx"
conn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server + ';DATABASE=' + db +';UID=' + user + ';PWD=' + password)
By Using Pyhton in my local i am able to connect but when i am trying in linux server getting below error
pyodbc.OperationalError: ('HYT00', u'[HYT00] [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
i tried with ODBC Driver 17 for SQL Server too but facing the same issue.Can any one please suggest me on this.
Microsoft's SQL Server ODBC drivers for Linux are unable to resolve instance names. You can use the free sqlserverport module to get the corresponding port number and use that to connect.
Try removing the instance from server and set it as
Server = "xxxER"
or if you have a port number append it as
Server = "xxxER,portNo"
I have this problem and solved my problem in here.
Just add version.
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server + ';DATABASE=' + db +';UID=' + user + ';PWD=' + password)
If not work change version 17 to 13 if not to 11 .
List versions of ODBC.
Check your mssql network config, my problem was there.
Enable port 1433 for all IPs.
Now is working with IP and host name.
I am using SSH Tunnel with remote server port forward to localhost,1433. This pyodbc connection intermittently fails if using the approach:
cnxn = pyodbc.connect('driver={ODBC Driver 17 for SQL Server};Server=localhost, 1433;'...
Replacing localhost with 127.0.0.1 seems to be working all the time
cnxn = pyodbc.connect('driver={ODBC Driver 17 for SQL Server};Server=127.0.0.1, 1433;'
Incidentally also did same using Ruby and FreeTDS, and Localhost,1433 worked all the time.. Must be something inside the pyodbc package or their approach that sometimes drop some info to make the DBMS on server fail to respond/timeout
This worked for me:
mssql://DRIVER={ODBC Driver 17 for SQL Server};SERVER={server_address,PORT};DATABASE=<my_database>;UID=<my_username>;PWD=<my_password>
I need to create a linked server against a SQL Server 2012 Availability Group and I want to have all requests routed to the read only replica. However, I have been unable to determine how I can specify the ReadOnly Application Intent in order to ensure that the request is routed to the correct replica.
Has anyone sucessfully configured a linked server in this manner?
I have tried both methods and the below (from the Microsoft tech site) works
EXEC sp_addlinkedserver
#server = N'linked_svr',
#srvproduct=N'SqlServer',
#provider=N'SQLNCLI11',
#datasrc=N'AG_Listener_Name',
#provstr=N'ApplicationIntent=ReadOnly',
#catalog=N'MY_DB_NAME';
When testing a Linked Server connection to the database I found that I was still hitting the primary database even when specifying ApplicationIntent=ReadOnly in the connection parameters.
After further investigations I found that the root cause for this was because the default database associated with that login was set to "master". This can be tested by running the following query:
sp_helplogins
To avoid this issue I now use the following connection parameters to ensure I am connecting to the database replica:
ApplicationIntent=ReadOnly;Database=database-db
Also, when connecting to a database via a linked server, please be sure to use the following query format:
SELECT * FROM [server].[database].[scheme].[table]
I Don't have an AlwaysOn availability group to test this on, but you can specify a connection string when setting up a linked server via sp_addlinkedserver.
SQL Server 2012 accepts the following and successfully creates a linked server that works, whether it honours the ApplicationIntent property you will have to test, but it should do as it is set to use the native client as a provider:
sp_addlinkedserver
#srvproduct = 'DB', --Don't use 'SQL Server' otherwise it won't let you set any properties
#server = 'LINKED-SERVER-NAME',
#provider = 'SQLNCLI',
#provstr = 'Data Source=SERVER\INSTANCE;Initial Catalog = Database;ApplicationIntent=ReadOnly'
https://learn.microsoft.com/en-us/windows-server/networking/core-network-guide/cncg/server-certs/create-an-alias-cname-record-in-dns-for-web1
I have used CNAME for READONLY NODE.
sql AO or windows cluster its very useful for readonly servers.
EX: Your second node server name is SERVERB, you redirect it to alias name as below.
SERVERB ----- >SERVERB.corp.contoso.com.
Then from the SSMS check if you can connect, or ping the alias name from the command promt screen if it works.
ping SERVERB.corp.contoso.com