mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2026-10-24 11:41:59 +08:00
feat: 完成数据库设置界面
This commit is contained in:
@@ -61,3 +61,23 @@ func (b *BaseApi) DeleteMysql(c *gin.Context) {
|
||||
}
|
||||
helper.SuccessWithData(c, nil)
|
||||
}
|
||||
|
||||
func (b *BaseApi) LoadStatus(c *gin.Context) {
|
||||
data, err := mysqlService.LoadStatus("")
|
||||
if err != nil {
|
||||
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||||
return
|
||||
}
|
||||
|
||||
helper.SuccessWithData(c, data)
|
||||
}
|
||||
|
||||
func (b *BaseApi) LoadConf(c *gin.Context) {
|
||||
data, err := mysqlService.LoadConf("")
|
||||
if err != nil {
|
||||
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||||
return
|
||||
}
|
||||
|
||||
helper.SuccessWithData(c, data)
|
||||
}
|
||||
|
||||
@@ -22,3 +22,60 @@ type MysqlDBCreate struct {
|
||||
PermissionIPs string `json:"permissionIPs"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type MysqlStatus struct {
|
||||
AbortedClients string `json:"Aborted_clients"`
|
||||
AbortedConnects string `json:"Aborted_connects"`
|
||||
BytesReceived string `json:"Bytes_received"`
|
||||
BytesSent string `json:"Bytes_sent"`
|
||||
ComCommit string `json:"Com_commit"`
|
||||
ComRollback string `json:"Com_rollback"`
|
||||
Connections string `json:"Connections"`
|
||||
CreatedTmpDiskTables string `json:"Created_tmp_disk_tables"`
|
||||
CreatedTmpTables string `json:"Created_tmp_tables"`
|
||||
InnodbBufferPoolPagesDirty string `json:"Innodb_buffer_pool_pages_dirty"`
|
||||
InnodbBufferPoolReadRequests string `json:"Innodb_buffer_pool_read_requests"`
|
||||
InnodbBufferPoolReads string `json:"Innodb_buffer_pool_reads"`
|
||||
KeyReadRequests string `json:"Key_read_requests"`
|
||||
KeyReads string `json:"Key_reads"`
|
||||
KeyWriteEequests string `json:"Key_write_requests"`
|
||||
KeyWrites string `json:"Key_writes"`
|
||||
MaxUsedConnections string `json:"Max_used_connections"`
|
||||
OpenTables string `json:"Open_tables"`
|
||||
OpenedFiles string `json:"Opened_files"`
|
||||
OpenedTables string `json:"Opened_tables"`
|
||||
QcacheHits string `json:"Qcache_hits"`
|
||||
QcacheInserts string `json:"Qcache_inserts"`
|
||||
Questions string `json:"Questions"`
|
||||
SelectFullJoin string `json:"Select_full_join"`
|
||||
SelectRangeCheck string `json:"Select_range_check"`
|
||||
SortMergePasses string `json:"Sort_merge_passes"`
|
||||
TableLocksWaited string `json:"Table_locks_waited"`
|
||||
ThreadsCached string `json:"Threads_cached"`
|
||||
ThreadsConnected string `json:"Threads_connected"`
|
||||
ThreadsCreated string `json:"Threads_created"`
|
||||
ThreadsRunning string `json:"Threads_running"`
|
||||
Uptime string `json:"Uptime"`
|
||||
Run string `json:"Run"`
|
||||
File string `json:"File"`
|
||||
Position string `json:"Position"`
|
||||
}
|
||||
|
||||
type MysqlConf struct {
|
||||
BinlogCachSize string `json:"binlog_cache_size"`
|
||||
InnodbBufferPoolSize string `json:"innodb_buffer_pool_size"`
|
||||
InnodbLogBufferSize string `json:"innodb_log_buffer_size"`
|
||||
JoinBufferSize string `json:"join_buffer_size"`
|
||||
KeyBufferSize string `json:"key_buffer_size"`
|
||||
MaxConnections string `json:"max_connections"`
|
||||
MaxHeapTableSize string `json:"max_heap_table_size"`
|
||||
QueryCacheSize string `json:"query_cache_size"`
|
||||
QueryCache_type string `json:"query_cache_type"`
|
||||
ReadBufferSize string `json:"read_buffer_size"`
|
||||
ReadRndBufferSize string `json:"read_rnd_buffer_size"`
|
||||
SortBufferSize string `json:"sort_buffer_size"`
|
||||
TableOpenCache string `json:"table_open_cache"`
|
||||
ThreadCacheSize string `json:"thread_cache_size"`
|
||||
ThreadStack string `json:"thread_stack"`
|
||||
Tmp_tableSize string `json:"tmp_table_size"`
|
||||
}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
||||
"github.com/1Panel-dev/1Panel/backend/constant"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@@ -13,6 +18,8 @@ type IMysqlService interface {
|
||||
SearchWithPage(search dto.SearchWithPage) (int64, interface{}, error)
|
||||
Create(mysqlDto dto.MysqlDBCreate) error
|
||||
Delete(ids []uint) error
|
||||
LoadStatus(version string) (*dto.MysqlStatus, error)
|
||||
LoadConf(version string) (*dto.MysqlConf, error)
|
||||
}
|
||||
|
||||
func NewIMysqlService() IMysqlService {
|
||||
@@ -56,3 +63,63 @@ func (u *MysqlService) Delete(ids []uint) error {
|
||||
}
|
||||
return mysqlRepo.Delete(commonRepo.WithIdsIn(ids))
|
||||
}
|
||||
|
||||
func (u *MysqlService) LoadConf(version string) (*dto.MysqlConf, error) {
|
||||
connArgs := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8", "root", "Calong@2015", "localhost", 2306)
|
||||
db, err := sql.Open("mysql", connArgs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
rows, err := db.Query("show variables")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
variableMap := make(map[string]string)
|
||||
for rows.Next() {
|
||||
var variableName, variableValue string
|
||||
if err := rows.Scan(&variableName, &variableValue); err != nil {
|
||||
continue
|
||||
}
|
||||
variableMap[variableName] = variableValue
|
||||
}
|
||||
|
||||
var info dto.MysqlConf
|
||||
arr, err := json.Marshal(variableMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_ = json.Unmarshal(arr, &info)
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
func (u *MysqlService) LoadStatus(version string) (*dto.MysqlStatus, error) {
|
||||
connArgs := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8", "root", "Calong@2015", "localhost", 2306)
|
||||
db, err := sql.Open("mysql", connArgs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
rows, err := db.Query("show status")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
variableMap := make(map[string]string)
|
||||
for rows.Next() {
|
||||
var variableName, variableValue string
|
||||
if err := rows.Scan(&variableName, &variableValue); err != nil {
|
||||
continue
|
||||
}
|
||||
variableMap[variableName] = variableValue
|
||||
}
|
||||
|
||||
var info dto.MysqlStatus
|
||||
arr, err := json.Marshal(variableMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_ = json.Unmarshal(arr, &info)
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func TestMysql(t *testing.T) {
|
||||
connArgs := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8", "root", "Calong@2015", "localhost", 2306)
|
||||
db, err := sql.Open("mysql", connArgs)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
rows, err := db.Query("show VARIABLES")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
variableMap := make(map[string]string)
|
||||
for rows.Next() {
|
||||
var variableName, variableValue string
|
||||
if err := rows.Scan(&variableName, &variableValue); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
variableMap[variableName] = variableValue
|
||||
}
|
||||
var info dto.MysqlConf
|
||||
arr, err := json.Marshal(variableMap)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
err = json.Unmarshal(arr, &info)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(info)
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
_ "gorm.io/driver/mysql"
|
||||
)
|
||||
|
||||
func TestMysql(t *testing.T) {
|
||||
connArgs := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8", "root", "Calong@2015", "localhost", 2306)
|
||||
db, err := sql.Open("mysql", connArgs)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
defer db.Close()
|
||||
ctx, cancelfunc := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancelfunc()
|
||||
res, err := db.ExecContext(ctx, "CREATE DATABASE IF NOT EXISTS "+"songli")
|
||||
if err != nil {
|
||||
log.Printf("Error %s when creating DB\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// res, err := db.Exec("SHOW DATABASES;")
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
fmt.Println(res)
|
||||
}
|
||||
@@ -24,5 +24,7 @@ func (s *DatabaseRouter) InitDatabaseRouter(Router *gin.RouterGroup) {
|
||||
withRecordRouter.POST("", baseApi.CreateMysql)
|
||||
withRecordRouter.POST("/del", baseApi.DeleteMysql)
|
||||
cmdRouter.POST("/search", baseApi.SearchMysql)
|
||||
cmdRouter.GET("/conf", baseApi.LoadConf)
|
||||
cmdRouter.GET("/status", baseApi.LoadStatus)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user