Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions mysql/mysql_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ MY_CNF="${MYSQL_INSTALLER_HOME}/my.cnf";
COPIED_MY_CNF="$MYSQL_HOME/my.cnf";
SERVER_BIN_DIR="$MYSQL_HOME/bin";
CLIENT_BIN_DIR="$SHELL_HOME/bin";
MY_CNF_REL="../my.cnf";
DATADIR_REL="../data";
INIT_SCHEMA_REL="../scadalts.sql";

MYSQL_PORT=-1;
MYSQL_HOST="";
Expand All @@ -33,6 +36,29 @@ PORT_REGEX='^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1
HOSTNAME_REGEX="^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^(localhost)|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$";
USERNAME_REGEX='^[a-zA-Z0-9_-]+$';

is_directory_empty() {
[ -z "$(ls -A "$1" 2>/dev/null)" ];
}

is_valid_mysql_datadir() {
local datadir="$1";

[ -d "${datadir}" ] || return 1;
[ -d "${datadir}/mysql" ] || return 1;
[ -f "${datadir}/auto.cnf" ] || return 1;
}

set_configured_datadir() {
local config_file="$1";
local datadir="$2";

if grep -Eq '^[[:space:]]*datadir[[:space:]]*=' "${config_file}"; then
sed -i "s|^[[:space:]]*datadir[[:space:]]*=.*$|datadir = ${datadir}|" "${config_file}";
else
printf '\ndatadir = %s\n' "${datadir}" >> "${config_file}";
fi
}

if ! command -v wget &> /dev/null
then
echo "wget could not be found then MySQL Community Server version ${MYSQL_VERSION} installation stop";
Expand Down Expand Up @@ -89,6 +115,7 @@ if [ ! -d "${SERVER_BIN_DIR}" ] && [ ! -z "${SERVER_MYSQL_DEST}" ]; then

cp -a "${MY_CNF}" "${COPIED_MY_CNF}";
cp -a "${INIT_SCHEMA}" "${COPIED_INIT_SCHEMA}";
set_configured_datadir "${COPIED_MY_CNF}" "${DATADIR}";

while [ -z "${MYSQL_HOST}" ] || ! [[ ${MYSQL_HOST} =~ ${HOSTNAME_REGEX} ]]
do
Expand Down Expand Up @@ -116,7 +143,6 @@ if [ ! -d "${SERVER_BIN_DIR}" ] && [ ! -z "${SERVER_MYSQL_DEST}" ]; then
echo -n "[MySQL Community Server] Enter username: ";
read -r MYSQL_USERNAME;
done
echo "user = ${MYSQL_USERNAME}" >> "${COPIED_MY_CNF}";

while [ -z "${MYSQL_PASSWORD}" ]
do
Expand Down Expand Up @@ -152,9 +178,21 @@ if [ ! -d "${CLIENT_BIN_DIR}" ] && [ ! -z "${SHELL_MYSQL_DEST}" ]; then
echo "MySQL Shell version ${MYSQL_VERSION} installed";
fi

if [ -d "${SERVER_BIN_DIR}" ] && [ ! -d "$DATADIR" ]; then
mkdir -p "$DATADIR";
cd "${SERVER_BIN_DIR}";
./mysqld --defaults-file="$MYSQL_HOME/my.cnf" --initialize-insecure --datadir "$DATADIR" --user="${MYSQL_USERNAME}" --init-file="${COPIED_INIT_SCHEMA}" --console;
echo "MySQL Community Server version ${MYSQL_VERSION} configured";
fi
if [ -d "${SERVER_BIN_DIR}" ]; then
if is_valid_mysql_datadir "$DATADIR"; then
echo "MySQL Community Server version ${MYSQL_VERSION} already configured";
elif [ -d "$DATADIR" ] && ! is_directory_empty "$DATADIR"; then
echo "Existing MySQL data directory is incomplete or invalid: $DATADIR";
echo "Remove or repair the directory and rerun installation";
exit 1;
else
mkdir -p "$DATADIR";
cd "${SERVER_BIN_DIR}";
./mysqld --defaults-file="${MY_CNF_REL}" --initialize-insecure --datadir "${DATADIR_REL}" --init-file="${INIT_SCHEMA_REL}" --console;
if [ $? -ne 0 ] || ! is_valid_mysql_datadir "$DATADIR"; then
echo "MySQL data directory initialization failed: $DATADIR";
exit 1;
fi
echo "MySQL Community Server version ${MYSQL_VERSION} configured";
fi
fi
49 changes: 48 additions & 1 deletion mysql_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export MYSQL_HOME="${MYSQL_BASE}/server";
export DATADIR="$MYSQL_HOME/data";
MY_CNF="$MYSQL_HOME/my.cnf";
BINDIR="$MYSQL_HOME/bin";
MY_CNF_REL="../my.cnf";
DATADIR_REL="../data";

MYSQLD_PID="$DATADIR/mysqld.pid";

Expand All @@ -22,6 +24,46 @@ JAVA_BASE="${INSTALLER_HOME}/java";
JDK_BASE="${JAVA_BASE}/jdk";
JAVA_HOME=$("${JAVA_BASE}"/java_install.sh "${JAVA_BASE}" "${JAVA_VERSION}" "${JAVA_UPDATE}" "${JDK_BASE}" | tail -n 1);

is_directory_empty() {
[ -z "$(ls -A "$1" 2>/dev/null)" ];
}

get_configured_datadir() {
sed -n 's/^[[:space:]]*datadir[[:space:]]*=[[:space:]]*//p' "$1" | tail -n 1;
}

validate_mysql_data_dir() {
local configured_datadir="";

if [ ! -f "${MY_CNF}" ]; then
echo "MySQL configuration file not found: ${MY_CNF}";
return 1;
fi

configured_datadir=$(get_configured_datadir "${MY_CNF}");
if [ -n "${configured_datadir}" ] && [ "${configured_datadir}" != "${DATADIR}" ]; then
echo "MySQL configuration points to a different data directory";
echo "Configured in my.cnf: ${configured_datadir}";
echo "Expected by installer: ${DATADIR}";
return 1;
fi

if [ ! -d "${DATADIR}" ]; then
echo "MySQL data directory not found: ${DATADIR}";
return 1;
fi

if is_directory_empty "${DATADIR}"; then
echo "MySQL data directory is empty: ${DATADIR}";
return 1;
fi

if [ ! -d "${DATADIR}/mysql" ] || [ ! -f "${DATADIR}/auto.cnf" ]; then
echo "MySQL data directory is incomplete or invalid: ${DATADIR}";
return 1;
fi
}

if [ ! -d "${BINDIR}" ]; then
"${MYSQL_BASE}"/mysql_install.sh ${MYSQL_MAJOR_VERSION} ${MYSQL_MINOR_VERSION} ${MYSQL_PATCH_VERSION} ${JAVA_HOME};
fi
Expand All @@ -31,5 +73,10 @@ if [ ! -d "${BINDIR}" ]; then
exit 1;
fi

if ! validate_mysql_data_dir; then
echo "MySQL data directory validation failed then running stop";
exit 1;
fi

cd "${BINDIR}";
./mysqld --defaults-file="${MY_CNF}" --datadir "$DATADIR" --console;
./mysqld --defaults-file="${MY_CNF_REL}" --datadir "${DATADIR_REL}" --console;