[debian-mysql] Bug#742900: [Pkg-systemd-maintainers] No mysqld.service File

merc1984 at f-m.fm merc1984 at f-m.fm
Mon Mar 31 20:52:37 UTC 2014


On Mon, Mar 31, 2014, at 10:47, Michael Stapelberg wrote:
> Hi merc,
> 
> merc1984 at f-m.fm writes:
> > On Fri, Mar 28, 2014, at 16:16, Michael Biebl wrote:
> >> You can certainly depend within a .service file on a sysv init script
> >> 
> >> Since the SysV init script is named /etc/init.d/mysql, you'd have to use
> >> something like
> >
> > That's very nice in theory, but I am telling you in practice it does not
> > work.  That's why I'm having to go to all this trouble.
> “It does not work” is not a proper description of a problem. As you seem
> to be convinced that this is broken, may I ask you to file a bug report
> against systemd? That will include more information (state dump) to
> start with. Also, please increase the log level to “debug” — see
> https://wiki.debian.org/systemd#Debugging — and provide the output of
> journalctl -b.
> 
> Thanks.
> 
> -- 
> Best regards,
> Michael

Those are your best regards?  I'd hate to see your worst ones...

Unfortunately the problem is not with systemd.  It is with Debian for
not having a .service file suited to its structure.  I've tried to adapt
the Arch and Fedora .service files, but can't make them work.  The
failures are diverse, and depend on the nature of differing  adaptations
I've made in them for Debian.  The solution is not to describe how my
attempts fail six ways from Sunday.  The solution is to specify an
official file which actually works in Debian.

The primary problem with my .service file is that it simply does not
start mysqld on boot.  I've concentrated on adapting the Fedora file, as
it's more likely to be credible, given that it's used in RHEL.

My mysql.service:
---------------------------------------------------------------------------------------
# Adapted  from Fedora

# For more info about custom unit files, see systemd.unit(5) or
#
http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F

# For example, if you want to increase mysql's open-files-limit to
10000,
# you need to increase systemd's LimitNOFILE setting, so create a file
named
# "/etc/systemd/system/mariadb.service.d/limits.conf" containing:
#       [Service]
#       LimitNOFILE=10000

# Note: /usr/lib/... is recommended in the .include line though /lib/... 
# still works.
# Don't forget to reload systemd daemon after you change unit
configuration:
# root> systemctl --system daemon-reload

[Unit]
Description=MySQL database server
After=syslog.target network.target

[Service]
Type=simple
User=mysql
Group=mysql

ExecStartPre=/usr/local/bin/mysql-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/local/bin/mysql-wait-ready $MAINPID

# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300

# Security
PrivateTmp=yes
#InaccessibleDirectories=/boot /.config /home/backups /home/bill
/home/sleeper /lib32 /media /mnt /opt /proc /root /srv /sys
#ReadOnlyDirectories=/bin /etc /sbin /usr
#CapabilityBoundingSet=~CAP_SYS_PTRACE
#DeviceAllow=/dev/null rw
#NoNewPrivileges=yes

[Install]
WantedBy=multi-user.target

systemctl status mysqld
---------------------------------------------------------------------------------------
# systemctl status mysqld
mysqld.service - MySQL database server
   Loaded: loaded (/usr/local/lib/systemd/system/mysqld.service;
   enabled)
   Active: failed (Result: exit-code) since Mon 2014-03-31 12:39:46 PDT;
   1h 3min ago
  Process: 2341 ExecStartPost=/usr/local/bin/mysql-wait-ready $MAINPID
  (code=exited, status=1/FAILURE)
  Process: 2340 ExecStart=/usr/bin/mysqld_safe --basedir=/usr
  (code=exited, status=0/SUCCESS)
  Process: 2004 ExecStartPre=/usr/local/bin/mysql-prepare-db-dir %n
  (code=exited, status=0/SUCCESS)

Mar 31 12:39:28 cygnus mysqld_safe[2340]: 140331 12:39:28 mysqld_safe
Can't log to error log and syslog at the same time.  Remove all
--log-error configuration options for --syslog to take effect.
Mar 31 12:39:28 cygnus mysqld_safe[2340]: 140331 12:39:28 mysqld_safe
Logging to '/var/log/mysql/error.log'.
Mar 31 12:39:28 cygnus mysqld_safe[2340]: mkdir: cannot create directory
‘/var/run/mysqld’: Permission denied
Mar 31 12:39:28 cygnus mysqld_safe[2340]: chown: cannot access
‘/var/run/mysqld’: No such file or directory
Mar 31 12:39:28 cygnus mysqld_safe[2340]: chmod: cannot access
‘/var/run/mysqld’: No such file or directory
Mar 31 12:39:29 cygnus mysqld_safe[2340]: 140331 12:39:29 mysqld_safe
Starting mysqld daemon with databases from /var/lib/mysql
Mar 31 12:39:46 cygnus mysqld_safe[2340]: 140331 12:39:46 mysqld_safe
mysqld from pid file /var/run/mysqld/mysqld.pid ended
Mar 31 12:39:46 cygnus systemd[1]: mysqld.service: control process
exited, code=exited status=1
Mar 31 12:39:46 cygnus systemd[1]: Failed to start MySQL database
server.
Mar 31 12:39:46 cygnus systemd[1]: Unit mysqld.service entered failed
state.
#

---------------------------------------------------------------------------------------
# journalctl -xn
-- Logs begin at Mon 2014-01-13 09:34:08 PST, end at Mon 2014-03-31
13:44:23 PDT. --
Mar 31 13:44:21 cygnus mysqld_safe[13377]: 140331 13:44:21 mysqld_safe
Can't log to error log and syslog at the same time.  Remove all
--log-error configuration options for --syslog to take effect.
Mar 31 13:44:21 cygnus mysqld_safe[13377]: 140331 13:44:21 mysqld_safe
Logging to '/var/log/mysql/error.log'.
Mar 31 13:44:21 cygnus mysqld_safe[13377]: mkdir: cannot create
directory ‘/var/run/mysqld’: Permission denied
Mar 31 13:44:21 cygnus mysqld_safe[13377]: chown: cannot access
‘/var/run/mysqld’: No such file or directory
Mar 31 13:44:21 cygnus mysqld_safe[13377]: chmod: cannot access
‘/var/run/mysqld’: No such file or directory
Mar 31 13:44:21 cygnus mysqld_safe[13377]: 140331 13:44:21 mysqld_safe
Starting mysqld daemon with databases from /var/lib/mysql
Mar 31 13:44:22 cygnus mysqld_safe[13377]: 140331 13:44:22 mysqld_safe
mysqld from pid file /var/run/mysqld/mysqld.pid ended
Mar 31 13:44:23 cygnus systemd[1]: mysqld.service: control process
exited, code=exited status=1
Mar 31 13:44:23 cygnus systemd[1]: Failed to start MySQL database
server.
-- Subject: Unit mysqld.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- Documentation:
http://www.freedesktop.org/wiki/Software/systemd/catalog/be02cf6855d2428ba40df7e9d022f03d
-- 
-- Unit mysqld.service has failed.
-- 
-- The result is failed.
Mar 31 13:44:23 cygnus systemd[1]: Unit mysqld.service entered failed
state.

---------------------------------------------------------------------------------------
Now see;  that didn't get us anywhere, did it.

I've adapted these two files from Fedora as well, and no I can't find
the cause of the above errors.

mysqld-prepare-db-dir
---------------------------------------------------------------------------------------
#!/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/mysql/error.log"
errlogfile="$result"
get_mysql_option mysqld socket "$datadir/mysql.sock"
socketfile="$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"

# We check if there is already a process using the socket file,
# since otherwise this systemd service file could report false
# positive result when starting and mysqld_safe could remove
# a socket file, which actually uses a different daemon.
if fuser "$socketfile" > /dev/null ; then
    echo "Socket file $socketfile exists." >&2
    echo "Is another MySQL daemon already running with the same unix
    socket?" >&2
    exit 1
fi

# 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

mysqld-wait-ready
---------------------------------------------------------------------------------------
#!/bin/sh

# 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.

# 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
	MYSQLDRUNNING=0
	if [ -d "/proc/${daemon_pid}" ] ; then
	    MYSQLDRUNNING=1
	fi
	RESPONSE=`/usr/bin/mysqladmin --no-defaults
	--socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1`
	mret=$?
	if [ $mret -eq 0 ] && [ $MYSQLDRUNNING -eq 1 ]; 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
	    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

exit $ret


-- 
http://www.fastmail.fm - Choose from over 50 domains or use your own



More information about the pkg-mysql-maint mailing list