diff --git a/internal/data/database_user.go b/internal/data/database_user.go index 9a75dc62fe..e1a3e3de3b 100644 --- a/internal/data/database_user.go +++ b/internal/data/database_user.go @@ -163,7 +163,7 @@ func (r databaseUserRepo) Delete(id uint) error { if err != nil { return err } - _ = mysql.UserDrop(user.Username) + _ = mysql.UserDrop(user.Username, user.Host) case biz.DatabaseTypePostgresql: postgres, err := db.NewPostgres(server.Username, server.Password, server.Host, server.Port) if err != nil { diff --git a/internal/data/website.go b/internal/data/website.go index 27cd4d3ad4..30bc488f2f 100644 --- a/internal/data/website.go +++ b/internal/data/website.go @@ -525,7 +525,7 @@ func (r *websiteRepo) Delete(req *request.WebsiteDelete) error { return err } if mysql, err := db.NewMySQL("root", rootPassword, "/tmp/mysql.sock", "unix"); err == nil { - _ = mysql.UserDrop(website.Name) + _ = mysql.UserDrop(website.Name, "localhost") _ = mysql.DatabaseDrop(website.Name) } if postgres, err := db.NewPostgres("postgres", "", "127.0.0.1", 5432); err == nil { diff --git a/pkg/db/mysql.go b/pkg/db/mysql.go index e167da8a5c..a83ed98bb8 100644 --- a/pkg/db/mysql.go +++ b/pkg/db/mysql.go @@ -37,44 +37,44 @@ func NewMySQL(username, password, address string, typ ...string) (*MySQL, error) }, nil } -func (m *MySQL) Close() error { - return m.db.Close() +func (r *MySQL) Close() error { + return r.db.Close() } -func (m *MySQL) Ping() error { - return m.db.Ping() +func (r *MySQL) Ping() error { + return r.db.Ping() } -func (m *MySQL) Query(query string, args ...any) (*sql.Rows, error) { - return m.db.Query(query, args...) +func (r *MySQL) Query(query string, args ...any) (*sql.Rows, error) { + return r.db.Query(query, args...) } -func (m *MySQL) QueryRow(query string, args ...any) *sql.Row { - return m.db.QueryRow(query, args...) +func (r *MySQL) QueryRow(query string, args ...any) *sql.Row { + return r.db.QueryRow(query, args...) } -func (m *MySQL) Exec(query string, args ...any) (sql.Result, error) { - return m.db.Exec(query, args...) +func (r *MySQL) Exec(query string, args ...any) (sql.Result, error) { + return r.db.Exec(query, args...) } -func (m *MySQL) Prepare(query string) (*sql.Stmt, error) { - return m.db.Prepare(query) +func (r *MySQL) Prepare(query string) (*sql.Stmt, error) { + return r.db.Prepare(query) } -func (m *MySQL) DatabaseCreate(name string) error { - _, err := m.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", name)) - m.flushPrivileges() +func (r *MySQL) DatabaseCreate(name string) error { + _, err := r.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", name)) + r.flushPrivileges() return err } -func (m *MySQL) DatabaseDrop(name string) error { - _, err := m.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", name)) - m.flushPrivileges() +func (r *MySQL) DatabaseDrop(name string) error { + _, err := r.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", name)) + r.flushPrivileges() return err } -func (m *MySQL) DatabaseExists(name string) (bool, error) { - rows, err := m.Query("SHOW DATABASES") +func (r *MySQL) DatabaseExists(name string) (bool, error) { + rows, err := r.Query("SHOW DATABASES") if err != nil { return false, err } @@ -92,38 +92,38 @@ func (m *MySQL) DatabaseExists(name string) (bool, error) { return false, nil } -func (m *MySQL) DatabaseSize(name string) (int64, error) { +func (r *MySQL) DatabaseSize(name string) (int64, error) { var size int64 - err := m.QueryRow(fmt.Sprintf("SELECT COALESCE(SUM(data_length) + SUM(index_length), 0) FROM information_schema.tables WHERE table_schema = '%s'", name)).Scan(&size) + err := r.QueryRow(fmt.Sprintf("SELECT COALESCE(SUM(data_length) + SUM(index_length), 0) FROM information_schema.tables WHERE table_schema = '%s'", name)).Scan(&size) return size, err } -func (m *MySQL) UserCreate(user, password, host string) error { - _, err := m.Exec(fmt.Sprintf("CREATE USER IF NOT EXISTS '%s'@'%s' IDENTIFIED BY '%s'", user, host, password)) - m.flushPrivileges() +func (r *MySQL) UserCreate(user, password, host string) error { + _, err := r.Exec(fmt.Sprintf("CREATE USER IF NOT EXISTS '%s'@'%s' IDENTIFIED BY '%s'", user, host, password)) + r.flushPrivileges() return err } -func (m *MySQL) UserDrop(user string) error { - _, err := m.Exec(fmt.Sprintf("DROP USER IF EXISTS '%s'", user)) - m.flushPrivileges() +func (r *MySQL) UserDrop(user, host string) error { + _, err := r.Exec(fmt.Sprintf("DROP USER IF EXISTS '%s'@'%s'", user, host)) + r.flushPrivileges() return err } -func (m *MySQL) UserPassword(user, password, host string) error { - _, err := m.Exec(fmt.Sprintf("ALTER USER '%s'@'%s' IDENTIFIED BY '%s'", user, host, password)) - m.flushPrivileges() +func (r *MySQL) UserPassword(user, password, host string) error { + _, err := r.Exec(fmt.Sprintf("ALTER USER '%s'@'%s' IDENTIFIED BY '%s'", user, host, password)) + r.flushPrivileges() return err } -func (m *MySQL) PrivilegesGrant(user, database, host string) error { - _, err := m.Exec(fmt.Sprintf("GRANT ALL PRIVILEGES ON %s.* TO '%s'@'%s'", database, user, host)) - m.flushPrivileges() +func (r *MySQL) PrivilegesGrant(user, database, host string) error { + _, err := r.Exec(fmt.Sprintf("GRANT ALL PRIVILEGES ON %s.* TO '%s'@'%s'", database, user, host)) + r.flushPrivileges() return err } -func (m *MySQL) UserPrivileges(user, host string) (map[string][]string, error) { - rows, err := m.Query(fmt.Sprintf("SHOW GRANTS FOR '%s'@'%s'", user, host)) +func (r *MySQL) UserPrivileges(user, host string) (map[string][]string, error) { + rows, err := r.Query(fmt.Sprintf("SHOW GRANTS FOR '%s'@'%s'", user, host)) if err != nil { return nil, err } @@ -164,14 +164,14 @@ func (m *MySQL) UserPrivileges(user, host string) (map[string][]string, error) { return privileges, nil } -func (m *MySQL) PrivilegesRevoke(user, database, host string) error { - _, err := m.Exec(fmt.Sprintf("REVOKE ALL PRIVILEGES ON %s.* FROM '%s'@'%s'", database, user, host)) - m.flushPrivileges() +func (r *MySQL) PrivilegesRevoke(user, database, host string) error { + _, err := r.Exec(fmt.Sprintf("REVOKE ALL PRIVILEGES ON %s.* FROM '%s'@'%s'", database, user, host)) + r.flushPrivileges() return err } -func (m *MySQL) Users() ([]types.MySQLUser, error) { - rows, err := m.Query("SELECT user, host FROM mysql.user") +func (r *MySQL) Users() ([]types.MySQLUser, error) { + rows, err := r.Query("SELECT user, host FROM mysql.user") if err != nil { return nil, err } @@ -183,7 +183,7 @@ func (m *MySQL) Users() ([]types.MySQLUser, error) { if err := rows.Scan(&user, &host); err != nil { continue } - grants, err := m.userGrants(user, host) + grants, err := r.userGrants(user, host) if err != nil { continue } @@ -198,7 +198,7 @@ func (m *MySQL) Users() ([]types.MySQLUser, error) { return users, nil } -func (m *MySQL) Databases() ([]types.MySQLDatabase, error) { +func (r *MySQL) Databases() ([]types.MySQLDatabase, error) { query := ` SELECT SCHEMA_NAME, @@ -208,7 +208,7 @@ func (m *MySQL) Databases() ([]types.MySQLDatabase, error) { WHERE SCHEMA_NAME NOT IN ('information_schema', 'performance_schema', 'mysql', 'sys') ` - rows, err := m.Query(query) + rows, err := r.Query(query) if err != nil { return nil, err } @@ -230,8 +230,8 @@ func (m *MySQL) Databases() ([]types.MySQLDatabase, error) { return databases, nil } -func (m *MySQL) userGrants(user, host string) ([]string, error) { - rows, err := m.Query(fmt.Sprintf("SHOW GRANTS FOR '%s'@'%s'", user, host)) +func (r *MySQL) userGrants(user, host string) ([]string, error) { + rows, err := r.Query(fmt.Sprintf("SHOW GRANTS FOR '%s'@'%s'", user, host)) if err != nil { return nil, err } @@ -248,6 +248,6 @@ func (m *MySQL) userGrants(user, host string) ([]string, error) { return grants, nil } -func (m *MySQL) flushPrivileges() { - _, _ = m.Exec("FLUSH PRIVILEGES") +func (r *MySQL) flushPrivileges() { + _, _ = r.Exec("FLUSH PRIVILEGES") }