File:  [LON-CAPA] / loncom / metadata_database / configure_mysql_db.pl
Revision 1.4: download - view: text, annotated - select for diffs
Tue May 31 02:40:08 2011 UTC (13 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_X, version_2_11_5_msu, version_2_11_5, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, version_2_10_X, version_2_10_1, version_2_10_0, loncapaMITrelate_1, language_hyphenation_merge, language_hyphenation, HEAD, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
- TYPE=MYISAM was replaced with ENGINE=MYISAM in MySQL 4.
- TYPE was deprecated in 4 and 5.0/5.1.  It is no longer supported in 5.5.

#!/usr/bin/perl -w
# The LearningOnline Network 
# Red Hat 7.3 installation script
#
# $Id: configure_mysql_db.pl,v 1.4 2011/05/31 02:40:08 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA 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.
#
# LON-CAPA 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 LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# http://www.lon-capa.org/
#
use strict;
use lib '/home/httpd/lib/perl/';
use LONCAPA::Configuration;

my $runstatus = shift();
if (! defined($runstatus)) {
    $runstatus = 'firstrun';
}
my $rhver=`perl ../build/distprobe`;
my $rpmname;
if ($rhver eq 'redhat9') {
    $rpmname = "perl-DBD-MySQL-2.9002-1.i386.rpm";
} else {
    $rpmname = "perl-DBD-MySQL-1.2216-4.i386.rpm";
}

my $perlvar = &LONCAPA::Configuration::read_conf('loncapa.conf');
delete($perlvar->{'lonReceipt'}); # remove since sensitive and not needed

##
## Set up mysql to run on boot
##
my $add_result = system("/sbin/chkconfig --add mysqld 2>/dev/null");
my $on_result = system("/sbin/chkconfig mysqld on 2>/dev/null");
if ($add_result != 0 || $on_result != 0) {
    print "Your system is misconfigured.  ".
        "You do not have MySQL installed.\n";
    exit 1;
}

##
## Make sure it is running now
##
my $status = system("/etc/rc.d/init.d/mysqld status 1>/dev/null");
if ($status != 0) {
    print(`/etc/rc.d/init.d/mysqld restart`);
    print("Waiting 5 seconds for mysql daemon to restart.\n");
    sleep 5;
    my $status = system("/etc/rc.d/init.d/mysqld status");
    if ($status != 0) {
        die "Unable to start mysql daemon\nHalting\n";
    }
}

##
## Make sure the mysql is configured
##
if ($runstatus eq 'firstrun' && (! exists($perlvar->{'lonSqlAccess'}) || 
                                 ! defined($perlvar->{'lonSqlAccess'}))) {
    ##
    ## Get root password for mysql client
    print <<END;
Please enter a root password for the MySQL database.
It does not have to match your root account password, but you will need
to remember it.
END
    my $rootpass = <>;
    chomp $rootpass;
    print("\n");

    my $extraargs;
    if ($rhver eq 'redhat9') { $extraargs=",'Y','Y'"; }
    print("Starting mysql client and setting up permissions\n");
    open MYSQL, "|mysql -u root mysql" || die "Unable to start mysql\n";
    print MYSQL <<"ENDMYSQLPERMISSIONINIT";
CREATE DATABASE IF NOT EXISTS loncapa;
USE mysql;
INSERT IGNORE INTO user (Host, User, Password)
    VALUES ('localhost','www',password('localhostkey'));
INSERT IGNORE INTO db VALUES ('localhost','loncapa','www',
    'Y','Y','Y','Y','Y','Y','N','Y','Y','Y'$extraargs);
SET PASSWORD FOR root\@localhost=PASSWORD('$rootpass');
DELETE FROM user WHERE host<>'localhost';
FLUSH PRIVILEGES;
EXIT
ENDMYSQLPERMISSIONINIT
    $perlvar->{'lonSqlAccess'} = 'localhostkey';
}

my $dbi_is_present = eval "use DBI;"; # will be defined on error
if (defined($dbi_is_present)) {
    print "Your system is misconfigured.  ".
        "You are missing the perl DBI package\n";
    exit 1;
} else {
    use DBI;
}

my $dbh;
my $MySQL_Check = eval '$dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar->{\'lonSqlAccess\'});';
if (! defined($MySQL_Check)) {
    if ($runstatus ne 'firstrun') {
        print "Your system is misconfigured.\n";
        exit 1;
    }
    #
    # Try to install from the current directory.  
    # This is potentially dangerous
    my $install_result = system("rpm -Uvh $rpmname 2>/dev/null 1>/dev/null");
    if (defined($install_result) && $install_result ne '0') {
        print "Your system is misconfigured.  ".
            "You are missing the perl DBD MySQL package\n";
        exit 1;
    }
    exec "./".$0." secondrun";
}

use DBI;
$dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar->{'lonSqlAccess'});
if (! defined($dbh)) {
    die "Unable to connect to mysql database.\n";
}
$dbh->do(<<ENDDBINIT);
CREATE TABLE IF NOT EXISTS metadata (title TEXT, 
                                     author TEXT, 
                                     subject TEXT, 
                                     url TEXT, 
                                     keywords TEXT, 
                                     version TEXT, 
                                     notes TEXT, 
                                     abstract TEXT, 
                                     mime TEXT, 
                                     language TEXT, 
                                     creationdate DATETIME, 
                                     lastrevisiondate DATETIME, 
                                     owner TEXT, 
                                     copyright TEXT, 
                                     FULLTEXT idx_title (title), 
                                     FULLTEXT idx_author (author), 
                                     FULLTEXT idx_subject (subject), 
                                     FULLTEXT idx_url (url), 
                                     FULLTEXT idx_keywords (keywords), 
                                     FULLTEXT idx_version (version), 
                                     FULLTEXT idx_notes (notes), 
                                     FULLTEXT idx_abstract (abstract), 
                                     FULLTEXT idx_mime (mime), 
                                     FULLTEXT idx_language (language), 
                                     FULLTEXT idx_owner (owner), 
                                     FULLTEXT idx_copyright (copyright)
                                     ) ENGINE=MYISAM
ENDDBINIT

$dbh->do(<<ENDDBINIT);
CREATE TABLE IF NOT EXISTS dynamicmetadata (url TEXT NOT NULL, 
                                            count INT UNSIGNED,
                                            num_uses INT UNSIGNED,
                                            clear REAL UNSIGNED,
                                            depth REAL UNSIGNED,
                                            helpful REAL UNSIGNED,
                                            correct REAL UNSIGNED,
                                            technical REAL UNSIGNED,
                                            avetries REAL UNSIGNED,
                                            stdno INT UNSIGNED)

ENDDBINIT

if ($dbh->err) {
    print "Error creating tables: ".$dbh->errstr()."\n";
}
$dbh->disconnect();



FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>