forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.go
62 lines (53 loc) · 1.88 KB
/
bootstrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2013 The ql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSES/QL-LICENSE file.
// Copyright 2015 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package tidb
import (
"fmt"
"github.com/ngaut/log"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/util/errors"
"github.com/pingcap/tidb/util/errors2"
)
const (
// CreateUserTable is the SQL statement creates User table in system db.
CreateUserTable = "CREATE TABLE if not exists mysql.user (Host CHAR(64), User CHAR(16), Password CHAR(41), PRIMARY KEY (Host, User));"
)
// Bootstrap initiates system DB for a store.
func bootstrap(s Session) {
// Create a test database.
mustExecute(s, "CREATE DATABASE IF NOT EXISTS test")
// Check if system db exists.
_, err := s.Execute(fmt.Sprintf("USE %s;", mysql.SystemDB))
if err == nil {
// We have already finished bootstrap.
return
} else if !errors2.ErrorEqual(err, errors.ErrDatabaseNotExist) {
log.Fatal(err)
}
mustExecute(s, fmt.Sprintf("CREATE DATABASE %s;", mysql.SystemDB))
initUserTable(s)
}
func initUserTable(s Session) {
mustExecute(s, CreateUserTable)
// Insert a default user with empty password.
mustExecute(s, `INSERT INTO mysql.user VALUES ("localhost", "root", ""), ("127.0.0.1", "root", ""), ("::1", "root", "");`)
}
func mustExecute(s Session, sql string) {
_, err := s.Execute(sql)
if err != nil {
log.Fatal(err)
}
}