~ubuntu-branches/ubuntu/precise/mysql-5.5/precise-201203300109

« back to all changes in this revision

Viewing changes to scripts/mysql_install_db.sh

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2011-11-08 11:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20111108113113-3ulw01fvi4vn8m25
Tags: upstream-5.5.17
ImportĀ upstreamĀ versionĀ 5.5.17

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
# Copyright (c) 2000-2008 MySQL AB, 2009, 2010 Sun Microsystems, Inc.
 
3
# Use is subject to license terms.
 
4
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation; version 2 of the License.
 
8
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
17
 
 
18
# This scripts creates the MySQL Server system tables
 
19
#
 
20
# All unrecognized arguments to this script are passed to mysqld.
 
21
 
 
22
basedir=""
 
23
builddir=""
 
24
ldata="@localstatedir@"
 
25
langdir=""
 
26
srcdir=""
 
27
 
 
28
args=""
 
29
defaults=""
 
30
mysqld_opt=""
 
31
user=""
 
32
 
 
33
force=0
 
34
in_rpm=0
 
35
ip_only=0
 
36
cross_bootstrap=0
 
37
 
 
38
usage()
 
39
{
 
40
  cat <<EOF
 
41
Usage: $0 [OPTIONS]
 
42
  --basedir=path       The path to the MySQL installation directory.
 
43
  --builddir=path      If using --srcdir with out-of-directory builds, you
 
44
                       will need to set this to the location of the build
 
45
                       directory where built files reside.
 
46
  --cross-bootstrap    For internal use.  Used when building the MySQL system
 
47
                       tables on a different host than the target.
 
48
  --datadir=path       The path to the MySQL data directory.
 
49
  --defaults-extra-file=name
 
50
                       Read this file after the global files are read.
 
51
  --defaults-file=name Only read default options from the given file name.
 
52
  --force              Causes mysql_install_db to run even if DNS does not
 
53
                       work.  In that case, grant table entries that normally
 
54
                       use hostnames will use IP addresses.
 
55
  --help               Display this help and exit.                     
 
56
  --ldata=path         The path to the MySQL data directory. Same as --datadir.
 
57
  --no-defaults        Don't read default options from any option file.
 
58
  --rpm                For internal use.  This option is used by RPM files
 
59
                       during the MySQL installation process.
 
60
  --skip-name-resolve  Use IP addresses rather than hostnames when creating
 
61
                       grant table entries.  This option can be useful if
 
62
                       your DNS does not work.
 
63
  --srcdir=path        The path to the MySQL source directory.  This option
 
64
                       uses the compiled binaries and support files within the
 
65
                       source tree, useful for if you don't want to install
 
66
                       MySQL yet and just want to create the system tables.
 
67
  --user=user_name     The login username to use for running mysqld.  Files
 
68
                       and directories created by mysqld will be owned by this
 
69
                       user.  You must be root to use this option.  By default
 
70
                       mysqld runs using your current login name and files and
 
71
                       directories that it creates will be owned by you.
 
72
 
 
73
All other options are passed to the mysqld program
 
74
 
 
75
EOF
 
76
  exit 1
 
77
}
 
78
 
 
79
s_echo()
 
80
{
 
81
  if test "$in_rpm" -eq 0 -a "$cross_bootstrap" -eq 0
 
82
  then
 
83
    echo "$1"
 
84
  fi
 
85
}
 
86
 
 
87
parse_arg()
 
88
{
 
89
  echo "$1" | sed -e 's/^[^=]*=//'
 
90
}
 
91
 
 
92
parse_arguments()
 
93
{
 
94
  # We only need to pass arguments through to the server if we don't
 
95
  # handle them here.  So, we collect unrecognized options (passed on
 
96
  # the command line) into the args variable.
 
97
  pick_args=
 
98
  if test "$1" = PICK-ARGS-FROM-ARGV
 
99
  then
 
100
    pick_args=1
 
101
    shift
 
102
  fi
 
103
 
 
104
  for arg
 
105
  do
 
106
    case "$arg" in
 
107
      --force) force=1 ;;
 
108
      --basedir=*) basedir=`parse_arg "$arg"` ;;
 
109
      --builddir=*) builddir=`parse_arg "$arg"` ;;
 
110
      --srcdir=*)  srcdir=`parse_arg "$arg"` ;;
 
111
      --ldata=*|--datadir=*) ldata=`parse_arg "$arg"` ;;
 
112
      --user=*)
 
113
        # Note that the user will be passed to mysqld so that it runs
 
114
        # as 'user' (crucial e.g. if log-bin=/some_other_path/
 
115
        # where a chown of datadir won't help)
 
116
        user=`parse_arg "$arg"` ;;
 
117
      --skip-name-resolve) ip_only=1 ;;
 
118
      --verbose) verbose=1 ;; # Obsolete
 
119
      --rpm) in_rpm=1 ;;
 
120
      --help) usage ;;
 
121
      --no-defaults|--defaults-file=*|--defaults-extra-file=*)
 
122
        defaults="$arg" ;;
 
123
 
 
124
      --cross-bootstrap|--windows)
 
125
        # Used when building the MySQL system tables on a different host than
 
126
        # the target. The platform-independent files that are created in
 
127
        # --datadir on the host can be copied to the target system.
 
128
        #
 
129
        # The most common use for this feature is in the Windows installer
 
130
        # which will take the files from datadir and include them as part of
 
131
        # the install package.  See top-level 'dist-hook' make target.
 
132
        #
 
133
        # --windows is a deprecated alias
 
134
        cross_bootstrap=1 ;;
 
135
 
 
136
      *)
 
137
        if test -n "$pick_args"
 
138
        then
 
139
          # This sed command makes sure that any special chars are quoted,
 
140
          # so the arg gets passed exactly to the server.
 
141
          # XXX: This is broken; true fix requires using eval and proper
 
142
          # quoting of every single arg ($basedir, $ldata, etc.)
 
143
          #args="$args "`echo "$arg" | sed -e 's,\([^a-zA-Z0-9_.-]\),\\\\\1,g'`
 
144
          args="$args $arg"
 
145
        fi
 
146
        ;;
 
147
    esac
 
148
  done
 
149
}
 
150
 
 
151
# Try to find a specific file within --basedir which can either be a binary
 
152
# release or installed source directory and return the path.
 
153
find_in_basedir()
 
154
{
 
155
  case "$1" in
 
156
    --dir)
 
157
      return_dir=1; shift
 
158
      ;;
 
159
  esac
 
160
 
 
161
  file=$1; shift
 
162
 
 
163
  for dir in "$@"
 
164
  do
 
165
    if test -f "$basedir/$dir/$file"
 
166
    then
 
167
      if test -n "$return_dir"
 
168
      then
 
169
        echo "$basedir/$dir"
 
170
      else
 
171
        echo "$basedir/$dir/$file"
 
172
      fi
 
173
      break
 
174
    fi
 
175
  done
 
176
}
 
177
 
 
178
cannot_find_file()
 
179
{
 
180
  echo
 
181
  echo "FATAL ERROR: Could not find $1"
 
182
 
 
183
  shift
 
184
  if test $# -ne 0
 
185
  then
 
186
    echo
 
187
    echo "The following directories were searched:"
 
188
    echo
 
189
    for dir in "$@"
 
190
    do
 
191
      echo "    $dir"
 
192
    done
 
193
  fi
 
194
 
 
195
  echo
 
196
  echo "If you compiled from source, you need to run 'make install' to"
 
197
  echo "copy the software into the correct location ready for operation."
 
198
  echo
 
199
  echo "If you are using a binary release, you must either be at the top"
 
200
  echo "level of the extracted archive, or pass the --basedir option"
 
201
  echo "pointing to that location."
 
202
  echo
 
203
}
 
204
 
 
205
# Ok, let's go.  We first need to parse arguments which are required by
 
206
# my_print_defaults so that we can execute it first, then later re-parse
 
207
# the command line to add any extra bits that we need.
 
208
parse_arguments PICK-ARGS-FROM-ARGV "$@"
 
209
 
 
210
#
 
211
# We can now find my_print_defaults.  This script supports:
 
212
#
 
213
#   --srcdir=path pointing to compiled source tree
 
214
#   --basedir=path pointing to installed binary location
 
215
#
 
216
# or default to compiled-in locations.
 
217
#
 
218
if test -n "$srcdir" && test -n "$basedir"
 
219
then
 
220
  echo "ERROR: Specify either --basedir or --srcdir, not both."
 
221
  exit 1
 
222
fi
 
223
if test -n "$srcdir"
 
224
then
 
225
  if test -z "$builddir"
 
226
  then
 
227
    builddir="$srcdir"
 
228
  fi
 
229
  print_defaults="$builddir/extra/my_print_defaults"
 
230
elif test -n "$basedir"
 
231
then
 
232
  print_defaults=`find_in_basedir my_print_defaults bin extra`
 
233
  if test -z "$print_defaults"
 
234
  then
 
235
    cannot_find_file my_print_defaults $basedir/bin $basedir/extra
 
236
    exit 1
 
237
  fi
 
238
else
 
239
  print_defaults="@bindir@/my_print_defaults"
 
240
fi
 
241
 
 
242
if test ! -x "$print_defaults"
 
243
then
 
244
  cannot_find_file "$print_defaults"
 
245
  exit 1
 
246
fi
 
247
 
 
248
# Now we can get arguments from the groups [mysqld] and [mysql_install_db]
 
249
# in the my.cfg file, then re-run to merge with command line arguments.
 
250
parse_arguments `$print_defaults $defaults mysqld mysql_install_db`
 
251
parse_arguments PICK-ARGS-FROM-ARGV "$@"
 
252
 
 
253
# Configure paths to support files
 
254
if test -n "$srcdir"
 
255
then
 
256
  basedir="$builddir"
 
257
  bindir="$basedir/client"
 
258
  extra_bindir="$basedir/extra"
 
259
  mysqld="$basedir/sql/mysqld"
 
260
  langdir="$srcdir/sql/share/english"
 
261
  pkgdatadir="$srcdir/scripts"
 
262
  scriptdir="$srcdir/scripts"
 
263
elif test -n "$basedir"
 
264
then
 
265
  bindir="$basedir/bin"
 
266
  extra_bindir="$bindir"
 
267
  mysqld=`find_in_basedir mysqld libexec sbin bin`
 
268
  if test -z "$mysqld"
 
269
  then
 
270
    cannot_find_file mysqld $basedir/libexec $basedir/sbin $basedir/bin
 
271
    exit 1
 
272
  fi
 
273
  langdir=`find_in_basedir --dir errmsg.sys share/english share/mysql/english`
 
274
  if test -z "$langdir"
 
275
  then
 
276
    cannot_find_file errmsg.sys $basedir/share/english $basedir/share/mysql/english
 
277
    exit 1
 
278
  fi
 
279
  pkgdatadir=`find_in_basedir --dir fill_help_tables.sql share share/mysql`
 
280
  if test -z "$pkgdatadir"
 
281
  then
 
282
    cannot_find_file fill_help_tables.sql $basedir/share $basedir/share/mysql
 
283
    exit 1
 
284
  fi
 
285
  scriptdir="$basedir/scripts"
 
286
else
 
287
  basedir="@prefix@"
 
288
  bindir="@bindir@"
 
289
  extra_bindir="$bindir"
 
290
  mysqld="@libexecdir@/mysqld"
 
291
  pkgdatadir="@pkgdatadir@"
 
292
  scriptdir="@scriptdir@"
 
293
fi
 
294
 
 
295
# Set up paths to SQL scripts required for bootstrap
 
296
fill_help_tables="$pkgdatadir/fill_help_tables.sql"
 
297
create_system_tables="$pkgdatadir/mysql_system_tables.sql"
 
298
fill_system_tables="$pkgdatadir/mysql_system_tables_data.sql"
 
299
 
 
300
for f in $fill_help_tables $create_system_tables $fill_system_tables
 
301
do
 
302
  if test ! -f "$f"
 
303
  then
 
304
    cannot_find_file "$f"
 
305
    exit 1
 
306
  fi
 
307
done
 
308
 
 
309
if test ! -x "$mysqld"
 
310
then
 
311
  cannot_find_file "$mysqld"
 
312
  exit 1
 
313
fi
 
314
 
 
315
if test -n "$langdir"
 
316
then
 
317
  if test ! -f "$langdir/errmsg.sys"
 
318
  then
 
319
    cannot_find_file "$langdir/errmsg.sys"
 
320
    exit 1
 
321
  fi
 
322
  mysqld_opt="--lc-messages-dir=$langdir/.."
 
323
fi
 
324
 
 
325
# Try to determine the hostname
 
326
hostname=`@HOSTNAME@`
 
327
 
 
328
# Check if hostname is valid
 
329
if test "$cross_bootstrap" -eq 0 -a "$in_rpm" -eq 0 -a "$force" -eq 0
 
330
then
 
331
  resolved=`$extra_bindir/resolveip $hostname 2>&1`
 
332
  if test $? -ne 0
 
333
  then
 
334
    resolved=`$extra_bindir/resolveip localhost 2>&1`
 
335
    if test $? -ne 0
 
336
    then
 
337
      echo "Neither host '$hostname' nor 'localhost' could be looked up with"
 
338
      echo "$extra_bindir/resolveip"
 
339
      echo "Please configure the 'hostname' command to return a correct"
 
340
      echo "hostname."
 
341
      echo "If you want to solve this at a later stage, restart this script"
 
342
      echo "with the --force option"
 
343
      exit 1
 
344
    fi
 
345
    echo "WARNING: The host '$hostname' could not be looked up with resolveip."
 
346
    echo "This probably means that your libc libraries are not 100 % compatible"
 
347
    echo "with this binary MySQL version. The MySQL daemon, mysqld, should work"
 
348
    echo "normally with the exception that host name resolving will not work."
 
349
    echo "This means that you should use IP addresses instead of hostnames"
 
350
    echo "when specifying MySQL privileges !"
 
351
  fi
 
352
fi
 
353
 
 
354
if test "$ip_only" -eq 1
 
355
then
 
356
  hostname=`echo "$resolved" | awk '/ /{print $6}'`
 
357
fi
 
358
 
 
359
# Create database directories
 
360
for dir in $ldata $ldata/mysql $ldata/test
 
361
do
 
362
  if test ! -d $dir
 
363
  then
 
364
    mkdir -p $dir
 
365
    chmod 700 $dir
 
366
  fi
 
367
  if test -n "$user"
 
368
  then
 
369
    chown $user $dir
 
370
    if test $? -ne 0
 
371
    then
 
372
      echo "Cannot change ownership of the database directories to the '$user'"
 
373
      echo "user.  Check that you have the necessary permissions and try again."
 
374
      exit 1
 
375
    fi
 
376
  fi
 
377
done
 
378
 
 
379
if test -n "$user"
 
380
then
 
381
  args="$args --user=$user"
 
382
fi
 
383
 
 
384
# When doing a "cross bootstrap" install, no reference to the current
 
385
# host should be added to the system tables.  So we filter out any
 
386
# lines which contain the current host name.
 
387
if test $cross_bootstrap -eq 1
 
388
then
 
389
  filter_cmd_line="sed -e '/@current_hostname/d'"
 
390
else
 
391
  filter_cmd_line="cat"
 
392
fi
 
393
 
 
394
# Configure mysqld command line
 
395
mysqld_bootstrap="${MYSQLD_BOOTSTRAP-$mysqld}"
 
396
mysqld_install_cmd_line="$mysqld_bootstrap $defaults $mysqld_opt --bootstrap \
 
397
  --basedir=$basedir --datadir=$ldata --log-warnings=0 --loose-skip-innodb \
 
398
  --loose-skip-ndbcluster $args --max_allowed_packet=8M \
 
399
  --default-storage-engine=myisam \
 
400
  --net_buffer_length=16K"
 
401
 
 
402
# Create the system and help tables by passing them to "mysqld --bootstrap"
 
403
s_echo "Installing MySQL system tables..."
 
404
if { echo "use mysql;"; cat $create_system_tables $fill_system_tables; } | eval "$filter_cmd_line" | $mysqld_install_cmd_line > /dev/null
 
405
then
 
406
  s_echo "OK"
 
407
else
 
408
  echo
 
409
  echo "Installation of system tables failed!  Examine the logs in"
 
410
  echo "$ldata for more information."
 
411
  echo
 
412
  echo "You can try to start the mysqld daemon with:"
 
413
  echo
 
414
  echo "    shell> $mysqld --skip-grant &"
 
415
  echo
 
416
  echo "and use the command line tool $bindir/mysql"
 
417
  echo "to connect to the mysql database and look at the grant tables:"
 
418
  echo
 
419
  echo "    shell> $bindir/mysql -u root mysql"
 
420
  echo "    mysql> show tables"
 
421
  echo
 
422
  echo "Try 'mysqld --help' if you have problems with paths.  Using --log"
 
423
  echo "gives you a log in $ldata that may be helpful."
 
424
  echo
 
425
  echo "Please consult the MySQL manual section"
 
426
  echo "'Problems running mysql_install_db', and the manual section that"
 
427
  echo "describes problems on your OS.  Another information source are the"
 
428
  echo "MySQL email archives available at http://lists.mysql.com/."
 
429
  echo
 
430
  echo "Please check all of the above before mailing us!  And remember, if"
 
431
  echo "you do mail us, you MUST use the $scriptdir/mysqlbug script!"
 
432
  echo
 
433
  exit 1
 
434
fi
 
435
 
 
436
s_echo "Filling help tables..."
 
437
if { echo "use mysql;"; cat $fill_help_tables; } | $mysqld_install_cmd_line > /dev/null
 
438
then
 
439
  s_echo "OK"
 
440
else
 
441
  echo
 
442
  echo "WARNING: HELP FILES ARE NOT COMPLETELY INSTALLED!"
 
443
  echo "The \"HELP\" command might not work properly."
 
444
fi
 
445
 
 
446
# Don't output verbose information if running inside bootstrap or using
 
447
# --srcdir for testing.  In such cases, there's no end user looking at
 
448
# the screen.
 
449
if test "$cross_bootstrap" -eq 0 && test -z "$srcdir"
 
450
then
 
451
  s_echo
 
452
  s_echo "To start mysqld at boot time you have to copy"
 
453
  s_echo "support-files/mysql.server to the right place for your system"
 
454
 
 
455
  echo
 
456
  echo "PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !"
 
457
  echo "To do so, start the server, then issue the following commands:"
 
458
  echo
 
459
  echo "$bindir/mysqladmin -u root password 'new-password'"
 
460
  echo "$bindir/mysqladmin -u root -h $hostname password 'new-password'"
 
461
  echo
 
462
  echo "Alternatively you can run:"
 
463
  echo "$bindir/mysql_secure_installation"
 
464
  echo
 
465
  echo "which will also give you the option of removing the test"
 
466
  echo "databases and anonymous user created by default.  This is"
 
467
  echo "strongly recommended for production servers."
 
468
  echo
 
469
  echo "See the manual for more instructions."
 
470
 
 
471
  if test "$in_rpm" -eq 0
 
472
  then
 
473
    echo
 
474
    echo "You can start the MySQL daemon with:"
 
475
    echo "cd $basedir ; $bindir/mysqld_safe &"
 
476
    echo
 
477
    echo "You can test the MySQL daemon with mysql-test-run.pl"
 
478
    echo "cd $basedir/mysql-test ; perl mysql-test-run.pl"
 
479
  fi
 
480
 
 
481
  echo
 
482
  echo "Please report any problems with the $scriptdir/mysqlbug script!"
 
483
  echo
 
484
fi
 
485
 
 
486
exit 0