From 4999a02de5c62c40123a66c49f1e4d7cf0f8b9da Mon Sep 17 00:00:00 2001 From: James Page Date: Tue, 30 Oct 2018 14:03:10 -0600 Subject: [PATCH] Rework mysql-server parts (#8) * Rework mysql-server parts Use packages from distribution and combine mysql parts into a single part. * Rework mysql, run as root --- scripts/mysql-show-help | 50 ----------------- scripts/mysql-start-server | 28 ---------- scripts/mysql-startup | 75 -------------------------- scripts/{ => mysql}/mysql-start-client | 14 ++--- scripts/mysql/mysql-start-server | 72 +++++++++++++++++++++++++ snapcraft.yaml | 56 +++---------------- 6 files changed, 87 insertions(+), 208 deletions(-) delete mode 100755 scripts/mysql-show-help delete mode 100755 scripts/mysql-start-server delete mode 100755 scripts/mysql-startup rename scripts/{ => mysql}/mysql-start-client (73%) create mode 100755 scripts/mysql/mysql-start-server diff --git a/scripts/mysql-show-help b/scripts/mysql-show-help deleted file mode 100755 index d84313f..0000000 --- a/scripts/mysql-show-help +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/bash -# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; version 2 of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - - -cat << EOF - -= MySQL Snap Image = -The MySQL snap image is an experimental release of MySQL for the snap universal Linux package system (http://snapcraft.io/). -This image should be used for testing only, and is not suitable for a production environment. - -== Commands == -- mysql.client: Runs the MySQL client -- mysql.server: Runs the MySQL server -- mysql.startup: Initializes database if necessary, then starts the server -- mysql.help: Shows this document - -== Usage guide == -The MySQL snap requires access to the process-control interface. You connect it by running: - snap connect mysql:process-control core:process-control - -=== Running the snap === -* Install the snap as usual -* Run sudo snap connect mysql:process-control ubuntu-core:process-control -* Run mysql.startup to initialize and start the server -* Run mysql.client -uroot -p - -=== Running the snap a system service === -Running MySQL as a system service requires root access, but the server itself should never run as root, so it drops privileges to a dedicated user. This user must own the server files and directories. Currently snapd blocks access to creating users and changing process user, so the only way to do this is to disable the restrictions by installing the snap with the --devmode argument. - -=== Files and directories === -The first time you run mysql.startup, it will generate the following in $HOME/snap/mysql/common (if run as root, /var/snap/mysql/common is used instead): -- conf/my.cnf: Basic configuration file -- data/: Data directoriy -- files/: Default location for the secure-file-priv option -- log/: Location of error log -- run/: Location of sock and pid files -EOF diff --git a/scripts/mysql-start-server b/scripts/mysql-start-server deleted file mode 100755 index 6275806..0000000 --- a/scripts/mysql-start-server +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/bash -# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; version 2 of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - -USERID=$(id -u) -if [ "${USERID}" = "0" ]; -then - MYSQL_SNAPDIR=${SNAP_COMMON} - VARS="--user=mysql" -else - MYSQL_SNAPDIR=${SNAP_USER_COMMON} - VARS="" -fi - -echo "Starting server..." -mysqld --defaults-file="${MYSQL_SNAPDIR}/conf/my.cnf" ${VARS} $@ diff --git a/scripts/mysql-startup b/scripts/mysql-startup deleted file mode 100755 index b0fb639..0000000 --- a/scripts/mysql-startup +++ /dev/null @@ -1,75 +0,0 @@ -#!/bin/bash -# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; version 2 of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - -set -e -init_config() { - mkdir "${CONFDIR}" - echo "Generating config file in ${CONFFILE}..." - touch "${CONFFILE}" - echo "[mysqld]" >> ${CONFFILE} - echo "pid-file=${RUNDIR}/mysqld.pid" >> ${CONFFILE} - echo "socket=${RUNDIR}/mysqld.sock" >> ${CONFFILE} - echo "datadir=${DATADIR}" >> ${CONFFILE} - echo "log-error=${LOGDIR}/error.log" >> ${CONFFILE} - echo "secure-file-priv=${FILESDIR}" >> ${CONFFILE} - echo "basedir=${BASEDIR}" >> ${CONFFILE} - echo "[mysql]" >> ${CONFFILE} - echo "socket=${RUNDIR}/mysqld.sock" >> ${CONFFILE} - echo "Done" -} - -init_database() { - echo "Initializing new database in ${DATADIR}..." - mkdir "${DATADIR}" - mysqld --defaults-file="${CONFFILE}" --initialize - echo "Done" - cat ${LOGDIR}/error.log | grep "temporary password" -} - -USERID=$(id -u) -if [ "${USERID}" = "0" ];then - MYSQL_SNAPDIR="${SNAP_COMMON}" -else - MYSQL_SNAPDIR="${SNAP_USER_COMMON}" -fi -DATADIR="${MYSQL_SNAPDIR}/data" -RUNDIR="${MYSQL_SNAPDIR}/run" -LOGDIR="${MYSQL_SNAPDIR}/log" -CONFDIR="${MYSQL_SNAPDIR}/conf" -CONFFILE="${CONFDIR}/my.cnf" -FILESDIR="${MYSQL_SNAPDIR}/files" -BASEDIR="${SNAP}/usr" - -[ -d "${LOGDIR}" ] || mkdir "${LOGDIR}" -[ -f "${LOGDIR}/error.log" ] || touch "${LOGDIR}/error.log" -[ -d "${FILESDIR}" ] || mkdir "${FILESDIR}" -[ -d "${RUNDIR}" ] || mkdir "${RUNDIR}" -[ -d "${CONFDIR}" ] || init_config -[ -d "${DATADIR}" ] || init_database - -if [ "${USERID}" = "0" ]; -then - # Ensure mysql user exists and that the correct permissions are set on various directories - adduser --system --disabled-login --ingroup mysql --home /nonexistent --gecos "MySQL Server" --shell /bin/false mysql >/dev/null - chown -R mysql:mysql "${LOGDIR}" "${FILESDIR}" "${DATADIR}" "${RUNDIR}" - chmod 750 "${LOGDIR}" "${DATADIR}" - chmod 770 "${FILESDIR}" - chmod 755 "${RUNDIR}" - VARS="--user=mysql" -fi -echo "Starting server..." -mysqld --defaults-file="${CONFFILE}" ${VARS} $@ - diff --git a/scripts/mysql-start-client b/scripts/mysql/mysql-start-client similarity index 73% rename from scripts/mysql-start-client rename to scripts/mysql/mysql-start-client index 910e887..912393d 100755 --- a/scripts/mysql-start-client +++ b/scripts/mysql/mysql-start-client @@ -14,19 +14,19 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -if [ -f "${SNAP_USER_COMMON}/conf/my.cnf" ]; +if [ -f "${SNAP_USER_COMMON}/etc/mysql/my.cnf" ]; then - MYSQL_CONFFILE="${SNAP_USER_COMMON}/conf/my.cnf" -elif [ -f "${SNAP_COMMON}/conf/my.cnf" ]; + MYSQL_CONFFILE="${SNAP_USER_COMMON}/etc/mysql/my.cnf" +elif [ -f "${SNAP_COMMON}/etc/mysql/my.cnf" ]; then - MYSQL_CONFFILE="${SNAP_COMMON}/conf/my.cnf" + MYSQL_CONFFILE="${SNAP_COMMON}/etc/mysql/my.cnf" else - echo "WARNING: Can't find config file. Did you run mysql.startup?" -fi + echo "WARNING: Can't find conf file. Did you run mysql-startup?" +fi if [ -f ${MYSQL_CONFFILE} ]; then - echo "Using config file: ${MYSQL_CONFFILE}" + echo "Using conf file: ${MYSQL_CONFFILE}" MYSQL_CONFPARAM="--defaults-file=${MYSQL_CONFFILE}" fi mysql "${MYSQL_CONFPARAM}" $@ diff --git a/scripts/mysql/mysql-start-server b/scripts/mysql/mysql-start-server new file mode 100755 index 0000000..3b15767 --- /dev/null +++ b/scripts/mysql/mysql-start-server @@ -0,0 +1,72 @@ +#!/bin/bash +# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +set -e + +init_config() { + mkdir -p "${CONFDIR}" + echo "Generating config file in ${CONFFILE}..." + cat > ${CONFFILE} <