From dfd59cb65f00d884dd98dd6cef1ce752de80e7c2 Mon Sep 17 00:00:00 2001 From: Honza Horak Date: Tue, 22 Jul 2014 10:27:55 +0200 Subject: [PATCH] Port scripts for systemd unit from MariaDB --- community-mysql-scripts.patch | 32 +++++++++ community-mysql.spec | 50 ++++++++----- mysql.tmpfiles.d | 1 - mysql.tmpfiles.d.in | 1 + mysqld-prepare-db-dir | 82 --------------------- mysqld-prepare-db-dir.sh | 87 +++++++++++++++++++++++ mysqld-wait-ready => mysqld-wait-ready.sh | 43 +++++------ mysqld.service => mysqld.service.in | 9 +-- 8 files changed, 175 insertions(+), 130 deletions(-) create mode 100644 community-mysql-scripts.patch delete mode 100644 mysql.tmpfiles.d create mode 100644 mysql.tmpfiles.d.in delete mode 100644 mysqld-prepare-db-dir create mode 100644 mysqld-prepare-db-dir.sh rename mysqld-wait-ready => mysqld-wait-ready.sh (53%) rename mysqld.service => mysqld.service.in (82%) diff --git a/community-mysql-scripts.patch b/community-mysql-scripts.patch new file mode 100644 index 0000000..f658b33 --- /dev/null +++ b/community-mysql-scripts.patch @@ -0,0 +1,32 @@ +diff -rup mysql-5.6.19-orig/scripts/CMakeLists.txt mysql-5.6.19/scripts/CMakeLists.txt +--- mysql-5.6.19-orig/scripts/CMakeLists.txt 2014-07-22 10:07:12.053410263 +0200 ++++ mysql-5.6.19/scripts/CMakeLists.txt 2014-07-22 10:10:44.102502369 +0200 +@@ -381,6 +381,28 @@ ELSE() + ENDIF() + INSTALL_SCRIPT(${CMAKE_CURRENT_BINARY_DIR}/${file} COMPONENT ${${file}_COMPONENT}) + ENDFOREACH() ++ ++ # files for systemd ++ SET(SYSTEMD_SCRIPTS ++ mysql.tmpfiles.d ++ mysqld.service ++ mysqld-prepare-db-dir ++ mysqld-wait-ready ++ mysqld-check-socket ++ mysqld-scripts-common ++ ) ++ FOREACH(file ${SYSTEMD_SCRIPTS}) ++ IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.sh) ++ CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/${file}.sh ++ ${CMAKE_CURRENT_BINARY_DIR}/${file} ESCAPE_QUOTES @ONLY) ++ ELSEIF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.in) ++ CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/${file}.in ++ ${CMAKE_CURRENT_BINARY_DIR}/${file} ESCAPE_QUOTES @ONLY) ++ ELSE() ++ MESSAGE(FATAL_ERROR "Can not find ${file}.sh or ${file}.in in " ++ "${CMAKE_CURRENT_SOURCE_DIR}" ) ++ ENDIF() ++ ENDFOREACH() + ENDIF() + + # Install libgcc as mylibgcc.a diff --git a/community-mysql.spec b/community-mysql.spec index ea04688..a738f19 100644 --- a/community-mysql.spec +++ b/community-mysql.spec @@ -22,9 +22,12 @@ # my.cnf and my.cnf.d %global ship_my_cnf 0 +# Name for the systemd unit file +%global daemon_name mysqld.service + Name: community-mysql Version: 5.6.19 -Release: 4%{?dist} +Release: 5%{?dist} Summary: MySQL client programs and shared libraries Group: Applications/Databases URL: http://www.mysql.com @@ -39,10 +42,12 @@ Source4: mysql_config_multilib.sh Source5: my_config.h Source6: README.mysql-docs Source7: README.mysql-license -Source10: mysql.tmpfiles.d -Source11: mysqld.service -Source12: mysqld-prepare-db-dir -Source13: mysqld-wait-ready +Source10: mysql.tmpfiles.d.in +Source11: mysqld.service.in +Source12: mysqld-prepare-db-dir.sh +Source13: mysqld-wait-ready.sh +Source14: mysqld-check-socket.sh +Source15: mysqld-scripts-common.sh # To track rpmlint warnings Source17: mysql-5.6.10-rpmlintrc @@ -63,6 +68,7 @@ Patch25: community-mysql-5.6.16-mysql-install.patch Patch26: community-mysql-5.6.13-major.patch Patch34: community-mysql-pluginerrmsg.patch Patch35: community-mysql-5.6.19-gcc49-aarch64-opt.patch +Patch37: community-mysql-scripts.patch BuildRequires: cmake BuildRequires: dos2unix @@ -147,7 +153,7 @@ MySQL package. Summary: The error messages files required by server and embedded Group: Applications/Databases -Requires: %{name}-common%{?_isa} = %{epoch}:%{version}-%{release} +Requires: %{name}-common%{?_isa} = %{version}-%{release} %description errmsg The package provides error messages files for the MySQL daemon and the @@ -276,6 +282,7 @@ the MySQL sources. %endif %patch34 -p1 %patch35 -p1 +%patch37 -p1 # Modify tests to pass on all archs pushd mysql-test @@ -313,6 +320,9 @@ add_test 'main.upgrade : unknown' %endif popd +cp %{SOURCE10} %{SOURCE11} %{SOURCE12} %{SOURCE13} %{SOURCE14} \ + %{SOURCE15} scripts + %build # fail quickly and obviously if user tries to build as root %if %runselftest @@ -332,6 +342,7 @@ mkdir build && pushd build cmake .. -DBUILD_CONFIG=mysql_release \ -DFEATURE_SET="community" \ -DINSTALL_LAYOUT=RPM \ + -DDAEMON_NAME="%{daemon_name}" \ -DCMAKE_INSTALL_PREFIX="%{_prefix}" \ %if 0%{?fedora} >= 20 -DINSTALL_DOCDIR="share/doc/%{name}" \ @@ -401,11 +412,13 @@ install -D -p -m 0644 %{SOURCE3} %{buildroot}%{_sysconfdir}/my.cnf %endif # install systemd unit files and scripts for handling server startup -install -D -p -m 644 %{SOURCE11} %{buildroot}%{_unitdir}/%{basename:%SOURCE11} -install -p -m 755 %{SOURCE12} %{buildroot}%{_libexecdir}/ -install -p -m 755 %{SOURCE13} %{buildroot}%{_libexecdir}/ +install -D -p -m 644 scripts/mysqld.service %{buildroot}%{_unitdir}/%{daemon_name} +install -p -m 755 scripts/mysqld-prepare-db-dir %{buildroot}%{_libexecdir}/mysqld-prepare-db-dir +install -p -m 755 scripts/mysqld-wait-ready %{buildroot}%{_libexecdir}/mysqld-wait-ready +install -p -m 755 scripts/mysqld-check-socket %{buildroot}%{_libexecdir}/mysqld-check-socket +install -p -m 644 scripts/mysqld-scripts-common %{buildroot}%{_libexecdir}/mysqld-scripts-common -install -D -p -m 0644 %{SOURCE10} %{buildroot}%{_tmpfilesdir}/%{name}.conf +install -D -p -m 0644 scripts/mysql.tmpfiles.d %{buildroot}%{_tmpfilesdir}/%{name}.conf # mysql-test includes one executable that doesn't belong under /usr/share, # so move it and provide a symlink @@ -489,18 +502,18 @@ popd %post embedded -p /sbin/ldconfig %post server -%systemd_post mysqld.service +%systemd_post %{daemon_name} /bin/touch %{_localstatedir}/log/mysqld.log %preun server -%systemd_preun mysqld.service +%systemd_preun %{daemon_name} %postun libs -p /sbin/ldconfig %postun embedded -p /sbin/ldconfig %postun server -%systemd_postun_with_restart mysqld.service +%systemd_postun_with_restart %{daemon_name} %files %doc README.mysql-docs @@ -648,9 +661,11 @@ popd %{_datadir}/%{name}/mysql_test_data_timezone.sql %{_datadir}/%{name}/my-*.cnf -%{_unitdir}/%{basename:%SOURCE11} -%{_libexecdir}/%{basename:%SOURCE12} -%{_libexecdir}/%{basename:%SOURCE13} +%{_unitdir}/%{daemon_name} +%{_libexecdir}/mysqld-prepare-db-dir +%{_libexecdir}/mysqld-wait-ready +%{_libexecdir}/mysqld-check-socket +%{_libexecdir}/mysqld-scripts-common %{_tmpfilesdir}/%{name}.conf %attr(0755,mysql,mysql) %dir %{_localstatedir}/run/mysqld @@ -687,6 +702,9 @@ popd %{_mandir}/man1/mysql_client_test.1* %changelog +* Tue Jul 22 2014 Honza Horak - 5.6.19-5 +- Port scripts for systemd unit from MariaDB + * Mon Jul 21 2014 Honza Horak - 5.6.19-4 - Port some latest changes from MariaDB package to sync those packages - Error messages now provided by a separate package (thanks Alexander Barkov) diff --git a/mysql.tmpfiles.d b/mysql.tmpfiles.d deleted file mode 100644 index 74cd5f8..0000000 --- a/mysql.tmpfiles.d +++ /dev/null @@ -1 +0,0 @@ -d /var/run/mysqld 0755 mysql mysql - diff --git a/mysql.tmpfiles.d.in b/mysql.tmpfiles.d.in new file mode 100644 index 0000000..0698a19 --- /dev/null +++ b/mysql.tmpfiles.d.in @@ -0,0 +1 @@ +d /var/run/@RPM_PACKAGE_PREFIX@mysqld 0755 mysql mysql - diff --git a/mysqld-prepare-db-dir b/mysqld-prepare-db-dir deleted file mode 100644 index f73bc66..0000000 --- a/mysqld-prepare-db-dir +++ /dev/null @@ -1,82 +0,0 @@ -#!/bin/sh - -# This script creates the mysql data directory during first service start. -# In subsequent starts, it does nothing much. - -# extract value of a MySQL option from config files -# Usage: get_mysql_option SECTION VARNAME DEFAULT -# result is returned in $result -# We use my_print_defaults which prints all options from multiple files, -# with the more specific ones later; hence take the last match. -get_mysql_option(){ - result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1` - if [ -z "$result" ]; then - # not found, use default - result="$3" - fi -} - -# Defaults here had better match what mysqld_safe will default to -get_mysql_option mysqld datadir "/var/lib/mysql" -datadir="$result" -get_mysql_option mysqld_safe log-error "/var/log/mysqld.log" -errlogfile="$result" - -# Absorb configuration settings from the specified systemd service file, -# or the default "mysqld" service if not specified -SERVICE_NAME="$1" -if [ x"$SERVICE_NAME" = x ] -then - SERVICE_NAME=mysqld.service -fi - -myuser=`systemctl show -p User "${SERVICE_NAME}" | - sed 's/^User=//'` -if [ x"$myuser" = x ] -then - myuser=mysql -fi - -mygroup=`systemctl show -p Group "${SERVICE_NAME}" | - sed 's/^Group=//'` -if [ x"$mygroup" = x ] -then - mygroup=mysql -fi - -# Set up the errlogfile with appropriate permissions -touch "$errlogfile" -chown "$myuser:$mygroup" "$errlogfile" -chmod 0640 "$errlogfile" -[ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile" - -# Make the data directory -if [ ! -d "$datadir/mysql" ] ; then - # First, make sure $datadir is there with correct permissions - # (note: if it's not, and we're not root, this'll fail ...) - if [ ! -e "$datadir" -a ! -h "$datadir" ] - then - mkdir -p "$datadir" || exit 1 - fi - chown "$myuser:$mygroup" "$datadir" - chmod 0755 "$datadir" - [ -x /sbin/restorecon ] && /sbin/restorecon "$datadir" - - # Now create the database - echo "Initializing MySQL database" - /usr/bin/mysql_install_db --datadir="$datadir" --user="$myuser" - ret=$? - if [ $ret -ne 0 ] ; then - echo "Initialization of MySQL database failed." >&2 - echo "Perhaps /etc/my.cnf is misconfigured." >&2 - # Clean up any partially-created database files - if [ ! -e "$datadir/mysql/user.frm" ] ; then - rm -rf "$datadir"/* - fi - exit $ret - fi - # In case we're running as root, make sure files are owned properly - chown -R "$myuser:$mygroup" "$datadir" -fi - -exit 0 diff --git a/mysqld-prepare-db-dir.sh b/mysqld-prepare-db-dir.sh new file mode 100644 index 0000000..8581a5f --- /dev/null +++ b/mysqld-prepare-db-dir.sh @@ -0,0 +1,87 @@ +#!/bin/sh + +# This script creates the mysql data directory during first service start. +# In subsequent starts, it does nothing much. + +source "`dirname ${BASH_SOURCE[0]}`/mariadb-scripts-common" + +# If two args given first is user, second is group +# otherwise the arg is the systemd service file +if [ "$#" -eq 2 ] +then + myuser="$1" + mygroup="$2" +else + # Absorb configuration settings from the specified systemd service file, + # or the default "mariadb" service if not specified + SERVICE_NAME="$1" + if [ x"$SERVICE_NAME" = x ] + then + SERVICE_NAME=@DAEMON_NAME@.service + fi + + myuser=`systemctl show -p User "${SERVICE_NAME}" | + sed 's/^User=//'` + if [ x"$myuser" = x ] + then + myuser=mysql + fi + + mygroup=`systemctl show -p Group "${SERVICE_NAME}" | + sed 's/^Group=//'` + if [ x"$mygroup" = x ] + then + mygroup=mysql + fi +fi + +# Set up the errlogfile with appropriate permissions +touch "$errlogfile" +ret=$? +# Provide some advice if the log file cannot be touched +if [ $ret -ne 0 ] ; then + errlogdir=$(dirname $errlogfile) + if ! [ -d "$errlogdir" ] ; then + echo "The directory $errlogdir does not exist." + elif [ -f "$errlogfile" ] ; then + echo "The log file $errlogfile cannot be touched, please, fix its permissions." + else + echo "The log file $errlogfile could not be created." + fi + echo "The daemon will be run under $myuser:$mygroup" + exit 1 +fi +chown "$myuser:$mygroup" "$errlogfile" +chmod 0640 "$errlogfile" +[ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile" + +# Make the data directory +if [ ! -d "$datadir/mysql" ] ; then + # First, make sure $datadir is there with correct permissions + # (note: if it's not, and we're not root, this'll fail ...) + if [ ! -e "$datadir" -a ! -h "$datadir" ] + then + mkdir -p "$datadir" || exit 1 + fi + chown "$myuser:$mygroup" "$datadir" + chmod 0755 "$datadir" + [ -x /sbin/restorecon ] && /sbin/restorecon "$datadir" + + # Now create the database + echo "Initializing MySQL database" + @bindir@/mysql_install_db --datadir="$datadir" --user="$myuser" + ret=$? + if [ $ret -ne 0 ] ; then + echo "Initialization of MySQL database failed." >&2 + echo "Perhaps @sysconfdir@/my.cnf is misconfigured." >&2 + # Clean up any partially-created database files + if [ ! -e "$datadir/mysql/user.frm" ] ; then + rm -rf "$datadir"/* + fi + exit $ret + fi + # In case we're running as root, make sure files are owned properly + chown -R "$myuser:$mygroup" "$datadir" +fi + +exit 0 diff --git a/mysqld-wait-ready b/mysqld-wait-ready.sh similarity index 53% rename from mysqld-wait-ready rename to mysqld-wait-ready.sh index 9e5d3e4..a5356c2 100644 --- a/mysqld-wait-ready +++ b/mysqld-wait-ready.sh @@ -1,55 +1,44 @@ #!/bin/sh +source "`dirname ${BASH_SOURCE[0]}`/mariadb-scripts-common" + # This script waits for mysqld to be ready to accept connections # (which can be many seconds or even minutes after launch, if there's # a lot of crash-recovery work to do). # Running this as ExecStartPost is useful so that services declared as # "After mysqld" won't be started until the database is really ready. +if [ $# -ne 1 ] ; then + echo "You need to pass daemon pid as an argument for this script." + exit 20 +fi + # Service file passes us the daemon's PID (actually, mysqld_safe's PID) daemon_pid="$1" -# extract value of a MySQL option from config files -# Usage: get_mysql_option SECTION VARNAME DEFAULT -# result is returned in $result -# We use my_print_defaults which prints all options from multiple files, -# with the more specific ones later; hence take the last match. -get_mysql_option(){ - result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1` - if [ -z "$result" ]; then - # not found, use default - result="$3" - fi -} - -# Defaults here had better match what mysqld_safe will default to -get_mysql_option mysqld datadir "/var/lib/mysql" -datadir="$result" -get_mysql_option mysqld socket "/var/lib/mysql/mysql.sock" -socketfile="$result" - # Wait for the server to come up or for the mysqld process to disappear ret=0 while /bin/true; do - RESPONSE=`/usr/bin/mysqladmin --no-defaults --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1` + # Check process still exists + if ! [ -d "/proc/${daemon_pid}" ] ; then + ret=1 + break + fi + RESPONSE=`@bindir@/mysqladmin --no-defaults --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1` mret=$? - if [ $mret -eq 0 ]; then + if [ $mret -eq 0 ] ; then break fi # exit codes 1, 11 (EXIT_CANNOT_CONNECT_TO_SERVICE) are expected, # anything else suggests a configuration error if [ $mret -ne 1 -a $mret -ne 11 ]; then - ret=1 + echo "Cannot check for MySQL Daemon startup because of mysqladmin failure." >&2 + ret=$mret break fi # "Access denied" also means the server is alive echo "$RESPONSE" | grep -q "Access denied for user" && break - # Check process still exists - if ! /bin/kill -0 $daemon_pid 2>/dev/null; then - ret=1 - break - fi sleep 1 done diff --git a/mysqld.service b/mysqld.service.in similarity index 82% rename from mysqld.service rename to mysqld.service.in index 3193ce2..d3f550c 100644 --- a/mysqld.service +++ b/mysqld.service.in @@ -18,7 +18,7 @@ # though /lib/... will still work. [Unit] -Description=MySQL database server +Description=MySQL @MAJOR_VERSION@.@MINOR_VERSION@ database server After=syslog.target After=network.target @@ -27,11 +27,12 @@ Type=simple User=mysql Group=mysql -ExecStartPre=/usr/libexec/mysqld-prepare-db-dir %n +ExecStartPre=@libexecdir@/mariadb-check-socket +ExecStartPre=@libexecdir@/mysqld-prepare-db-dir %n # Note: we set --basedir to prevent probes that might trigger SELinux alarms, # per bug #547485 -ExecStart=/usr/bin/mysqld_safe --basedir=/usr -ExecStartPost=/usr/libexec/mysqld-wait-ready $MAINPID +ExecStart=@bindir@/mysqld_safe --basedir=/usr +ExecStartPost=@libexecdir@/mysqld-wait-ready $MAINPID # Give a reasonable amount of time for the server to start up/shut down TimeoutSec=300