mysql_fix_privilege_tables.sh 5.62 KB
Newer Older
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1
#!/bin/sh
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright (C) 2000-2006 MySQL AB
# 
# 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; version 2 of the License.
# 
# 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

17 18
# This script is a wrapper to pipe the mysql_fix_privilege_tables.sql
# through the mysql client program to the mysqld server
bk@work.mysql.com's avatar
bk@work.mysql.com committed
19

20 21
# Default values (Can be changed in my.cnf)
password=""
bk@work.mysql.com's avatar
bk@work.mysql.com committed
22
host="localhost"
23
user="root"
24
sql_only=0
monty@mysql.com's avatar
monty@mysql.com committed
25
basedir="@prefix@"
26 27
verbose=0
args=""
28 29 30
port=""
socket=""
database="mysql"
31
bindir=""
monty@mysql.com's avatar
monty@mysql.com committed
32
pkgdatadir="@pkgdatadir@"
33
print_defaults_bindir="."
34 35 36

file=mysql_fix_privilege_tables.sql

37 38 39 40 41 42 43 44 45 46
# The following test is to make this script compatible with the 4.0 where
# the single argument could be a password
if test "$#" = 1
then
  case "$1" in
  --*) ;;
  *) old_style_password="$1" ; shift ;;
  esac
fi

47 48
# The following code is almost identical to the code in mysql_install_db.sh

49 50 51 52 53 54
case "$1" in
    --no-defaults|--defaults-file=*|--defaults-extra-file=*)
      defaults="$1"; shift
      ;;
esac

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
parse_arguments() {
  # We only need to pass arguments through to the server if we don't
  # handle them here.  So, we collect unrecognized options (passed on
  # the command line) into the args variable.
  pick_args=
  if test "$1" = PICK-ARGS-FROM-ARGV
  then
    pick_args=1
    shift
  fi

  for arg do
    case "$arg" in
      --basedir=*) basedir=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
      --user=*) user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
      --password=*) password=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
      --host=*) host=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
72
      --sql|--sql-only) sql_only=1 ;;
73
      --verbose) verbose=1 ;;
74 75 76
      --port=*) port=`echo "$arg" | sed -e "s;--port=;;"` ;;
      --socket=*) socket=`echo "$arg" | sed -e "s;--socket=;;"` ;;
      --database=*) database=`echo "$arg" | sed -e "s;--database=;;"` ;;
77 78 79
      --bindir=*) bindir=`echo "$arg" | sed -e "s;--bindir=;;"`
                  print_defaults_bindir=$bindir
		  ;;
80 81 82 83 84
      *)
        if test -n "$pick_args"
        then
          # This sed command makes sure that any special chars are quoted,
          # so the arg gets passed exactly to the server.
85
          args="$args "`echo "$arg" | sed -e 's,\([^=a-zA-Z0-9_.-]\),\\\\\1,g'`
86 87 88 89 90 91 92 93
        fi
        ;;
    esac
  done
}

# Get first arguments from the my.cfg file, groups [mysqld] and
# [mysql_install_db], and then merge with the command line arguments
94

95
print_defaults=my_print_defaults
96
for dir in ./bin @bindir@ @bindir@ extra $print_defaults_bindir/../bin $print_defaults_bindir/../extra
97 98 99 100 101 102 103
do
  if test -x $dir/my_print_defaults
  then
    print_defaults="$dir/my_print_defaults"
    break
  fi
done
bk@work.mysql.com's avatar
bk@work.mysql.com committed
104

105 106
parse_arguments `$print_defaults $defaults mysql_install_db mysql_fix_privilege_tables`
parse_arguments PICK-ARGS-FROM-ARGV "$@"
107

monty@mysql.com's avatar
monty@mysql.com committed
108
if test -z "$password"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
109
then
monty@mysql.com's avatar
monty@mysql.com committed
110
  password=$old_style_password
bk@work.mysql.com's avatar
bk@work.mysql.com committed
111 112
fi

monty@mysql.com's avatar
monty@mysql.com committed
113 114
# Find where 'mysql' command is located

115 116
dirname=`dirname "$0"`

monty@mysql.com's avatar
monty@mysql.com committed
117
if test -z "$bindir"
118
then
119
  for i in @bindir@ $basedir/bin "$dirname/../client"
monty@mysql.com's avatar
monty@mysql.com committed
120 121 122 123 124 125 126
  do
    if test -f $i/mysql
    then
      bindir=$i
      break
    fi
  done
127 128
fi

129 130 131 132 133 134 135
if test -z "$bindir"
then
  echo "Could not find MySQL command-line client (mysql)."
  echo "Please use --basedir to specify the directory where MySQL is installed."
  exit 1
fi

serg@serg.mylan's avatar
serg@serg.mylan committed
136
cmd="$bindir/mysql --no-defaults --force --user=$user --host=$host"
monty@mysql.com's avatar
monty@mysql.com committed
137 138 139 140 141 142 143
if test ! -z "$port"; then
  cmd="$cmd --port=$port"
fi
if test ! -z "$socket"; then
  cmd="$cmd --socket=$socket"
fi
cmd="$cmd --database=$database"
144

145 146 147 148
if test $sql_only = 1
then
  cmd="cat"
fi
peter@mysql.com's avatar
peter@mysql.com committed
149

ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
150
# Find where first mysql_fix_privilege_tables.sql is located
151
for i in $basedir/support-files $basedir/share $basedir/share/mysql \
152
        $basedir/scripts $pkgdatadir . "$dirname"
153 154 155 156
do
  if test -f $i/$file
  then
    pkgdatadir=$i
157
    break
158 159 160 161 162 163 164 165 166 167
  fi
done

sql_file="$pkgdatadir/$file"
if test ! -f $sql_file
then
  echo "Could not find file '$file'."
  echo "Please use --basedir to specify the directory where MySQL is installed"
  exit 1
fi
peter@mysql.com's avatar
peter@mysql.com committed
168

169 170 171 172 173 174 175 176
s_echo()
{
   if test $sql_only = 0
   then
     echo $1
   fi
}

177
s_echo "This script updates all the mysql privilege tables to be usable by"
andrey@lmy004's avatar
andrey@lmy004 committed
178
s_echo "MySQL 5.1 and above."
179 180 181 182 183
s_echo ""

if test $verbose = 1
then
  s_echo "You can safely ignore all 'Duplicate column' and 'Unknown column' errors"
184
  s_echo "because these just mean that your tables are already up to date."
185 186 187
  s_echo "This script is safe to run even if your tables are already up to date!"
  s_echo ""
fi
188

189 190 191 192 193 194 195 196 197 198 199
run_cmd() {
  # Password argument is added here to allow for spaces in password.
  
  if test ! -z "$password"
  then
    cat $sql_file | $cmd --password="$password"
  else
    cat $sql_file | $cmd
  fi
}

200 201
if test $verbose = 0
then
202
  run_cmd > /dev/null 2>&1
203
else
204
  run_cmd > /dev/null
205 206 207 208 209 210
fi
if test $? = 0
then
  s_echo "done"
else
  s_echo "Got a failure from command:"
monty@mysql.com's avatar
monty@mysql.com committed
211
  s_echo "cat $sql_file | $cmd"
212 213 214 215 216 217 218 219 220 221 222
  s_echo "Please check the above output and try again."
  if test $verbose = 0
  then
    s_echo ""
    s_echo "Running the script with the --verbose option may give you some information"
    s_echo "of what went wrong."
  fi
  s_echo ""
  s_echo "If you get an 'Access denied' error, you should run this script again and"
  s_echo "give the MySQL root user password as an argument with the --password= option"
fi