mysql

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2021 License: MIT Imports: 9 Imported by: 1

README

ubuntu mysql5.7最大连接数设置

修改mysql中的mysqld.cnf文件
cd /etc/mysql/mysql.conf.d
执行下面的操作来修改配置文件
sudo vim mysqld.cnf
在 [mysqld] 中新加
max_connections  =1000;
按下esc按键输入:wq保存退出

重启mysql服务器
sudo service mysql restart;

登录进去查看mysql配置
mysql -uroot -p
//输入密码登录
//查看刚刚配置信息
mysql>show variables like '%max_connections%';
发现没有改为默认最大是214
因为ubuntu系统本身有限制文件打开和连接数量,所以需要修改系统配置来达到我们的要求

修改系统配置
cd  /etc/systemd/system/multi-user.target.wants
sudo vim mysql.service
//在 [Service] 最后加入:
LimitNOFILE=65535
LimitNPROC=65535
按下esc按键输入:wq保存退出

刷新系统配置
systemctl daemon-reload
systemctl restart mysql.service

检验配置是否成功
mysql -uroot -p
//输入密码登录
//查看刚刚配置信息
mysql>show variables like '%max_connections%';

这是可以看到已修改为1000了

Documentation

Overview

* Package mysql of gorm library. * gorm mysql封装,支持多个数据库实例化为连接池对象 * 结合了xorm思想,将每个数据库对象作为一个数据库引擎句柄 * xorm设计思想:在xorm里面,可以同时存在多个Orm引擎 * 一个Orm引擎称为Engine,一个Engine一般只对应一个数据库 * 因此,可以将gorm的每个数据库连接句柄,可以作为一个Engine来进行处理 * 容易踩坑的地方: * 对于golang的官方sql引擎,sql.open并非立即连接db,用的时候才会真正的建立连接 * 但是gorm.Open在设置完db对象后,还发送了一个Ping操作,判断连接是否连接上去 * 对于短连接的话,建议用完就调用db.Close()方法释放db连接资源 * 对于长连接服务,一般建议在main/init中关闭连接就可以 * 具体可以看gorm/main.go源码85行 * 对于gorm实现读写分离: * 可以实例化master,slaves实例,对于curd用不同的句柄就可以 * 由于gorm自己对mysql做了一次包裹,所以重命名处理 * gMysql "gorm.io/driver/mysql" * gorm v2版本仓库地址:https://github.com/go-gorm/gorm

Package mysql gMysql option for gorm v2 config.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseAllDb

func CloseAllDb()

CloseAllDb 由于gorm db.Close()是关闭当前连接 一般建议如下函数放在main/init关闭连接就可以

func CloseDbByName

func CloseDbByName(name string) error

CloseDbByName 关闭指定name的db engine

func GetDbObj

func GetDbObj(name string) (*gorm.DB, error)

========================辅助函数=============== GetDbObj 从db pool获取一个数据库连接句柄 根据数据库连接句柄name获取指定的连接句柄

Types

type DbConf

type DbConf struct {
	Ip        string
	Port      int // 默认3306
	User      string
	Password  string
	Database  string
	Charset   string // 字符集 utf8mb4 支持表情符号
	Collation string // 整理字符集 utf8mb4_unicode_ci

	UsePool      bool // 当前db实例是否采用db连接池,默认不采用,如采用请求配置该参数
	MaxIdleConns int  // 空闲pool个数
	MaxOpenConns int  // 最大open connection个数

	// sets the maximum amount of time a connection may be reused.
	// 设置连接可以重用的最大时间
	// 给db设置一个超时时间,时间小于数据库的超时时间
	MaxLifetime int64 // 数据库超时时间,单位s

	// 连接超时/读取超时/写入超时设置
	Timeout      time.Duration // Dial timeout
	ReadTimeout  time.Duration // I/O read timeout
	WriteTimeout time.Duration // I/O write timeout

	ParseTime bool   // 格式化时间类型
	Loc       string // 时区字符串 Local,PRC

	ShowSql bool // sql语句是否输出

	// sql输出logger句柄接口
	// logger.Writer 接口需要实现Printf(string, ...interface{}) 方法
	// 具体可以看gorm v2 logger包源码
	// https://github.com/go-gorm/gorm
	Logger logger.Writer

	LoggerConfig logger.Config // gorm v2新增参数logger.Config
	// contains filtered or unexported fields
}

DbConf mysql连接信息 parseTime=true changes the output type of DATE and DATETIME values to time.Time instead of []byte / string The date or datetime like 0000-00-00 00:00:00 is converted into zero value of time.Time.

func (*DbConf) Close

func (conf *DbConf) Close() error

Close 关闭当前数据库连接 一般建议,将当前db engine close函数放在main/init关闭连接就可以

func (*DbConf) DSN

func (conf *DbConf) DSN() (string, error)

DSN 设置mysql dsn mysql charset查看 mysql> show character set where charset="utf8mb4"; +---------+---------------+--------------------+--------+ | Charset | Description | Default collation | Maxlen | +---------+---------------+--------------------+--------+ | utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 | +---------+---------------+--------------------+--------+ 1 row in set (0.00 sec)

func (*DbConf) Db

func (conf *DbConf) Db() *gorm.DB

Db 返回当前db对象

func (*DbConf) InitInstance

func (conf *DbConf) InitInstance() error

InitInstance 建立db连接句柄 创建当前数据库db对象,并非连接,在使用的时候才会真正建立db连接 为兼容之前的版本,这里新增SetDb创建db对象

func (*DbConf) SetDbPool

func (conf *DbConf) SetDbPool() error

SetDbPool 设置db pool连接池

func (*DbConf) SetEngineName

func (conf *DbConf) SetEngineName(name string) error

SetEngineName 给当前数据库指定engineName

func (*DbConf) ShortConnect

func (conf *DbConf) ShortConnect() error

ShortConnect 建立短连接,用完需要调用Close()进行关闭连接,释放资源 否则就会出现too many connection

func (*DbConf) SqlDB

func (conf *DbConf) SqlDB() (*sql.DB, error)

SqlDB 返回sql DB

type Option

type Option func(conf *DbConf)

Option DbConf 功能函数模式

func WithDefaultStringSize

func WithDefaultStringSize(size uint) Option

WithDefaultStringSize 设置字符串默认长度

func WithDisableDatetimePrecision

func WithDisableDatetimePrecision(b bool) Option

WithDisableDatetimePrecision 是否禁用 datetime 精度,MySQL 5.6 之前的数据库不支持

func WithDontSupportRenameColumn

func WithDontSupportRenameColumn(b bool) Option

WithDontSupportRenameColumn 是否用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列

func WithDontSupportRenameIndex

func WithDontSupportRenameIndex(b bool) Option

WithDontSupportRenameIndex 是否重命名索引时采用删除并新建的方式 MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引

func WithDriverName

func WithDriverName(name string) Option

WithDriverName 设置db driver name.

func WithDsn

func WithDsn(dsn string) Option

WithDsn 设置dsn

func WithGormConfig

func WithGormConfig(config gorm.Config) Option

WithGormConfig 设置gorm.Config

func WithGormConnPool

func WithGormConnPool(conn gorm.ConnPool) Option

WithGormConnPool 设置gorm conn pool.

func WithLogLevel

func WithLogLevel(logLevel logger.LogLevel) Option

WithLogLevel 设置sql logger level

func WithLogger

func WithLogger(gLogger logger.Writer) Option

WithLogger 设置gorm logger实例对象

func WithSkipInitializeWithVersion

func WithSkipInitializeWithVersion(b bool) Option

WithSkipInitializeWithVersion 根据当前 MySQL 版本自动配置

Jump to

Keyboard shortcuts

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