-
Notifications
You must be signed in to change notification settings - Fork 141
/
mysql.functions.sh
125 lines (111 loc) · 4.08 KB
/
mysql.functions.sh
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
#!/usr/bin/env bash
#
# mysql functions
#
# (c) 2016-2021, Hetzner Online GmbH
#
mysql_running() { execute_command_wo_debug mysqladmin ping &> /dev/null; }
start_mysql() {
if installed_os_uses_systemd; then
if ! systemd_nspawn_booted; then
boot_systemd_nspawn || return 1
fi
systemd_nspawn_wo_debug systemctl start mysql &> /dev/null || return 1
else
execute_chroot_command_wo_debug service mysql start &> /dev/null || return 1
fi
until mysql_running; do :; done
}
stop_mysql() {
start_mysql || return 1
if installed_os_uses_systemd; then
systemd_nspawn_wo_debug systemctl stop mysql &> /dev/null || return 1
else
execute_chroot_command_wo_debug service mysql stop &> /dev/null || return 1
fi
while mysql_running; do :; done
}
query_mysql() {
if ! mysql_running; then
start_mysql || return 1
fi
echo "$@" | execute_command_wo_debug mysql -N
return "${PIPESTATUS[1]}"
}
mysql_version() {
# shellcheck disable=SC1001
[[ "$(query_mysql 'SELECT VERSION();')" =~ ^([0-9]*\.[0-9]*\.[0-9]*)\- ]] && echo "${BASH_REMATCH[1]}"
}
mysql_version_ge() {
local other="$1"
[[ "$(echo -e "$(mysql_version)\n$other" | sort -V | head -n 1)" == "$other" ]]
}
set_mysql_password() {
local user="$1"
local password="$2"
if mysql_version_ge 5.7.6; then
if ! [[ "$(query_mysql "SELECT plugin FROM mysql.user WHERE user = '${user//\'/\\\'}';")" =~ ^mysql_native_password$|^unix_socket$|^auth_socket$ ]]; then
local password_field='password'
else
local password_field='authentication_string'
fi
else
local password_field='password'
fi
query_mysql "UPDATE mysql.user SET password_last_changed = NOW() WHERE user = '${user//\'/\\\'}';" &> /dev/null
query_mysql "UPDATE mysql.user SET $password_field = PASSWORD('${password//\'/\\\'}') WHERE user = '${user//\'/\\\'}';" |& debugoutput
(("${PIPESTATUS[0]}" == 0)) || return 1
query_mysql 'FLUSH PRIVILEGES;' |& debugoutput
(("${PIPESTATUS[0]}" == 0)) || return 1
echo QUIT | execute_command_wo_debug mysql -u "$user" -p"$password" |& debugoutput
return "${PIPESTATUS[1]}"
}
reset_mysql_root_password() {
local new_root_password="$1"
if ! mysql_running; then
start_mysql || return 1
fi
stop_mysql || return 1
execute_command_wo_debug mkdir -p /var/run/mysqld || return 1
execute_command_wo_debug chown mysql:mysql /var/run/mysqld || return 1
# work around INSTALL PLUGIN ERROR 1030 (HY000) at line 1: Got error 1 from storage engine
execute_command_wo_debug 'mysqld_safe --skip-grant-tables &> /dev/null &'
until mysql_running; do :; done
local add_args
if [[ "$(query_mysql "SELECT plugin FROM mysql.user WHERE user = 'root';")" =~ ^auth_socket$ ]]; then
if ! query_mysql 'SHOW PLUGINS' | grep -q '^auth_socket[[:space:]]'; then
add_args+=' --plugin-load-add=auth_socket.so'
fi
fi
execute_command_wo_debug mysqladmin shutdown &> /dev/null || return 1
while mysql_running; do :; done
execute_command_wo_debug "mysqld_safe --skip-grant-tables $add_args &> /dev/null &"
until mysql_running; do :; done
set_mysql_password root "$new_root_password" || return 1
{
echo '[client]'
echo 'user=root'
echo "password=$new_root_password"
} > "$FOLD/hdd/root/.my.cnf"
query_mysql QUIT || return 1
execute_command_wo_debug mysqladmin shutdown &> /dev/null || return 1
while mysql_running; do :; done
}
mysql_user_exists() {
local mysql_user="$1"
[[ "$(query_mysql "SELECT COUNT(*) FROM mysql.user WHERE user = '${mysql_user//\'/\\\'}';")" == '0' ]] && return 1
return
}
create_mysql_user() {
local user="$1"
local password="$2"
query_mysql "CREATE USER '${user//\'/\\\'}'@'localhost' IDENTIFIED BY '${password//\'/\\\'}';" |& debugoutput
(("${PIPESTATUS[0]}" == 0)) || return 1
query_mysql "GRANT ALL ON *.* TO '${user//\'/\\\'}'@'localhost' WITH GRANT OPTION;" |& debugoutput
(("${PIPESTATUS[0]}" == 0)) || return 1
query_mysql "FLUSH PRIVILEGES;" |& debugoutput
(("${PIPESTATUS[0]}" == 0)) || return 1
echo QUIT | execute_command_wo_debug mysql -u "$user" -p"$password" |& debugoutput
return "${PIPESTATUS[1]}"
}
# vim: ai:ts=2:sw=2:et