MySQL study notes 07 [transactions, user management and authority management]

  • MySQL Document-Dark Horse Programmer (Tencent Weiyun): https://share.weiyun.com/RaCdIwas
  • 1-MySQL foundation.pdf, 2-MySQL constraints and design.pdf, 3-MySQL multi-table query and transaction operation.pdf
  1. MySQL study notes 01 [database concept, MySQL installation and use] [day01]
  2. MySQL study notes 02 [SQL basic concepts and general grammar, database CRUD operations] [day01]
  3. MySQL study notes 03 [CRUD operations of database tables, basic operations recorded in database tables, client-side graphical interface tool SQLyog] [day01]

  4. MySQL study notes 04 [database query operations, table constraints] [day01, day02]

  5. MySQL study notes 05 [multi-table operation, three paradigms, database backup and restore] [day02]

  6. MySQL study notes 06 [multi-table query, sub-query, multi-table query exercises] [day03]

  7. MySQL study notes 07 [transaction, user management and authority management] [day03]

table of Contents

15 affairs

Business_Basic Demo

Transaction_default auto commit & manual commit

Transaction_Four characteristics of transaction

Transaction_Transaction isolation level introduction

Transaction_transaction isolation level demonstration 1

Transaction _ transaction isolation level demo 2

16 User Management and Authority Management

DCL_Manage Users_Add, Delete and Check

Create user

delete users

DCL_Manage User_Change Password

Change password method 1

Change password method 2

Forgot the password of the root user in mysql?

DCL_Administrative authority

View user permissions

Grant user permissions

Grant users multiple permissions

Grant all permissions to the user

Revoke authority


15 affairs

Business_Basic Demo

1. Basic introduction of transaction
    1. Concept:
        * If a business operation containing multiple steps is managed by a transaction, then these operations either succeed or fail at the same time.        
    2. Operation:
        1. Start transaction: start transaction;
        2. Rollback: rollback;
        3. Submit: commit;
    3. Example:
        CREATE TABLE account (
            id INT PRIMARY KEY AUTO_INCREMENT,
            NAME VARCHAR(10),
            balance DOUBLE
        );
        -
        -Add data INSERT INTO account (NAME, balance) VALUES ('zhangsan', 1000), ('lisi', 1000);
        SELECT * FROM account;
        UPDATE account SET balance = 1000;
        - Zhang San transfers 500 yuan to Li Si
        - 0. Start transaction
        START TRANSACTION;
        - 1. Zhang San account-500
        
        UPDATE account SET balance = balance-500 WHERE NAME ='zhangsan';
        - 2. Li Si account +500
        - something went wrong...
        UPDATE account SET balance = balance + 500 WHERE NAME ='lisi';
        
        - Found that there is no problem with the execution, commit the transaction
        COMMIT;
        
        - Found a problem, roll back the transaction
        ROLLBACK;

Transaction_default auto commit & manual commit

    4. Transactions in MySQL database are automatically committed by default
        * There are two ways to commit transactions:
            * Auto-commit:
                * mysql is automatically submitted
                * A DML (addition, deletion, modification) statement will automatically commit a transaction.
            * Manual submission:
                * Oracle database commits the transaction manually by default.
                * You need to start the transaction before submitting it.
        * Modify the default commit method of the
            transaction : * View the default commit method of the transaction: SELECT @@autocommit; - 1 stands for automatic commit 0 stands for manual Submit
            * Modify the default submission method: set @@autocommit = 0;

  

Transaction_Four characteristics of transaction

2. Four major characteristics of transactions:
    1. Atomicity: It is the smallest unit of indivisible operation that either succeeds or fails at the same time.
    2. Persistence: When the transaction is committed or rolled back, the database will save the data persistently.
    3. Isolation: between multiple transactions. Independent.
    4. Consistency: The total amount of data remains unchanged before and after the transaction operation
3. The isolation level of the transaction (understand)

Transaction_Transaction isolation level introduction

3. Transaction isolation level (understand)
    * Concept: isolation between multiple transactions, independent of each other. But if multiple transactions operate on the same batch of data, it will cause some problems, and setting different isolation levels can solve these problems.
    * Problems:
        1. Dirty read: one transaction reads data that is not committed in another transaction
        2. Non-repeatable read (virtual read): in the same transaction, the data read twice is different.
        3. Phantom read: All records in a transaction operation (DML) data table, and another transaction adds a piece of data, the first transaction cannot query its own modification.
    * Isolation level:
        1. read uncommitted: read uncommitted
            * Problems generated: dirty read, non-repeatable read, phantom read
        2. read committed: read committed (Oracle)
            * Problems generated: non-repeatable read, phantom read
        3. repeatable read: repeatable read (MySQL default)
            *problem: phantom read
        4. serializable: serialization
            * can solve all problems

        * Note: From small to large isolation level, security is getting higher and higher, but efficiency is getting lower and lower.
        * Database query isolation level:
            * select @@tx_isolation;
        * Database setting isolation level:
            * set global transaction isolation level level string;

    * 演示:
        set global transaction isolation level read uncommitted;
        start transaction;
        -- 转账操作
        update account set balance = balance - 500 where id = 1;
        update account set balance = balance + 500 where id = 2;

  

Transaction_transaction isolation level demonstration 1

The phantom reading cannot be demonstrated in the MySQL database, so it will not be demonstrated. Solve the problem of dirty reads: set the isolation level of the transaction to "read committed".

Transaction _ transaction isolation level demo 2

16 User Management and Authority Management

DCL_Manage Users_Add, Delete and Check

* SQL classification:
    1. DDL: operating database and tables
    2. DML: adding, deleting and modifying data in tables
    3. DQL: querying data in tables
    4. DCL: managing users and authorization

* DBA: database administrator

* DCL: Manage users, authorize
    1. Manage users
        1. Add users:
            * Syntax: CREATE USER'Username'@'Hostname' IDENTIFIED BY'Password';
        2. Delete users:
            * Syntax: DROP USER'Username' @'Hostname';
        3. Modify user password
        4. Query user:
            - 1. Switch to mysql database
            USE myql;
            - 2. Query user table
            SELECT * FROM USER;
            
            * Wildcard:% means that the user can be used on any host Log in to the database

Create user

  

delete users

DCL_Manage User_Change Password

        3. Modify user password:
            
            UPDATE USER SET PASSWORD = PASSWORD('new password') WHERE USER ='user name'; [Modify password method 1]
            UPDATE USER SET PASSWORD = PASSWORD('abc') WHERE USER ='lisi';
            
            SET PASSWORD FOR'User Name'@'Host Name' = PASSWORD('New Password'); [Modify Password Method 2]
            SET PASSWORD FOR'root'@'localhost' = PASSWORD('123');

            * Forgot the password of the root user in mysql?
                1. cmd --> net stop mysql to stop the mysql service
                    * requires the administrator to run the cmd

                2. Start the mysql service with no authentication: mysqld --skip-grant-tables
                3. Open a new cmd window, directly enter the mysql command, and press Enter. You can log in successfully
                4. use mysql;
                5. update user set password = password('your new password') where user ='root';
                6. Close the two windows
                7. Open the task manager and manually end mysqld.exe the process
                8. start mysql service
                9. use the new password.

Change password method 1

Change password method 2

Forgot the password of the root user in mysql?

  1. cmd -> net stop mysql stop mysql service (the administrator is required to run the cmd)
  2. Start the mysql service without authentication: mysqld --skip-grant-tables
  3. Open a new cmd window, enter the mysql command directly, and hit enter. You can log in successfully
  4. use mysql;
  5. update user set password = password('your new password') where user ='root';
  6. Close two windows
  7. Open the task manager and manually end the process of mysqld.exe
  8. Start mysql service
  9. Log in with the new password.

  

  

DCL_Administrative authority

2. Authority management:
    1. Inquiry authority:
        - Inquiry authority
        SHOW GRANTS
        FOR'username'@'hostname';SHOW GRANTS FOR'lisi'@'%';

    2. Grant permissions:
        - Grant permissions
        grant permission list on database name. Table name to'user name'@'hostname';
        - Grant all permissions to user Zhang San,
        GRANT ALL ON on any table in any database *. * TO'zhangsan'@'localhost';
    3. Revoke permission:
        - Revoke permission:
        revoke permission list on database name. Table name
        from'user name'@'host name'; REVOKE UPDATE ON db3.`account` FROM ' lisi'@'%';

View user permissions

Grant user permissions

  

Grant users multiple permissions

Grant all permissions to the user

Revoke authority

Guess you like

Origin blog.csdn.net/weixin_44949135/article/details/113697413