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

MySQL में GRANT और REVOKE

GRANT और REVOKE Authorization Changes हैं

GRANT privileges/roles assign और REVOKE remove करता है। Current MySQL में account पहले create करें:

CREATE USER 'teacher_app'@'10.20.30.%'
 IDENTIFIED BY RANDOM PASSWORD REQUIRE SSL;
GRANT SELECT ON school_app.students
TO 'teacher_app'@'10.20.30.%';
REVOKE SELECT ON school_app.students
FROM 'teacher_app'@'10.20.30.%';

User/host separately quote और exact host लिखें। localhost और subnet अलग accounts हैं।

Change model: request → owner approval → SQL review → controlled apply → SHOW GRANTS diff → allowed/denied tests → evidence और rollback.

Safe Least-Privilege Lab

CREATE DATABASE IF NOT EXISTS school_app;
CREATE TABLE IF NOT EXISTS school_app.attendance (
 attendance_id BIGINT PRIMARY KEY AUTO_INCREMENT,
 student_id INT NOT NULL,attendance_date DATE NOT NULL,
 status ENUM('P','A','L') NOT NULL,
 UNIQUE KEY uq_attendance(student_id,attendance_date)
);
CREATE USER 'attendance_demo'@'localhost'
 IDENTIFIED BY RANDOM PASSWORD REQUIRE SSL;
GRANT SELECT,INSERT,UPDATE ON school_app.attendance
TO 'attendance_demo'@'localhost';
SHOW GRANTS FOR 'attendance_demo'@'localhost';

Account attendance rows maintain कर सकता है, पर table drop, users create, unrelated schema read या server files access नहीं। Lab isolated server पर चलाएँ। Initial grants baseline save करें।

Correct Privilege और Scope चुनें

GRANT SELECT ON school_app.* TO 'reporter'@'localhost';
GRANT SELECT,INSERT ON school_app.attendance
TO 'attendance_demo'@'localhost';
GRANT SELECT(student_id,attendance_date,status)
ON school_app.attendance TO 'auditor'@'localhost';
GRANT EXECUTE ON PROCEDURE school_app.close_attendance_day
TO 'operator'@'localhost';
NeedPreferAvoid
One table readTable SELECTGlobal SELECT
Approved operationRoutine EXECUTEBroad writes
Client CSVLOCAL/stagingWeb app FILE
MigrationTemporary identityPermanent runtime DDL

Dynamic admin privileges और ON *.* special review माँगते हैं।

Privileges Role को, फिर Role User को दें

CREATE ROLE IF NOT EXISTS
 'attendance_read'@'%','attendance_write'@'%';
GRANT SELECT ON school_app.attendance
TO 'attendance_read'@'%';
GRANT SELECT,INSERT,UPDATE ON school_app.attendance
TO 'attendance_write'@'%';
GRANT 'attendance_write'@'%'
TO 'attendance_demo'@'localhost';
SET DEFAULT ROLE 'attendance_write'@'%'
TO 'attendance_demo'@'localhost';

Privilege GRANT में ON, role GRANT में नहीं। One statement privileges और roles mix नहीं कर सकता। Future sessions के लिए default role, current session में SET ROLE रखें। Deep role graph से बचें।

Direct, Role और Effective Access Verify करें

SHOW GRANTS FOR 'attendance_demo'@'localhost';
SHOW GRANTS FOR 'attendance_write'@'%';
SELECT USER(),CURRENT_USER(),CURRENT_ROLE();
SET ROLE DEFAULT;

-- Succeed होना चाहिए
SELECT COUNT(*) FROM school_app.attendance;
-- Fail होना चाहिए
DROP TABLE school_app.attendance;

Allowed test useful access और denied test boundary prove करता है। Production जैसे connector, host, TLS और role behavior से test करें। Views, routines और DEFINER/INVOKER extra paths बना सकते हैं।

Exact Assignment Safely Revoke करें

REVOKE INSERT,UPDATE ON school_app.attendance
FROM 'attendance_write'@'%';
REVOKE 'attendance_write'@'%'
FROM 'attendance_demo'@'localhost';
ALTER USER 'attendance_demo'@'localhost' ACCOUNT LOCK;

REVOKE ALL PRIVILEGES, GRANT OPTION
FROM 'legacy_app'@'localhost';
DROP USER 'legacy_app'@'localhost';

Role revoke करने से role/direct grants नहीं हटते। One role से privilege हटने पर other role का same privilege रह सकता है। User और हर role का SHOW GRANTS फिर check करें।

Revoke connection pools, jobs, views/routines तोड़ सकता है। Stage, monitor और reviewed rollback रखें।

GRANT OPTION और ADMIN OPTION High Risk हैं

GRANT SELECT ON school_app.*
TO 'data_lead'@'localhost' WITH GRANT OPTION;
GRANT 'attendance_read'@'%'
TO 'team_lead'@'localhost' WITH ADMIN OPTION;

GRANT OPTION privilege और ADMIN OPTION role delegation देता है। Normal application accounts में न दें। Named admin, business reason, narrow scope, regular review और nonproduction revoke test रखें। Delegation capability को read/write जितनी गंभीरता से audit करें।

Production Authorization Workflow

  1. Exact account, host, owner, expiry पहचानें।
  2. Required actions/objects list करें।
  3. Reviewed capability role prefer करें।
  4. Apply SQL और reverse rollback बनाएँ।
  5. Wildcards/global/delegation peer-review करें।
  6. Verified TLS से controlled admin apply करे।
  7. SHOW GRANTS before/after compare करें।
  8. Reconnect करके allowed/denied test करें।
  9. Errors/business flow monitor करें।
  10. Stale access revoke करें।

GRANT/REVOKE के बाद FLUSH PRIVILEGES नहीं चाहिए। Grant tables direct edit न करें। users और security पढ़ें।

Official संदर्भ

Syntax, scope, roles और delegation official MySQL 8.4 manual से verified हैं।

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

क्या GRANT MySQL user automatically बनाता है?
Current MySQL में नहीं। CREATE USER से authentication, host और TLS सहित account बनाएँ, फिर privileges या roles grant करें।
GRANT या REVOKE के बाद FLUSH PRIVILEGES चाहिए?
नहीं। Account-management statements grant system update करते हैं। Grant tables का direct edit discouraged है और normal workflow नहीं होना चाहिए।
WITH GRANT OPTION क्या है?
यह recipient को relevant scope के specified privileges दूसरे accounts को देने देता है। यह administrative delegation है, इसलिए rare, documented और reviewed हो।
User से role revoke करने पर क्या होता है?
Role assignment हटती है; केवल उस role से inherited privileges unavailable होते हैं। Direct grants या दूसरे roles के privileges रहते हैं।
REVOKE सफल हुआ कैसे verify करें?
Account और relevant roles के SHOW GRANTS देखें, reconnect/roles activate करें, फिर expected allowed और expected denied operation test करें।
🔗

Share this topic with a friend

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

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

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

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

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