source: alternc/trunk/install/mysql.sh @ 2168

Revision 2168, 4.8 KB checked in by anarcat, 5 years ago (diff)

add a [mysql] block to our my.cnf that contains the database= settings
so that mysqldump and other utilities using this file don't get a
warning.

Note that we do not convert existing configuration so that
milestone:0.9.8 installs will have to be manually patched to remove
the warning on mysqldump.

See #1127 and Errata_0.9.8.

  • Property svn:executable set to *
Line 
1#!/bin/sh
2#
3# $Id: mysql.sh,v 1.11 2006/01/11 22:51:28 anarcat Exp $
4# ----------------------------------------------------------------------
5# AlternC - Web Hosting System
6# Copyright (C) 2002 by the AlternC Development Team.
7# http://alternc.org/
8# ----------------------------------------------------------------------
9# Based on:
10# Valentin Lacambre's web hosting softwares: http://altern.org/
11# ----------------------------------------------------------------------
12# LICENSE
13#
14# This program is free software; you can redistribute it and/or
15# modify it under the terms of the GNU General Public License (GPL)
16# as published by the Free Software Foundation; either version 2
17# of the License, or (at your option) any later version.
18#
19# This program is distributed in the hope that it will be useful,
20# but WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22# GNU General Public License for more details.
23#
24# To read the license please visit http://www.gnu.org/copyleft/gpl.html
25# ----------------------------------------------------------------------
26# Original Author of file: Benjamin Sonntag
27# Purpose of file: Install a fresh new mysql database system
28# USAGE : "mysql.sh loginroot passroot systemdb"
29# ----------------------------------------------------------------------
30#
31 
32# This script expects the following environment to exist:
33# * host
34# * user
35# * password
36# * database
37# * MYSQL_CLIENT
38#
39# XXX: the sed script should be generated here
40#
41# So this file should generally be sourced like this:
42# . /usr/share/alternc/install/mysql.sh
43#
44# Those values are used to set the username/passwords...
45
46# The grant all is the most important right needed in this script.
47echo "Granting users..."
48# cat <<EOF
49# host: $host
50# user: $user
51# password: $password
52# database: $database
53# EOF
54
55MYSQL_CONFIG="/etc/alternc/my.cnf"
56
57. /etc/alternc/local.sh
58# the purpose of this "grant" is to make sure that the generated my.cnf works
59# this means (a) creating the user and (b) creating the database
60grant="GRANT ALL ON *.* TO '$user'@'${MYSQL_CLIENT}' IDENTIFIED BY '$password' WITH GRANT OPTION;
61CREATE DATABASE IF NOT EXISTS $database;"
62
63echo -n "Trying debian.cnf: "
64mysql="/usr/bin/mysql --defaults-file=/etc/mysql/debian.cnf"
65# If this call fail, we may be connected to a mysql-server version 5.0.
66# In that case, change mysql parameters and retry. Use root / nopassword.
67if ! $mysql <<EOF
68$grant
69EOF
70then
71    echo "failed: debian-sys-maintainer doesn't have the right credentials"
72    echo -n "are we doing an upgrade? "
73    mysql="/usr/bin/mysql --defaults-file=$MYSQL_CONFIG"
74    if ! $mysql <<EOF
75$grant
76EOF
77    then
78        echo "No"
79        echo -n "Assuming clean install (empty root password)... "
80        mysql="/usr/bin/mysql -h$host -uroot "
81        if ! $mysql <<EOF
82$grant
83EOF
84        then
85            echo "Failed"
86            echo -n "Assuming pre 0.9.8 version... "
87            mysql="/usr/bin/mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS"
88            if ! $mysql <<EOF
89$grant
90EOF
91            then
92                echo "No."
93                echo "Can't grant system user $user, aborting";
94                exit 1
95            fi
96        fi
97    fi
98fi
99echo "ok!"
100
101if [ -f $MYSQL_CONFIG ]; then
102    echo "Updating mysql configuration in $MYSQL_CONFIG"
103else
104    echo "Creating mysql configuration in $MYSQL_CONFIG"
105    cat > $MYSQL_CONFIG <<EOF
106# AlternC - Web Hosting System - MySQL Configuration
107# Automatically generated by AlternC configuration, do not edit
108# This file will be modified on package configuration
109# (e.g. upgrade or dpkg-reconfigure alternc)
110[mysql]
111database=
112
113[client]
114EOF
115    chown root:www-data $MYSQL_CONFIG
116    chmod 640 $MYSQL_CONFIG
117fi
118
119# create a sed script to create/update the file
120function set_value() {
121    var=$1
122    RET=$2
123    grep -Eq "^ *$var=" $MYSQL_CONFIG || echo "$var=" >> $MYSQL_CONFIG
124    SED_SCRIPT="$SED_SCRIPT;s\\^ *$var=.*\\$var=\"$RET\"\\"
125}
126
127SED_SCRIPT=""
128# hostname was empty in older (pre-0.9.6?) versions
129if [ -z "$host" ]; then
130    host="localhost"
131fi
132set_value host $host
133set_value database $database
134set_value user $user
135set_value password $password
136
137# take extra precautions here with the mysql password:
138# put the sed script in a temporary file
139SED_SCRIPT_NAME=`mktemp`
140cat > $SED_SCRIPT_NAME <<EOF
141$SED_SCRIPT
142EOF
143sed -f "$SED_SCRIPT_NAME" < $MYSQL_CONFIG > $MYSQL_CONFIG.$$
144mv -f $MYSQL_CONFIG.$$ $MYSQL_CONFIG
145rm -f $SED_SCRIPT_NAME
146
147# Now we should be able to use the mysql configuration
148mysql="/usr/bin/mysql --defaults-file=$MYSQL_CONFIG"
149
150echo "Checking for MySQL connectivity"
151$mysql -e "SHOW TABLES" >/dev/null && echo "MYSQL.SH OK!" || echo "MYSQL.SH FAILED!"
152
153# Final mysql setup: db schema
154echo "installing AlternC schema in $database..."
155$mysql < /usr/share/alternc/install/mysql.sql || echo cannot load database schema
156
Note: See TracBrowser for help on using the repository browser.