summaryrefslogtreecommitdiffstats
path: root/Services/mariadb/sx/mariadb.sh
blob: af6124b7b624355be42c710ca36ed17c03e4f64a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash

export TERM=dumb
export logfile="/var/log/mysql/mysqld.log"

# Begin configuration before starting daemonized process
# redirect mysql.log to /dev/stderr
function begin_config {
    echo "=> Begin mariadb configuration for host $HOSTNAME"
    ln -s /dev/stderr $logfile
    if [ "$(ls -1 /var/lib/mysql | wc -l)" -le "3"  ]; then
        echo "=> directory /var/lib/mysql is empty, start mysql installation ..."
        install_db
    else 
        echo "=> data found in /var/lib/mysql, skip mysql installation ..."
    fi;
    if [ ! -f /var/lib/mysql/mysql.sock ]; then
        echo "=> mysqld is not running, start server ..."
        start_server
    else
        echo "=> mysqld is already running ..."
    fi;
    update_rootuser
}

# End configuration process just before starting daemon
# stop output of mysql.log to /dev/stderr and create mysql.log file
function end_config {
    stop_server
    rm $logfile
    touch $logfile
    echo "=> End mariadb configuration ..."
}

# Preform installation of database structure. Must be used when no
# database is already set
function install_db {
    echo "===> Installing mariadb databases ..."
    mysql_install_db -u mysql > /dev/null 2>&1
    chown -R mysql:mysql /var/lib/mysql
}

# Start the mysqld server in background. Used to perform config
# against the database structure such as user creation
function start_server {
    echo "===> Starting mariadb server ..."
    /usr/bin/mysqld_safe > /dev/null 2>&1 &
    sleep 8
}

# Stop the mysqld server running in background. 
function stop_server {
    echo "===> Stopping mariadb server ..."
    killall mysqld mysqld_safe
    sleep 8
}

# Start the mysqld server as a deamon and execute it inside 
# the running shell
function start_daemon {
    echo "=> Starting mariadb daemon ..."
    exec /usr/libexec/mysqld
}

# Set new root password and grant permissions to all databases
function update_rootuser {
    if [ "$mysql_newadminpwd" = "" ]; then
       export mysql_newadminpwd=$(pwgen 13 1);
    fi
    local n=$mysql_newadminpwd;
    echo "===> Update root user password and permission"
    mysqladmin -u root password $n
    mysql -u root -p$n -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$n' WITH GRANT OPTION; FLUSH PRIVILEGES;"
    unset mysql_newadminpwd;
    export MARIADB_ROOTPWD=$n;
    echo "========================================================================";
    echo "You can now connect to this MariaDB Server using the following credentials:";
    echo " ";
    echo "        user type : administrator";
    echo "        username  : root";
    echo "        password  : $n";
    echo " ";
    echo "        mysql -u root -p$n";
    echo "========================================================================";
    echo " ";
    return 0
}

# Find all sqlfiles in /tmp/ and import then using admin user
function import_sqlfiles {
    local filedir=$1; local p=$MARIADB_ROOTPWD;
    if [ "$(ls -1 $filedir | wc -l)" -ge "1"  ]; then
        echo "=> Found SQL files to import ..."
        for filename in "$filedir"; do
                import_sqlfile $filename
        done;
    fi;
    return 0
}

# Find all sqlfiles in /tmp/ and import then using admin user
function import_sqlfile {
    local filename=$1; local p=$MARIADB_ROOTPWD;
    if [ -f "$filename" ]; then
        echo "===> Importing sql file : $filename"
        mysql -u root -p$p < $filename
    else 
        echo "====> Could not find sql file $filename. Skip import..."
    fi;
    return 0
}

# Set new root password and grant permissions to all databases
function create_userdb {
    local userdb="$1"; local pass="$2"; local p=$MARIADB_ROOTPWD;
    if [ "$pass" = "" ]; then
       local pass=$(pwgen 13 1);
    fi
    echo "===> Create new user $userdb with database $userdb"
    mysql -u root -p$p -e "CREATE USER '$userdb'@'%';SET PASSWORD FOR '$userdb'@'%' = PASSWORD('$pass');\
                           CREATE USER '$userdb'@'localhost';SET PASSWORD FOR '$userdb'@'localhost' = PASSWORD('$pass');\
                           DROP DATABASE IF EXISTS $userdb; \
                           CREATE DATABASE $userdb DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; \
                           GRANT ALL PRIVILEGES ON $userdb.* TO '$userdb'@'%' IDENTIFIED BY '$pass'; \
                           GRANT ALL PRIVILEGES ON $userdb.* TO '$userdb'@'localhost' IDENTIFIED BY '$pass'; \
                           FLUSH PRIVILEGES;"
    echo "========================================================================";
    echo "You can now connect to this MariaDB Server using the following credentials:";
    echo " ";
    echo "        user type : user ";
    echo "        username  : $userdb ";
    echo "        password  : $pass";
    echo "        database  : $userdb ";
    echo " ";
    echo "        mysql -u $userdb -p$pass $userdb";
    echo "========================================================================";
    echo " ";
    return 0
}

# Set new root password and grant permissions to all databases
function create_user {
    local user="$1"; local pass="$2"; local p=$MARIADB_ROOTPWD;
    if [ "$pass" = "" ]; then
       local pass=$(pwgen 13 1);
    fi
    echo "===> Create new user $user"
    mysql -u root -p$p -e "CREATE USER '$user'@'%';SET PASSWORD FOR '$user'@'%' = PASSWORD('$pass');\
                           CREATE USER '$user'@'localhost';SET PASSWORD FOR '$user'@'localhost' = PASSWORD('$pass');"
    echo "========================================================================";
    echo "You can now connect to this MariaDB Server using the following credentials:";
    echo " ";
    echo "        user type : user ";
    echo "        username  : $user ";
    echo "        password  : $pass ";
    echo " ";
    echo "        mysql -u $user -p$pass";
    echo "========================================================================";
    echo " ";
    return 0
}

# Create a new database
function create_db {
    local db=$1; local p=$MARIADB_ROOTPWD;
    echo "===> Create new database $db"
    mysql -u root -p$p -e "CREATE DATABASE $db DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;";
    echo "===> New database $db CREATED"
    return 0
}

if [[ "$0" == *"mariadb.sh" && ! $1 = "" ]];then
    eval "$@"; 
fi