-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmysqlc
executable file
·86 lines (67 loc) · 1.62 KB
/
mysqlc
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long qw/:config posix_default no_ignore_case bundling permute/;
GetOptions(
'h|help' => \my $help,
) or usage();
if ($help) {
usage();
}
my $cmd = shift or usage();
eval {
my $cmd = main->can("cmd_$cmd") or die "no such command: $cmd\n";
$cmd->(@ARGV);
};
if ($@) {
usage($@);
}
sub mysql_root {
my (@args) = @_;
if (@args == 1) {
# -e option by default
system 'mysql', '-uroot', '-proot', '-e', @args;
} else {
system 'mysql', '-uroot', '-proot', @args;
}
}
sub cmd_user {
my $name = shift or die "$0 user <name>\n";
mysql_root("grant all privileges on `$name`.* to `$name`\@localhost");
mysql_root("flush privileges");
mysql_root("show grants for `$name`\@localhost");
mysql_root("create database `$name`");
}
sub cmd_table {
my $name = shift or die "$0 table <name>\n";
# drop all tables
system "mysqldump -u$name --no-data $name | grep ^DROP | mysql -u$name $name";
open my $fh, '|-', 'mysql', "-u$name", $name or die $!;
print {$fh} <STDIN>;
mysql_root($name, '-e', 'show tables');
}
sub usage {
my $message = shift;
my $usage = <<"USAGE";
Usage: $0 <command>
<command>:
user, table
USAGE
if (defined $message) {
warn "$message";
} else {
$usage .= <<"USAGE";
user:
% $0 user <name>
table:
% $0 table <name>
mysqlc is mysql command line wrapper.
Example:
1) Create user and database
% $0 user webboard
2) Setup table schema
% $0 table webboard < schema.sql
USAGE
}
die $usage;
}