SQL Server 2012 linked server Application Intent - sql

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

Related

sp_addlinkedsrvlogin doesn't work but sp_addlinkedserver does?

I can add a link server with EXEC sp_addlinkedserver, (With no credentials) successfully, however, I need to provide a username and password to a server if the Windows Authentication doesn't work.
So I have tried using
EXEC sp_addlinkedsrvlogin #rmtsrvname='ServerName',
#useself=N'False',
#locallogin=NULL,
#rmtuser='sa',
#rmtpassword='Password'
Which this error appears
Msg 15015, Level 16, State 1, Procedure sp_addlinkedsrvlogin, Line 49
The server 'ServerName' does not exist. Use sp_helpserver to show
available servers
.
I dont understand as looking online, I have understood the two prodcecures to be the same, but sp_addlinkedsrvlogin takes a username and password and then creates the link server. I guess I have this wrong? So if I have, is there a SQL Server Procedure that will do what I want?
Cheers
I have understood the two prodcecures to be the same
Incorrect. sp_addlinkedsrvlogin is documented as:
Creates or updates a mapping between a login on the local instance of SQL Server and a security account on a remote server.
Nowhere is said that it creates a linked server. In fact the very first argument is properly documented as:
#rmtsrvname Is the name of a linked server that the login mapping applies to
You first create the linked server via sp_addlinkedserver, you then add the linked server login via sp_addlinkedsrvlogin.

Creating a Linked Server and Database Alias

I have two servers Server1 and Server2
On both servers i have a Database called QAI
is there a way that i can create a linked server with an alias from Server2 to Server1 but instead of
Server1.QAI.dbo.tbBlah
Have it aliased as
QAI.dbo.tbBlah
This is needed due to the QAI DB on server2 failing and needing to fall back to the Server1 instance while it is fixed
Thanks
This works. Tested on a SQL2012
http://alexpinsker.blogspot.se/2007/08/how-to-give-alias-to-sql-linked-server.html
There are no problems to add SQL linked server to the specific host
running it. But what if you need to give it an alias, rather than use
hostname as linked server name? Here is how to do it:
1) Step 1:
• In SQL Server Management Studio open Linked Servers and then 'New Linked
Server'.
• Inside of appeared wizard – Select the General tab.
• Specify alias name in "Linked server" field.
• Select SQL Native Client as provider.
• Add sql_server in "Product Name" field (that's the magic).
• In "Data Source" – specify name of the host to be used as linked server.
2) Step 2:
• In Security tab – specify proper security options (e.g. security context)
3) Step 3:
• In Server Options tab – put "Data Access", RPC, "Rpc Out" and "Use Remote Collaboration" to be true.
4) Step 4:
• Enjoy.
If you don't want to specify the database name when querying a table you can create synonym for it.
CREATE SYNONYM [dbo].[DimProduct]
FOR
[AdventureWorksDW].[dbo].[DimProduct]
this will create DimProduct in your Synonyms folder
I'm afraid it's not possible, you'll always have to refer to Server1 explicitly.
But if your application is a on server and not on many clients, so there is a limited number of usage points, you can modify the connection string there, so the app would refer to server1 instead of server2 temporarily.

SQL Server 2008 Error 233

I'm creating new login in SQL Server 2008 with following sql script:
CREATE LOGIN [xyz] WITH PASSWORD='xyz',
DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english],
CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
It creates new login successfully. But when I try to login with it using SQL Server Management Studio it fails saying:
A connection was successfully established with the server, but then an error occurred during the login process. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.) (Microsoft SQL Server, Error: 233)
What's wrong? How do I solve this issue?
Here is how I done it, maybe it works for you too.
login Microsoft SQL Server 2012 with windows authentication.
right-click onto the server name in Object Explorer and click Properties
In the new tab click Security
select SQL Server and Windows Authentication
Ok
Close the SQL server management studio.
start+run
write services.msc
search for SQL there and restart all services.
that works for me.
It's also possible that you're trying to use SQL Server Authentication without having enabled it. To fix this, right-click Properties on your server instance in SQL Server Management Studio, and update the security settings to include "SQL Server and Windows Authentication mode".
Looks like you're trying to connect using named pipes, but SQL Server is not listening on that protocol. See MSDN.
The two fixes MSDN suggests are:
Connect using TCP/IP, or use the SQL
Server Configuration Manager to
enable remote connections using named
pipes.
Using SQL Server Configuration
Manager on the client computer, move
TCP before named pipes in the
protocol order list.
I had a similar issue:
1. log in as the master user or windows authenticated user.
2. right click on the database --> properties --> security -->
3. change Windows Authentication mode to "SQL server and windows authentication mode" by clicking on the radio button. (if it is not)
4. restart the server
I had the same issue when i first setup SQL Server 2014 on my local machine.
In my case the solution was to set a correct defualt database.
Login with Administrator in SQL Server
Go to Securities >> Logins >> select your user name and go to properties
From Status >> uncheck user account lock check box
Change password for the user
Restart the sql server and login with your username.
I was facing the same error.
I've resolved the error by following below mentioned steps:
Disable named pipes and restart sql services.
After restart sql server I enabled names pipes and did a sql server restart again (Link for Step 1 and 2)
Connect to SQL server via studio.
Right click on SQL instance --> Properties --> Connections --> "Set the Maximum number of 5. concurrent connections to '0' ".
Save the change.
Restart the SQL server if possible. (Link for step 3 to 6)
I hope this will help someone
This is might not be a connection issue . Check your default database and if that is online . More commonly this issues seen when the default database will be offline or not exists . If your default database other than master ,better check this option.
I got a way to go around the problem.
Open one instance and login using the windows authentication
allow sql and windows auth both by right cliking on the db server.
Open second instance and login using sql authentication.
bingo the sql authenticated instance open .. :)
Actually in this way we cheat the sql authenticated instance as it tries to find an already running instance.. worked fr me.. good luck
I tried most of the solution but was not able to solve it until I found this URL which says to do the following:
Open SQL Server Management Studio and run the these queries:
sp_configure 'show advanced options', 1;
go
reconfigure
go
sp_configure 'user connections', 0
go
reconfigure
go
The reason why we got this error is that the user connections was reset to 1, so only one user was able to connect with the SQL server.
just a simple query worked for. I hope this will work for others as well.
I have not used the script style, but login through GUI I encountered the same error code. I had entered wrong user name and this is why I was getting the Sql Server, Error: 233. In order to resolve this, you should input the following information:
Server Name: MachineName\SQLEXPRESS
Authentication: SqlServer Authentication
User Name: Assigned user-name or simply sa
Password: xyzpqr
NOTE: Here I have wrote above data for demo purpose only, actual data is your machine & software's properties.
According to: https://msdn.microsoft.com/en-us/library/bb326280.aspx
Go to --> Remot setting
Go to "Remote" tab
in "Remote Assistance", Tick "Allow Remote Assistance connection to this computer", Click the "Advance" button and tick the "Allow..." and in the "Invitation" set the "30 days"
Then in the "Remote Desktop" part
Just tick "Allow remote connection to this computer"
After following the examples here and still not getting in, I found that my sa login was disabled. The following got me in:
Logged back in under windows authentication.
Expanded Security Tab
Expanded Logins Tab
Right-clicked sa and selected Properties
Went to the Status Tab
Under Login: Clicked 'Enabled' radio
Restarted Server and logged in as sa.
This assumes you have set sa password already using
ALTER LOGIN sa WITH PASSWORD = '<enterStrongPasswordHere>' ;
"A connection was successfully established with the server, but then an error occurred during the login process."
I was getting this problem from sqllocaldb when used from within Docker. The problem was the Docker image was not allocated enough memory. Increasing the memory actually fixed the problem.

Create linked server with SQL command

can you help me to create linked server using some SQL command ?
subquestion : How to remove it ?
Check this sp_addlinkedserver command in SQL server to add a linked server
Check sp_dropserver command in SQL server to remove a linked server.
Only the servername is mandatory to be supplied to this command, rest all parameters are optional.
Here is a code example
--local linked server
EXEC sp_addlinkedserver
#server='FUSIONRIS',
#srvproduct='',
#provider='SQLNCLI',
#datasrc='MylocalServerName'
There is a also a GUI in SSMS to set up linked servers. Be sure to create a login -> db user on each server and to create the mapping between these two db users correctly either via your script or in the GUI. Also these users should have the least possible permissions, as when you are using linked servers, you are doubling your attack surface.

Stored Procedure: Linked server login failure

I'm getting "Msg 18456, Level 14, State 1, Line 1
Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'." from when trying to execute the code below. I've change all of the critical information, but you should get the idea.
Are some of my parameter incorrect? The local sql admin username is correct and the remote username and password is correct, but it still keeps telling me that the login failed. Any ideas?
Overall, are there other changes I need to make? Can I insert data this way?
Both DBs are sql server 2005. One is local, one is offsite and accessible via a secure vpn tunnel. I have no trouble acessing the offsite DB using SSMS using the username and password i've been provided(those i've been using in my SP).
-- establish the linked server and login.
EXEC sp_addlinkedserver #server=SERVER1,#srvproduct='',#provider='SQLNCLI', #datasrc='SERVER IP ADDRESS'
EXEC sp_addlinkedsrvlogin SERVER1, 'false', 'LOCAL SQL ADMIN USERNAME', 'REMOTE USERNAME', 'REMOTE PASSWORD'
insert into [SERVER1].DATABASE.dbo.INSERTTABLE(....) select fields from localtable
-- drop the linked server and login
EXEC Sp_DropServer SERVER1, 'droplogins'
I suspect the target server is set to "Windows Authentication" only.
When you try and connect as a SQL login (specified in sp_addlinkedsrvlogin), it tries to interpret the credentials as windows and fails
This error normally occurs when #useself = 'true' for sp_addlinkedsrvlogin and the calling SQL Server is not configured for delegation. The server (not SQL server) can not pass through the Windows credentials.
1) use named parameters for sp_addlinkedsrvlogin for explicitness.
2) use "go" between statements (don't execute the above as a single batch).
3) set "local sql admin username" to null - to rule that out
4) just to note, it appears that remote username/password (if specified) need to be sql-server logins, not NT Network Logins
Start with those...!
Unsure of the exact cause folks. I just moved on a created a Linked Server through the SSMS GUI.