Add Logins & Users to MS SQL Server
Overview
Logins can connect to the SQL server and have limited access to the master database. Users are specific to each database and must be separately created in each database to grant them access.
The login is a server-level principle and the user is a database-level principal.
Add Logins & Users
Windows
Connect to SQL server through RDP SSMS.
In the example below, the server name islocalhost(since the machine is the host) and authentication method isWindows, so no further input was needed.Create a new login.
In the
Object Explorer, expandSecurityand right-click onLogins.Click on
New Login.If using
Windows authentication, clickSearch. Then type and add authorizedLogin name.
If usingSQL Server authentication, setLogin nameand enterpassword.Note:
User must change password at next loginis selected by default.Then click
Ok.
Create a new database user.
In the
Object Explorer, expand theDatabasesfolder.Expand the database in which to create the new database user.
Right-click the
Securityfolder, right-click theUsersfolder then chooseNew User.Input the desired Username.
Click on the ellipses
(…)to select the Login Name.In the
Select Loginwindow, type the login to be associated to. Confirm by clickingCheck NamesorBrowse, then clickOk.Then click
Okagain to finish the new database user creation.
Linux (Ubuntu 16.04 or 18.04)
Connect by running the following command, where
servernameis the SQL server name (orlocalhostfor local connection) andsais the username. Notesais the default admin username.sqlcmd -S servername -U saCreate a new login.
CREATE LOGIN MyLogin WITH PASSWORD = 'Password12'; GOwhere
MyLoginis the login name andPassword12is the login password.
Create a new database user.
USE database_name; GO CREATE USER newuser FOR LOGIN MyLogin; GOwhere
newuseris the newdatabase_nameuser for the loginMyLogin.
[Optional] You can authorize other logins to create more logins and inside a database you can authorize other users to create more users.
GRANT ALTER ANY LOGIN TO MyLogin; GO USE database_name; GO GRANT ALTER ANY USER TO newuser; GOThe login
MyLogincan create more logins and the usernewusercan create more users within the databasedatabase_name.
Add Admin Logins & Users
Windows
Connect to SQL server through RDP SSMS.
In the example below, the server name islocalhost(since the machine is the host) and authentication method isWindows, so no further input was needed.Create a New Login.
In the
Object Explorer, expandSecurityand right-click onLogins.Click on
New Login.In the
Generalpage:set
Login nameselect
SQL Server authenticationenter
password
Note:User must change password at next loginis selected by default.
Select the
Server Rolespage in the left-menu, and then check thesysadmincheck box in theServer roleslist.Select the
User Mappingpage. Here, select which databases users associated to this login will have permissions to.In the
Mapcolumn, check the box for the database that the new login can access.
By default, the login name appears in the User column.In the
Database role membershipfor list, check thedb_ownerbox.Click OK.
Linux (Ubuntu 16.04 or 18.04)
Connect locally by running the following command, where
servernameis the SQL server name (orlocalhostfor local connection) andsais the username. Notesais the default admin username.sqlcmd -S servername -U saCreate a New Login and assign admin role to it.
CREATE LOGIN MyLogin WITH PASSWORD = 'Password12'; GO ALTER SERVER ROLE [sysadmin] ADD MEMBER MyLogin; GOwhere
MyLoginis the login name andPassword12is the login password.
Create a new database user.
USE database_name; GO CREATE USER newuser FOR LOGIN MyLogin; GO ALTER USER newuser WITH DEFAULT_SCHEMA=[dbo]; ALTER ROLE [db_owner] ADD MEMBER newuser; GOwhere
newuseris the newdatabase_nameuser for the loginMyLogin.