Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 107

MySQL Users और Privileges

MySQL Account Identity समझें

MySQL account 'user'@'host' है। दोनों parts account पहचानते हैं। Host वह जगह है जहाँ से client connect करता है।

SELECT USER() AS client_identity,
 CURRENT_USER() AS matched_account,
 CURRENT_ROLE() AS active_roles;
AccountUse
'school_app'@'localhost'DB host पर app
'school_app'@'10.20.30.%'Approved private subnet
'school_app'@'%'Any host; usually too broad

Host explicit रखें। Omit करने पर % default होता है। Account restriction के साथ network controls भी रखें।

Separate identities: web app, reporting, backup, migration और admin के अलग accounts हों। Shared account accountability खत्म करता है।

Secure User Deliberately बनाएँ

CREATE DATABASE IF NOT EXISTS school_app
 CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
CREATE USER 'school_reader'@'10.20.30.%'
 IDENTIFIED BY RANDOM PASSWORD REQUIRE SSL
 COMMENT 'Read-only school reporting service';
GRANT SELECT ON school_app.*
TO 'school_reader'@'10.20.30.%';
SHOW GRANTS FOR 'school_reader'@'10.20.30.%';

Random secret authorized administrator को मिलता है; उसे vault में transfer करें, source/chat/history में नहीं। New user के privileges नहीं होते, इसलिए creation और authorization separate हैं।

REQUIRE SSL encryption require करता है, लेकिन client server identity verify भी करे—trusted CA के साथ VERIFY_IDENTITY use करें।

Smallest Useful Privilege Scope

ScopeSyntaxRisk
GlobalON *.*हर schema; admin only
DatabaseON school_app.*Schema के सभी objects
TableON school_app.studentsOne table
ColumnSELECT(student_id,name)Narrow, maintenance ज्यादा
RoutineON PROCEDURE school_app.close_termControlled operation

Attendance writer को attendance tables पर SELECT,INSERT,UPDATE चाहिए—DROP, FILE, user management या every database नहीं। Shortcut में ALL PRIVILEGES न दें। Schema names में _/% हों तो grant wildcard behavior carefully review करें।

Reusable Roles Lab

CREATE ROLE IF NOT EXISTS
 'school_read_role'@'%','school_write_role'@'%';
GRANT SELECT ON school_app.* TO 'school_read_role'@'%';
GRANT SELECT,INSERT,UPDATE ON school_app.attendance
TO 'school_write_role'@'%';
CREATE USER 'attendance_app'@'10.20.30.%'
 IDENTIFIED BY RANDOM PASSWORD REQUIRE SSL;
GRANT 'school_write_role'@'%'
TO 'attendance_app'@'10.20.30.%';
SET DEFAULT ROLE 'school_write_role'@'%'
TO 'attendance_app'@'10.20.30.%';

Role named privilege collection है। Grant और default activation अलग operations हैं। Existing session में SET ROLE DEFAULT; और CURRENT_ROLE() check करें। Expected: attendance read/insert/update allowed, DROP और unrelated schema denied।

Effective Access Inspect और Test करें

SHOW GRANTS FOR 'attendance_app'@'10.20.30.%';
SHOW GRANTS FOR 'school_write_role'@'%';
SHOW CREATE USER 'attendance_app'@'10.20.30.%';
SELECT USER(),CURRENT_USER(),CURRENT_ROLE();

SHOW GRANTS direct privileges/role assignment दिखाता है; role privileges के लिए role अलग inspect करें।

  1. Application वाले host/network से connect करें।
  2. TLS और CURRENT_USER verify करें।
  3. Default role activate करें।
  4. Expected allowed read/write चलाएँ।
  5. DROP/other schema जैसा denied test करें।
  6. Owner, date और approved diff record करें।

Username से access infer न करें; host match, active role, definers, views और routines behavior बदलते हैं।

Complete Account Lifecycle

ALTER USER 'attendance_app'@'10.20.30.%' ACCOUNT LOCK;
ALTER USER 'attendance_app'@'10.20.30.%' ACCOUNT UNLOCK;
ALTER USER 'attendance_app'@'10.20.30.%'
 IDENTIFIED BY RANDOM PASSWORD;
DROP USER 'attendance_app'@'10.20.30.%';

Owner, purpose, ticket, creation, review/expiry और rotation procedure रखें। Investigation में पहले lock करें। Drop से पहले jobs, pools, replicas और account को DEFINER बनाने वाले objects check करें; orphan definers views/routines/triggers/events तोड़ सकते हैं।

mysql.user को direct edit न करें। CREATE/ALTER USER, GRANT/REVOKE और DROP USER use करें।

TLS, Locking और Resource Controls

ALTER USER 'report_user'@'10.20.30.%'
 REQUIRE SSL
 WITH MAX_USER_CONNECTIONS 5 MAX_QUERIES_PER_HOUR 5000
 FAILED_LOGIN_ATTEMPTS 5 PASSWORD_LOCK_TIME 2;

Limits accidental overload घटाते हैं, optimization/pool limits replace नहीं करते। Failed-login tracking temporarily lock कर सकता है; deployed authentication/recovery पर test करें।

  • Managed certificate design में REQUIRE X509 use करें।
  • Appropriate server पर require_secure_transport enable करें।
  • Clients certificate/hostname verify करें।
  • Password expiry केवल policy/risk में रखें।
  • Break-glass accounts protect, monitor और test करें।

Account Security Checklist

  1. हर account का owner/purpose।
  2. Explicit narrow host।
  3. Application root/human admin use न करे।
  4. Secrets vault में और code-free rotation।
  5. TLS + server identity verification।
  6. Minimum roles; unjustified FILE/GRANT OPTION नहीं।
  7. Intentional, tested default roles।
  8. Direct/role grants regular review।
  9. Inactive accounts safely lock/remove।
  10. Secure account/role recovery plan।

GRANT/REVOKE, security और recovery पढ़ें।

Official संदर्भ

Account defaults, host identity, role activation और locking official MySQL 8.4 manual से verified हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

MySQL में user@host का क्या अर्थ है?
MySQL account user name और allowed client host का combination है। school_app@localhost और school_app@10.20.30.% अलग authentication/privileges वाले अलग accounts हैं।
क्या CREATE USER database access भी देता है?
नहीं। Newly created account के पास कोई privilege/default role नहीं होता जब तक specify न करें। बाद में केवल required role या object privileges दें।
MySQL roles क्यों use करें?
Roles named privilege collections हैं। Assignment और review consistent होते हैं, लेकिन granted role activate करना पड़ता है; future sessions के लिए SET DEFAULT ROLE common तरीका है।
क्या application को MySQL root से connect करना चाहिए?
नहीं। हर workload के लिए narrow host, TLS और minimum schema actions वाला dedicated account रखें। Administrative accounts अलग रहें।
Current session के effective privileges कैसे देखें?
CURRENT_USER(), CURRENT_ROLE() और SHOW GRANTS देखें। Application की same connection settings से allowed और denied operations भी test करें।
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.