mysqlknife 1.4.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +45 -62
- data/lib/mysqlknife.rb +5 -2
- data/lib/mysqlknife/cli.rb +120 -101
- data/lib/mysqlknife/command.rb +26 -0
- data/lib/mysqlknife/configs.rb +123 -0
- data/lib/mysqlknife/iterm.rb +29 -0
- data/lib/mysqlknife/log.rb +34 -0
- data/lib/mysqlknife/mysql.rb +24 -0
- data/lib/mysqlknife/mysql/command.rb +63 -0
- data/lib/mysqlknife/mysql/kill.rb +73 -0
- data/lib/mysqlknife/mysql/replica.rb +41 -0
- data/lib/mysqlknife/mysql/sql.rb +58 -0
- data/lib/mysqlknife/parameters.rb +84 -0
- data/lib/mysqlknife/ssh.rb +18 -0
- data/lib/mysqlknife/version.rb +1 -1
- metadata +13 -9
- data/lib/mysqlknife/checksum.rb +0 -26
- data/lib/mysqlknife/config.rb +0 -60
- data/lib/mysqlknife/connection.rb +0 -30
- data/lib/mysqlknife/kill.rb +0 -72
- data/lib/mysqlknife/replica.rb +0 -51
- data/lib/mysqlknife/sql.rb +0 -114
- data/lib/mysqlknife/swap.rb +0 -67
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db1b26e73684c076fb6e0b2cf993b4767b17ebf5
|
4
|
+
data.tar.gz: 37d88cd748be7657a8887eecc55053f35ad51ca0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 636335f0afbe89bd19897bfaed2b03a44ad02fa76f0adf42a1a670327f9a9b8ff13eba7fcc6371ecdea8efc8ed3af16d7180ae5633e666a7c735bc6e251262b0
|
7
|
+
data.tar.gz: a1be16f270047d7e224cbf3366ba79c576949b87f278ec0581eeda220cbbb0eed35c25c39df3fda044a549c701a2e933566f226798fec1d92122685366d137fa
|
data/README.md
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
# Mysqlknife
|
2
2
|
|
3
|
-
MySQL Knife is a Ruby Gem and command line tools, written for
|
4
|
-
|
5
|
-
normal installation or in Amazon RDS. You can:
|
3
|
+
MySQL Knife is a Ruby Gem and command line tools, written for MAC OS because use
|
4
|
+
iTerm2 to set colors. That allows many tasks in MySQL Administration is easily for
|
5
|
+
normal installation (MySQL) or in Amazon RDS. You can:
|
6
6
|
|
7
|
-
-
|
7
|
+
- Configure many connections to access and run any commands.
|
8
8
|
- Skip error is slave.
|
9
|
-
-
|
10
|
-
- Kill process for RDS or MySQL.
|
9
|
+
- Kill process.
|
11
10
|
|
12
11
|
## Installation
|
13
12
|
|
@@ -17,65 +16,49 @@ Install this tool executing the following command:
|
|
17
16
|
$ gem install mysqlknife
|
18
17
|
```
|
19
18
|
|
20
|
-
##
|
19
|
+
## Configurarion
|
21
20
|
|
22
|
-
|
21
|
+
vim ~/.db.yml
|
23
22
|
|
24
|
-
Compare two tables with checksum:
|
25
|
-
|
26
|
-
```Shell
|
27
|
-
$ mysqlknife checksum --from h=127.0.0.1,P=3306,u=root,p=admin,d=demo_from \
|
28
|
-
--to h=127.0.0.1,P=3306,u=root,p=admin,d=demo_to \
|
29
|
-
--table foo
|
30
|
-
```
|
31
|
-
|
32
|
-
### Skip error in slave.
|
33
|
-
|
34
|
-
```Shell
|
35
|
-
$ mysqlknife skip --host 127.0.0.1 \
|
36
|
-
--user root \
|
37
|
-
--password admin \
|
38
|
-
--behind -120
|
39
23
|
```
|
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
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
--kill
|
24
|
+
ssh:
|
25
|
+
user: root
|
26
|
+
host: demo.com
|
27
|
+
port: 22
|
28
|
+
keys: '~/.ssh/default.pem'
|
29
|
+
|
30
|
+
databases:
|
31
|
+
localhost:
|
32
|
+
ssh: false
|
33
|
+
color: green
|
34
|
+
port: 3306
|
35
|
+
host: 127.0.0.1
|
36
|
+
username: root
|
37
|
+
password: admin
|
38
|
+
|
39
|
+
devel:
|
40
|
+
ssh: false
|
41
|
+
color: orange
|
42
|
+
port: 3306
|
43
|
+
host: master.demo.com
|
44
|
+
username: root
|
45
|
+
password: admin
|
46
|
+
slaves:
|
47
|
+
- devel-mysql-slave01.demo.com
|
48
|
+
- devel-mysql-slave02.demo.com
|
49
|
+
tools:
|
50
|
+
- 'innotop -h #{@host} -u #{@username} #{@password}'
|
51
|
+
|
52
|
+
prod:
|
53
|
+
ssh: true
|
54
|
+
color: red
|
55
|
+
port: 3306
|
56
|
+
host: prod-mysql-master.demo.com
|
57
|
+
username: root
|
58
|
+
password: admin
|
59
|
+
slaves:
|
60
|
+
- prod-mysql-slave01.demo.com
|
61
|
+
- prod-mysql-slave02.demo.com
|
79
62
|
```
|
80
63
|
|
81
64
|
## Warning
|
data/lib/mysqlknife.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
+
require 'logger'
|
3
4
|
require 'mysql2'
|
5
|
+
require 'resolv'
|
6
|
+
require 'singleton'
|
4
7
|
require 'terminal-table'
|
5
8
|
require 'thor'
|
6
9
|
require 'yaml'
|
@@ -9,9 +12,9 @@ module Mysqlknife
|
|
9
12
|
LIBRARY_PATH = File.join(File.dirname(__FILE__), 'mysqlknife')
|
10
13
|
|
11
14
|
# Autoload libraries:
|
12
|
-
Dir["#{LIBRARY_PATH}
|
15
|
+
Dir["#{LIBRARY_PATH}/**/*.rb"].select do |file|
|
13
16
|
if File.file? file
|
14
|
-
require
|
17
|
+
require file
|
15
18
|
end
|
16
19
|
end
|
17
20
|
end
|
data/lib/mysqlknife/cli.rb
CHANGED
@@ -1,144 +1,163 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module Mysqlknife
|
2
4
|
class CLI < Thor
|
5
|
+
class_option :verbose, :type => :boolean
|
6
|
+
|
3
7
|
def initialize(*args)
|
4
8
|
super
|
9
|
+
|
10
|
+
@config = Configs.instance
|
11
|
+
@mysql = MySQL.new
|
12
|
+
@mysql_cmd = Mysql::Command.new
|
13
|
+
@mysql_sql = Mysql::SQL.new
|
14
|
+
@command = Command.new
|
15
|
+
@parameters = Parameters.new
|
16
|
+
$DEBUG = options[:verbose]
|
5
17
|
end
|
6
18
|
|
7
|
-
desc '
|
8
|
-
|
9
|
-
|
19
|
+
desc 'config [CONFIG]', 'Show connection details.'
|
20
|
+
def config(conn = nil)
|
21
|
+
@parameters.connection = conn
|
22
|
+
@parameters.connections
|
23
|
+
@parameters.connection_details
|
24
|
+
end
|
10
25
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
26
|
+
desc 'console [CONFIG] [HOST] [DATABASE]', 'Connect to specific CONNECTION.'
|
27
|
+
option :execute,
|
28
|
+
desc: 'Execute query.',
|
29
|
+
type: :string,
|
30
|
+
aliases: :e
|
31
|
+
def console(conn = nil, host = nil, database = nil)
|
32
|
+
@parameters.connection = conn
|
33
|
+
@parameters.host = host
|
34
|
+
@parameters.connections
|
35
|
+
@parameters.hosts
|
36
|
+
|
37
|
+
if @parameters.host_selected
|
38
|
+
@config.settings(conn)
|
39
|
+
@config.mysql_host = @parameters.host
|
40
|
+
@config.mysql_database = database
|
41
|
+
|
42
|
+
@command.execute(@mysql_cmd.console(options[:execute]))
|
43
|
+
end
|
44
|
+
end
|
16
45
|
|
17
|
-
|
46
|
+
desc 'lag [CONFIG] [HOST]', 'Show replica lag status.'
|
47
|
+
def lag(conn = nil, host = nil)
|
48
|
+
@parameters.connection = conn
|
49
|
+
@parameters.host = host
|
50
|
+
@parameters.connections
|
51
|
+
@parameters.slaves
|
18
52
|
|
19
|
-
|
53
|
+
if @parameters.slave_selected
|
54
|
+
@config.settings(conn)
|
55
|
+
@config.mysql_host = @parameters.host
|
20
56
|
|
21
|
-
|
57
|
+
@command.execute(@mysql_cmd.console(@mysql_sql.replica_lag))
|
58
|
+
end
|
59
|
+
end
|
22
60
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
type: :string
|
36
|
-
option :table,
|
37
|
-
desc: 'Name of table to compare.',
|
38
|
-
required: true,
|
39
|
-
type: :string
|
40
|
-
def checksum
|
41
|
-
unless Mysqlknife::Checksum.new(options).equal?
|
42
|
-
puts "-- Table '#{options[:table]}' is not equal data."
|
43
|
-
exit 1
|
61
|
+
desc 'describe [CONFIG] [HOST] [DATABASE] [TABLE]', 'Describe database, table, keys details.'
|
62
|
+
def describe(conn = nil, host = nil, database = nil, table = nil)
|
63
|
+
@parameters.connection = conn
|
64
|
+
@parameters.host = host
|
65
|
+
@parameters.connections
|
66
|
+
@parameters.hosts
|
67
|
+
|
68
|
+
if @parameters.host_selected
|
69
|
+
@config.settings(conn)
|
70
|
+
@config.mysql_host = @parameters.host
|
71
|
+
|
72
|
+
@command.execute(@mysql_cmd.describe(database, table))
|
44
73
|
end
|
45
74
|
end
|
46
75
|
|
47
|
-
desc '
|
76
|
+
desc 'command [CONFIG] [HOST] [TOOL]', 'Execute specific command line.'
|
77
|
+
def command(conn = nil, host = nil, tool = nil)
|
78
|
+
@parameters.connection = conn
|
79
|
+
@parameters.host = host
|
80
|
+
@parameters.tool = tool
|
81
|
+
@parameters.connections
|
82
|
+
@parameters.hosts
|
83
|
+
@parameters.tools
|
84
|
+
|
85
|
+
if @parameters.tool_selected
|
86
|
+
@config.settings(conn)
|
87
|
+
@config.mysql_host = @parameters.host
|
88
|
+
|
89
|
+
@command.execute(@mysql_cmd.parse(@parameters.tool))
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
desc 'kill [CONFIG] [HOST]', 'Kill process in MySQL or RDS'
|
48
94
|
long_desc <<-LONGDESC
|
49
95
|
Syntax:
|
96
|
+
This action no bypass to ssh.
|
50
97
|
|
51
|
-
mysqlknife kill
|
52
|
-
--where "time >= 28800"
|
98
|
+
mysqlknife kill [CONFIG] [HOST] --where "user = 'foo' AND time >= 28800"
|
53
99
|
LONGDESC
|
54
|
-
option :host,
|
55
|
-
type: :string
|
56
|
-
option :user,
|
57
|
-
type: :string
|
58
|
-
option :password,
|
59
|
-
type: :string
|
60
|
-
option :port,
|
61
|
-
type: :numeric
|
62
100
|
option :where,
|
63
|
-
default:
|
64
|
-
desc:
|
101
|
+
default: 'time >= 28800',
|
102
|
+
desc: 'Conditions to filter.',
|
65
103
|
required: true,
|
66
|
-
type:
|
104
|
+
type: :string,
|
105
|
+
aliases: :w
|
67
106
|
option :kill,
|
68
107
|
desc: 'Execute kill statements.',
|
69
108
|
type: :boolean
|
70
|
-
def kill
|
71
|
-
|
109
|
+
def kill(conn = nil, host = nil)
|
110
|
+
@parameters.connection = conn
|
111
|
+
@parameters.host = host
|
112
|
+
@parameters.connections
|
113
|
+
@parameters.hosts
|
114
|
+
|
115
|
+
if @parameters.host_selected
|
116
|
+
@config.settings(conn)
|
117
|
+
@config.mysql_host = @parameters.host
|
118
|
+
|
119
|
+
@kill = Mysql::Kill.new
|
120
|
+
@kill.where = options[:where]
|
72
121
|
|
73
|
-
if kill.check_privileges
|
74
122
|
if options[:kill]
|
75
|
-
kill.clear
|
123
|
+
@kill.clear
|
76
124
|
else
|
77
|
-
|
125
|
+
@kill.show
|
78
126
|
end
|
79
|
-
else
|
80
|
-
puts 'User does not have process privileges.'
|
81
127
|
end
|
82
128
|
end
|
83
129
|
|
84
|
-
desc 'skip', 'Skipping the current replication error in Amazon RDS.'
|
130
|
+
desc 'skip [CONFIG] [HOST]', 'Skipping the current replication error in Amazon RDS.'
|
85
131
|
long_desc <<-LONGDESC
|
86
132
|
Syntax:
|
87
133
|
|
88
|
-
mysqlknife skip --
|
89
|
-
-120
|
134
|
+
mysqlknife skip --behind -120
|
90
135
|
LONGDESC
|
91
|
-
option :host,
|
92
|
-
type: :string
|
93
|
-
option :user,
|
94
|
-
type: :string
|
95
|
-
option :password,
|
96
|
-
type: :string
|
97
|
-
option :port,
|
98
|
-
type: :numeric
|
99
136
|
option :behind,
|
100
137
|
default: 120,
|
101
138
|
desc: 'Set Seconds Behind Master',
|
102
139
|
required: true,
|
103
|
-
type: :numeric
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
LONGDESC
|
119
|
-
option :host,
|
120
|
-
type: :string
|
121
|
-
option :user,
|
122
|
-
type: :string
|
123
|
-
option :password,
|
124
|
-
type: :string
|
125
|
-
option :port,
|
126
|
-
type: :numeric
|
127
|
-
option :databases,
|
128
|
-
desc: 'Name of databases to swap.',
|
129
|
-
required: true,
|
130
|
-
type: :string
|
131
|
-
def swap
|
132
|
-
if options[:databases].split(',').count == 2
|
133
|
-
swap = Mysqlknife::Swap.new(options).all
|
134
|
-
|
135
|
-
if swap
|
136
|
-
swap.each do |sql|
|
137
|
-
puts sql
|
138
|
-
end
|
139
|
-
end
|
140
|
+
type: :numeric,
|
141
|
+
aliases: :b
|
142
|
+
def skip(conn = nil, host = nil)
|
143
|
+
@parameters.connection = conn
|
144
|
+
@parameters.host = host
|
145
|
+
@parameters.connections
|
146
|
+
@parameters.slaves
|
147
|
+
|
148
|
+
if @parameters.slave_selected
|
149
|
+
@config.settings(conn)
|
150
|
+
@config.mysql_host = @parameters.host
|
151
|
+
|
152
|
+
@replica = Mysql::Replica.new
|
153
|
+
@replica.behind = options[:behind]
|
154
|
+
@replica.skip
|
140
155
|
end
|
141
156
|
end
|
142
157
|
|
158
|
+
desc 'version', 'Show version number.'
|
159
|
+
def version
|
160
|
+
puts Mysqlknife::VERSION
|
161
|
+
end
|
143
162
|
end
|
144
163
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Mysqlknife
|
4
|
+
class Command
|
5
|
+
def initialize
|
6
|
+
@cnf = Configs.instance
|
7
|
+
@ssh = SSH.new
|
8
|
+
@iterm = Iterm.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def execute(cmd)
|
12
|
+
@iterm.color(@cnf.ssh_color)
|
13
|
+
@iterm.name(@cnf.conn)
|
14
|
+
|
15
|
+
if @cnf.ssh_use
|
16
|
+
cmd = @ssh.execute(cmd)
|
17
|
+
end
|
18
|
+
|
19
|
+
Mysqlknife::Log.new.command(cmd)
|
20
|
+
system cmd
|
21
|
+
ensure
|
22
|
+
@iterm.name
|
23
|
+
@iterm.reset
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|