Skip to content

Commit

Permalink
mariadb logs fixed
Browse files Browse the repository at this point in the history
Fixed: Conf file in mariadb 10.5 changed and break our log command. Now we have a better default conf file for webinoly.
  • Loading branch information
QROkes committed Nov 5, 2020
1 parent 56feade commit b113668
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 59 deletions.
14 changes: 14 additions & 0 deletions lib/general
Original file line number Diff line number Diff line change
Expand Up @@ -915,3 +915,17 @@ db_user_role() {
echo $priv
}


mysql_default_cnf() {
# Creates the default Webinoly Configuration File (.cnf) for mysql if not exists.
if [[ ! -f /etc/mysql/mariadb.conf.d/90-webinoly.cnf ]]; then
sudo touch /etc/mysql/mariadb.conf.d/90-webinoly.cnf
sudo chmod 644 /etc/mysql/mariadb.conf.d/90-webinoly.cnf
sudo chown -R root:root /etc/mysql/mariadb.conf.d/90-webinoly.cnf

echo "# Webinoly MySQL Configuration File
[mysqld]
log_error = /var/log/mysql/error.log" >> /etc/mysql/mariadb.conf.d/90-webinoly.cnf
fi
}

7 changes: 3 additions & 4 deletions lib/install
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,11 @@ mysql_install() {
echo "mariadb-server-10.5 mysql-server/root_password password $AUTOGENPASS_ROOT" | debconf-set-selections
echo "mariadb-server-10.5 mysql-server/root_password_again password $AUTOGENPASS_ROOT" | debconf-set-selections
sudo apt -y install mariadb-server

# Binaly logs disabled by default and Error logging not in syslog (syslog is default).
sudo sed -i "/Logging and Replication/a \log_error = \/var\/log\/mysql\/error.log" /etc/mysql/my.cnf
sudo rm -rf /etc/mysql/conf.d/mysqld_safe_syslog.cnf

conf_write mysql true

# Binaly logs disabled by default and Error logging not in syslog (syslog is default).
mysql_default_cnf
sudo log -mysql=binary -disable > /dev/null 2>&1

#Instead of mysql_secure_installation we do this: (same but manually, because not acept unattended)
Expand Down
10 changes: 5 additions & 5 deletions lib/sites
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ wpinstall() {
# Starting with MySQL 8 you no longer can (implicitly) create a user using the GRANT command.
sudo mysql --connect-timeout=10 --user=admin -p$ADMIN_PASS <<_EOF_
CREATE DATABASE $dbname;
CREATE USER '${dbuser}'@'%' IDENTIFIED BY '${dbpass}';
GRANT $(db_user_role) on ${dbname}.* to '${dbuser}'@'%';
CREATE USER '${dbuser}'@'localhost' IDENTIFIED BY '${dbpass}';
GRANT $(db_user_role) on ${dbname}.* to '${dbuser}'@'localhost';
FLUSH PRIVILEGES;
_EOF_

Expand All @@ -381,8 +381,8 @@ _EOF_
elif [[ $dbhost == "localhost" && $dbreassign == "db" ]]; then
# Starting with MySQL 8 you no longer can (implicitly) create a user using the GRANT command.
sudo mysql --connect-timeout=10 --user=admin -p$ADMIN_PASS <<_EOF_
CREATE USER '${dbuser}'@'%' IDENTIFIED BY '${dbpass}';
GRANT $(db_user_role) on ${dbname}.* to '${dbuser}'@'%';
CREATE USER '${dbuser}'@'localhost' IDENTIFIED BY '${dbpass}';
GRANT $(db_user_role) on ${dbname}.* to '${dbuser}'@'localhost';
FLUSH PRIVILEGES;
_EOF_

Expand All @@ -400,7 +400,7 @@ _EOF_
# Starting with MySQL 8 you no longer can (implicitly) create a user using the GRANT command.
sudo mysql --connect-timeout=10 --user=admin -p$ADMIN_PASS <<_EOF_
CREATE DATABASE ${dbname};
GRANT $(db_user_role) on ${dbname}.* to '${dbuser}'@'%';
GRANT $(db_user_role) on ${dbname}.* to '${dbuser}'@'localhost';
FLUSH PRIVILEGES;
_EOF_

Expand Down
9 changes: 2 additions & 7 deletions lib/webin
Original file line number Diff line number Diff line change
Expand Up @@ -1580,13 +1580,8 @@ dd_mysql_logs() {
if [[ $(is_dd_log mysql) == "true" ]]; then
echo "${gre}Datadog MySQL Log is already enabled!${end}"
elif [[ -f /etc/datadog-agent/conf.d/mysql.d/conf.yaml ]]; then

# REMOVE: Check for stacks built before v1.11.0
if [[ -f /etc/mysql/conf.d/mysqld_safe_syslog.cnf ]]; then
sudo sed -i "/Logging and Replication/a \log_error = \/var\/log\/mysql\/error.log" /etc/mysql/my.cnf
sudo rm -rf /etc/mysql/conf.d/mysqld_safe_syslog.cnf
sudo service mysql restart
fi

mysql_default_cnf # REMOVE: In case the stack was built with an old configuration

logrotate_perm mysql
[[ -f /var/log/mysql/error.log ]] && sudo chmod 644 /var/log/mysql/error.log
Expand Down
84 changes: 41 additions & 43 deletions plugins/log
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ error() {
waiting_for_log() {
echo "${blu}[Log File is Empty] ${dim}Waiting to receive some data, you will see it in real-time here...${end}"
}
cnf_delete() {
#Example: cnf_delete error_log
[[ -f /etc/mysql/mariadb.conf.d/90-webinoly.cnf ]] && sudo sed -i "/^$1 /d" /etc/mysql/mariadb.conf.d/90-webinoly.cnf
}
cnf_write() {
#Example: cnf_write error_log /var/log/mysql/error.log
mysql_default_cnf
cnf_delete $1
[[ -n $2 ]] && local value="= $2"
echo "$1 $value" >> /etc/mysql/mariadb.conf.d/90-webinoly.cnf
}
cnf_read() {
#Example: cnf_read error_log
echo $( grep -P "^$1 = " /etc/mysql/mariadb.conf.d/90-webinoly.cnf | cut -f 2 -d "=" -s | sed 's/ //g' )
}


# Check for custom "lines" value
[[ -n $(conf_read log-lines) && $(conf_read log-lines) =~ ^[0-9]+$ && $(conf_read log-lines) -gt 0 ]] && clines=$(conf_read log-lines) || clines=10
Expand Down Expand Up @@ -106,25 +122,19 @@ elif [[ -n $mysql ]]; then

# General Log
if [[ $mysql == "general" ]]; then
isgrl=$( grep -P "^general_log[\t =]" /etc/mysql/my.cnf | cut -f 2 -d "=" -s | sed 's/[^0-9]*//g' )
if [[ -n $enable && $isgrl != 1 ]]; then
sudo sed -i -E "/^[#]?general_log[\[ \t=]/c general_log = 1" /etc/mysql/my.cnf
sudo sed -i -E "/^[#]?general_log_file[\[ \t=]/c general_log_file = /var/log/mysql/mysql.log" /etc/mysql/my.cnf
if [[ -n $enable ]]; then
cnf_write general_log 1
cnf_write general_log_file /var/log/mysql/mysql.log
sudo service mysql restart
echo "${gre}MariaDB General log was successfully enabled!${end}"
elif [[ -n $enable && $isgrl == 1 ]]; then
echo "${gre}MariaDB General log is already enabled!${end}"
elif [[ -n $disable && $isgrl != 0 ]]; then
sudo sed -i -E "/^[#]?general_log[\[ \t=]/c general_log = 0" /etc/mysql/my.cnf
sudo sed -i -E "/^[#]?general_log_file[\[ \t=]/c #general_log_file = /var/log/mysql/mysql.log" /etc/mysql/my.cnf
elif [[ -n $disable ]]; then
cnf_write general_log 0
sudo service mysql restart
echo "${gre}MariaDB General log was successfully disabled!${end}"
elif [[ -n $disable && $isgrl == 0 ]]; then
echo "${gre}MariaDB General log is already disabled!${end}"
elif [[ -s /var/log/mysql/mysql.log && $isgrl == 1 ]]; then
elif [[ -f /var/log/mysql/mysql.log && $(cnf_read general_log) == 1 ]]; then
[[ ! -s /var/log/mysql/mysql.log ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/mysql/mysql.log
elif [[ $isgrl != 1 ]]; then
elif [[ $(cnf_read general_log) != 1 ]]; then
echo "${red}[ERROR] MariaDB General log is not enabled!${end}"
exit 1
else
Expand All @@ -134,23 +144,18 @@ elif [[ -n $mysql ]]; then

# Slow Query Log
elif [[ $mysql == "slow" ]]; then
islow=$( grep -P "^slow_query_log[\t =]" /etc/mysql/my.cnf | cut -f 2 -d "=" -s | sed 's/[^0-9]*//g' )
if [[ -n $enable && $islow != 1 ]]; then
sudo sed -i -E "/^[#]?slow_query_log[\[ \t=]/c slow_query_log = 1" /etc/mysql/my.cnf
if [[ -n $enable ]]; then
cnf_write slow_query_log 1
sudo service mysql restart
echo "${gre}MariaDB Slow Query log was successfully enabled!${end}"
elif [[ -n $enable && $islow == 1 ]]; then
echo "${gre}MariaDB Slow Query log is already enabled!${end}"
elif [[ -n $disable && $islow != 0 ]]; then
sudo sed -i -E "/^[#]?slow_query_log[\[ \t=]/c slow_query_log = 0" /etc/mysql/my.cnf
elif [[ -n $disable ]]; then
cnf_write slow_query_log 0
sudo service mysql restart
echo "${gre}MariaDB Slow Query log was successfully disabled!${end}"
elif [[ -n $disable && $islow == 0 ]]; then
echo "${gre}MariaDB Slow Query log is already disabled!${end}"
elif [[ -s /var/log/mysql/mariadb-slow.log && $islow == 1 ]]; then
elif [[ -f /var/log/mysql/mariadb-slow.log && $(cnf_read slow_query_log) == 1 ]]; then
[[ ! -s /var/log/mysql/mariadb-slow.log ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/mysql/mariadb-slow.log
elif [[ $islow != 1 ]]; then
elif [[ $(cnf_read slow_query_log) != 1 ]]; then
echo "${red}[ERROR] MariaDB Slow Query log is not enabled!${end}"
exit 1
else
Expand All @@ -160,25 +165,22 @@ elif [[ -n $mysql ]]; then

# Binary Log
elif [[ $mysql == "binary" ]]; then
isbin=$( grep -P "^log_bin[\t =]" /etc/mysql/my.cnf | cut -f 2 -d "=" -s )
if [[ -n $enable && -z $isbin ]]; then
sudo sed -i -E "/^[#]?log_bin[\[ \t=]/c log_bin = /var/log/mysql/mariadb-bin" /etc/mysql/my.cnf
sudo sed -i -E "/^[#]?log_bin_index[\[ \t=]/c log_bin_index = /var/log/mysql/mariadb-bin.index" /etc/mysql/my.cnf
if [[ -n $enable ]]; then
cnf_delete skip-log-bin
cnf_write log_bin /var/log/mysql/mariadb-bin
cnf_write log_bin_index /var/log/mysql/mariadb-bin.index
sudo service mysql restart
echo "${gre}MariaDB Binary log was successfully enabled!${end}"
elif [[ -n $enable && -n $isbin ]]; then
echo "${gre}MariaDB Binary log is already enabled!${end}"
elif [[ -n $disable && -n $isbin ]]; then
sudo sed -i -E "/^[#]?log_bin[\[ \t=]/c #log_bin = /var/log/mysql/mariadb-bin" /etc/mysql/my.cnf
sudo sed -i -E "/^[#]?log_bin_index[\[ \t=]/c #log_bin_index = /var/log/mysql/mariadb-bin.index" /etc/mysql/my.cnf
elif [[ -n $disable ]]; then
cnf_delete log_bin
cnf_delete log_bin_index
cnf_write skip-log-bin
sudo service mysql restart
echo "${gre}MariaDB Binary log was successfully disabled!${end}"
elif [[ -n $disable && -z $isbin ]]; then
echo "${gre}MariaDB Binary log is already disabled!${end}"
elif [[ -s /var/log/mysql/mariadb-bin && -n $isbin ]]; then
elif [[ -f /var/log/mysql/mariadb-bin && -n $(cnf_read log_bin) ]]; then
[[ ! -s /var/log/mysql/mariadb-bin ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/mysql/mariadb-bin
elif [[ -z $isbin ]]; then
elif [[ -z $(cnf_read log_bin) ]]; then
echo "${red}[ERROR] MariaDB Binary log is not enabled!${end}"
exit 1
else
Expand All @@ -188,15 +190,11 @@ elif [[ -n $mysql ]]; then

# Error Log
elif [[ $mysql =~ ^(error|true)$ ]]; then
# REMOVE: Temporal check because we change log path in v1.11.0 - by default mysql send logs to syslog
if [[ ! -f /etc/mysql/conf.d/mysqld_safe_syslog.cnf && -s /var/log/mysql/error.log ]]; then
if [[ -f /var/log/mysql/error.log ]]; then
[[ ! -s /var/log/mysql/error.log ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/mysql/error.log
elif [[ -f /etc/mysql/conf.d/mysqld_safe_syslog.cnf && -s /var/log/syslog ]]; then
[[ ! -s /var/log/syslog ]] && waiting_for_log
sudo tail -f --lines=$clines /var/log/syslog
else
echo "${red}[ERROR] MariaDB Error log (syslog) file not found or still empty!${end}"
echo "${red}[ERROR] MariaDB Error log file not found or still empty! ${dim}(Older versions sent MySQL error log to syslog, check it!)${end}"
exit 1
fi

Expand Down

0 comments on commit b113668

Please sign in to comment.