vitess.io/vitess@v0.16.2/go/vt/mysqlctl/cmd.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 /* 18 This file contains common functions for cmd/mysqlctl and cmd/mysqlctld. 19 */ 20 21 package mysqlctl 22 23 import ( 24 "fmt" 25 26 "vitess.io/vitess/go/vt/dbconfigs" 27 ) 28 29 // CreateMysqldAndMycnf returns a Mysqld and a Mycnf object to use for working with a MySQL 30 // installation that hasn't been set up yet. 31 func CreateMysqldAndMycnf(tabletUID uint32, mysqlSocket string, mysqlPort int32) (*Mysqld, *Mycnf, error) { 32 mycnf := NewMycnf(tabletUID, mysqlPort) 33 // Choose a random MySQL server-id, since this is a fresh data dir. 34 // We don't want to use the tablet UID as the MySQL server-id, 35 // because reusing server-ids is not safe. 36 // 37 // For example, if a tablet comes back with an empty data dir, it will restore 38 // from backup and then connect to the primary. But if this tablet has the same 39 // server-id as before, and if this tablet was recently a primary, then it can 40 // lose data by skipping binlog events due to replicate-same-server-id=FALSE, 41 // which is the default setting. 42 if err := mycnf.RandomizeMysqlServerID(); err != nil { 43 return nil, nil, fmt.Errorf("couldn't generate random MySQL server_id: %v", err) 44 } 45 if mysqlSocket != "" { 46 mycnf.SocketFile = mysqlSocket 47 } 48 49 dbconfigs.GlobalDBConfigs.InitWithSocket(mycnf.SocketFile) 50 return NewMysqld(&dbconfigs.GlobalDBConfigs), mycnf, nil 51 } 52 53 // OpenMysqldAndMycnf returns a Mysqld and a Mycnf object to use for working with a MySQL 54 // installation that already exists. The Mycnf will be built based on the my.cnf file 55 // of the MySQL instance. 56 func OpenMysqldAndMycnf(tabletUID uint32) (*Mysqld, *Mycnf, error) { 57 // We pass a port of 0, this will be read and overwritten from the path on disk 58 mycnf, err := ReadMycnf(NewMycnf(tabletUID, 0)) 59 if err != nil { 60 return nil, nil, fmt.Errorf("couldn't read my.cnf file: %v", err) 61 } 62 63 dbconfigs.GlobalDBConfigs.InitWithSocket(mycnf.SocketFile) 64 return NewMysqld(&dbconfigs.GlobalDBConfigs), mycnf, nil 65 }