~ubuntu-branches/ubuntu/trusty/mariadb-5.5/trusty-proposed

« back to all changes in this revision

Viewing changes to debian/dist/Debian/mariadb-server-5.5.postinst

  • Committer: Package Import Robot
  • Author(s): Otto Kekäläinen
  • Date: 2013-12-22 10:27:05 UTC
  • Revision ID: package-import@ubuntu.com-20131222102705-mndw7s12mz0szrcn
Tags: upstream-5.5.32
Import upstream version 5.5.32

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash -e
 
2
 
 
3
. /usr/share/debconf/confmodule
 
4
 
 
5
if [ -n "$DEBIAN_SCRIPT_DEBUG" ]; then set -v -x; DEBIAN_SCRIPT_TRACE=1; fi
 
6
${DEBIAN_SCRIPT_TRACE:+ echo "#42#DEBUG# RUNNING $0 $*" 1>&2 }
 
7
 
 
8
export PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin
 
9
 
 
10
# This command can be used as pipe to syslog. With "-s" it also logs to stderr.
 
11
ERR_LOGGER="logger -p daemon.err -t mysqld_safe -i"
 
12
# This will make an error in a logged command immediately apparent by aborting
 
13
# the install, rather than failing silently and leaving a broken install.
 
14
set -o pipefail
 
15
 
 
16
invoke() {
 
17
  if [ -x /usr/sbin/invoke-rc.d ]; then
 
18
    invoke-rc.d mysql $1
 
19
  else
 
20
    /etc/init.d/mysql $1
 
21
  fi
 
22
}
 
23
 
 
24
MYSQL_BOOTSTRAP="/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables --loose-innodb=OFF --default-storage-engine=myisam"
 
25
 
 
26
test_mysql_access() {
 
27
       mysql --no-defaults -u root -h localhost </dev/null >/dev/null 2>&1
 
28
}
 
29
 
 
30
# call with $1 = "online" to connect to the server, otherwise it bootstraps
 
31
set_mysql_rootpw() {
 
32
       # forget we ever saw the password.  don't use reset to keep the seen status
 
33
       db_set mysql-server/root_password ""
 
34
 
 
35
       tfile=`mktemp`
 
36
       if [ ! -f "$tfile" ]; then
 
37
               return 1
 
38
       fi
 
39
 
 
40
       # this avoids us having to call "test" or "[" on $rootpw
 
41
       cat << EOF > $tfile
 
42
USE mysql;
 
43
UPDATE user SET password=PASSWORD("$rootpw") WHERE user='root';
 
44
FLUSH PRIVILEGES;
 
45
EOF
 
46
       if grep -q 'PASSWORD("")' $tfile; then
 
47
               retval=0
 
48
       elif [ "$1" = "online" ]; then
 
49
               mysql --no-defaults -u root -h localhost <$tfile >/dev/null
 
50
               retval=$?
 
51
       else
 
52
               $MYSQL_BOOTSTRAP <$tfile
 
53
               retval=$?
 
54
       fi
 
55
       rm -f $tfile
 
56
       return $retval
 
57
}
 
58
 
 
59
# This is necessary because mysql_install_db removes the pid file in /var/run
 
60
# and because changed configuration options should take effect immediately.
 
61
# In case the server wasn't running at all it should be ok if the stop
 
62
# script fails. I can't tell at this point because of the cleaned /var/run.
 
63
set +e; invoke stop; set -e
 
64
    
 
65
case "$1" in
 
66
  configure)
 
67
    mysql_datadir=/usr/share/mysql
 
68
    mysql_statedir=/var/lib/mysql
 
69
    mysql_rundir=/var/run/mysqld
 
70
    mysql_logdir=/var/log
 
71
    mysql_cfgdir=/etc/mysql
 
72
    mysql_newlogdir=/var/log/mysql
 
73
    mysql_upgradedir=/var/lib/mysql-upgrade
 
74
 
 
75
    # first things first, if the following symlink exists, it is a preserved
 
76
    # copy the old data dir from a mysql upgrade that would have otherwise
 
77
    # been replaced by an empty mysql dir.  this should restore it.
 
78
    for dir in DATADIR LOGDIR; do
 
79
        if [ "$dir" = "DATADIR" ]; then targetdir=$mysql_statedir; else targetdir=$mysql_newlogdir; fi
 
80
        savelink="$mysql_upgradedir/$dir.link"
 
81
        if [ -L "$savelink" ]; then
 
82
            # If the targetdir was a symlink before we upgraded it is supposed
 
83
            # to be either still be present or not existing anymore now.
 
84
            if [ -L "$targetdir" ]; then
 
85
                rm "$savelink"
 
86
            elif [ ! -d "$targetdir" ]; then
 
87
                mv "$savelink" "$targetdir"
 
88
            else
 
89
                # this should never even happen, but just in case...
 
90
                mysql_tmp=`mktemp -d -t mysql-symlink-restore-XXXXXX`
 
91
                echo "this is very strange!  see $mysql_tmp/README..." >&2
 
92
                mv "$targetdir" "$mysql_tmp"
 
93
                cat << EOF > "$mysql_tmp/README"
 
94
 
 
95
if you're reading this, it's most likely because you had replaced /var/lib/mysql
 
96
with a symlink, then upgraded to a new version of mysql, and then dpkg
 
97
removed your symlink (see #182747 and others).  the mysql packages noticed
 
98
that this happened, and as a workaround have restored it.  however, because
 
99
/var/lib/mysql seems to have been re-created in the meantime, and because
 
100
we don't want to rm -rf something we don't know as much about, we're going
 
101
to leave this unexpected directory here.  if your database looks normal,
 
102
and this is not a symlink to your database, you should be able to blow
 
103
this all away.
 
104
 
 
105
EOF
 
106
            fi
 
107
        fi
 
108
        rmdir $mysql_upgradedir 2>/dev/null || true
 
109
    done
 
110
    
 
111
    # Ensure the existence and right permissions for the database and
 
112
    # log files.
 
113
    if [ ! -d "$mysql_statedir"       -a ! -L "$mysql_statedir"       ]; then mkdir "$mysql_statedir"; fi
 
114
    if [ ! -d "$mysql_statedir/mysql" -a ! -L "$mysql_statedir/mysql" ]; then mkdir "$mysql_statedir/mysql"; fi
 
115
    if [ ! -d "$mysql_newlogdir"      -a ! -L "$mysql_newlogdir"      ]; then mkdir "$mysql_newlogdir"; fi
 
116
    # When creating an ext3 jounal on an already mounted filesystem like e.g.
 
117
    # /var/lib/mysql, you get a .journal file that is not modifyable by chown.
 
118
    # The mysql_datadir must not be writable by the mysql user under any
 
119
    # circumstances as it contains scripts that are executed by root.
 
120
    set +e
 
121
    chown -R 0:0 $mysql_datadir
 
122
    chown -R mysql $mysql_statedir
 
123
    chown -R mysql $mysql_rundir
 
124
    chown -R mysql:adm $mysql_newlogdir;        chmod 2750 $mysql_newlogdir;
 
125
    for i in log err; do
 
126
      touch             $mysql_logdir/mysql.$i
 
127
      chown mysql:adm   $mysql_logdir/mysql.$i
 
128
      chmod 0640        $mysql_logdir/mysql.$i
 
129
    done
 
130
    set -e
 
131
 
 
132
    # This is important to avoid dataloss when there is a removed
 
133
    # mysql-server version from Woody lying around which used the same
 
134
    # data directory and then somewhen gets purged by the admin.
 
135
    db_set mysql-server/postrm_remove_database false || true
 
136
 
 
137
    # To avoid downgrades.
 
138
    touch $mysql_statedir/debian-5.5.flag
 
139
 
 
140
    # initiate databases. Output is not allowed by debconf :-(
 
141
    # This will fail if we are upgrading an existing database; in this case
 
142
    # mysql_upgrade, called from the /etc/init.d/mysql start script, will
 
143
    # handle things.
 
144
    # Debian: beware of the bashisms... 
 
145
    # Debian: can safely run on upgrades with existing databases 
 
146
    set +e
 
147
    /bin/bash /usr/bin/mysql_install_db --rpm --user=mysql 2>&1 | $ERR_LOGGER
 
148
    set -e
 
149
    
 
150
    ## On every reconfiguration the maintenance user is recreated.
 
151
    #
 
152
    # - It is easier to regenerate the password every time but as people
 
153
    #   use fancy rsync scripts and file alteration monitors, the existing
 
154
    #   password is used and existing files not touched.
 
155
    # - The mysqld statement is like that in mysql_install_db because the
 
156
    #   server is not already running. This has some implications:
 
157
    #   - The amount of newlines and semicolons in the query is important!
 
158
    #   - GRANT is not possible with --skip-grant-tables and "INSERT
 
159
    #     (user,host..) VALUES" is not --ansi compliant
 
160
    # - The echo is just for readability. ash's buildin has no "-e" so use /bin/echo.
 
161
    # - The Super_priv, Show_db_priv, Create_tmp_table_priv and Lock_tables_priv
 
162
    #   may not be present as old Woody 3.23 databases did not have it and the
 
163
    #   admin might not already have run mysql_upgrade which adds them.
 
164
    #   As the binlog cron scripts to need at least the Super_priv, I do first
 
165
    #   the old query which always succeeds and then the new which may or may not.
 
166
 
 
167
    # recreate the credentials file if not present or without mysql_upgrade stanza
 
168
    dc=$mysql_cfgdir/debian.cnf; 
 
169
    if [ -e "$dc" -a -n "`fgrep mysql_upgrade $dc 2>/dev/null`" ]; then
 
170
        pass="`sed -n 's/^[     ]*password *= *// p' $dc | head -n 1`"
 
171
    else
 
172
        pass=`perl -e 'print map{("a".."z","A".."Z",0..9)[int(rand(62))]}(1..16)'`;
 
173
        if [ ! -d "$mysql_cfgdir" ]; then install -o 0 -g 0 -m 0755 -d $mysql_cfgdir; fi
 
174
        cat /dev/null > $dc
 
175
        echo "# Automatically generated for Debian scripts. DO NOT TOUCH!" >>$dc
 
176
        echo "[client]"                                                    >>$dc
 
177
        echo "host     = localhost"                                        >>$dc
 
178
        echo "user     = debian-sys-maint"                                 >>$dc
 
179
        echo "password = $pass"                                            >>$dc
 
180
        echo "socket   = $mysql_rundir/mysqld.sock"                        >>$dc
 
181
        echo "[mysql_upgrade]"                                             >>$dc
 
182
        echo "host     = localhost"                                        >>$dc
 
183
        echo "user     = debian-sys-maint"                                 >>$dc
 
184
        echo "password = $pass"                                            >>$dc
 
185
        echo "socket   = $mysql_rundir/mysqld.sock"                        >>$dc
 
186
        echo "basedir  = /usr"                                             >>$dc
 
187
    fi
 
188
    # If this dir chmod go+w then the admin did it. But this file should not.
 
189
    chown 0:0 $dc
 
190
    chmod 0600 $dc
 
191
 
 
192
    # update privilege tables
 
193
    password_column_fix_query=`/bin/echo -e \
 
194
        "USE mysql\n" \
 
195
        "ALTER TABLE user CHANGE Password Password char(41) character set latin1 collate latin1_bin DEFAULT '' NOT NULL"`;
 
196
    replace_query=`/bin/echo -e \
 
197
        "USE mysql\n" \
 
198
        "SET sql_mode='';\n" \
 
199
        "REPLACE INTO user SET " \
 
200
        "  host='localhost', user='debian-sys-maint', password=password('$pass'), " \
 
201
        "  Select_priv='Y', Insert_priv='Y', Update_priv='Y', Delete_priv='Y', " \
 
202
        "  Create_priv='Y', Drop_priv='Y', Reload_priv='Y', Shutdown_priv='Y', " \
 
203
        "  Process_priv='Y',  File_priv='Y', Grant_priv='Y', References_priv='Y', " \
 
204
        "  Index_priv='Y', Alter_priv='Y', Super_priv='Y', Show_db_priv='Y', "\
 
205
        "  Create_tmp_table_priv='Y', Lock_tables_priv='Y', Execute_priv='Y', "\
 
206
        "  Repl_slave_priv='Y', Repl_client_priv='Y', Create_view_priv='Y', "\
 
207
        "  Show_view_priv='Y', Create_routine_priv='Y', Alter_routine_priv='Y', "\
 
208
        "  Create_user_priv='Y', Event_priv='Y', Trigger_priv='Y',"\
 
209
        "  ssl_cipher='', x509_issuer='', x509_subject='';"`;
 
210
    # Engines supported by etch should be installed per default. The query sequence is supposed
 
211
    # to be aborted if the CREATE TABLE fails due to an already existent table in which case the
 
212
    # admin might already have chosen to remove one or more plugins. Newlines are necessary.
 
213
    install_plugins=`/bin/echo -e \
 
214
        "USE mysql;\n" \
 
215
        "CREATE TABLE IF NOT EXISTS plugin (name char(64) COLLATE utf8_bin NOT NULL DEFAULT '', " \
 
216
        "  dl char(128) COLLATE utf8_bin NOT NULL DEFAULT '', " \
 
217
        "  PRIMARY KEY (name)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='MySQL plugins';" `
 
218
 
 
219
    # Upgrade password column format before the root password gets set.
 
220
    echo "$password_column_fix_query"                        | $MYSQL_BOOTSTRAP 2>&1 | $ERR_LOGGER
 
221
 
 
222
    db_get mysql-server/root_password && rootpw="$RET"
 
223
    if ! set_mysql_rootpw; then
 
224
        password_error="yes"
 
225
    fi
 
226
 
 
227
    set +e
 
228
    echo "$replace_query"                                    | $MYSQL_BOOTSTRAP 2>&1 | $ERR_LOGGER
 
229
    echo "$install_plugins"                                  | $MYSQL_BOOTSTRAP 2>&1 | $ERR_LOGGER
 
230
    set -e
 
231
  ;;
 
232
 
 
233
  abort-upgrade|abort-remove|abort-configure)
 
234
  ;;
 
235
 
 
236
  *)
 
237
    echo "postinst called with unknown argument '$1'" 1>&2
 
238
    exit 1
 
239
  ;;
 
240
esac
 
241
 
 
242
# here we check to see if we can connect as root without a password
 
243
# this should catch upgrades from previous versions where the root
 
244
# password wasn't set.  if there is a password, or if the connection
 
245
# fails for any other reason, nothing happens.
 
246
if [ "$1" = "configure" ]; then
 
247
       if test_mysql_access; then
 
248
               db_input medium mysql-server/root_password || true
 
249
               db_go
 
250
               db_get mysql-server/root_password && rootpw="$RET"
 
251
 
 
252
               if ! set_mysql_rootpw "online"; then
 
253
                       password_error="yes"
 
254
               fi
 
255
       fi
 
256
 
 
257
       if [ "$password_error" = "yes" ]; then
 
258
               db_input high mysql-server/error_setting_password || true
 
259
               db_go
 
260
       fi
 
261
 
 
262
fi
 
263
 
 
264
db_stop # in case invoke failes
 
265
 
 
266
#DEBHELPER#
 
267
 
 
268
exit 0