sqlschool.gr logo

articles

Articles of SQLschool.gr Team

How to clone / move logins to another server

Antonios Chatzipavlis
Saturday 20 July 2019

Δεν είναι λίγες οι φορές που χρειάζεται να μεταφέρεις logins από ένα SQL Server σε ένα άλλον. Οι λόγοι είναι αρκετοί για να θέλεις να κάνεις κάτι τέτοιο όπως Database Replication, Always On Availability groups, Log Shipping etc.

Σε κάθε περίπτωση θέλεις να πάρεις από τον primary SQL Server αυτά τα logins που σου χρειάζονται, και στον άλλον, ώστε να μπορούν και από εκεί αυτά να χρησιμοποιούνται.

Αντί να καταφύγουμε σε κάτι περίπλοκο όπως μερικά που είμαι σίγουρος ότι θα έχει βρει στο Internet μπορείτε να εκτελέσετε το παρακάτω query, στον primary, και να πάρετε το αποτέλεσμα του, που δεν είναι άλλο από τα CREATE LOGIN statements για κάθε login έχει o primary, και αυτά απλά να τα εκτελέσετε στο άλλον ή στους άλλους. Φυσικά και μπορείτε να εκτελέσετε μόνο όσα χρειάζεστε. Aν κατά λάθος τα εκτελέσετε όλα μαζί όσα υπάρχουν δεν θα δημιουργηθούν ξανά απλά θα πάρετε πολλά μηνύματα λάθους που θα σας λένε αυτό. Η επιλογή είναι δική σας.

Μερικές τεχνικές λεπτομέρειες καθώς ποτέ δε μου αρέσει να δίνω απλά ένα query ή tip χωρίς να εξηγώ πως αυτό δουλεύει.

Το συγκεκριμένο κάνει χρήση των sys.server_principals, sys_sql_logins, sys.credentials. Δεν θα σας γράψω τι κάνει το κάθε ένα από αυτά καθώς είναι εύκολο να τα βρείτε στα BOL. Θα δείτε ότι υπάρχουν δύο βασικά conditional statements με τα οποία ξεχωρίζω τα windows και sql logins ώστε να διαμορφωθεί κατάλληλα το create login statement. Επειδή δεν με ενδιαφέρουν όλα τα logins στο where φιλτράρω αυτά που με ενδιαφέρουν βγάζοντας εκτός όλα τα systemic logins.

Τέλος θα δείτε ότι στα SQL Logins στο password χρησιμοποιώ το όποιον HASHED καθώς με αυτό μπορώ να έχω το ίδιο password. Δείτε για αυτό στα BOL το εξηγεί εξαιρετικά.

select 'create login ' + QUOTENAME( p.name ) 
        + iif ( p.type IN ('U','G'), ' from windows ', ' '  )  
        + 'with '  
        + iif ( p.type = 'S', 'password = ' + CONVERT(VARCHAR(8000),l.password_hash,1) + ' hashed, '  
        + 'sid = ' + CONVERT(VARCHAR(8000),l.sid,1) 
        + ', check_expiration = ' + iif ( l.is_expiration_checked > 0, 'ON', 'OFF' )  
        + ', check_policy = ' + iif ( l.is_policy_checked > 0 ,'ON, ', 'OFF, ' )
        +  iif (l.credential_id > 0, 'credential = ' + c.name +',',  '' ) , '' ) 
        + 'default_database = ' + p.default_database_name 
        + iif ( len(p.default_language_name) > 0 , ', default_language = ' + p.default_language_name,  '' )
        +';'
from sys.server_principals as p
left join sys.sql_logins as l on p.principal_id = l.principal_id
left join sys.credentials as c on l.credential_id = c.credential_id
where p.type in ('S','U','G')
              and p.name <> 'sa'
              and p.name not like 'NT Authority%'
              and p.name not like 'NT Service%'
              and p.name not like '##%'

//antonch

Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios is a Data Solutions Consultant and Trainer. He has been working in IT since 1988. In his career, he has worked as senior developer, IT Manager, Solutions Architect and IT Consultant. Since 1995 he has been devoted on new technologies and software development tools, mainly by Microsoft, either by training company staff and colleagues or assisting them in design, development and implementation as a consultant or chief developer. He has focused in Databases and Data Science since 1995. He specialized in Microsoft SQL Server since version 6.0 in areas like SQL Server Internals, Database Design and Development, Business Intelligence and in 2010 he has started working with Azure Data Platform, NoSQL databases, Big Data Technologies and Machine Learning. He is an active member of many IT communities in Greece, answering colleagues' questions and writing articles in his web site. He is the owner of SQLschool.gr which is a community portal with a lot of information about Microsoft SQL Server. He has been a Microsoft Certified Trainer (MCT) since 2000. Microsoft honored him as MVP on Data Platform due to his activities in SQL Server since 2010. He holds a large number of Microsoft Certifications and Microsoft SQL Server Certifications since version 6.5.

Episode

Task Flows in Microsoft Fabric

image

More Episodes...

Tip

What's New in SQL Server 2022 - Episodes

More Tips...

Become a member

If you want to receive updates from us become a member to our community.

Connect

Explore

Learn


sqlschool.gr © 2010-2025 All rights reserved

This site uses cookies for operational and analytics purposes only. By continuing to browse this site, you agree to their use.