forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndb_dist_priv.sql
265 lines (250 loc) · 10 KB
/
ndb_dist_priv.sql
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
-- Copyright (c) 2011, 2021, Oracle and/or its affiliates.
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License, version 2.0,
-- as published by the Free Software Foundation.
--
-- This program is also distributed with certain software (including
-- but not limited to OpenSSL) that is licensed under separate terms,
-- as designated in a particular file or component or in included license
-- documentation. The authors of MySQL hereby grant you an additional
-- permission to link the program and your derivative works with the
-- separately licensed software that they have included with MySQL.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License, version 2.0, for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
delimiter |
drop function if exists mysql.mysql_cluster_privileges_are_distributed|
drop procedure if exists mysql.mysql_cluster_backup_privileges|
drop procedure if exists mysql.mysql_cluster_move_grant_tables|
drop procedure if exists mysql.mysql_cluster_restore_privileges_from_local|
drop procedure if exists mysql.mysql_cluster_restore_privileges|
drop procedure if exists mysql.mysql_cluster_restore_local_privileges|
drop procedure if exists mysql.mysql_cluster_move_privileges|
-- Count number of privilege tables in NDB, require
-- all the tables to be in NDB in order to return "true"
create function mysql.mysql_cluster_privileges_are_distributed()
returns bool
reads sql data
begin
declare distributed bool default 0;
select COUNT(table_name) = 6
into distributed
from information_schema.tables
where table_schema = "mysql" and
table_name IN ("user", "db", "tables_priv",
"columns_priv", "procs_priv",
"proxies_priv") and
table_type = 'BASE TABLE' and
engine = 'NDBCLUSTER';
return distributed;
end|
create procedure mysql.mysql_cluster_backup_privileges()
begin
declare distributed_privileges bool default 0;
declare first_backup bool default 1;
declare first_distributed_backup bool default 1;
select mysql.mysql_cluster_privileges_are_distributed()
into distributed_privileges;
select 0 into first_backup
from information_schema.tables
where table_schema = "mysql" and table_name = "user_backup";
select 0 into first_distributed_backup
from information_schema.tables
where table_schema = "mysql" and table_name = "ndb_user_backup";
if first_backup = 1 then
create table if not exists mysql.user_backup
like mysql.user;
create table if not exists mysql.db_backup
like mysql.db;
create table if not exists mysql.tables_priv_backup
like mysql.tables_priv;
create table if not exists mysql.columns_priv_backup
like mysql.columns_priv;
create table if not exists mysql.procs_priv_backup
like mysql.procs_priv;
create table if not exists mysql.proxies_priv_backup
like mysql.proxies_priv;
if distributed_privileges = 1 then
alter table mysql.user_backup engine = myisam;
alter table mysql.db_backup engine = myisam;
alter table mysql.tables_priv_backup engine = myisam;
alter table mysql.columns_priv_backup engine = myisam;
alter table mysql.procs_priv_backup engine = myisam;
alter table mysql.proxies_priv_backup engine = myisam;
end if;
else
truncate mysql.user_backup;
truncate mysql.db_backup;
truncate mysql.tables_priv_backup;
truncate mysql.columns_priv_backup;
truncate mysql.procs_priv_backup;
truncate mysql.proxies_priv_backup;
end if;
if first_distributed_backup = 1 then
create table if not exists mysql.ndb_user_backup
like mysql.user;
create table if not exists mysql.ndb_db_backup
like mysql.db;
create table if not exists mysql.ndb_tables_priv_backup
like mysql.tables_priv;
create table if not exists mysql.ndb_columns_priv_backup
like mysql.columns_priv;
create table if not exists mysql.ndb_procs_priv_backup
like mysql.procs_priv;
create table if not exists mysql.ndb_proxies_priv_backup
like mysql.proxies_priv;
if distributed_privileges = 0 then
alter table mysql.ndb_user_backup engine = ndbcluster;
alter table mysql.ndb_db_backup engine = ndbcluster;
alter table mysql.ndb_tables_priv_backup engine = ndbcluster;
alter table mysql.ndb_columns_priv_backup engine = ndbcluster;
alter table mysql.ndb_procs_priv_backup engine = ndbcluster;
alter table mysql.ndb_proxies_priv_backup engine = ndbcluster;
end if;
else
truncate mysql.ndb_user_backup;
truncate mysql.ndb_db_backup;
truncate mysql.ndb_tables_priv_backup;
truncate mysql.ndb_columns_priv_backup;
truncate mysql.ndb_procs_priv_backup;
truncate mysql.ndb_proxies_priv_backup;
end if;
insert into mysql.user_backup select * from mysql.user;
insert into mysql.db_backup select * from mysql.db;
insert into mysql.tables_priv_backup select * from mysql.tables_priv;
insert into mysql.columns_priv_backup select * from mysql.columns_priv;
insert into mysql.procs_priv_backup select * from mysql.procs_priv;
insert into mysql.proxies_priv_backup select * from mysql.proxies_priv;
insert into mysql.ndb_user_backup select * from mysql.user;
insert into mysql.ndb_db_backup select * from mysql.db;
insert into mysql.ndb_tables_priv_backup select * from mysql.tables_priv;
insert into mysql.ndb_columns_priv_backup select * from mysql.columns_priv;
insert into mysql.ndb_procs_priv_backup select * from mysql.procs_priv;
insert into mysql.ndb_proxies_priv_backup select * from mysql.proxies_priv;
end|
create procedure mysql.mysql_cluster_restore_privileges_from_local()
begin
declare local_backup bool default 0;
select 1 into local_backup
from information_schema.tables
where table_schema = "mysql" and table_name = "user_backup";
if local_backup = 1 then
create table if not exists mysql.user
like mysql.user_backup;
create table if not exists mysql.db
like mysql.db_backup;
create table if not exists mysql.tables_priv
like mysql.tables_priv_backup;
create table if not exists mysql.columns_priv
like mysql.columns_priv_backup;
create table if not exists mysql.procs_priv
like mysql.procs_priv_backup;
create table if not exists mysql.proxies_priv
like mysql.proxies_priv_backup;
delete from mysql.user;
insert into mysql.user select * from mysql.user_backup;
delete from mysql.db;
insert into mysql.db select * from mysql.db_backup;
delete from mysql.tables_priv;
insert into mysql.tables_priv select * from mysql.tables_priv_backup;
delete from mysql.columns_priv;
insert into mysql.columns_priv select * from mysql.columns_priv_backup;
delete from mysql.procs_priv;
insert into mysql.procs_priv select * from mysql.procs_priv_backup;
delete from mysql.proxies_priv;
insert into mysql.proxies_priv select * from mysql.proxies_priv_backup;
end if;
end|
create procedure mysql.mysql_cluster_restore_privileges()
begin
declare distributed_backup bool default 0;
select 1 into distributed_backup
from information_schema.tables
where table_schema = "mysql" and table_name = "ndb_user_backup";
if distributed_backup = 1 then
flush tables;
create table if not exists mysql.user
like mysql.ndb_user_backup;
create table if not exists mysql.db
like mysql.ndb_db_backup;
create table if not exists mysql.tables_priv
like mysql.ndb_tables_priv_backup;
create table if not exists mysql.columns_priv
like mysql.ndb_columns_priv_backup;
create table if not exists mysql.procs_priv
like mysql.ndb_procs_priv_backup;
create table if not exists mysql.proxies_priv
like mysql.ndb_proxies_priv_backup;
delete from mysql.user;
insert into mysql.user
select * from mysql.ndb_user_backup;
delete from mysql.db;
insert into mysql.db
select * from mysql.ndb_db_backup;
delete from mysql.tables_priv;
insert into mysql.tables_priv
select * from mysql.ndb_tables_priv_backup;
delete from mysql.columns_priv;
insert into mysql.columns_priv
select * from mysql.ndb_columns_priv_backup;
delete from mysql.procs_priv;
insert into mysql.procs_priv
select * from mysql.ndb_procs_priv_backup;
delete from mysql.proxies_priv;
insert into mysql.proxies_priv
select * from mysql.ndb_proxies_priv_backup;
else
call mysql_cluster_restore_privileges_from_local();
end if;
end|
create procedure mysql.mysql_cluster_restore_local_privileges()
begin
declare distributed_privileges bool default 0;
select mysql.mysql_cluster_privileges_are_distributed()
into distributed_privileges;
if distributed_privileges = 1 then
begin
drop table mysql.user;
drop table mysql.db;
drop table mysql.tables_priv;
drop table mysql.columns_priv;
drop table mysql.procs_priv;
drop table mysql.proxies_priv;
end;
end if;
call mysql_cluster_restore_privileges_from_local();
end|
create procedure mysql.mysql_cluster_move_grant_tables()
begin
declare distributed_privileges bool default 0;
declare revert bool default 0;
select mysql.mysql_cluster_privileges_are_distributed()
into distributed_privileges;
if distributed_privileges = 0 then
begin
declare exit handler for sqlexception set revert = 1;
alter table mysql.user engine = ndb;
alter table mysql.db engine = ndb;
alter table mysql.tables_priv engine = ndb;
alter table mysql.columns_priv engine = ndb;
alter table mysql.procs_priv engine = ndb;
alter table mysql.proxies_priv engine = ndb;
end;
end if;
if revert = 1 then
call mysql_cluster_restore_privileges();
end if;
end|
create procedure mysql.mysql_cluster_move_privileges()
begin
call mysql_cluster_backup_privileges();
call mysql_cluster_move_grant_tables();
end|
delimiter ;