mysqlknife 2.0.5 → 2.1.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 +10 -7
- data/lib/mysqlknife.rb +0 -2
- data/lib/mysqlknife/cli.rb +34 -118
- data/lib/mysqlknife/command.rb +8 -10
- data/lib/mysqlknife/configs.rb +39 -57
- data/lib/mysqlknife/iterm.rb +6 -5
- data/lib/mysqlknife/log.rb +0 -23
- data/lib/mysqlknife/mysql.rb +60 -16
- data/lib/mysqlknife/parameter/base.rb +39 -0
- data/lib/mysqlknife/parameter/command.rb +34 -0
- data/lib/mysqlknife/parameter/config.rb +18 -0
- data/lib/mysqlknife/parameter/console.rb +18 -0
- data/lib/mysqlknife/parameter/describe.rb +18 -0
- data/lib/mysqlknife/version.rb +1 -1
- metadata +10 -42
- data/lib/mysqlknife/mysql/command.rb +0 -82
- data/lib/mysqlknife/mysql/kill.rb +0 -75
- data/lib/mysqlknife/mysql/replica.rb +0 -39
- data/lib/mysqlknife/mysql/sql.rb +0 -58
- data/lib/mysqlknife/parameters.rb +0 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a03ea9be5431114a2994d4555e8b180154bcfb9e
|
4
|
+
data.tar.gz: 123936c664b5f65e7ab9acbccbca92f5207ae916
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 09df895d047d11cd9381dcde799f1230075b9bb71fd1ca0b2ccc1b30c027b1dc3725094e8d6aad8d78af3ff9614b269060f49c2112664c35caa9d137f5787a9a
|
7
|
+
data.tar.gz: a53af626107070712aa14ed86a5a3ef4a684250f6cfb48f70defc5511785ff4ad9351bc0e10dfea4a9acb9265402154693a020e7ab49f8ee1f945972f13ee043
|
data/README.md
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
# Mysqlknife [![Build Status](https://travis-ci.org/swapbytes/mysqlknife.svg?branch=newfeature%2F3)](https://travis-ci.org/swapbytes/mysqlknife)
|
2
2
|
|
3
3
|
MySQL Knife is a Ruby Gem and command line tools, written for MAC OS because use
|
4
|
-
iTerm2 to set tab colors on terminal. That allows many
|
5
|
-
|
4
|
+
iTerm2 to set tab colors on terminal. That allows many connections bookmark and
|
5
|
+
tasks for MySQL Administration. You can:
|
6
6
|
|
7
7
|
- Connections bookmark.
|
8
|
-
-
|
9
|
-
-
|
10
|
-
- Kill process.
|
8
|
+
- Connections over SSH.
|
9
|
+
- Run command on active connection.
|
11
10
|
|
12
11
|
![mysqlknife](https://github.com/nicola51980/mysqlknife/blob/master/assets/mysqlknife.gif)
|
13
12
|
|
@@ -36,6 +35,12 @@ ssh:
|
|
36
35
|
port: 22
|
37
36
|
keys: '~/.ssh/default.pem'
|
38
37
|
|
38
|
+
commands:
|
39
|
+
innotop:
|
40
|
+
'innotop -h #{host} -u #{username} #{password}'
|
41
|
+
lag:
|
42
|
+
'mysql -h #{host} -u #{username} #{password} --execute="SHOW SLAVE STATUS\G"'
|
43
|
+
|
39
44
|
databases:
|
40
45
|
localhost:
|
41
46
|
ssh: false
|
@@ -55,8 +60,6 @@ databases:
|
|
55
60
|
slaves:
|
56
61
|
- devel-mysql-slave01.demo.com
|
57
62
|
- devel-mysql-slave02.demo.com
|
58
|
-
tools:
|
59
|
-
- 'innotop -h #{@host} -u #{@username} #{@password}'
|
60
63
|
|
61
64
|
prod:
|
62
65
|
ssh: true
|
data/lib/mysqlknife.rb
CHANGED
data/lib/mysqlknife/cli.rb
CHANGED
@@ -2,143 +2,59 @@
|
|
2
2
|
|
3
3
|
module Mysqlknife
|
4
4
|
class CLI < Thor
|
5
|
-
class_option :
|
5
|
+
class_option :debug, :type => :boolean
|
6
6
|
|
7
7
|
def initialize(*args)
|
8
8
|
super
|
9
9
|
|
10
|
-
|
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]
|
10
|
+
$DEBUG = options[:debug]
|
17
11
|
end
|
18
12
|
|
19
13
|
desc 'config [CONFIG]', 'Show connection details.'
|
20
14
|
def config(conn = nil)
|
21
|
-
|
22
|
-
|
23
|
-
|
15
|
+
config = Parameter::Config.new
|
16
|
+
config.connection = conn
|
17
|
+
config.connections
|
18
|
+
config.show
|
24
19
|
end
|
25
20
|
|
26
|
-
desc 'console [CONFIG] [
|
21
|
+
desc 'console [CONFIG] [HOST]', 'Connect to specific CONNECTION.'
|
27
22
|
option :execute,
|
28
23
|
desc: 'Execute query.',
|
29
24
|
type: :string,
|
30
25
|
aliases: :e
|
31
|
-
def console(conn = nil,
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
@config.mysql_host = @parameters.host
|
40
|
-
@config.mysql_database = database
|
41
|
-
|
42
|
-
@command.execute(@mysql_cmd.console(options[:execute]))
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
desc 'lag [CONFIG]', '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
|
52
|
-
|
53
|
-
if @parameters.slave_selected
|
54
|
-
@config.connection(conn)
|
55
|
-
@config.mysql_host = @parameters.host
|
56
|
-
|
57
|
-
@command.execute(@mysql_cmd.console(@mysql_sql.replica_lag))
|
58
|
-
end
|
26
|
+
def console(conn = nil, host = nil)
|
27
|
+
console = Parameter::Console.new
|
28
|
+
console.connection = conn
|
29
|
+
console.host = host
|
30
|
+
console.execute = options[:execute]
|
31
|
+
console.connections
|
32
|
+
console.hosts
|
33
|
+
console.connect
|
59
34
|
end
|
60
35
|
|
61
|
-
desc 'describe [CONFIG] [DATABASE] [TABLE]', 'Describe database, table, keys details.'
|
36
|
+
desc 'describe [CONFIG] [HOST] [DATABASE] [TABLE]', 'Describe database, table, keys details.'
|
62
37
|
def describe(conn = nil, host = nil, database = nil, table = nil)
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
@command.execute(@mysql_cmd.describe(database, table))
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
desc 'command [CONFIG] [TOOL]', 'Execute specific command line.'
|
77
|
-
def command(conn = nil, tool = nil)
|
78
|
-
@parameters.connection = conn
|
79
|
-
@parameters.tool = tool
|
80
|
-
@parameters.connections
|
81
|
-
@parameters.tools
|
82
|
-
|
83
|
-
if @parameters.tool_selected
|
84
|
-
@config.connection(conn)
|
85
|
-
@config.mysql_host = @parameters.host
|
86
|
-
|
87
|
-
@command.execute(@mysql_cmd.parse(@parameters.tool))
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
desc 'kill [CONFIG]', 'Kill process in MySQL or RDS'
|
92
|
-
long_desc <<-LONGDESC
|
93
|
-
Syntax:
|
94
|
-
This action no bypass to ssh.
|
95
|
-
|
96
|
-
mysqlknife kill [CONFIG] --where "user = 'foo' AND time >= 28800"
|
97
|
-
LONGDESC
|
98
|
-
option :where,
|
99
|
-
default: 'time >= 28800',
|
100
|
-
desc: 'Conditions to filter.',
|
101
|
-
required: true,
|
102
|
-
type: :string,
|
103
|
-
aliases: :w
|
104
|
-
option :kill,
|
105
|
-
desc: 'Execute kill statements.',
|
106
|
-
type: :boolean
|
107
|
-
def kill(conn = nil, host = nil)
|
108
|
-
@parameters.connection = conn
|
109
|
-
@parameters.host = host
|
110
|
-
@parameters.connections
|
111
|
-
@parameters.hosts
|
112
|
-
|
113
|
-
if @parameters.host_selected
|
114
|
-
@config.connection(conn)
|
115
|
-
@config.mysql_host = @parameters.host
|
116
|
-
|
117
|
-
@kill = Mysql::Kill.new
|
118
|
-
@kill.where = options[:where]
|
119
|
-
|
120
|
-
if options[:kill]
|
121
|
-
@kill.clear
|
122
|
-
else
|
123
|
-
@kill.show
|
124
|
-
end
|
125
|
-
end
|
38
|
+
describe = Parameter::Describe.new
|
39
|
+
describe.connection = conn
|
40
|
+
describe.host = host
|
41
|
+
describe.database = database
|
42
|
+
describe.table = table
|
43
|
+
describe.connections
|
44
|
+
describe.hosts
|
45
|
+
describe.connect
|
126
46
|
end
|
127
47
|
|
128
|
-
desc '
|
129
|
-
def
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
@replica = Mysql::Replica.new
|
140
|
-
@replica.skip
|
141
|
-
end
|
48
|
+
desc 'command [CONFIG] [HOST] [COMMAND]', 'Execute specific command line.'
|
49
|
+
def command(conn = nil, host = nil, cmd = nil)
|
50
|
+
command = Parameter::Command.new
|
51
|
+
command.connection = conn
|
52
|
+
command.host = host
|
53
|
+
command.cmd = cmd
|
54
|
+
command.connections
|
55
|
+
command.hosts
|
56
|
+
command.commands
|
57
|
+
command.connect
|
142
58
|
end
|
143
59
|
|
144
60
|
desc 'version', 'Show version number.'
|
data/lib/mysqlknife/command.rb
CHANGED
@@ -3,25 +3,23 @@
|
|
3
3
|
module Mysqlknife
|
4
4
|
class Command
|
5
5
|
def initialize
|
6
|
-
@
|
7
|
-
@ssh
|
8
|
-
@iterm
|
6
|
+
@config = Configs.instance
|
7
|
+
@ssh = SSH.new
|
8
|
+
@iterm = Iterm.new
|
9
9
|
end
|
10
10
|
|
11
11
|
def execute(cmd)
|
12
|
-
if cmd.nil?
|
13
|
-
exit 1
|
14
|
-
end
|
12
|
+
return false if cmd.nil?
|
15
13
|
|
16
|
-
@iterm.color(@
|
17
|
-
@iterm.name(@
|
14
|
+
@iterm.color(@config.color)
|
15
|
+
@iterm.name(@config.name)
|
18
16
|
|
19
|
-
if @
|
17
|
+
if @config.ssh[:use]
|
20
18
|
cmd = @ssh.execute(cmd)
|
21
19
|
end
|
22
20
|
|
23
21
|
Mysqlknife::Log.new.command(cmd)
|
24
|
-
system
|
22
|
+
system(cmd)
|
25
23
|
ensure
|
26
24
|
@iterm.name
|
27
25
|
@iterm.reset
|
data/lib/mysqlknife/configs.rb
CHANGED
@@ -4,25 +4,14 @@ module Mysqlknife
|
|
4
4
|
class Configs
|
5
5
|
include Singleton
|
6
6
|
|
7
|
-
attr_writer :
|
8
|
-
|
9
|
-
|
10
|
-
:ssh_host,
|
11
|
-
:ssh_port,
|
12
|
-
:ssh_user,
|
13
|
-
:ssh_password,
|
14
|
-
:ssh_key,
|
15
|
-
:ssh_color,
|
16
|
-
:ssh_use,
|
7
|
+
attr_writer :connection
|
8
|
+
attr_reader :mysql,
|
9
|
+
:ssh,
|
17
10
|
:connections,
|
18
|
-
:
|
11
|
+
:color,
|
19
12
|
:name,
|
20
|
-
:
|
21
|
-
:
|
22
|
-
:mysql_password,
|
23
|
-
:mysql_port,
|
24
|
-
:mysql_database,
|
25
|
-
:mysql_slave
|
13
|
+
:host,
|
14
|
+
:commands
|
26
15
|
|
27
16
|
def initialize
|
28
17
|
# Define path for config file:
|
@@ -41,13 +30,16 @@ module Mysqlknife
|
|
41
30
|
end
|
42
31
|
|
43
32
|
# Define generic variables:
|
44
|
-
@
|
45
|
-
@
|
46
|
-
|
47
|
-
@
|
48
|
-
@
|
49
|
-
@
|
50
|
-
@
|
33
|
+
@ssh = {}
|
34
|
+
@mysql = {}
|
35
|
+
|
36
|
+
@ssh[:host] = @configs['ssh']['host']
|
37
|
+
@ssh[:port] = @configs['ssh']['port']
|
38
|
+
@ssh[:user] = @configs['ssh']['user']
|
39
|
+
@ssh[:password] = @configs['ssh']['password']
|
40
|
+
@ssh[:key] = @configs['ssh']['keys']
|
41
|
+
@connections = @configs['connections'].keys.sort
|
42
|
+
@commands = @configs['commands'].keys.sort
|
51
43
|
end
|
52
44
|
|
53
45
|
def method_missing(name, *args)
|
@@ -56,52 +48,42 @@ module Mysqlknife
|
|
56
48
|
param = method[1]
|
57
49
|
conn = args[0]
|
58
50
|
|
59
|
-
if @configs['
|
60
|
-
@configs['
|
61
|
-
else
|
62
|
-
puts "Not exist connecion name: #{conn}"
|
63
|
-
exit 1
|
51
|
+
if @configs['connections'].include?(conn)
|
52
|
+
@configs['connections'][conn][param]
|
64
53
|
end
|
65
54
|
end
|
66
55
|
end
|
67
56
|
|
68
57
|
def connection(name)
|
69
|
-
@name
|
70
|
-
@
|
71
|
-
@
|
72
|
-
@
|
73
|
-
@
|
74
|
-
@
|
75
|
-
@
|
76
|
-
@
|
58
|
+
@name = name
|
59
|
+
@color = db_color(@name)
|
60
|
+
@ssh[:use] = db_ssh(@name)
|
61
|
+
@mysql[:host] = db_host(@name)
|
62
|
+
@mysql[:port] = db_port(@name)
|
63
|
+
@mysql[:username] = db_username(@name)
|
64
|
+
@mysql[:password] = db_password(@name)
|
65
|
+
@mysql[:slaves] = db_slaves(@name)
|
77
66
|
end
|
78
67
|
|
79
|
-
def
|
80
|
-
|
81
|
-
|
82
|
-
username = db_username(name)
|
83
|
-
password = db_password(name)
|
84
|
-
slave = db_slave(name)
|
68
|
+
def hosts
|
69
|
+
(@mysql[:slaves] = []) if @mysql[:slaves].nil?
|
70
|
+
(@mysql[:host] = '') if @mysql[:host].nil?
|
85
71
|
|
86
|
-
|
87
|
-
Slaves:\ #{slave}
|
88
|
-
Port:\ #{port}
|
89
|
-
Username:\ #{username}
|
90
|
-
Password:\ #{password}].join("\n")
|
72
|
+
(@mysql[:host] + ',' + @mysql[:slaves].join(',')).split(',')
|
91
73
|
end
|
92
74
|
|
93
|
-
def
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
exit 1
|
75
|
+
def select(name)
|
76
|
+
if hosts.is_a?(Array)
|
77
|
+
hosts.each do |host|
|
78
|
+
return @host = host if host.include?(name)
|
79
|
+
end
|
99
80
|
end
|
81
|
+
nil
|
100
82
|
end
|
101
83
|
|
102
|
-
def
|
103
|
-
|
104
|
-
|
84
|
+
def command(name)
|
85
|
+
if @configs['commands'].key?(name)
|
86
|
+
@configs['commands'][name]
|
105
87
|
end
|
106
88
|
end
|
107
89
|
end
|
data/lib/mysqlknife/iterm.rb
CHANGED
@@ -2,9 +2,11 @@
|
|
2
2
|
|
3
3
|
module Mysqlknife
|
4
4
|
class Iterm
|
5
|
-
|
5
|
+
def initialize
|
6
|
+
@color = { orange: [255,128,000],
|
6
7
|
green: [000,255,000],
|
7
8
|
red: [255,000,000] }
|
9
|
+
end
|
8
10
|
|
9
11
|
def name(name = nil)
|
10
12
|
print "\033]0;#{name}\007"
|
@@ -12,10 +14,9 @@ module Mysqlknife
|
|
12
14
|
|
13
15
|
def color(name)
|
14
16
|
name = name.to_sym
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
blue = @@colors[name][2]
|
17
|
+
red = @color[name][0]
|
18
|
+
green = @color[name][1]
|
19
|
+
blue = @color[name][2]
|
19
20
|
|
20
21
|
print "\033]6;1;bg;red;brightness;#{red}\a"
|
21
22
|
print "\033]6;1;bg;green;brightness;#{green}\a"
|
data/lib/mysqlknife/log.rb
CHANGED
@@ -8,31 +8,8 @@ module Mysqlknife
|
|
8
8
|
@log = Logger.new(STDOUT)
|
9
9
|
end
|
10
10
|
|
11
|
-
def kill(process)
|
12
|
-
process = process.map { |k, v| "#{k}: #{v}" }.join(', ')
|
13
|
-
@log.info("Kill MySQL Process: #{process}")
|
14
|
-
end
|
15
|
-
|
16
|
-
def sql(statement)
|
17
|
-
@log.debug("SQL Statement: #{statement}") if $DEBUG
|
18
|
-
end
|
19
|
-
|
20
|
-
def sql_error(error, sql)
|
21
|
-
@log.debug("SQL Error: #{error}")
|
22
|
-
@log.debug("SQL Statement: #{sql}")
|
23
|
-
end
|
24
|
-
|
25
11
|
def command(command)
|
26
12
|
@log.debug("Bash Command: #{command}") if $DEBUG
|
27
13
|
end
|
28
|
-
|
29
|
-
def skip_repl_error(status)
|
30
|
-
status = status.map { |k, v| "#{k}: #{v}" }.join(', ')
|
31
|
-
@log.info("MySQL Skip error replica: #{status}")
|
32
|
-
end
|
33
|
-
|
34
|
-
def message(text)
|
35
|
-
@log.info(text)
|
36
|
-
end
|
37
14
|
end
|
38
15
|
end
|
data/lib/mysqlknife/mysql.rb
CHANGED
@@ -1,29 +1,73 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
module Mysqlknife
|
4
|
-
class
|
4
|
+
class Mysql
|
5
5
|
def initialize
|
6
|
-
@
|
6
|
+
@config = Configs.instance
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
9
|
+
def command?(name)
|
10
|
+
`which #{name}`
|
11
|
+
$?.success?
|
12
|
+
end
|
13
|
+
|
14
|
+
def check
|
15
|
+
command?('mysql')
|
16
|
+
command?('mysqlshow')
|
17
|
+
end
|
16
18
|
|
17
|
-
|
19
|
+
def purge(string)
|
20
|
+
string.squeeze(' ').strip
|
21
|
+
end
|
18
22
|
|
19
|
-
|
23
|
+
def parse(command)
|
24
|
+
host = @config.host
|
25
|
+
port = @config.mysql[:port]
|
26
|
+
username = @config.mysql[:username]
|
27
|
+
password = password(@config.mysql[:password])
|
28
|
+
command = eval("\"#{command}\"")
|
29
|
+
purge(command)
|
30
|
+
end
|
31
|
+
|
32
|
+
def prompt
|
33
|
+
if !!(@config.host =~ Resolv::IPv4::Regex)
|
34
|
+
@config.name
|
20
35
|
else
|
21
|
-
|
22
|
-
exit(1)
|
36
|
+
@config.host.partition('.').first
|
23
37
|
end
|
24
|
-
|
25
|
-
|
26
|
-
|
38
|
+
end
|
39
|
+
|
40
|
+
def execute(sentence)
|
41
|
+
"--execute='#{sentence}'" unless sentence.nil? || sentence.empty?
|
42
|
+
end
|
43
|
+
|
44
|
+
def password(password)
|
45
|
+
"-p#{password}" unless password.nil? || password.empty?
|
46
|
+
end
|
47
|
+
|
48
|
+
def console(sentence = nil)
|
49
|
+
command = %W[mysql
|
50
|
+
-h #{@config.host}
|
51
|
+
-P #{@config.mysql[:port]}
|
52
|
+
-u #{@config.mysql[:username]}
|
53
|
+
#{password(@config.mysql[:password])}
|
54
|
+
--prompt='#{prompt}[\\d]> '
|
55
|
+
#{@config.mysql[:database]}
|
56
|
+
#{execute(sentence)}
|
57
|
+
2>/dev/null].join(' ')
|
58
|
+
purge(command)
|
59
|
+
end
|
60
|
+
|
61
|
+
def describe(*args)
|
62
|
+
command = %W[mysqlshow
|
63
|
+
-h #{@config.host}
|
64
|
+
-P #{@config.mysql[:port]}
|
65
|
+
-u #{@config.mysql[:username]}
|
66
|
+
#{password(@config.mysql[:password])}
|
67
|
+
--keys
|
68
|
+
#{args.join(' ')}
|
69
|
+
2>/dev/null].join(' ')
|
70
|
+
purge(command)
|
27
71
|
end
|
28
72
|
end
|
29
73
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Mysqlknife
|
4
|
+
module Parameter
|
5
|
+
class Base
|
6
|
+
attr_writer :connection, :host
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@command = Mysqlknife::Command.new
|
10
|
+
@config = Mysqlknife::Configs.instance
|
11
|
+
@mysql = Mysqlknife::Mysql.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def connections
|
15
|
+
if @connection.nil?
|
16
|
+
puts "Connections:"
|
17
|
+
@config.connections.each do |conn|
|
18
|
+
puts " - #{conn}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def hosts
|
24
|
+
if @connection.nil? == false &&
|
25
|
+
@host.nil? == true
|
26
|
+
@config.connection(@connection)
|
27
|
+
puts "Hosts for connection: #{@connection}"
|
28
|
+
if @config.hosts.nil? == false &&
|
29
|
+
@config.hosts.count > 0 &&
|
30
|
+
@config.hosts.is_a?(Array)
|
31
|
+
@config.hosts.each do |host|
|
32
|
+
puts " - #{host}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Mysqlknife
|
4
|
+
module Parameter
|
5
|
+
class Command < Parameter::Base
|
6
|
+
attr_writer :cmd
|
7
|
+
|
8
|
+
def commands
|
9
|
+
if @connection.nil? == false &&
|
10
|
+
@host.nil? == false &&
|
11
|
+
@cmd.nil?
|
12
|
+
puts "Commands:"
|
13
|
+
if @config.commands.nil? == false &&
|
14
|
+
@config.commands.count > 0 &&
|
15
|
+
@config.commands.is_a?(Array)
|
16
|
+
@config.commands.each do |command|
|
17
|
+
puts " - #{command}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def connect
|
24
|
+
if @connection.nil? == false &&
|
25
|
+
@host.nil? == false &&
|
26
|
+
@cmd.nil? == false
|
27
|
+
@config.connection(@connection)
|
28
|
+
puts "Connecting to: #{@config.select(@host)}"
|
29
|
+
@command.execute(@mysql.parse(@config.command(@cmd)))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Mysqlknife
|
4
|
+
module Parameter
|
5
|
+
class Config < Parameter::Base
|
6
|
+
def show
|
7
|
+
unless @connection.nil?
|
8
|
+
@config.connection(@connection)
|
9
|
+
puts "Connection: #{@connection}"
|
10
|
+
puts " Hosts: #{@config.hosts.join(', ')}"
|
11
|
+
puts " Port: #{@config.mysql[:port]}"
|
12
|
+
puts " Username: #{@config.mysql[:username]}"
|
13
|
+
puts " Password: #{@config.mysql[:password]}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Mysqlknife
|
4
|
+
module Parameter
|
5
|
+
class Console < Parameter::Base
|
6
|
+
attr_writer :execute
|
7
|
+
|
8
|
+
def connect
|
9
|
+
@config.connection(@connection)
|
10
|
+
if @connection.nil? == false &&
|
11
|
+
@host.nil? == false
|
12
|
+
puts "Connecting to: #{@config.select(@host)}"
|
13
|
+
@command.execute(@mysql.console(@execute))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Mysqlknife
|
4
|
+
module Parameter
|
5
|
+
class Describe < Parameter::Base
|
6
|
+
attr_writer :database, :table
|
7
|
+
|
8
|
+
def connect
|
9
|
+
@config.connection(@connection)
|
10
|
+
if @connection.nil? == false &&
|
11
|
+
@host.nil? == false
|
12
|
+
puts "Connecting to: #{@config.select(@host)}"
|
13
|
+
@command.execute(@mysql.describe(@database, @table))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/mysqlknife/version.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mysqlknife
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicola Strappazzon C.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: mysql2
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - '='
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: 0.3.18
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - '='
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: 0.3.18
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: thor
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,20 +24,6 @@ dependencies:
|
|
38
24
|
- - '='
|
39
25
|
- !ruby/object:Gem::Version
|
40
26
|
version: 0.19.1
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: terminal-table
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - '='
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: 1.4.5
|
48
|
-
type: :runtime
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - '='
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: 1.4.5
|
55
27
|
- !ruby/object:Gem::Dependency
|
56
28
|
name: minitest
|
57
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -95,11 +67,8 @@ dependencies:
|
|
95
67
|
- !ruby/object:Gem::Version
|
96
68
|
version: '10.1'
|
97
69
|
description: MySQL Knife is a Ruby Gem and command line tools, written for UNIX-like
|
98
|
-
operating systems. That allows many tasks
|
99
|
-
|
100
|
-
servers (Master/Slave for example), Kill process with query, skipping the current
|
101
|
-
replication error.
|
102
|
-
email: nicola51980@gmail.com
|
70
|
+
operating systems. That allows many connections bookmark and tasks for MySQL Administration.
|
71
|
+
email: nicola@swapbytes.com
|
103
72
|
executables:
|
104
73
|
- mysqlknife
|
105
74
|
extensions: []
|
@@ -111,12 +80,12 @@ files:
|
|
111
80
|
- lib/mysqlknife/configs.rb
|
112
81
|
- lib/mysqlknife/iterm.rb
|
113
82
|
- lib/mysqlknife/log.rb
|
114
|
-
- lib/mysqlknife/mysql/command.rb
|
115
|
-
- lib/mysqlknife/mysql/kill.rb
|
116
|
-
- lib/mysqlknife/mysql/replica.rb
|
117
|
-
- lib/mysqlknife/mysql/sql.rb
|
118
83
|
- lib/mysqlknife/mysql.rb
|
119
|
-
- lib/mysqlknife/
|
84
|
+
- lib/mysqlknife/parameter/base.rb
|
85
|
+
- lib/mysqlknife/parameter/command.rb
|
86
|
+
- lib/mysqlknife/parameter/config.rb
|
87
|
+
- lib/mysqlknife/parameter/console.rb
|
88
|
+
- lib/mysqlknife/parameter/describe.rb
|
120
89
|
- lib/mysqlknife/ssh.rb
|
121
90
|
- lib/mysqlknife/version.rb
|
122
91
|
- lib/mysqlknife.rb
|
@@ -145,6 +114,5 @@ rubyforge_project:
|
|
145
114
|
rubygems_version: 2.0.14.1
|
146
115
|
signing_key:
|
147
116
|
specification_version: 4
|
148
|
-
summary: MySQL
|
149
|
-
error.
|
117
|
+
summary: MySQL Connections bookmark and tasks.
|
150
118
|
test_files: []
|
@@ -1,82 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module Mysqlknife
|
4
|
-
module Mysql
|
5
|
-
class Command
|
6
|
-
def initialize
|
7
|
-
@cnf = Configs.instance
|
8
|
-
end
|
9
|
-
|
10
|
-
def connection(name)
|
11
|
-
@cnf.connection(name)
|
12
|
-
end
|
13
|
-
|
14
|
-
def host(host)
|
15
|
-
@cnf.mysql_host = host
|
16
|
-
end
|
17
|
-
|
18
|
-
def command?(name)
|
19
|
-
`which #{name}`
|
20
|
-
$?.success?
|
21
|
-
end
|
22
|
-
|
23
|
-
def check
|
24
|
-
command?('mysql')
|
25
|
-
command?('mysqlshow')
|
26
|
-
end
|
27
|
-
|
28
|
-
def purge(string)
|
29
|
-
string.squeeze(' ').strip
|
30
|
-
end
|
31
|
-
|
32
|
-
def parse(command)
|
33
|
-
host = @cnf.mysql_host
|
34
|
-
port = @cnf.mysql_port
|
35
|
-
username = @cnf.mysql_username
|
36
|
-
password = password(@cnf.mysql_password)
|
37
|
-
command = eval("\"#{command}\"")
|
38
|
-
purge(command)
|
39
|
-
end
|
40
|
-
|
41
|
-
def prompt
|
42
|
-
if !!(@cnf.mysql_host =~ Resolv::IPv4::Regex)
|
43
|
-
@cnf.name
|
44
|
-
else
|
45
|
-
@cnf.mysql_host.partition('.').first
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def execute(sentence)
|
50
|
-
"--execute='#{sentence}'" unless sentence.nil? || sentence.empty?
|
51
|
-
end
|
52
|
-
|
53
|
-
def password(password)
|
54
|
-
"-p#{password}" unless password.nil? || password.empty?
|
55
|
-
end
|
56
|
-
|
57
|
-
def console(sentence = nil)
|
58
|
-
command = %W[mysql
|
59
|
-
-h #{@cnf.mysql_host}
|
60
|
-
-P #{@cnf.mysql_port}
|
61
|
-
-u #{@cnf.mysql_username}
|
62
|
-
#{password(@cnf.mysql_password)}
|
63
|
-
--prompt='#{prompt}[\\d]> '
|
64
|
-
#{@cnf.mysql_database}
|
65
|
-
#{execute(sentence)}
|
66
|
-
].join(' ')
|
67
|
-
purge(command)
|
68
|
-
end
|
69
|
-
|
70
|
-
def describe(*args)
|
71
|
-
command = %W[mysqlshow
|
72
|
-
-h #{@cnf.mysql_host}
|
73
|
-
-P #{@cnf.mysql_port}
|
74
|
-
-u #{@cnf.mysql_username}
|
75
|
-
#{password(@cnf.mysql_password)}
|
76
|
-
--keys
|
77
|
-
#{args.join(' ')}].join(' ')
|
78
|
-
purge(command)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
@@ -1,75 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module Mysqlknife
|
4
|
-
module Mysql
|
5
|
-
class Kill
|
6
|
-
attr_writer :where
|
7
|
-
|
8
|
-
def initialize
|
9
|
-
@mysql = MySQL.new
|
10
|
-
@mysql_sql = Mysql::SQL.new
|
11
|
-
end
|
12
|
-
|
13
|
-
def clear
|
14
|
-
if check_privileges
|
15
|
-
list.each do |process|
|
16
|
-
kill(process)
|
17
|
-
end
|
18
|
-
else
|
19
|
-
Mysqlknife::Log.new.message("Don't have privileges to kill process")
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def check_user
|
24
|
-
sql = @mysql_sql.current_user
|
25
|
-
result = @mysql.execute(sql).first['CURRENT_USER'] =~ /^root\@/
|
26
|
-
|
27
|
-
return true if result
|
28
|
-
end
|
29
|
-
|
30
|
-
def check_grants
|
31
|
-
sql = @mysql_sql.current_user_grants
|
32
|
-
result = @mysql.execute(sql).first.values.to_s =~ /(PROCESS)|(ALL PRIVILEGES)/
|
33
|
-
|
34
|
-
return true if result
|
35
|
-
end
|
36
|
-
|
37
|
-
def check_privileges
|
38
|
-
check_user || check_grants
|
39
|
-
end
|
40
|
-
|
41
|
-
def list
|
42
|
-
@mysql.execute(@mysql_sql.show_processlist(@where))
|
43
|
-
end
|
44
|
-
|
45
|
-
def show
|
46
|
-
process = list
|
47
|
-
|
48
|
-
unless process.first.nil?
|
49
|
-
table = Terminal::Table.new do |t|
|
50
|
-
t.add_row(process.first.keys)
|
51
|
-
t.add_separator
|
52
|
-
process.each do |row|
|
53
|
-
t.add_row(row.values)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
puts table
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def rds?
|
61
|
-
! @mysql.execute(@mysql_sql.show_procedure('rds_kill')).first
|
62
|
-
end
|
63
|
-
|
64
|
-
def kill(process)
|
65
|
-
if rds?
|
66
|
-
@mysql.execute(@mysql_sql.mysql_kill(process['id']))
|
67
|
-
else
|
68
|
-
@mysql.execute(@mysql_sql.rds_kill(process['id']))
|
69
|
-
end
|
70
|
-
|
71
|
-
Mysqlknife::Log.new.kill(process)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
@@ -1,39 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module Mysqlknife
|
4
|
-
module Mysql
|
5
|
-
class Replica
|
6
|
-
def initialize
|
7
|
-
@mysql = MySQL.new
|
8
|
-
@mysql_cmd = Mysql::Command.new
|
9
|
-
@mysql_sql = Mysql::SQL.new
|
10
|
-
|
11
|
-
@status = slave_status
|
12
|
-
end
|
13
|
-
|
14
|
-
def sql_running?
|
15
|
-
@status['Slave_SQL_Running'] == 'Yes'
|
16
|
-
end
|
17
|
-
|
18
|
-
def rds?
|
19
|
-
! @mysql.execute(@mysql_sql.show_procedure('mysql.rds_skip_repl_error')).first
|
20
|
-
end
|
21
|
-
|
22
|
-
def slave_status
|
23
|
-
@mysql.execute(@mysql_sql.slave_status).first
|
24
|
-
end
|
25
|
-
|
26
|
-
def skip
|
27
|
-
unless sql_running?
|
28
|
-
Mysqlknife::Log.new.skip_repl_error(@status)
|
29
|
-
|
30
|
-
if rds?
|
31
|
-
@mysql.execute(@mysql_sql.rds_skip_repl_error)
|
32
|
-
else
|
33
|
-
@mysql.execute(@mysql_sql.mysql_skip_repl_error)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
data/lib/mysqlknife/mysql/sql.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module Mysqlknife
|
4
|
-
module Mysql
|
5
|
-
class SQL
|
6
|
-
def current_user
|
7
|
-
'SELECT CURRENT_USER;'
|
8
|
-
end
|
9
|
-
|
10
|
-
def current_user_grants
|
11
|
-
'SHOW GRANTS FOR CURRENT_USER;'
|
12
|
-
end
|
13
|
-
|
14
|
-
def replica_lag
|
15
|
-
'SHOW SLAVE STATUS\G'
|
16
|
-
end
|
17
|
-
|
18
|
-
def show_processlist(where = nil)
|
19
|
-
sql = %w(SELECT id, user, host, db, command, time, state, info
|
20
|
-
FROM INFORMATION_SCHEMA.PROCESSLIST
|
21
|
-
WHERE state NOT REGEXP '(slave|relay|event)'
|
22
|
-
AND user NOT IN ('rdsadmin',
|
23
|
-
'rdsrepladmin',
|
24
|
-
'system user',
|
25
|
-
'event_scheduler')
|
26
|
-
AND id != CONNECTION_ID()
|
27
|
-
AND command != 'Binlog Dump').join(' ')
|
28
|
-
sql << " AND #{where};" unless where.nil?
|
29
|
-
end
|
30
|
-
|
31
|
-
def show_procedure(name)
|
32
|
-
"SHOW PROCEDURE STATUS LIKE '#{name}';"
|
33
|
-
end
|
34
|
-
|
35
|
-
def mysql_skip_repl_error
|
36
|
-
%w(STOP SLAVE;
|
37
|
-
SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;
|
38
|
-
START SLAVE;).join(' ')
|
39
|
-
end
|
40
|
-
|
41
|
-
def mysql_kill(pid)
|
42
|
-
"KILL #{pid};"
|
43
|
-
end
|
44
|
-
|
45
|
-
def slave_status
|
46
|
-
'SHOW SLAVE STATUS;'
|
47
|
-
end
|
48
|
-
|
49
|
-
def rds_kill(pid)
|
50
|
-
"CALL mysql.rds_kill(#{pid});"
|
51
|
-
end
|
52
|
-
|
53
|
-
def rds_skip_repl_error
|
54
|
-
'CALL mysql.rds_skip_repl_error;'
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module Mysqlknife
|
4
|
-
class Parameters
|
5
|
-
attr_writer :connection, :tool, :database
|
6
|
-
attr_reader :command, :database
|
7
|
-
|
8
|
-
def initialize
|
9
|
-
@cnf = Configs.instance
|
10
|
-
end
|
11
|
-
|
12
|
-
def connections
|
13
|
-
if @connection.nil?
|
14
|
-
puts @cnf.connections
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def connection_details
|
19
|
-
unless @connection.nil?
|
20
|
-
puts @cnf.show(@connection)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def tools
|
25
|
-
if @connection.nil? == false &&
|
26
|
-
@tool.nil? == true
|
27
|
-
puts @cnf.tools(@connection)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|