-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathcmd.go
66 lines (55 loc) · 2.56 KB
/
cmd.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
63
64
65
66
/*
Copyright 2019 The Vitess Authors.
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.
*/
/*
This file contains common functions for cmd/mysqlctl and cmd/mysqlctld.
*/
package mysqlctl
import (
"fmt"
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/vt/dbconfigs"
)
// CreateMysqldAndMycnf returns a Mysqld and a Mycnf object to use for working with a MySQL
// installation that hasn't been set up yet.
func CreateMysqldAndMycnf(tabletUID uint32, mysqlSocket string, mysqlPort int, collationEnv *collations.Environment) (*Mysqld, *Mycnf, error) {
mycnf := NewMycnf(tabletUID, mysqlPort)
// Choose a random MySQL server-id, since this is a fresh data dir.
// We don't want to use the tablet UID as the MySQL server-id,
// because reusing server-ids is not safe.
//
// For example, if a tablet comes back with an empty data dir, it will restore
// from backup and then connect to the primary. But if this tablet has the same
// server-id as before, and if this tablet was recently a primary, then it can
// lose data by skipping binlog events due to replicate-same-server-id=FALSE,
// which is the default setting.
if err := mycnf.RandomizeMysqlServerID(); err != nil {
return nil, nil, fmt.Errorf("couldn't generate random MySQL server_id: %v", err)
}
if mysqlSocket != "" {
mycnf.SocketFile = mysqlSocket
}
dbconfigs.GlobalDBConfigs.InitWithSocket(mycnf.SocketFile, collationEnv)
return NewMysqld(&dbconfigs.GlobalDBConfigs), mycnf, nil
}
// OpenMysqldAndMycnf returns a Mysqld and a Mycnf object to use for working with a MySQL
// installation that already exists. The Mycnf will be built based on the my.cnf file
// of the MySQL instance.
func OpenMysqldAndMycnf(tabletUID uint32, collationEnv *collations.Environment) (*Mysqld, *Mycnf, error) {
// We pass a port of 0, this will be read and overwritten from the path on disk
mycnf, err := ReadMycnf(NewMycnf(tabletUID, 0), 0)
if err != nil {
return nil, nil, fmt.Errorf("couldn't read my.cnf file: %v", err)
}
dbconfigs.GlobalDBConfigs.InitWithSocket(mycnf.SocketFile, collationEnv)
return NewMysqld(&dbconfigs.GlobalDBConfigs), mycnf, nil
}