Datenbank per Skript anlegen

F

flugopa

Den Abschnitt "Setup MySQL" unter http://docs.moodle.org/en/RedHat_Linux_installation
möchte ich gern in ein Skript packen.
Code:
#
    * mysql -u root -p
          o (at the password prompt, enter the password from above) 

# At the '>' MySQL prompt, enter the following commands (MySQL commands are ended with a ';')

    * CREATE DATABASE mymoodle;
          o (the name 'mymoodle' is the same name as the database from Step 4) 
    * GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,INDEX,ALTER ON mymoodle.*
          o (as above, 'mymoodle' is from Step 4) 
    * TO moodleuser@localhost IDENTIFIED BY 'moodlepass';
          o ('moodleuser' and 'moodlepass' are from Step 4) 
    * flush privileges;
    * quit
Wie löst ihr das?
 
Ungetesteter Schnellschuss:

Code:
mysql -u root -p **** < dein_mysql_script.sql

wobei dein_mysql_script.sql halt eben die Befehle enthält wie

Code:
CREATE DATABASE mymoodle;
......
 
Ich könnte nochmal eure Hilfe gebrauchen.
Was habe ich in den Skript vergessen/übersehen?
Code:
-rwxr-x--- 1 root        root  1332 13. Mai 23:27 mysql-install.sh

Inhalt von mysql-install.sh:

#!/bin/sh
clear

grep mysql_admin /etc/passwd || useradd -s /sbin/nologin mysql_admin

[ -d /MySQL/ ] || mkdir /MySQL
chown -R mysql_admin /MySQL
chmod -R 740 /MySQL

if [ -f /etc/my.cnf ]; then
    echo -e "\n\n\tBestehende MySQL-Konfiguration überschreiben? [y/N] \c";
    read qa;
    if [ "$qa" = "y" ]; then
	ps -e | egrep mysqld && service mysqld stop
        mv /etc/my.cnf /etc/my.cnf.`date +%s`
    
	echo -e "\n\tErstelle eine neue my.cnf" 
cat > /etc/my.cnf <<EOF_MySQL
[mysqld]
datadir=/MySQL
socket=/var/lib/mysql/mysql.sock
user=mysql_admin
#old_passwords=1
bind_address = 127.0.0.1
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
EOF_MySQL

	echo -e "\n\t Starte MySQL"
	service mysqld restart

	echo -e "\n\tErstelle eine neue Datenbank-Vorgabe"
cat >vorgabe.sql<<EOF_VOR
CREATE DATABASE mymoodle;
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,INDEX,ALTER ON mymoodle.* TO mysql_admin@localhost IDENTIFIED BY 'moodlepass';
flush privileges;
quit
EOF_VOR

	ps -e | egrep mysqld
        if [ "$?" = "0" ]; then
#	    mysqladmin -u mysql_admin password mysql
	    mysql -u mysql_admin --password=mysql < vorgabe.sql
        else
	    echo -e "\n\n\tDer Dienst MySQL konnte nicht gestartet werden"
        fi
    fi
fi
 
Öhm, was passiert denn bzw. passiert nicht bei Scriptausführung?
Ne kleine Ausgabe oder Statusmeldung dazu wär schon was feines ;) .
 
Code:
...
grep mysql_admin /etc/passwd || useradd -s /sbin/nologin mysql_admin
...
mysql -u mysql_admin --password=mysql < vorgabe.sql
...

mysql_admin muss ein MySQL-User sein, kein User vom System
 
... Statusmeldung dazu wär schon was feines ;) .

Was ich möchte?
Einen MySQL-Admin der die Datenbanken nach belieben verwalten kann und einen MySQL-User mit eingeschränkten Rechten.

Hier meine kleinen Änderung: (etwas tiefer die Logfiles)

Code:
[root@pc1600 MySQL]# cat mysql-install.sh
#!/bin/sh
clear
# Verwendung auf eigene Gefahr

userdel mysql_admin
groupdel GRP_MySQL
rm -rf /MySQL

grep GRP_MySQL /etc/group || groupadd GRP_MySQL

PW=mein_mysql_password
MySQL_USER=root

grep mysql_admin /etc/passwd || useradd -u 929 -G GRP_MySQL -s /bin/bash -c "MySQL-Administrator" mysql_admin -p $PW
usermod -p $PW mysql_admin

[ -d /MySQL/ ] || mkdir /MySQL
chmod -R 0750 /MySQL
chown -R $MySQL_USER:GRP_MySQL /MySQL

if [ -f /etc/my.cnf ]; then
    echo -e "\n\n\tBestehende MySQL-Konfiguration überschreiben? [y/N] \c";
    read qa;
    if [ "$qa" = "y" ]; then
        ps -e | egrep mysqld
        if [ "$?" = "0" ]; then
            echo -e "\n\tMySQL wird angehalten"
            service mysqld stop
        fi
        mv /etc/my.cnf /etc/my.cnf.`date +%s`
        echo -e "\n\tErstelle eine neue my.cnf"

cat > /etc/my.cnf <<EOF_MySQL
[mysqld]
datadir=/MySQL
socket=/var/lib/mysql/mysql.sock
user=mysql
#old_passwords=1
bind_address = 127.0.0.1
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

#[mysqld_safe]
#log-error=/var/log/mysqld.log
#pid-file=/var/run/mysqld/mysqld.pid
EOF_MySQL

        echo -e "\n\t Starte MySQL"
        service mysqld start

        echo -e "\n\tErstelle eine neue Datenbank-Vorgabe"
cat >vorgabe.sql<<EOF_VOR
CREATE DATABASE mymoodle;
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,INDEX,ALTER ON mymoodle.* TO $MySQL_USER@localhost IDENTIFIED BY 'moodlepass';
flush privileges;
quit
EOF_VOR

        ps -e | egrep mysqld
        if [ "$?" = "0" ]; then
#           mysqladmin -u mysql_admin password $PW
#           mysql -u $MySQL_USER --password=$PW < vorgabe.sql
            mysql -u root < vorgabe.sql
        else
            echo -e "\n\n\tDer Dienst MySQL konnte nicht gestartet werden"
        fi
    fi
fi
tac /var/log/mysqld.log | more

So sieht es während der Ausführung aus:
Code:
[root@pc1600 MySQL]# ./mysql-install.sh
Erzeuge Mailbox-Datei: Die Datei existiert bereits
useradd: Warnung: Das Home-Verzeichnis existiert bereits.
Es werden keine Dateien vom skel-Verzeichnis dorthin kopiert.


        Bestehende MySQL-Konfiguration überschreiben? [y/N] y

        Erstelle eine neue my.cnf

         Starte MySQL
MySQL-Datenbank initialisieren:  Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h pc1600.netzwerk.local password 'new-password'
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
                                                           [  OK  ]
MySQL starten:                                             [  OK  ]

        Erstelle eine neue Datenbank-Vorgabe
 5057 pts/2    00:00:00 mysqld_safe
 5105 pts/2    00:00:00 mysqld
Version: '5.0.45'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  Source distribution
080514 21:08:11 [Note] /usr/libexec/mysqld: ready for connections.
080514 21:08:11  InnoDB: Started; log sequence number 0 0
InnoDB: Foreign key constraint system tables created
InnoDB: Creating foreign key constraint system tables
InnoDB: Doublewrite buffer created
InnoDB: Doublewrite buffer not found: creating new
InnoDB: Database physically writes the file full: wait...
InnoDB: Setting log file ./ib_logfile1 size to 5 MB
080514 21:08:11  InnoDB: Log file ./ib_logfile1 did not exist: new to be created
InnoDB: Database physically writes the file full: wait...
InnoDB: Setting log file ./ib_logfile0 size to 5 MB
080514 21:08:11  InnoDB: Log file ./ib_logfile0 did not exist: new to be created
InnoDB: Database physically writes the file full: wait...
080514 21:08:11  InnoDB: Setting file ./ibdata1 size to 10 MB
InnoDB: a new database to be created!
InnoDB: The first specified data file ./ibdata1 did not exist:
080514 21:08:11  mysqld started

Und hier mit mysql_admin
Code:
080514 21:00:46  mysqld ended
InnoDB: Cannot continue operation.
InnoDB: File operation call: 'create'.
InnoDB: File name ./ibdata1
InnoDB: the directory.
InnoDB: The error means mysqld does not have the access rights to
080514 21:00:46  InnoDB: Operating system error number 13 in a file operation.
080514 21:00:46  mysqld started
 

Ähnliche Themen

Zugriff Ubuntu 16.04. auf Freigabe 18.04. LTS nicht möglich

JBidWatcher: Problem bei loading Auctions in Verbindung mit mySQL

Samba 4 Gast Zugang unter Ubuntu funktioniert nicht

MacBook Pro hat Benutzer-Konten vergessen

Akonadi startet nicht mehr

Zurück
Oben