0
0

Go 各种数据库连接字符串汇总

鸟窝 发表于 2019年01月11日 11:43 | Hits: 339

各种数据库的连接字符串的连接格式(一般叫做database source name, 简称DSN )是不同的,本文汇总了各个数据库驱动程序的字符串连接方式。

关系型数据库

MySQL

12345678910111213
[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]// user@unix(/path/to/socket)/dbname// root:pw@unix(/tmp/mysql.sock)/myDatabase?loc=Local// user:password@tcp(localhost:5555)/dbname?tls=skip-verify&autocommit=true// user:password@/dbname?sql_mode=TRADITIONAL// user:password@tcp([de:ad:be:ef::ca:fe]:80)/dbname?timeout=90s&collation=utf8mb4_unicode_ci// id:password@tcp(your-amazonaws-uri.com:3306)/dbname// user@cloudsql(project-id:instance-name)/dbname// user@cloudsql(project-id:regionname:instance-name)/dbname// user:password@tcp/dbname?charset=utf8mb4,utf8&sys_var=esc%40ped// user:password@/dbname// user:password@/

Postgres (pure Go)

123456789
postgres://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...]postgres://postgres://localhostpostgres://localhost:5433postgres://localhost/mydbpostgres://user@localhostpostgres://user:secret@localhostpostgres://other@localhost/otherdb?connect_timeout=10&application_name=myapppostgres://host1:123,host2:456/somedb?target_session_attrs=any&application_name=myapp

SQLite (uses cgo)

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

更多参数可以查看SQLiteDriver.Open

Apache Ignite/GridGain

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

各部分的详细设置可以查看How to use SQL driver

Apache Avatica/Phoenix

1
http://[username:password@]address:port[/schema][?parameter1=value&...parameterN=value]

AWS Athena

1
db=default&output_location=s3://results

可选值poll_frequency和region

ClickHouse Via TCP

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

ClickHouse Via HTTP

1
schema://user:password@host[:port]/database?param1=value1&...&paramN=valueN

DB2

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

DB2 LUW Via CGO

1234
sqlconnect;[DATABASE=<database_name>;][UID=<user_id>;][PWD=<password>;]DSN=Sample; UID=asif; PWD=secrect; AUTOCOMMIT=0; CONNECTTYPE=1;DATABASE=db; HOSTNAME=dbhost; PORT=40000; PROTOCOL=TCPIP; UID=me; PWD=secret;

Firebird SQL

1
user:password@servername[:port_number]/database_name_or_file[?params1=value1[&param2=value2]...]

MS ADODB

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

MS SQL Server (pure go)

1
qlserver://username:password@host/instance?param1=value&param2=value

MS SQL Server (uses cgo)

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

MySQL by ziutek

1234567
[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

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

ODBC

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

Oracle

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

Oracle

1
username/password@dblink

Oracle

1234567891011
ora://login:password@sid/? \  sysdba=0& \  sysoper=0& \  poolMinSessions=1& \  poolMaxSessions=1000& \  poolIncrement=1& \  connectionClass=POOLED& \  standaloneConnection=0& \  enableEvents=0& \  heterogeneousPool=0& \  prelim=0

QL

123
ql.dbfile://ql.dbmemory://mem.db

Postgres (pure Go)

12
user=username password=password host=1.2.3.4 port=5432 dbname=mydb sslmode=disableapplication_name=pgxtest search_path=admin user=username password=password host=1.2.3.4 dbname=mydb

Presto

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

SAP HANA (pure go)

12345
hdb://<username>:<password>@<host address>:<port number>// "hdb://myuser:mypassword@localhost:39013?TLSRootCAFile=trust.pem"// "hdb://myuser:mypassword@localhost:39013?TLSRootCAFile=trust.pem&TLSServerName=hostname"// "hdb://myuser:mypassword@localhost:39013?TLSInsecureSkipVerify"

Snowflake (pure Go)

12345
jsmith:mypassword@myaccount/mydb/testschema?warehouse=mywhusername[:password]@accountname/dbname/schemaname[?param1=value&...&paramN=valueNusername[:password]@accountname/dbname[?param1=value&...&paramN=valueNusername[:password]@hostname:port/dbname/schemaname?account=<your_account>[&param1=value&...&paramN=valueN]

SQLite (uses cgo)

123456789
file:data.dbfile:/home/fred/data.dbfile:///home/fred/data.db file://localhost/home/fred/data.db file:data.db?mode=ro&cache=privatefile:/home/fred/data.db?vfs=unix-dotfilefile:data.db?mode=readonly// ":memory:" for memory db, "" for temp file db

Sybase SQL Anywhere

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

Vitess

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

NoSQL数据库

Memcached

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

Mongodb

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

Redis

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

redis-go-cluster

123456789
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,    })

原文链接: https://colobu.com/2019/01/10/drivers-connection-string-in-Go/

0     0

评价列表(0)