Home  >  Article  >  Database  >  How to use Go language to control data permissions of MySQL database

How to use Go language to control data permissions of MySQL database

PHPz
PHPzOriginal
2023-06-17 17:33:111273browse

With the advent of the data era, data security and privacy protection have increasingly become the focus of attention. In enterprises, databases are important tools for data storage and management, so data permission control is also particularly important. This article will introduce how to use Go language to control data permissions of MySQL database.

1. Data permission control of MySQL database

MySQL is an open source relational database that is widely used in enterprise databases. MySQL provides many built-in security features, including users, roles, permissions, etc. These features can be used to restrict user operations in the database to ensure data security.

Data permission control refers to controlling each user's access permissions to data in the database. The permissions provided by MySQL include:

  1. SELECT: Allows users to query data in the database.
  2. INSERT: Allows users to insert data into the database.
  3. UPDATE: Allows users to update data in the database.
  4. DELETE: Allows users to delete data in the database.
  5. CREATE: Allows users to create databases and tables.
  6. DROP: Allows users to delete databases and tables.
  7. INDEX: Allows users to create and delete indexes.
  8. ALTER: Allows users to modify the table structure.

In MySQL, you can use the GRANT and REVOKE statements to authorize and deauthorize.

2. Method of using Go language to control data permissions of MySQL database

Go language is a programming language for developing efficient and reliable network and system services. It is used for its characteristics Database management and development. For data permission control of MySQL database, programs developed using Go language can improve the efficiency and security of business logic.

The following are the steps to use Go language to control data permissions of MySQL database:

  1. Introduce the MySQL driver library

First we need to introduce the Go language MySQL driver library, this driver library can help us connect to the MySQL database and perform related operations on it.

Installation method:

go get github.com/go-sql-driver/mysql
  1. Connect to MySQL database

In Go language, you can use the sql.Open() function to connect to the MySQL database, example The code is as follows:

import (
    "database/sql"
    "fmt"
    _ "github.com/go-sql-driver/mysql"
)

func Connect() (*sql.DB, error) {
    db, err := sql.Open("mysql", "root:password@tcp(127.0.0.1:3306)/test")
    if err != nil {
        return nil, err
    }

    err = db.Ping()
    if err != nil {
        return nil, err
    }

    fmt.Println("Successfully connected to MySQL database!")
    return db, nil
}
  1. Create user and authorization

In Go language, you can use the Exec() method to execute SQL statements. In MySQL, the GRANT statement can be used Create users and authorizations. The sample code is as follows:

func CreateUser(db *sql.DB, username string, password string) error {
    query := fmt.Sprintf(`
        CREATE USER '%s'@'localhost' IDENTIFIED BY '%s';
    `, username, password)

    _, err := db.Exec(query)
    if err != nil {
        return err
    }

    fmt.Println("User created successfully!")
    return nil
}

func GrantPrivileges(db *sql.DB, username string) error {
    query := fmt.Sprintf(`
        GRANT SELECT, INSERT, UPDATE, DELETE ON database.* TO '%s'@'localhost';
    `, username)

    _, err := db.Exec(query)
    if err != nil {
        return err
    }

    fmt.Println("User privileges granted successfully!")
    return nil
}
  1. Cancel authorization and delete users

Similarly, the Go language can use the Exec() method to execute SQL statements, and the REVOKE statement can be used to cancel authorization. , DROP USER can be used to delete users. The sample code is as follows:

func RevokePrivileges(db *sql.DB, username string) error {
    query := fmt.Sprintf(`
        REVOKE SELECT, INSERT, UPDATE, DELETE ON database.* FROM '%s'@'localhost';
    `, username)

    _, err := db.Exec(query)
    if err != nil {
        return err
    }

    fmt.Println("User privileges revoked successfully!")
    return nil
}

func DropUser(db *sql.DB, username string) error {
    query := fmt.Sprintf(`
        DROP USER '%s'@'localhost';
    `, username)

    _, err := db.Exec(query)
    if err != nil {
        return err
    }

    fmt.Println("User dropped successfully!")
    return nil
}
  1. Call permission control function

Finally, we can put the above functions in a main function and call them in the application to control Data permissions for the MySQL database. The sample code is as follows:

func main() {
    db, err := Connect()
    if err != nil {
        panic(err)
    }
    defer db.Close()

    username := "test"
    password := "test123"

    err = CreateUser(db, username, password)
    if err != nil {
        panic(err)
    }

    err = GrantPrivileges(db, username)
    if err != nil {
        panic(err)
    }

    err = RevokePrivileges(db, username)
    if err != nil {
        panic(err)
    }

    err = DropUser(db, username)
    if err != nil {
        panic(err)
    }
}

Summary

This article introduces how to use Go language to control data permissions of MySQL database. Through the Go language driver library and the use of SQL statements, user creation and authorization are realized and cancel authorization and other operations. Through these operations, the security and privacy of data involved in the enterprise can be effectively protected.

The above is the detailed content of How to use Go language to control data permissions of MySQL database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn