Skip to content

[Update] serverless tier example #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions serverless/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2022 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


.PHONY: all build run

all:
make build run

build:
go build -o bin/sql-driver-example

run:
./bin/sql-driver-example
5 changes: 5 additions & 0 deletions serverless/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/pingcap-inc/tidb-example-golang/serverless

go 1.18

require github.com/go-sql-driver/mysql v1.6.0
2 changes: 2 additions & 0 deletions serverless/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
43 changes: 43 additions & 0 deletions serverless/serverless.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"crypto/tls"
"database/sql"
"fmt"
"github.com/go-sql-driver/mysql"
)

const (
tlsConfigKeyName = "tidb"
host = "{serverless tier host}"
port = 4000
username = "{serverless tier username}"
password = "{serverless tier password}"
)

func main() {
mysql.RegisterTLSConfig(tlsConfigKeyName, &tls.Config{
MinVersion: tls.VersionTLS12,
ServerName: host,
})

dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/test?tls=%s",
username, password, host, port, tlsConfigKeyName)
db, err := sql.Open("mysql", dsn)
if err != nil {
panic(err)
}
defer db.Close()

rows, err := db.Query("SELECT VERSION()")
if err != nil {
panic(err)
}
defer rows.Close()

for rows.Next() {
version := ""
rows.Scan(&version)
fmt.Println(version)
}
}