#!/bin/sh # Set the CCSID so text files are created in a shell readable format # # Set the stdin output to nont be passed back to QSH # export QIBM_CCSID; QIBM_CCSID=819 export QIBM_QSH_CMD_OUTPUT; QIBM_QSH_CMD_OUTPUT=NONE export QIBM_MULTI_THREADED; QIBM_MULTI_THREADED='Y' # Unset necessary variables # base_dir= os_user= data_dir= for arg in "$@"; do case "$arg" in --mysql-user=*) mysql_user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;; --os-user=*) os_user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;; --datadir=*) data_dir=`echo "$arg" | sed -e 's/^[^=]*=//'` ;; --password=*) password=`echo "$arg" | sed -e 's/^[^=]*=//'` ;; --basedir=*) base_dir=`echo "$arg" | sed -e 's/^[^=]*=//'` ;; --method=*) method=`echo "$arg" | sed -e 's/^[^=]*=//'` ;; --current-install=*) cur_install_dir=`echo "$arg" | sed -e 's/^[^=]*=//'` ;; esac done # Check for required unset variables # if [ -z "$base_dir" ] ; then error_exit $LINENO "Required --basedir option was not provided!" fi if [ -z "$os_user" ] ; then error_exit $LINENO "Required --os-user option was not provided!" fi if [ -z "$data_dir" ] ; then error_exit $LINENO "Required --datadir option was not provided!" fi # Check that base directory exists or create it # if [ ! -d $base_dir ] ; then if [ -z "$cur_install_dir" ] ; then mkdir -p $base_dir || error_exit "$LINENO: Could not create base directory." fi fi # Tar file is placed in /tmp by the CL program. Check the mysqlinst # # library for the TAR SAVF file if the tar file is not expanded to /tmp # tarbase="/usr/bin/tar" tarfile="/tmp/mysql_i5os_install.tar" # install_ver verifies that only a minor upgrade is being performed # # i.e. 5.1.28 to 5.1.30 NOT 5.1.30 to 6.0.7 # install_ver=5.1 # Upgrade Functions # verify_datadir () { # Verify that the my.cnf file exists and that the data # # directory is listed and exists # if [ -r /etc/my.cnf ] ; then data_dir=`$cur_install_dir/bin/my_print_defaults mysqld | grep datadir | awk -F"=" '{print $2}'` else error_exit $LINENO "No /etc/my.cnf file!" fi if [ -z "$data_dir" -o ! -d "$data_dir" ] ; then error_exit $LINENO "Specified datadir ($data_dir) does not exist!" fi } verify_basedir () { # Verify that the Base directory exists # if [ ! -d $base_dir ] ; then error_exit $LINENO "Specified basedir ($base_dir) does not exist!" fi } verify_installdir () { # Verify that the current installation directory exists # if [ ! -d $cur_install_dir ] ; then error_exit $LINENO "Specified current-install ($cur_install_dir) does not exist!" fi } check_ver () { # Verify that the this is a minor upgrade # # No upgrades from 5.0 to 5.1 or 5.1 to 6.0 # cur_ver=`$cur_install_dir/bin/mysqld --version | awk {'print $3'} | sed -n -e 's,^\([1-9][0-9]*\.[0-9][0-9]*\)\..*$,\1,p'` if [ "$cur_ver" != "$install_ver" ] ; then error_exit $LINENO "Major upgrades are not allowed using the upgrade installer" fi } check_running () { # Verify that no MySQL servers are running during upgrade # running=`ps -efl | grep '[m]ysqld'` echo $running >> $log if [ ! -z "$running" ]; then error_exit $LINENO "MySQL appears to be running from the Process list. Unable to upgrade! Matching process listing: $running" fi } upgrade_db () { # Start the MySQL server with no networking and no grants # # Run the upgrade program on the data directory # # Check for a password with the user. The upgrade program # # reloads the grant tables. # # Shutdown the MySQL server. # i=0 echo "Starting MySQL Server with:\nnew data directory: $data_dir \ntemporary directory: $temp \ninstall directory: $newinstalldir" >> $log $newinstalldir/bin/mysqld --no-defaults --user=mysql --skip-networking --skip-grant-tables --socket=$data_dir/upgrade.sock --basedir=$newinstalldir --datadir=$data_dir >> $log 2>&1 & while [ ! -r $data_dir/upgrade.sock ] ; do let i=i+1 sleep 1 if [ $i -eq 200 ] ; then error_exit $LINENO "MySQL server did not start within 200 seconds. Unable to continue the upgrade process." fi done echo "Starting mysql_upgrade on data directory: $data_dir basedir: $newinstalldir" >> $log $newinstalldir/bin/mysql_upgrade --socket=$data_dir/upgrade.sock --datadir=$data_dir --basedir=$newinstalldir --verbose --force >> $log 2>&1 if [ $? -ne 0 ]; then if [ -z "$password" ] ; then $newinstalldir/bin/mysqladmin --socket=$data_dir/upgrade.sock shutdown -u $mysql_user >> $log else $newinstalldir/bin/mysqladmin --socket=$data_dir/upgrade.sock shutdown -u $mysql_user -p$password >> $log fi error_exit $LINENO "mysql_upgrade exited with an error" fi echo "Shutting down the MySQL server." >> $log if [ -z "$password" ] ; then $newinstalldir/bin/mysqladmin --socket=$data_dir/upgrade.sock shutdown -u $mysql_user >> $log else $newinstalldir/bin/mysqladmin --socket=$data_dir/upgrade.sock shutdown -u $mysql_user -p$password >> $log fi } # New Installation Functions # create_datadir () { # Check if the data directory already exists and if it does # # not then create it # echo $data_dir >> $log echo $base_dir >> $log echo $mysql_user >> $log if [ -z "$data_dir" ]; then error_exit $LINENO "No data directory specified during data directory creation." fi if [ ! -d "$data_dir" ] ; then mkdir $data_dir || error_exit $LINENO "Could not create new data directory." fi } initial_db () { # Check for the user.frm file in the data directory. # # If it exists generate error and prevent overwriting # # The mysql database. # # If user.frm does not exist then call mysql_install_db # # to create the mysql and help databases. # cd $newinstalldir if [ ! -r $data_dir/mysql/user.frm ] ; then ${newinstalldir}/scripts/mysql_install_db --basedir=$newinstalldir --datadir=$data_dir --user=$os_user >> $log 2>&1 else error_exit $LINENO "MySQL system schema exists in $data_dir/mysql! Database Initialization has failed! Exiting installation." fi if [ ! -r $data_dir/mysql/user.frm ] ; then error_exit $LINENO "MySQL database Initialization failed! mysql_install_db script did not create files." fi chown -R $os_user $data_dir || error_exit "$LINENO:" } create_cnf () { # Check for a current my.cnf file. If it exists then error. # # If it does not create the default my.cnf file in /etc # if [ ! -r /etc/my.cnf ] ; then #Export the CCSID for the /etc/my.cnf file for the correct text format # export QIBM_CCSID; QIBM_CCSID=819 echo '# Created at installation.' > /etc/my.cnf echo "[mysqld]" >> /etc/my.cnf echo "datadir = $data_dir" >> /etc/my.cnf echo "user = $os_user" >> /etc/my.cnf chmod 660 /etc/my.cnf chown $os_user /etc/my.cnf else error_exit $LINENO "Configuration file can not be created. /etc/my.cnf file already exists!" fi } # Common Functions # unpack () { # Extract the new installation from the tar file. # # Check for old symlink and remove if it exists. # # Create new symlink to the MySQL installation directory # # Change the file rights to the OS user account. # echo "Extracting tar file for installation." >> $log cd $base_dir $tarbase -xf $tarfile || error_exit $LINENO "Error decompressing tar file ($tarfile) in /tmp directory." echo "Create new symbolic link to the MySQL installation." >> $log if [ -h mysql ] ; then rm mysql || error_exit $LINENO: "Unable to remove the original symbolic link from the base directory." fi symlink=`$tarbase -tf $tarfile | head -n 1 | sed -e 's,/$,,'` ln -s $symlink mysql || error_exit $LINENO "Unable to create a new symbolic link to the installation directory." newinstalldir=$base_dir/$symlink echo "Changing ownership of the installation directory to the MYSQL user." >> $log chown -R $os_user $newinstalldir || error_exit $LINENO: "Unable to change the ownership of the installation directory to the $os_user user." #cd $newinstalldir } error_exit () { # ---------------------------------------------------------------- # Function for exit due to fatal program error # Accepts 1 argument: # string containing descriptive error message # ---------------------------------------------------------------- msg="$1: ${2:-"Unknown Error"}" >> $log echo "$msg" >> $log echo "$msg" 1>&2 exit 1 } if [ -z "$cur_install_dir" ] ; then # no current installation directory -- perform fresh install # log="$base_dir/install.log" echo "Installation started at: `date`" >> $log create_datadir unpack create_cnf initial_db echo "Installation completed at `date`" >> $log exit 0 else # perform an upgrade # log="$base_dir/upgrade.log" echo "upgrade started at: `date`" >> $log verify_datadir verify_basedir verify_installdir check_ver check_running unpack upgrade_db echo "Upgrade completed at `date`" >> $log exit 0 fi