mysql_install_db.c 9.98 KB
Newer Older
unknown's avatar
unknown committed
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
/*
  Copyright (c) 2002 Novell, Inc. All Rights Reserved. 

  This program is free software; you can redistribute it and/or modify 
  it under the terms of the GNU General Public License as published by 
  the Free Software Foundation; either version 2 of the License, or 
  (at your option) any later version. 

  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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ 

#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/stat.h>
#include <monitor.h>
#include <strings.h>
#include <getopt.h>
#include <screen.h>
27
#include <errno.h>
unknown's avatar
unknown committed
28 29 30 31 32 33 34 35 36

#include "my_config.h"
#include "my_manage.h"

/******************************************************************************

	global variables
	
******************************************************************************/
37
int autoclose;
unknown's avatar
unknown committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
char basedir[PATH_MAX];
char datadir[PATH_MAX];
char err_log[PATH_MAX];
char out_log[PATH_MAX];
char mysqld[PATH_MAX];
char hostname[PATH_MAX];
char sql_file[PATH_MAX];
char default_option[PATH_MAX];

/******************************************************************************

	prototypes
	
******************************************************************************/

void start_defaults(int, char*[]);
void finish_defaults();
55
void read_defaults(arg_list_t *);
unknown's avatar
unknown committed
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
void parse_args(int, char*[]);
void get_options(int, char*[]);
void create_paths();
int mysql_install_db(int argc, char *argv[]);

/******************************************************************************

	functions
	
******************************************************************************/

/******************************************************************************

	start_defaults()
	
	Start setting the defaults.

******************************************************************************/
void start_defaults(int argc, char *argv[])
{
  struct stat buf;
  int i;
  
  // default options
  static char *default_options[] =
  {
  	"--no-defaults",
  	"--defaults-file=",
  	"--defaults-extra-file=",
  	NULL
  };
  
  // autoclose
  autoclose = FALSE;
  
  // basedir
  get_basedir(argv[0], basedir);
  
  // hostname
  if (gethostname(hostname,PATH_MAX) < 0)
  {
    // default
    strcpy(hostname,"mysql");
  }

  // default option
102
	default_option[0] = 0;
unknown's avatar
unknown committed
103 104 105 106 107 108 109 110 111 112
  for (i=0; (argc > 1) && default_options[i]; i++)
	{
		if(!strnicmp(argv[1], default_options[i], strlen(default_options[i])))
		{
			strncpy(default_option, argv[1], PATH_MAX);
			break;
		}
	}
  
  // set after basedir is established
113 114 115 116 117
  datadir[0] = 0;
  err_log[0] = 0;
  out_log[0] = 0;
  mysqld[0] = 0;
  sql_file[0] = 0;
unknown's avatar
unknown committed
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
}

/******************************************************************************

	finish_defaults()
	
	Finish setting the defaults.

******************************************************************************/
void finish_defaults()
{
  struct stat buf;
  int i;
  
  // datadir
  if (!datadir[0]) snprintf(datadir, PATH_MAX, "%s/data", basedir);
  
  // err-log
  if (!err_log[0]) snprintf(err_log, PATH_MAX, "%s/%s.err", datadir, hostname);

  // out-log
  if (!out_log[0]) snprintf(out_log, PATH_MAX, "%s/%s.out", datadir, hostname);

  // sql-file
  if (!sql_file[0]) snprintf(sql_file, PATH_MAX, "%s/bin/init_db.sql", basedir);

  // mysqld
  if (!mysqld[0]) snprintf(mysqld, PATH_MAX, "%s/bin/mysqld", basedir);
}

/******************************************************************************

	read_defaults()
	
	Read the defaults.

******************************************************************************/
155
void read_defaults(arg_list_t *pal)
unknown's avatar
unknown committed
156
{
157
  arg_list_t al;
unknown's avatar
unknown committed
158 159 160 161 162 163 164 165 166 167 168 169 170
  char defaults_file[PATH_MAX];
  char mydefaults[PATH_MAX];
  char line[PATH_MAX];
  FILE *fp;
  
	// defaults output file
	snprintf(defaults_file, PATH_MAX, "%s/bin/defaults.out", basedir);
	remove(defaults_file);

	// mysqladmin file
  snprintf(mydefaults, PATH_MAX, "%s/bin/my_print_defaults", basedir);
	
  // args
171 172 173 174 175
  init_args(&al);
  add_arg(&al, mydefaults);
  if (default_option[0]) add_arg(&al, default_option);
  add_arg(&al, "mysqld");
  add_arg(&al, "mysql_install_db");
unknown's avatar
unknown committed
176

177
	spawn(mydefaults, &al, TRUE, NULL, defaults_file, NULL);
unknown's avatar
unknown committed
178

179
  free_args(&al);
unknown's avatar
unknown committed
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 265 266 267 268 269 270

	// gather defaults
	if((fp = fopen(defaults_file, "r")) != NULL)
	{
	  while(fgets(line, PATH_MAX, fp))
	  {
      char *p;
      
      // remove end-of-line character
      if ((p = strrchr(line, '\n')) != NULL) *p = '\0';
      
      // add the option as an argument
	    add_arg(pal, line);
	  }
	  
	  fclose(fp);
	}
	
	// remove file
	remove(defaults_file);
}

/******************************************************************************

	parse_args()
	
	Get the options.

******************************************************************************/
void parse_args(int argc, char *argv[])
{
  int index = 0;
  int c;
  
  // parse options
  enum opts
  {
    OPT_BASEDIR = 0xFF,
    OPT_DATADIR,
    OPT_SQL_FILE
  };
  
  static struct option options[] =
  {
    {"autoclose",     no_argument,        &autoclose,   TRUE},
    {"basedir",       required_argument,  0,            OPT_BASEDIR},
    {"datadir",       required_argument,  0,            OPT_DATADIR},
    {"sql-file",      required_argument,  0,            OPT_SQL_FILE},
    {0,               0,                  0,            0}
  };
  
  // we have to reset getopt_long because we use it multiple times
  optind = 1;
  
  // turn off error reporting
  opterr = 0;
  
  while ((c = getopt_long(argc, argv, "b:h:", options, &index)) >= 0)
  {
    switch (c)
    {
    case OPT_BASEDIR:
    case 'b':
      strcpy(basedir, optarg);
      break;
      
    case OPT_DATADIR:
    case 'h':
      strcpy(datadir, optarg);
      break;
    
    case OPT_SQL_FILE:
      strcpy(sql_file, optarg);
      break;
    
    default:
      // ignore
      break;
    }
  }
}

/******************************************************************************

	get_options()
	
	Get the options.

******************************************************************************/
void get_options(int argc, char *argv[])
{
271
  arg_list_t al;
unknown's avatar
unknown committed
272 273 274 275 276
  
  // start defaults
  start_defaults(argc, argv);

  // default file arguments
277 278 279 280 281
  init_args(&al);
  add_arg(&al, "ignore");
  read_defaults(&al);
  parse_args(al.argc, al.argv);
  free_args(&al);
unknown's avatar
unknown committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
  
  // command-line arguments
  parse_args(argc, argv);

  // finish defaults
  finish_defaults();
}

/******************************************************************************

	create_paths()
	
	Create database paths.

******************************************************************************/
void create_paths()
{
	struct stat info;
  char temp[PATH_MAX];
  
  // check for tables
  snprintf(temp, PATH_MAX, "%s/mysql/host.frm", datadir);
  if (!stat(temp, &info))
  {
    printf("A database already exists in the directory:\n");
    printf("\t%s\n\n", datadir);
    exit(-1);
  }
  
  // data directory
  if (stat(datadir, &info))
  {
    mkdir(datadir, 0);
  }
}

/******************************************************************************

	mysql_install_db()
	
	Install the database.

******************************************************************************/
int mysql_install_db(int argc, char *argv[])
{
327 328 329 330
  arg_list_t al;
  int i, j, err;
  char skip;
  struct stat info;
unknown's avatar
unknown committed
331 332 333 334 335 336 337 338 339 340
  
  // private options
  static char *private_options[] =
  {
  	"--autoclose",
  	"--sql-file=",
  	NULL
  };
  
	// args
341 342
	init_args(&al);
	add_arg(&al, "%s", mysqld);
unknown's avatar
unknown committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	
	// parent args
	for(i = 1; i < argc; i++)
	{
    skip = FALSE;
    
    // skip private arguments
    for (j=0; private_options[j]; j++)
    {
      if(!strnicmp(argv[i], private_options[j], strlen(private_options[j])))
      {
        skip = TRUE;
        break;
      }
    }
		
359
    if (!skip) add_arg(&al, "%s", argv[i]);
unknown's avatar
unknown committed
360 361
	}
	
362 363 364
	add_arg(&al, "--bootstrap");
	add_arg(&al, "--skip-grant-tables");
	add_arg(&al, "--skip-innodb");
unknown's avatar
unknown committed
365

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
  if ((err = stat(sql_file, &info)) != 0)
  {
    printf("ERROR - %s:\n", strerror(errno));
    printf("\t%s\n\n", sql_file);
    // free args
    free_args(&al);
    exit(-1);
  }

  if ((err = stat(sql_file, &info)) != 0)
  {
    printf("ERROR - %s:\n", strerror(errno));
    printf("\t%s\n\n", sql_file);
    // free args
    free_args(&al);
    exit(-1);
  }

unknown's avatar
unknown committed
384
  // spawn mysqld
385
  err = spawn(mysqld, &al, TRUE, sql_file, out_log, err_log);
unknown's avatar
unknown committed
386 387

	// free args
388
	free_args(&al);
unknown's avatar
unknown committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
  
  return err;
}

/******************************************************************************

	main()
	
******************************************************************************/
int main(int argc, char **argv)
{
	// get options
	get_options(argc, argv);

  // check for an autoclose option
  if (!autoclose) setscreenmode(SCR_NO_MODE);
  
406
  // header
unknown's avatar
unknown committed
407 408
  printf("MySQL Server %s, for %s (%s)\n\n", VERSION, SYSTEM_TYPE,
         MACHINE_TYPE);
409
  
unknown's avatar
unknown committed
410 411 412 413 414 415
  // create paths
  create_paths();

	// install the database
  if (mysql_install_db(argc, argv))
  {
416
    printf("ERROR - Failed to create the database!\n");
417
    printf("        %s\n", strerror(errno));
418
    printf("See the following log for more information:\n");
unknown's avatar
unknown committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    printf("\t%s\n\n", err_log);
    exit(-1);
  }
	
  // status
  printf("Initial database successfully created in the directory:\n");
  printf("\t%s\n", datadir);

  // info
  printf("\nPLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !\n");

  printf("\nThis is done with:\n");
  printf("\tmysqladmin -u root password 'new-password'\n");
  
  printf("\nSee the manual for more instructions.\n");
    
  printf("\nYou can start the MySQL daemon with:\n");
  printf("\tmysqld_safe\n");
	
  printf("\nPlease report any problems with:\n");
  printf("\t/mysql/mysqlbug.txt\n");
  
  printf("\nThe latest information about MySQL is available on the web at\n");
  printf("\thttp://www.mysql.com\n");
  
444
  printf("\nSupport MySQL by buying support at http://shop.mysql.com\n\n");
unknown's avatar
unknown committed
445 446 447
  
  return 0;
}