Go 各種數據庫連接字符串匯總

收藏待读

Go 各種數據庫連接字符串匯總

各種數據庫的連接字符串的連接格式(一般叫做database source name, 簡稱 DSN )是不同的,本文匯總了各個數據庫驅動程序的字符串連接方式。

關係型數據庫

MySQL

[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]

// [email protected](/path/to/socket)/dbname
// root:[email protected](/tmp/mysql.sock)/myDatabase?loc=Local
// user:[email protected](localhost:5555)/dbname?tls=skip-verify&autocommit=true
// user:[email protected]/dbname?sql_mode=TRADITIONAL
// user:[email protected]([de:ad:be:ef::ca:fe]:80)/dbname?timeout=90s&collation=utf8mb4_unicode_ci
// id:[email protected](your-amazonaws-uri.com:3306)/dbname
// [email protected](project-id:instance-name)/dbname
// [email protected](project-id:regionname:instance-name)/dbname
// user:[email protected]/dbname?charset=utf8mb4,utf8&sys_var=esc%40ped
// user:[email protected]/dbname
// user:[email protected]/

Postgres (pure Go)

postgres://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...]
postgres://
postgres://localhost
postgres://localhost:5433
postgres://localhost/mydb
postgres://[email protected]
postgres://user:[email protected]
postgres://[email protected]/otherdb?connect_timeout=10&application_name=myapp
postgres://host1:123,host2:456/somedb?target_session_attrs=any&application_name=myapp

SQLite (uses cgo)

test.db
file:test.db?cache=shared&mode=memory
:memory:
file::memory:

更多參數可以查看 SQLiteDriver.Open

Apache Ignite/GridGain

protocol://host:port/cache?param1=value1&param2=value2&paramN=valueN

各部分的詳細設置可以查看 How to use SQL driver

Apache Avatica/Phoenix

http://[username:[email protected]]address:port[/schema][?parameter1=value&...parameterN=value]

AWS Athena

db=default&output_location=s3://results

可選值 poll_frequencyregion

ClickHouse Via TCP

tcp://host1:9000?username=user&password=qwerty&database=clicks&read_timeout=10&write_timeout=20&alt_hosts=host2:9000,host3:9000

ClickHouse Via HTTP

schema://user:[email protected][:port]/database?param1=value1&...&paramN=valueN

DB2

DATABASE=db; HOSTNAME=dbhost; PORT=40000; PROTOCOL=TCPIP; UID=me; PWD=secret;

DB2 LUW Via CGO

sqlconnect;[DATABASE=;][UID=;][PWD=;]

DSN=Sample; UID=asif; PWD=secrect; AUTOCOMMIT=0; CONNECTTYPE=1;
DATABASE=db; HOSTNAME=dbhost; PORT=40000; PROTOCOL=TCPIP; UID=me; PWD=secret;

Firebird SQL

user:[email protected][:port_number]/database_name_or_file[?params1=value1[&param2=value2]...]

MS ADODB

DSN=myDsn;Uid=myUsername;Pwd=;
FILEDSN=c:myDsnFile.dsn;Uid=myUsername;Pwd=;

MS SQL Server (pure go)

qlserver://username:[email protected]/instance?param1=value&param2=value

MS SQL Server (uses cgo)

Server=myServerAddress;Database=myDatabase;User Id=myUsername;Password=myPassword;Failover Partner=myMirror;Max Pool Size=200;Compatibility Mode=Sybase

MySQL by ziutek

[PROTOCOL_SPECIFIC*]DBNAME/USER/PASSWD

// DBNAME/USER/PASSWD
// unix:SOCKPATH*DBNAME/USER/PASSWD
// unix:SOCKPATH,OPTIONS*DBNAME/USER/PASSWD
// tcp:ADDR*DBNAME/USER/PASSWD
// tcp:ADDR,OPTIONS*DBNAME/USER/PASSWD

ODBC

DSN=myDsn;Uid=myUsername;Pwd=;
FILEDSN=c:myDsnFile.dsn;Uid=myUsername;Pwd=;

ODBC

DSN=myDsn;Uid=myUsername;Pwd=;
FILEDSN=c:myDsnFile.dsn;Uid=myUsername;Pwd=;

Oracle

[username/[password]@]host[:port][/instance_name][?param1=value1&...&paramN=valueN]

Oracle

username/[email protected]

Oracle

ora://login:[email protected]/? 
  sysdba=0& 
  sysoper=0& 
  poolMinSessions=1& 
  poolMaxSessions=1000& 
  poolIncrement=1& 
  connectionClass=POOLED& 
  standaloneConnection=0& 
  enableEvents=0& 
  heterogeneousPool=0& 
  prelim=0

QL

ql.db
file://ql.db
memory://mem.db

Postgres (pure Go)

user=username password=password host=1.2.3.4 port=5432 dbname=mydb sslmode=disable
application_name=pgxtest search_path=admin user=username password=password host=1.2.3.4 dbname=mydb

Presto

http[s]://user[:pass]@host[:port][?parameters]

SAP HANA (pure go)

hdb://:@:

// "hdb://myuser:[email protected]:39013?TLSRootCAFile=trust.pem"
// "hdb://myuser:[email protected]:39013?TLSRootCAFile=trust.pem&TLSServerName=hostname"
// "hdb://myuser:[email protected]:39013?TLSInsecureSkipVerify"

Snowflake (pure Go)

jsmith:[email protected]/mydb/testschema?warehouse=mywh

username[:password]@accountname/dbname/schemaname[?param1=value&...&paramN=valueN
username[:password]@accountname/dbname[?param1=value&...&paramN=valueN
username[:password]@hostname:port/dbname/schemaname?account=[&param1=value&...&paramN=valueN]

SQLite (uses cgo)

file:data.db
file:/home/fred/data.db
file:///home/fred/data.db
file://localhost/home/fred/data.db
file:data.db?mode=ro&cache=private
file:/home/fred/data.db?vfs=unix-dotfile
file:data.db?mode=readonly

// ":memory:" for memory db, "" for temp file db

Sybase SQL Anywhere

parameter1=value1;parameter2=value2;...
UID=DBA;PWD=sql;Host=demo12;DatabaseName=demo;ServerName=myserver
HOST=myhost;DBN=demo;UID=DBA;PWD=sql;ServerName=demo12

Vitess

db, err := vitessdriver.Open("localhost:15991", "keyspace", "master",30*time.Second)

NoSQL數據庫

Memcached

mc := memcache.New("10.0.0.1:11211", "10.0.0.2:11211", "10.0.0.3:11212")

Mongodb

client, err := mongo.NewClient("mongodb://localhost:27017")

Redis

c, err := redis.Dial("tcp", ":6379")

redis-go-cluster

cluster, err := redis.NewCluster(
    &redis.Options{
	StartNodes: []string{"127.0.0.1:7000", "127.0.0.1:7001", "127.0.0.1:7002"},
	ConnTimeout:50 * time.Millisecond,
	ReadTimeout:50 * time.Millisecond,
	WriteTimeout:50 * time.Millisecond,
	KeepAlive:16,
	AliveTime:60 * time.Second,
    })

原文 : colobu bolg

相關閱讀

免责声明:本文内容来源于colobu bolg,已注明原文出处和链接,文章观点不代表立场,如若侵犯到您的权益,或涉不实谣言,敬请向我们提出检举。