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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5bf6062612d1868ec0ace249f6411307680db9b5
4
- data.tar.gz: 82cd3ccbc43e2dd9faebb746a4c05afd466187e5
3
+ metadata.gz: db1b26e73684c076fb6e0b2cf993b4767b17ebf5
4
+ data.tar.gz: 37d88cd748be7657a8887eecc55053f35ad51ca0
5
5
  SHA512:
6
- metadata.gz: cffcc0fa53b48aa686b8cf0f73a59e2eedd16dff643aba68f23c48a2c15a4c1ba3d20d56156e38d1c6d5f2d79e32ef0f64956e2f5e1f9d66d416481500c89843
7
- data.tar.gz: aaf2eda2aca81c62e18795b312c7e4b73d7e9d3ae64f2bffad99804da79b322061b6616ab4180e01cdaf10374b8c7eba700b51e6f7ea623b7e760ede4e18dca9
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 UNIX-like
4
- operating systems. That allows many tasks in MySQL Administration is easily for
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
- - Checksum tables.
7
+ - Configure many connections to access and run any commands.
8
8
  - Skip error is slave.
9
- - Swap Databases
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
- ## Usage
19
+ ## Configurarion
21
20
 
22
- ### Checksum table
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
- ### Swap Databases
42
-
43
- Swap tables and views between two databases.
44
-
45
- You need to rename two databases, for example, foo and bar. You only need to run the
46
- following command and save the STDOUT output to a file and then execute DML
47
- statements.
48
-
49
- ```Shell
50
- $ mysqlknife swap --host 127.0.0.1 \
51
- --user root \
52
- --password admin \
53
- --databases from,to > swap.sql
54
- $ mysql -h 127.0.0.1 -u root -padmin < swap.sql
55
- ```
56
-
57
- ### Kill process
58
-
59
- This action permit kill MySQL process in Amazon RDS or normal service, and log
60
- output who kill.
61
-
62
- Please, follow next instructions to kill process:
63
-
64
- ```Shell
65
- $ mysqlknife kill --host 127.0.0.1 \
66
- --user root \
67
- --pass admin \
68
- --where "time >= 200"
69
- ```
70
-
71
- And kill the listed process:
72
-
73
- ```Shell
74
- $ mysqlknife kill --host 127.0.0.1 \
75
- --user root \
76
- --pass admin \
77
- --where "time >= 200" \
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}/*.rb"].select do |file|
15
+ Dir["#{LIBRARY_PATH}/**/*.rb"].select do |file|
13
16
  if File.file? file
14
- require File.join(LIBRARY_PATH, File.basename(file, '.rb'))
17
+ require file
15
18
  end
16
19
  end
17
20
  end
@@ -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 'checksum', 'Checksum table.'
8
- long_desc <<-LONGDESC
9
- DSN:
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
- These DSN (Data Source Name) options are used to create a DSN. Each option
12
- is given like option=value. The options are case-sensitive, so P and p are
13
- not the same option. There cannot be whitespace before or after the = and
14
- if the value contains whitespace it must be quoted. DSN options are
15
- comma-separated. This idea is get from percona-toolkit.
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
- h=<host>,P=<port>,u=<user>,p=<password>,d=<database>
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
- Example: h=127.0.0.1,P=3306,u=root,p=admin,d=demo_from
53
+ if @parameters.slave_selected
54
+ @config.settings(conn)
55
+ @config.mysql_host = @parameters.host
20
56
 
21
- Syntax:
57
+ @command.execute(@mysql_cmd.console(@mysql_sql.replica_lag))
58
+ end
59
+ end
22
60
 
23
- mysqlknife checksum
24
- --from h=127.0.0.1,P=3306,u=root,p=admin,d=demo_from
25
- --to h=127.0.0.1,P=3306,u=root,p=admin,d=demo_to
26
- --table foo
27
- LONGDESC
28
- option :from,
29
- desc: 'DSN to connect to source database.',
30
- required: true,
31
- type: :string
32
- option :to,
33
- desc: 'DSN to connect to destination database.',
34
- required: true,
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 'kill', 'Kill process in MySQL or RDS'
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 --host 127.0.0.1 --user root --password admin
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: 'time >= 28800',
64
- desc: 'Conditions to filter.',
101
+ default: 'time >= 28800',
102
+ desc: 'Conditions to filter.',
65
103
  required: true,
66
- type: :string
104
+ type: :string,
105
+ aliases: :w
67
106
  option :kill,
68
107
  desc: 'Execute kill statements.',
69
108
  type: :boolean
70
- def kill
71
- kill = Mysqlknife::Kill.new(options)
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
- puts kill.show
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 --host 127.0.0.1 --user root --password admin --behind
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
- def skip
105
- rep = Mysqlknife::Replica.new(options)
106
- rep.skip
107
- end
108
-
109
- desc 'swap', 'Invert two databases.'
110
- long_desc <<-LONGDESC
111
- Move all tables and views from DB(A) to DB(B), and DB(B) to DB(A).
112
- Use rename of SQL Statements for this action.
113
-
114
- Syntax:
115
-
116
- mysqlknife swap --host 127.0.0.1 --user root --password admin
117
- --databases from,to
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