各种数据库的连接字符串的连接格式(一般叫做database source name, 简称DSN)是不同的,本文汇总了各个数据库驱动程序的字符串连接方式。
关系型数据库
1 2 3 4 5 6 7 8 9 10 11 12 13
| [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=valueN]
|
1 2 3 4 5 6 7 8 9
| postgres: postgres: postgres: postgres: postgres: postgres: postgres: postgres: postgres:
|
1 2 3 4
| test.db file:test.db?cache=shared&mode=memory :memory: file::memory:
|
更多参数可以查看SQLiteDriver.Open
各部分的详细设置可以查看How to use SQL driver
1
| db=default&output_location=s3:
|
可选值 poll_frequency
和region
1
| DATABASE=db; HOSTNAME=dbhost; PORT=40000; PROTOCOL=TCPIP; UID=me; PWD=secret;
|
1 2 3 4
| 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;
|
1
| user:password@servername[:port_number]/database_name_or_file[?params1=value1[¶m2=value2]...]
|
1 2
| DSN=myDsn;Uid=myUsername;Pwd=; FILEDSN=c:\myDsnFile.dsn;Uid=myUsername;Pwd=;
|
1
| Server=myServerAddress;Database=myDatabase;User Id=myUsername;Password=myPassword;Failover Partner=myMirror;Max Pool Size=200;Compatibility Mode=Sybase
|
1 2 3 4 5 6 7
| [PROTOCOL_SPECIFIC*]DBNAME/USER/PASSWD
|
1 2
| DSN=myDsn;Uid=myUsername;Pwd=; FILEDSN=c:\myDsnFile.dsn;Uid=myUsername;Pwd=;
|
1 2
| DSN=myDsn;Uid=myUsername;Pwd=; FILEDSN=c:\myDsnFile.dsn;Uid=myUsername;Pwd=;
|
1
| [username/[password]@]host[:port][/instance_name][?param1=value1&...¶mN=valueN]
|
1
| username/password@dblink
|
1 2 3 4 5 6 7 8 9 10 11
| ora: sysdba=0& \ sysoper=0& \ poolMinSessions=1& \ poolMaxSessions=1000& \ poolIncrement=1& \ connectionClass=POOLED& \ standaloneConnection=0& \ enableEvents=0& \ heterogeneousPool=0& \ prelim=0
|
1 2
| 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
|
1 2 3 4 5
| jsmith:mypassword@myaccount/mydb/testschema?warehouse=mywh username[:password]@accountname/dbname/schemaname[?param1=value&...¶mN=valueN username[:password]@accountname/dbname[?param1=value&...¶mN=valueN username[:password]@hostname:port/dbname/schemaname?account=<your_account>[¶m1=value&...¶mN=valueN]
|
1 2 3 4 5 6 7 8 9
| file:data.db file:/home/fred/data.db file: file: file:data.db?mode=ro&cache=private file:/home/fred/data.db?vfs=unix-dotfile file:data.db?mode=readonly
|
1 2 3
| parameter1=value1;parameter2=value2;... UID=DBA;PWD=sql;Host=demo12;DatabaseName=demo;ServerName=myserver HOST=myhost;DBN=demo;UID=DBA;PWD=sql;ServerName=demo12
|
1
| db, err := vitessdriver.Open("localhost:15991", "keyspace", "master", 30*time.Second)
|
NoSQL数据库
1
| mc := memcache.New("10.0.0.1:11211", "10.0.0.2:11211", "10.0.0.3:11212")
|
1
| client, err := mongo.NewClient("mongodb://localhost:27017")
|
1
| c, err := redis.Dial("tcp", ":6379")
|
1 2 3 4 5 6 7 8 9
| 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, })
|