@@ -118,17 +118,18 @@ static my_bool verbose= 0, opt_no_create_info= 0, opt_no_data= 0, opt_no_data_m
118
118
opt_alltspcs = 0 , opt_notspcs = 0 , opt_logging ,
119
119
opt_drop_trigger = 0 ;
120
120
#define OPT_SYSTEM_ALL 1
121
- #define OPT_SYSTEM_USER 2
122
- #define OPT_SYSTEM_PLUGIN 4
123
- #define OPT_SYSTEM_UDF 8
121
+ #define OPT_SYSTEM_USERS 2
122
+ #define OPT_SYSTEM_PLUGINS 4
123
+ #define OPT_SYSTEM_UDFS 8
124
124
#define OPT_SYSTEM_SERVERS 16
125
125
#define OPT_SYSTEM_STATS 32
126
126
#define OPT_SYSTEM_TIMEZONES 64
127
127
static const char * opt_system_type_values []=
128
- {"all" , "users" , "plugins" , "udf " , "servers" , "stats" , "timezones" };
128
+ {"all" , "users" , "plugins" , "udfs " , "servers" , "stats" , "timezones" };
129
129
static TYPELIB opt_system_types =
130
130
{
131
- array_elements (opt_system_type_values ), "system dump options" , opt_system_type_values , NULL
131
+ array_elements (opt_system_type_values ), "system dump options" ,
132
+ opt_system_type_values , NULL
132
133
};
133
134
static ulonglong opt_system = 0ULL ;
134
135
static my_bool insert_pat_inited = 0 , debug_info_flag = 0 , debug_check_flag = 0 ;
@@ -595,9 +596,9 @@ static int init_dumping_tables(char *);
595
596
static int init_dumping (char * , int init_func (char * ));
596
597
static int dump_databases (char * * );
597
598
static int dump_all_databases ();
598
- static int dump_all_users ();
599
+ static int dump_all_users_roles_and_grants ();
599
600
static int dump_all_plugins ();
600
- static int dump_all_udf ();
601
+ static int dump_all_udfs ();
601
602
static int dump_all_servers ();
602
603
static int dump_all_stats ();
603
604
static int dump_all_timezones ();
@@ -1054,32 +1055,32 @@ static int get_options(int *argc, char ***argv)
1054
1055
if (opt_system & OPT_SYSTEM_ALL )
1055
1056
opt_system |= ~0 ;
1056
1057
1057
- if (opt_system & OPT_SYSTEM_USER &&
1058
+ if (opt_system & OPT_SYSTEM_USERS &&
1058
1059
(my_hash_insert (& ignore_table ,
1059
1060
(uchar * ) my_strdup ("mysql.db" , MYF (MY_WME ))) ||
1060
1061
my_hash_insert (& ignore_table ,
1061
1062
(uchar * ) my_strdup ("mysql.global_priv" , MYF (MY_WME ))) ||
1062
1063
my_hash_insert (& ignore_table ,
1063
- (uchar * ) my_strdup ("mysql.table_priv " , MYF (MY_WME ))) ||
1064
+ (uchar * ) my_strdup ("mysql.tables_priv " , MYF (MY_WME ))) ||
1064
1065
my_hash_insert (& ignore_table ,
1065
- (uchar * ) my_strdup ("mysql.column_priv " , MYF (MY_WME ))) ||
1066
+ (uchar * ) my_strdup ("mysql.columns_priv " , MYF (MY_WME ))) ||
1066
1067
my_hash_insert (& ignore_table ,
1067
1068
(uchar * ) my_strdup ("mysql.procs_priv" , MYF (MY_WME ))) ||
1068
1069
my_hash_insert (& ignore_table ,
1069
1070
(uchar * ) my_strdup ("mysql.roles_mapping" , MYF (MY_WME ))) ||
1070
- /* and MySQL-8.0 role tables as well */
1071
+ /* and MySQL-8.0 role tables (role_edges and default_roles) as well */
1071
1072
my_hash_insert (& ignore_table ,
1072
1073
(uchar * ) my_strdup ("mysql.role_edges" , MYF (MY_WME ))) ||
1073
1074
my_hash_insert (& ignore_table ,
1074
1075
(uchar * ) my_strdup ("mysql.default_roles" , MYF (MY_WME )))))
1075
1076
return (EX_EOM );
1076
1077
1077
- if (opt_system & OPT_SYSTEM_PLUGIN &&
1078
+ if (opt_system & OPT_SYSTEM_PLUGINS &&
1078
1079
my_hash_insert (& ignore_table ,
1079
- (uchar * ) my_strdup ("mysql.plugins " , MYF (MY_WME ))))
1080
+ (uchar * ) my_strdup ("mysql.plugin " , MYF (MY_WME ))))
1080
1081
return (EX_EOM );
1081
1082
1082
- if (opt_system & OPT_SYSTEM_UDF &&
1083
+ if (opt_system & OPT_SYSTEM_UDFS &&
1083
1084
my_hash_insert (& ignore_table ,
1084
1085
(uchar * ) my_strdup ("mysql.func" , MYF (MY_WME ))))
1085
1086
return (EX_EOM );
@@ -4254,16 +4255,18 @@ static char *getTableName(int reset)
4254
4255
4255
4256
/*
4256
4257
dump user/role grants
4258
+ ARGS
4259
+ user_role: is either a user, or a role
4257
4260
*/
4258
4261
4259
- static int dump_grants (const char * ur )
4262
+ static int dump_grants (const char * user_role )
4260
4263
{
4261
4264
DYNAMIC_STRING sqlbuf ;
4262
4265
MYSQL_ROW row ;
4263
4266
MYSQL_RES * tableres ;
4264
4267
4265
4268
init_dynamic_string_checked (& sqlbuf , "SHOW GRANTS FOR " , 256 , 1024 );
4266
- dynstr_append_checked (& sqlbuf , ur );
4269
+ dynstr_append_checked (& sqlbuf , user_role );
4267
4270
4268
4271
if (mysql_query_with_error_report (mysql , & tableres , sqlbuf .str ))
4269
4272
{
@@ -4281,7 +4284,7 @@ static int dump_grants(const char *ur)
4281
4284
4282
4285
4283
4286
/*
4284
- dump creater user
4287
+ dump create user
4285
4288
*/
4286
4289
4287
4290
static int dump_create_user (const char * user )
@@ -4310,12 +4313,11 @@ static int dump_create_user(const char *user)
4310
4313
}
4311
4314
4312
4315
4313
-
4314
4316
/*
4315
4317
dump all users and roles
4316
4318
*/
4317
4319
4318
- static int dump_all_users ()
4320
+ static int dump_all_users_roles_and_grants ()
4319
4321
{
4320
4322
MYSQL_ROW row ;
4321
4323
MYSQL_RES * tableres ;
@@ -4327,21 +4329,24 @@ static int dump_all_users()
4327
4329
if (mysql_query_with_error_report (mysql , & tableres ,
4328
4330
"SELECT CONCAT(QUOTE(u.user), '@', QUOTE(u.Host)) AS u "
4329
4331
"FROM mysql.user u "
4330
- " /*!80001 LEFT JOIN mysql.role_edges e ON u.user=e.from_user AND u.host=e.from_host "
4331
- " WHERE e.from_user IS NULL */"
4332
- " /*M!100005 WHERE is_role='N' */" ))
4332
+ " /*!80001 LEFT JOIN mysql.role_edges e "
4333
+ " ON u.user=e.from_user "
4334
+ " AND u.host=e.from_host "
4335
+ " WHERE e.from_user IS NULL */"
4336
+ " /*M!100005 WHERE is_role='N' */" ))
4333
4337
return 1 ;
4334
4338
while ((row = mysql_fetch_row (tableres )))
4335
4339
{
4336
4340
if (opt_replace_into )
4337
4341
/* Protection against removing the current import user */
4338
4342
/* MySQL-8.0 export capability */
4339
4343
fprintf (md_result_file ,
4340
- "DELIMITER |\n"
4344
+ "DELIMITER |\n"
4341
4345
"/*M!100101 IF current_user()=\"%s\" THEN\n"
4342
- " SIGNAL SQLSTATE '45000' SET MYSQL_ERRNO=30001, MESSAGE_TEXT=\"Don't remove current user %s'\";\n"
4346
+ " SIGNAL SQLSTATE '45000' SET MYSQL_ERRNO=30001,"
4347
+ " MESSAGE_TEXT=\"Don't remove current user %s'\";\n"
4343
4348
"END IF */|\n"
4344
- "DELIMITER ;\n"
4349
+ "DELIMITER ;\n"
4345
4350
"/*!50701 DROP USER IF EXISTS %s */;\n" , row [0 ], row [0 ], row [0 ]);
4346
4351
if (dump_create_user (row [0 ]))
4347
4352
result = 1 ;
@@ -4354,15 +4359,37 @@ static int dump_all_users()
4354
4359
if (!(maria_roles_exist || mysql_roles_exist ))
4355
4360
goto exit ;
4356
4361
4357
- /* preserve current role. Create a new role for importing that becomes the
4358
- * default admin for new roles, so it can be dropped after roles are assigned */
4362
+ /*
4363
+ Preserve current role active role, in case this dump is imported
4364
+ in the same connection that assumes the active role at the beginning
4365
+ is the same as at the end of the connection. This is so:
4366
+
4367
+ #!/bin/sh
4368
+ (
4369
+ echo "set role special_role; ";
4370
+ cat mysqldump.sql;
4371
+ echo "$dosomethingspecial"
4372
+ ) | mysql -h $host
4373
+
4374
+ doesn't end up with a suprise that the $dosomethingspecial cannot
4375
+ be done because `special_role` isn't active.
4376
+
4377
+ We create a new role for importing that becomes the default admin for new
4378
+ roles. This is because without being a admin on new roles we don't
4379
+ have the necessary privileges to grant users to a created role or to
4380
+ create new admins for the created role.
4381
+ At the end of the import the mariadb_dump_import_role is be dropped,
4382
+ which implictly drops all its admin aspects of all the role.
4383
+ This is significiantly easlier than revoking the ADMIN of each role
4384
+ from the current user.
4385
+ */
4359
4386
fputs ("SELECT COALESCE(CURRENT_ROLE(),'NONE') into @current_role;\n"
4360
4387
"CREATE ROLE IF NOT EXISTS mariadb_dump_import_role;\n"
4361
4388
"GRANT mariadb_dump_import_role TO CURRENT_USER();\n"
4362
4389
"SET ROLE mariadb_dump_import_role;\n"
4363
4390
, md_result_file );
4364
4391
/* No show create role yet, MDEV-22311 */
4365
- /* Roles, with user admins first, then roles they adminster , and recurse on that */
4392
+ /* Roles, with user admins first, then roles they administer , and recurse on that */
4366
4393
if (maria_roles_exist && mysql_query_with_error_report (mysql , & tableres ,
4367
4394
"WITH RECURSIVE create_role_order AS"
4368
4395
" (SELECT 1 as n, roles_mapping.*"
@@ -4401,7 +4428,8 @@ static int dump_all_users()
4401
4428
" JOIN mysql.role_edges re ON c.FROM_USER=re.TO_USER"
4402
4429
" AND c.FROM_HOST=re.TO_HOST) "
4403
4430
"SELECT CONCAT(QUOTE(FROM_USER), '/*!80001 @', QUOTE(FROM_HOST), '*/') AS r,"
4404
- " CONCAT(QUOTE(TO_USER), IF(n=1, CONCAT('@', QUOTE(TO_HOST)), CONCAT('/*!80001 @', QUOTE(TO_HOST), ' */'))) AS u,"
4431
+ " CONCAT(QUOTE(TO_USER), IF(n=1, CONCAT('@', QUOTE(TO_HOST)),"
4432
+ " CONCAT('/*!80001 @', QUOTE(TO_HOST), ' */'))) AS u,"
4405
4433
" WITH_ADMIN_OPTION "
4406
4434
"FROM create_role_order "
4407
4435
"ORDER BY n,"
@@ -4436,7 +4464,8 @@ static int dump_all_users()
4436
4464
"/*M!100005 WHERE is_role='N' */" ))
4437
4465
return 1 ;
4438
4466
if (mysql_roles_exist && mysql_roles_exist && mysql_query_with_error_report (mysql , & tableres ,
4439
- "SELECT IF(DEFAULT_ROLE_HOST IS NULL, 'NONE', CONCAT(QUOTE(DEFAULT_ROLE_USER), '@', QUOTE(DEFAULT_ROLE_HOST))) as r,"
4467
+ "SELECT IF(DEFAULT_ROLE_HOST IS NULL, 'NONE', CONCAT(QUOTE(DEFAULT_ROLE_USER),"
4468
+ " '@', QUOTE(DEFAULT_ROLE_HOST))) as r,"
4440
4469
" CONCAT(QUOTE(mu.USER),'@',QUOTE(mu.HOST)) as u "
4441
4470
"FROM mysql.user mu LEFT JOIN mysql.default_roles using (USER, HOST)" ))
4442
4471
return 1 ;
@@ -4489,16 +4518,22 @@ static int dump_all_plugins()
4489
4518
4490
4519
if (mysql_query_with_error_report (mysql , & tableres , "SHOW PLUGINS" ))
4491
4520
return 1 ;
4492
- /* Name, Status, Type, Library,License */
4521
+ /* Name, Status, Type, Library, License */
4493
4522
while ((row = mysql_fetch_row (tableres )))
4494
4523
{
4495
4524
if (strcmp ("ACTIVE" , row [1 ]) != 0 )
4496
4525
continue ;
4497
4526
/* Should we be skipping builtins? */
4498
4527
if (row [3 ] == NULL )
4499
4528
continue ;
4529
+ if (opt_replace_into )
4530
+ {
4531
+ fprintf (md_result_file , "/*M!100401 UNINSTALL PLUGIN IF EXIST %s */;\n" ,
4532
+ row [0 ]);
4533
+ }
4500
4534
fprintf (md_result_file ,
4501
- "INSTALL PLUGIN %s %s SONAME '%s';\n" , row [0 ], opt_ignore ? "/*!100401 IF NOT EXISTS */" : "" , row [3 ]);
4535
+ "INSTALL PLUGIN %s %s SONAME '%s';\n" , row [0 ],
4536
+ opt_ignore ? "/*M!100401 IF NOT EXISTS */" : "" , row [3 ]);
4502
4537
}
4503
4538
mysql_free_result (tableres );
4504
4539
@@ -4507,16 +4542,16 @@ static int dump_all_plugins()
4507
4542
4508
4543
4509
4544
/*
4510
- dump all udf
4545
+ dump all udfs
4511
4546
*/
4512
4547
4513
- static int dump_all_udf ()
4548
+ static int dump_all_udfs ()
4514
4549
{
4515
4550
/* we don't support all these types yet, but get prepared if we do */
4516
4551
static const char * udf_types [] = {"STRING" , "REAL" , "INT" , "ROW" , "DECIMAL" , "TIME" };
4517
4552
MYSQL_ROW row ;
4518
4553
MYSQL_RES * tableres ;
4519
- int retresult ;
4554
+ int retresult , result = 0 ;
4520
4555
4521
4556
if (mysql_query_with_error_report (mysql , & tableres , "SELECT * FROM mysql.func" ))
4522
4557
return 1 ;
@@ -4526,9 +4561,15 @@ static int dump_all_udf()
4526
4561
retresult = atoi (row [1 ]);
4527
4562
if (retresult < 0 || array_elements (udf_types ) <= (size_t ) retresult )
4528
4563
{
4529
- fprintf (stderr , "%s: Error: invalid return type on udf function '%s'\n" ,
4564
+ fprintf (stderr , "%s: Error: invalid return type on udf function '%s'\n" ,
4530
4565
my_progname_short , row [0 ]);
4531
- return 1 ;
4566
+ result = 1 ;
4567
+ continue ;
4568
+ }
4569
+ if (opt_replace_into )
4570
+ {
4571
+ fprintf (md_result_file , "/*!50701 DROP FUNCTION IF EXISTS %s */;\n" ,
4572
+ row [0 ]);
4532
4573
}
4533
4574
fprintf (md_result_file ,
4534
4575
"CREATE %s%sFUNCTION %s%s RETURNS %s SONAME '%s';\n" ,
@@ -4538,12 +4579,12 @@ static int dump_all_udf()
4538
4579
}
4539
4580
mysql_free_result (tableres );
4540
4581
4541
- return 0 ;
4582
+ return result ;
4542
4583
}
4543
4584
4544
4585
4545
4586
/*
4546
- dump all plugins
4587
+ dump all servers
4547
4588
*/
4548
4589
4549
4590
static int dump_all_servers ()
@@ -4583,7 +4624,7 @@ static int dump_all_servers()
4583
4624
4584
4625
4585
4626
/*
4586
- dump all system statitical tables
4627
+ dump all system statistical tables
4587
4628
*/
4588
4629
4589
4630
static int dump_all_stats ()
@@ -6662,14 +6703,14 @@ int main(int argc, char **argv)
6662
6703
}
6663
6704
}
6664
6705
6665
- if (opt_system & OPT_SYSTEM_PLUGIN )
6706
+ if (opt_system & OPT_SYSTEM_PLUGINS )
6666
6707
dump_all_plugins ();
6667
6708
6668
- if (opt_system & OPT_SYSTEM_USER )
6669
- dump_all_users ();
6709
+ if (opt_system & OPT_SYSTEM_USERS )
6710
+ dump_all_users_roles_and_grants ();
6670
6711
6671
- if (opt_system & OPT_SYSTEM_UDF )
6672
- dump_all_udf ();
6712
+ if (opt_system & OPT_SYSTEM_UDFS )
6713
+ dump_all_udfs ();
6673
6714
6674
6715
if (opt_system & OPT_SYSTEM_SERVERS )
6675
6716
dump_all_servers ();
0 commit comments