@@ -376,6 +376,7 @@ def __init__(self, basedir, topdir, cnf, bind_address, port, mysqlx_port,
376
376
super (MySQLServer , self ).__init__ (self ._basedir ,
377
377
self ._option_file ,
378
378
sharedir = self ._sharedir )
379
+ self ._init_sql = os .path .join (self ._topdir , 'init.sql' )
379
380
380
381
def _create_directories (self ):
381
382
"""Create directory structure for bootstrapping
@@ -389,9 +390,12 @@ def _create_directories(self):
389
390
dirs = [
390
391
self ._topdir ,
391
392
os .path .join (self ._topdir , 'tmp' ),
392
- self ._datadir ,
393
- os .path .join (self ._datadir , 'mysql' )
394
393
]
394
+
395
+ if self ._version [0 :3 ] < (8 , 0 , 1 ):
396
+ dirs .append (self ._datadir )
397
+ dirs .append (os .path .join (self ._datadir , 'mysql' ))
398
+
395
399
for adir in dirs :
396
400
LOGGER .debug ("Creating directory %s" , adir )
397
401
os .mkdir (adir )
@@ -407,7 +411,6 @@ def _get_bootstrap_cmd(self):
407
411
cmd = [
408
412
os .path .join (self ._sbindir , EXEC_MYSQLD ),
409
413
'--no-defaults' ,
410
- '--bootstrap' ,
411
414
'--basedir=%s' % self ._basedir ,
412
415
'--datadir=%s' % self ._datadir ,
413
416
'--log-warnings=0' ,
@@ -417,6 +420,13 @@ def _get_bootstrap_cmd(self):
417
420
'--tmpdir=%s' % self ._tmpdir ,
418
421
'--innodb_log_file_size=1Gb' ,
419
422
]
423
+
424
+ if self ._version [0 :2 ] >= (8 , 0 ):
425
+ cmd .append ("--initialize-insecure" )
426
+ cmd .append ("--init-file={0}" .format (self ._init_sql ))
427
+ else :
428
+ cmd .append ("--bootstrap" )
429
+
420
430
if self ._version [0 :2 ] < (5 , 5 ):
421
431
cmd .append ('--language={0}/english' .format (self ._lc_messages_dir ))
422
432
else :
@@ -453,68 +463,62 @@ def bootstrap(self):
453
463
extra_sql = [
454
464
"CREATE DATABASE myconnpy;"
455
465
]
456
- insert = (
457
- "INSERT INTO mysql.user VALUES ('localhost','root'{0},"
458
- "'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y',"
459
- "'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y',"
460
- "'Y','Y','Y','Y','Y','','','','',0,0,0,0,"
461
- "@@default_authentication_plugin,'','N',"
462
- "CURRENT_TIMESTAMP,NULL{1}), ('::1','root'{0},"
463
- "'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y',"
464
- "'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y',"
465
- "'Y','Y','Y','Y','Y','','','','',0,0,0,0,"
466
- "@@default_authentication_plugin,'','N',"
467
- "CURRENT_TIMESTAMP,NULL{1});"
468
- )
469
- # MySQL 5.7.5+ creates no user while bootstrapping
470
- if self ._version [0 :3 ] >= (5 , 7 , 6 ):
471
- # MySQL 5.7.6+ have extra account_locked col and no password col
472
- extra_sql .append (insert .format ("" , ",'N'" ))
466
+ defaults = ("'root'{0}, "
467
+ "'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y',"
468
+ "'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y',"
469
+ "'Y','Y','Y','Y','Y','','','','',0,0,0,0,"
470
+ "@@default_authentication_plugin,'','N',"
471
+ "CURRENT_TIMESTAMP,NULL{1}" )
472
+
473
+ hosts = ["::1" , "127.0.0.1" ]
474
+ if self ._version [0 :3 ] < (8 , 0 , 1 ):
475
+ # because we use --initialize-insecure for 8.0 above
476
+ # which already creates root @ localhost
477
+ hosts .append ("localhost" )
478
+
479
+ insert = "INSERT INTO mysql.user VALUES {0};" .format (
480
+ ", " .join ("('{0}', {{0}})" .format (host ) for host in hosts ))
481
+
482
+ if self ._version [0 :3 ] >= (8 , 0 , 1 ):
483
+ # No password column, has account_locked, Create_role_priv and
484
+ # Drop_role_priv columns
485
+ defaults = defaults .format ("" , ", 'N', 'Y', 'Y'" )
486
+ elif self ._version [0 :3 ] >= (5 , 7 , 6 ):
487
+ # No password column, has account_locked column
488
+ defaults = defaults .format ("" , ", 'N'" )
473
489
elif self ._version [0 :3 ] >= (5 , 7 , 5 ):
474
- extra_sql .append (insert .format (",''" , "" ))
475
-
476
- insert_localhost = (
477
- "INSERT INTO mysql.user SELECT '127.0.0.1', `User`{0},"
478
- " `Select_priv`, `Insert_priv`, `Update_priv`, `Delete_priv`,"
479
- " `Create_priv`, `Drop_priv`, `Reload_priv`, `Shutdown_priv`,"
480
- " `Process_priv`, `File_priv`, `Grant_priv`, `References_priv`,"
481
- " `Index_priv`, `Alter_priv`, `Show_db_priv`, `Super_priv`,"
482
- " `Create_tmp_table_priv`, `Lock_tables_priv`, `Execute_priv`,"
483
- " `Repl_slave_priv`, `Repl_client_priv`, `Create_view_priv`,"
484
- " `Show_view_priv`, `Create_routine_priv`, "
485
- "`Alter_routine_priv`,"
486
- " `Create_user_priv`, `Event_priv`, `Trigger_priv`, "
487
- "`Create_tablespace_priv`, `ssl_type`, `ssl_cipher`,"
488
- "`x509_issuer`, `x509_subject`, `max_questions`, `max_updates`,"
489
- "`max_connections`, `max_user_connections`, `plugin`,"
490
- "`authentication_string`, `password_expired`,"
491
- "`password_last_changed`, `password_lifetime`{1} FROM mysql.user "
492
- "WHERE `user` = 'root' and `host` = 'localhost';"
493
- )
494
- # MySQL 5.7.4+ only creates root@localhost
495
- if self ._version [0 :3 ] >= (5 , 7 , 6 ):
496
- extra_sql .append (insert_localhost .format ("" , ",`account_locked`" ))
497
- elif self ._version [0 :3 ] >= (5 , 7 , 4 ):
498
- extra_sql .append (insert_localhost .format (",`Password`" , "" ))
490
+ # The password column
491
+ defaults = defaults .format (", ''" , "" )
499
492
493
+ extra_sql .append (insert .format (defaults ))
500
494
bootstrap_log = os .path .join (self ._topdir , 'bootstrap.log' )
501
495
try :
502
496
self ._create_directories ()
503
497
cmd = self ._get_bootstrap_cmd ()
504
498
sql = ["USE mysql;" ]
505
- for filename in script_files :
506
- full_path = os .path .join (self ._scriptdir , filename )
507
- LOGGER .debug ("Reading SQL from '%s'" , full_path )
508
- with open (full_path , 'r' ) as fp :
509
- sql .extend ([line .strip () for line in fp .readlines ()])
510
- sql .extend (extra_sql )
499
+
500
+ if self ._version [0 :2 ] >= (8 , 0 ):
501
+ test_sql = open (self ._init_sql , "w" )
502
+ test_sql .write ("\n " .join (extra_sql ))
503
+ test_sql .close ()
504
+ else :
505
+ for filename in script_files :
506
+ full_path = os .path .join (self ._scriptdir , filename )
507
+ LOGGER .debug ("Reading SQL from '%s'" , full_path )
508
+ with open (full_path , 'r' ) as fp :
509
+ sql .extend ([line .strip () for line in fp .readlines ()])
510
+
511
511
fp_log = open (bootstrap_log , 'w' )
512
- prc = subprocess .Popen (cmd , stdin = subprocess .PIPE ,
513
- stderr = subprocess .STDOUT , stdout = fp_log )
514
- if sys .version_info [0 ] == 2 :
515
- prc .communicate ('\n ' .join (sql ))
512
+ if self ._version [0 :2 ] < (8 , 0 ):
513
+ sql .extend (extra_sql )
514
+ prc = subprocess .Popen (cmd , stdin = subprocess .PIPE ,
515
+ stderr = subprocess .STDOUT ,
516
+ stdout = fp_log )
517
+ prc .communicate ('\n ' .join (sql ) if sys .version_info [0 ] == 2
518
+ else bytearray ('\n ' .join (sql ), 'utf8' ))
516
519
else :
517
- prc .communicate (bytearray ('\n ' .join (sql ), 'utf8' ))
520
+ prc = subprocess .call (cmd , stderr = subprocess .STDOUT ,
521
+ stdout = fp_log )
518
522
fp_log .close ()
519
523
except OSError as err :
520
524
raise MySQLBootstrapError (
0 commit comments