mysql_defaults_file

package module
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2022 License: BSD-2-Clause Imports: 7 Imported by: 5

README

mysql_defaults_file

Access in Go to MySQL via a defaults_file.

If using the MySQL command line utilities such as mysql or mysqladmin you can provide a defaults-file option which stores the credentials of the MySQL server you want to connect to. If no specific defaults file is mentioned these utilities look in ~/.my.cnf for this file.

The Go sql interface does not support this functionality yet it can be quite convenient as it avoids the need to explicitly provide credentials.

This small module fills in that gap by providing a function to allow you to connect to MySQL using a specified defaults-file, or using the ~/.my.cnf if you do not specify a defaults-file path.

There is also a function BuildDSN which allows you to build up a Go dsn for MySQL using various entries in a mysql .ini file.

This logic could be simplified by using github.com/go-sql-driver/mysql.Config together with Config.FormatDSN(), but there are a few minor differences in behaviour such as the default timezone handling using mysql.Config being UTC compared to mysql's command line client using the system timezone.

The functions provided are used by ps-top to simplify the connectivity and have been split off from it as they may be useful for other programs that connect to MySQL.

The code has been extended to handle quoted usernames and passwords, removing whitespace and quotes if found. Quoting with single or double quotes is permitted.

Usage

Usage:

import (
	...
	"database/sql"

	_ "github.com/go-sql-driver/mysql"
	"github.com/sjmudd/mysql_defaults_file"
	...
)

// open the connection to the database using the default defaults-file (original way).
dbh, err = mysql_defaults_file.OpenUsingDefaultsFile("mysql", "", "performance_schema")

// open the connection to the database using the default defaults-file (shorter form).
dbh, err = mysql_defaults_file.Open("", "")

// open the connection to the database using a specific defaults-file and to mydb.
dbh, err = mysql_defaults_file.Open("/path/to/my.ini", "mydb")

The errors you get back will be the same as calling sql.Open( "mysql",..... ).

Licensing

BSD 2-Clause License

Feedback

Feedback and patches welcome.

Simon J Mudd sjmudd@pobox.com

Code Documenton

godoc.org/github.com/sjmudd/mysql_defaults_file

Documentation

Overview

Package mysql_defaults_file provides a way of accessing MySQL via a defaults-file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildDSN

func BuildDSN(config Config, database string) string

BuildDSN builds the dsn we're going to use to connect with based on a parameter / value string map and return the dsn as a string.

Note: Config should be replaced with the mysql.Config structure and then we can use Config.FormatDSN() to generate the dsn directly. However, there are some differences between mysql.Config default behaviour and that from the mysql command line such as timezone handling that would need to be taken into account.

func Open added in v0.0.12

func Open(defaultsFile string, database string) (*sql.DB, error)

Open just wraps OpenUsingDefaultsFile, assuming "mysql" as the driver.

func OpenUsingDefaultsFile

func OpenUsingDefaultsFile(sqlDriver string, defaultsFile string, database string) (*sql.DB, error)

OpenUsingDefaultsFile opens a connection only using a defaults file

func OpenUsingEnvironment

func OpenUsingEnvironment(sqlDriver string) (*sql.DB, error)

OpenUsingEnvironment will assume MYSQL_DSN is set and use that value for connecting.

Types

type Config added in v0.0.11

type Config struct {
	Filename string
	User     string
	Password string
	Socket   string
	Host     string
	Port     uint16
	Database string
}

Config holds the configuration taken out of the defaults file.

func NewConfig added in v0.0.13

func NewConfig(defaultsFile string) Config

NewConfig creates a Config struct using the values from the provided defaults file. - if defaultsFile is empty use the default of ~/.my.cnf

Jump to

Keyboard shortcuts

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