Hi Salman, you may want to take a look at https://docs.moodle.org/31/en/External_database_enrolment and https://docs.moodle.org/31/en/External_database_authentication
Both of those are built into Moodle and should do most if not all of what you need.