mysql

package
v1.10.7 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2020 License: MIT Imports: 6 Imported by: 0

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用不同的句柄就可以

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
	MaxIdleConns int    // 空闲pool个数
	MaxOpenConns int    // 最大open connection个数

	Timeout      time.Duration // Dial timeout
	ReadTimeout  time.Duration // I/O read timeout
	WriteTimeout time.Duration // I/O write timeout

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

	ParseTime bool
	Loc       string // 时区字符串 Local,PRC

	SqlCmd  bool // sql语句是否输出到终端,true输出到终端,生产环境建议关闭,因为log会加锁
	UsePool bool // 当前db实例是否采用db连接池,默认不采用,如采用请求配置该参数
	// 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) Db

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

Db 返回当前db对象

func (*DbConf) SetDbObj

func (conf *DbConf) SetDbObj() error

SetDbObj 创建当前数据库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

Jump to

Keyboard shortcuts

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