mysql

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2025 License: MIT Imports: 9 Imported by: 0

README

MySQL CLI

Go Reference Go Report Card

A standalone, feature-rich MySQL interactive CLI client for Go applications.

Features

  • 🚀 Full MySQL protocol support
  • 🔧 Customizable connection parameters
  • 💾 Connection pooling and timeout control
  • 🔒 TLS/SSL support
  • 🌍 Charset and timezone configuration
  • 📊 Vertical and horizontal display modes
  • ⏱️ Query timing
  • 🔄 Transaction support
  • 📝 Multi-line SQL input
  • 🎨 Syntax highlighting support

Installation

go get github.com/binrchq/mysql-cli

Quick Start

package main

import (
    "log"
    "os"
    
    mysqlcli "github.com/binrchq/mysql-cli"
)

func main() {
    cli := mysqlcli.NewCLI(
        os.Stdin,   // terminal reader
        "localhost", // host
        3306,        // port
        "root",      // username
        "password",  // password
        "mydb",      // database
    )
    
    if err := cli.Connect(); err != nil {
        log.Fatal(err)
    }
    defer cli.Close()
    
    if err := cli.Start(); err != nil {
        log.Fatal(err)
    }
}

Advanced Configuration

config := &mysqlcli.Config{
    Host:            "localhost",
    Port:            3306,
    Username:        "root",
    Password:        "password",
    Database:        "mydb",
    Charset:         "utf8mb4",
    Timeout:         15 * time.Second,
    ReadTimeout:     60 * time.Second,
    MaxOpenConns:    20,
    MaxIdleConns:    10,
    ConnMaxLifetime: 2 * time.Hour,
    TLS:             "skip-verify",
    CustomParams:    "autocommit=1",
}

cli := mysqlcli.NewCLIWithConfig(terminal, config)

Configuration Options

Option Type Default Description
Host string - MySQL server host
Port int 3306 MySQL server port
Username string - MySQL username
Password string - MySQL password
Database string - Database name
Charset string utf8mb4 Character set
Collation string - Collation
Timeout duration 10s Connection timeout
ReadTimeout duration 30s Read timeout
WriteTimeout duration 30s Write timeout
MaxOpenConns int 10 Max open connections
MaxIdleConns int 5 Max idle connections
ConnMaxLifetime duration 1h Connection max lifetime
ParseTime bool true Parse time values
Loc string Local Timezone location
TLS string - TLS configuration
CustomParams string - Custom DSN parameters

Supported Commands

SQL Commands
  • SELECT, INSERT, UPDATE, DELETE
  • CREATE TABLE, ALTER TABLE, DROP TABLE
  • CREATE INDEX, DROP INDEX
  • All standard MySQL SQL syntax
Special Commands
  • help, \h - Show help
  • exit, quit, \q - Exit CLI
  • \c - Cancel current command
  • \s - Show server status
  • \d, \db - Show current database
  • \u <db>, \use <db> - Switch database
  • \G - Display results vertically
  • timing - Toggle timing display
Transaction Commands
  • BEGIN, START TRANSACTION - Start transaction
  • COMMIT - Commit transaction
  • ROLLBACK - Rollback transaction

Examples

See the example directory for complete examples.

Requirements

  • Go 1.21 or higher
  • MySQL 5.7 or higher

Dependencies

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Author

Maintained by binrc.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CLI

type CLI struct {
	// contains filtered or unexported fields
}

CLI MySQL 交互式命令行客户端

func NewCLI

func NewCLI(term Terminal, host string, port int, username, password, database string) *CLI

NewCLI 创建新的 MySQL CLI 实例(兼容旧接口)

func NewCLIWithConfig

func NewCLIWithConfig(term Terminal, config *Config) *CLI

NewCLIWithConfig 使用配置创建 MySQL CLI 实例

func (*CLI) Close

func (c *CLI) Close() error

Close 关闭数据库连接

func (*CLI) Connect

func (c *CLI) Connect() error

Connect 连接到 MySQL 数据库

func (*CLI) Start

func (c *CLI) Start() error

Start 启动交互式命令行

type Config

type Config struct {
	Host              string
	Port              int
	Username          string
	Password          string
	Database          string
	Charset           string        // 字符集,默认 utf8mb4
	Collation         string        // 排序规则
	Timeout           time.Duration // 连接超时,默认 10s
	ReadTimeout       time.Duration // 读取超时,默认 30s
	WriteTimeout      time.Duration // 写入超时,默认 30s
	MaxOpenConns      int           // 最大连接数,默认 10
	MaxIdleConns      int           // 最大空闲连接数,默认 5
	ConnMaxLifetime   time.Duration // 连接最大生命周期,默认 1h
	ParseTime         bool          // 是否解析时间,默认 true
	Loc               string        // 时区,默认 Local
	AllowNativePasswd bool          // 允许原生密码,默认 true
	TLS               string        // TLS 配置:true/false/skip-verify/preferred
	CustomParams      string        // 自定义参数,如 "param1=value1&param2=value2"
}

Config MySQL 连接配置

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader 从终端读取输入

func NewReader

func NewReader(term Terminal) *Reader

NewReader 创建新的 Reader

func (*Reader) ReadLine

func (r *Reader) ReadLine() (string, error)

ReadLine 读取一行输入

func (*Reader) SetPrompt added in v0.1.1

func (r *Reader) SetPrompt(prompt string)

SetPrompt 设置提示符

type ServerInfo

type ServerInfo struct {
	Version         string
	ProtocolVersion int
	ConnectionID    int
	Charset         string
	Uptime          int64
}

ServerInfo MySQL 服务器信息

type Terminal

type Terminal interface {
	io.Reader
	io.Writer
}

Terminal 终端接口,用于输入输出

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL