Unable to connect to Azure SQL with a login/user

Michael Payne 121 Reputation points
2020-10-26T10:49:53.4+00:00

I am trying to create a readonly user and login for Azure SQL DB and have followed the instructions below:

https://azure.microsoft.com/en-us/blog/adding-users-to-your-sql-azure-database/

I create the login in the master database, and then create the user in my stats database.

In master Database:
CREATE LOGIN readonlylogin WITH password='1231!#ASDF!a';

In Stats Database:

CREATE USER readonlyuser FROM LOGIN readonlylogin WITH DEFAULT_SCHEMA = CGReport;

I then execute the sp_addrolemember stored procedure:

EXEC sp_addrolemember 'db_datareader', 'readonlyuser';

This works but when I try and connect using SSMS I get the following:

35061-error.png

It appears as if the user is trying to connect to the master database and not my Stats database, however in Azure SQL DB there is no option to specify a default database.

How can I get this to work? We need a secure readonly login as the database will be connected to a website.

Azure SQL Database
{count} votes

Accepted answer
  1. Michael Payne 121 Reputation points
    2020-10-26T13:27:24.947+00:00

    In the Master Database:

    CREATE LOGIN readonlylogin WITH password='1231!#ASDF!a';
    CREATE USER readonlyuser FROM LOGIN readonlylogin WITH DEFAULT_SCHEMA = CGReport;

    In Stats Database:

    CREATE USER readonlyuser FROM LOGIN readonlylogin WITH DEFAULT_SCHEMA = CGReport;

    EXEC sp_addrolemember 'db_datareader', 'readonlyuser';

    From SSMS enter readonlylogin /1231!#ASDF!a as the credentials to my server, then switch to the advanced tab and enter then database name in the connect to database.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anurag Sharma 17,631 Reputation points
    2020-10-26T11:12:30+00:00

    Hi @Michael Payne , welcome to Microsoft Q&A forum.

    The reason for getting this error when trying to connect through SSMS is, you have created the user in 'Stats' Database and not in Master database and by default when you login using SSMS, it connect to master database. there are 2 ways to resolve this.

    1). Provide the default database in SSMS as shown in below screenshot:

    35004-image.png

    2). Create the user in Master database as well and you should be able to connect.

    Please let me know if this helps.

    ----------

    If this resolves your query, please select 'Accept Answer' as it could help other community members looking for similar queries.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.