<div dir="ltr">I've got mysql running with a systemd service file on my machine. As was suggested earlier in the bug report, I started with the fedora scripts so there may be some fedoraisms to check on. The only problem I had just using them was an issue with mysql-wait-ready where the ping line was improperly formated resulting in usage info getting printed out. As you can see, I added a little bit of extra output around that section to help debug. Since /usr/bin/mysqld_safe sources /etc/mysql/debian.cnf and systemd runs everything as the mysql user, I also had to chgrp mysql /etc/mysql/debian.cnf; chmod g+r /etc/mysql/debian.cnf.<div><div><div><br></div><div><br></div><div><br></div><div>####################### mysql.service </div><div><div># For more info about custom unit files, see systemd.unit(5) or</div><div># <a href="http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F">http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F</a></div><div><br></div><div># For example, if you want to increase mysql's open-files-limit to 10000,</div><div># you need to increase systemd's LimitNOFILE setting, so create a file named</div><div># "/etc/systemd/system/mariadb.service.d/limits.conf" containing:</div><div>#       [Service]</div><div>#       LimitNOFILE=10000</div><div><br></div><div>[Unit]</div><div>Description=MySQL database server</div><div>After=syslog.target network.target</div><div><br></div><div>[Service]</div><div>Type=simple</div><div>User=mysql</div><div>Group=mysql</div><div><br></div><div>ExecStartPre=/usr/bin/mysql-prepare-db-dir %n</div><div># Note: we set --basedir to prevent probes that might trigger SELinux alarms,</div><div># per bug #547485</div><div>ExecStart=/usr/bin/mysqld_safe --basedir=/usr</div><div>ExecStartPost=/usr/bin/mysql-wait-ready $MAINPID</div><div><br></div><div># Give a reasonable amount of time for the server to start up/shut down</div><div>TimeoutSec=300</div><div><br></div><div># Security</div><div>PrivateTmp=yes</div><div>#InaccessibleDirectories=/boot /.config /home/backups /home/bill /home/sleeper /lib32 /media /mnt /opt /proc /root /srv /sys</div><div>#ReadOnlyDirectories=/bin /etc /sbin /usr</div><div>#CapabilityBoundingSet=~CAP_SYS_PTRACE</div><div>#DeviceAllow=/dev/null rw</div><div>#NoNewPrivileges=yes</div><div><br></div><div>[Install]</div><div>WantedBy=multi-user.target</div></div><div><br></div><div><br></div><div><br></div><div><div>####################### /usr/bin/mysql-prepare-db-dir</div><div><div>#!/bin/sh</div><div><br></div><div># This script creates the mysql data directory during first service start.</div><div># In subsequent starts, it does nothing much.</div><div><br></div><div># extract value of a MySQL option from config files</div><div># Usage: get_mysql_option SECTION VARNAME DEFAULT</div><div># result is returned in $result</div><div># We use my_print_defaults which prints all options from multiple files,</div><div># with the more specific ones later; hence take the last match.</div><div>get_mysql_option(){</div><div>        result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" |</div><div>        tail -n 1`</div><div>        if [ -z "$result" ]; then</div><div>            # not found, use default</div><div>            result="$3"</div><div>        fi</div><div>}</div><div><br></div><div># Defaults here had better match what mysqld_safe will default to</div><div>get_mysql_option mysqld datadir "/var/lib/mysql"</div><div>datadir="$result"</div><div>get_mysql_option mysqld_safe log-error "/var/log/mysql/error.log"</div><div>errlogfile="$result"</div><div>get_mysql_option mysqld socket "$datadir/mysql.sock"</div><div>socketfile="$result"</div><div><br></div><div># Absorb configuration settings from the specified systemd service file,</div><div># or the default "mysqld" service if not specified</div><div>SERVICE_NAME="$1"</div><div>if [ x"$SERVICE_NAME" = x ]</div><div>then</div><div>    SERVICE_NAME=mysqld.service</div><div>fi</div><div><br></div><div>myuser=`systemctl show -p User "${SERVICE_NAME}" |</div><div>  sed 's/^User=//'`</div><div>if [ x"$myuser" = x ]</div><div>then</div><div>    myuser=mysql</div><div>fi</div><div><br></div><div>mygroup=`systemctl show -p Group "${SERVICE_NAME}" |</div><div>  sed 's/^Group=//'`</div><div>if [ x"$mygroup" = x ]</div><div>then</div><div>    mygroup=mysql</div><div>fi</div><div><br></div><div># Set up the errlogfile with appropriate permissions</div><div>touch "$errlogfile"</div><div>chown "$myuser:$mygroup" "$errlogfile"</div><div>chmod 0640 "$errlogfile"</div><div>#[ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile"</div><div><br></div><div># We check if there is already a process using the socket file,</div><div># since otherwise this systemd service file could report false</div><div># positive result when starting and mysqld_safe could remove</div><div># a socket file, which actually uses a different daemon.</div><div>if fuser "$socketfile" > /dev/null ; then</div><div>    echo "Socket file $socketfile exists." >&2</div><div>    echo "Is another MySQL daemon already running with the same unix</div><div>    socket?" >&2</div><div>    exit 1</div><div>fi</div><div><br></div><div># Make the data directory</div><div>if [ ! -d "$datadir/mysql" ] ; then</div><div>    # First, make sure $datadir is there with correct permissions</div><div>    # (note: if it's not, and we're not root, this'll fail ...)</div><div>    if [ ! -e "$datadir" -a ! -h "$datadir" ]</div><div>    then</div><div>        mkdir -p "$datadir" || exit 1</div><div>    fi</div><div>    chown "$myuser:$mygroup" "$datadir"</div><div>    chmod 0755 "$datadir"</div><div>#    [ -x /sbin/restorecon ] && /sbin/restorecon "$datadir"</div><div><br></div><div>    # Now create the database</div><div>    echo "Initializing MySQL database"</div><div>    /usr/bin/mysql_install_db --datadir="$datadir" --user="$myuser"</div><div>    ret=$?</div><div>    if [ $ret -ne 0 ] ; then</div><div>        echo "Initialization of MySQL database failed." >&2</div><div>        echo "Perhaps /etc/my.cnf is misconfigured." >&2</div><div>        # Clean up any partially-created database files</div><div>        if [ ! -e "$datadir/mysql/user.frm" ] ; then</div><div>            rm -rf "$datadir"/*</div><div>        fi</div><div>        exit $ret</div><div>    fi</div><div>    # In case we're running as root, make sure files are owned properly</div><div>    chown -R "$myuser:$mygroup" "$datadir"</div><div>fi</div><div><br></div><div>exit 0</div></div><div><br></div><div></div></div><div><br></div><div><br></div><div><div>####################### /usr/bin/mysql-wait-ready</div><div><div>#!/bin/sh</div><div><br></div><div># This script waits for mysqld to be ready to accept connections</div><div># (which can be many seconds or even minutes after launch, if there's</div><div># a lot of crash-recovery work to do).</div><div># Running this as ExecStartPost is useful so that services declared as</div><div># "After mysqld" won't be started until the database is really ready.</div><div><br></div><div># Service file passes us the daemon's PID (actually, mysqld_safe's PID)</div><div>daemon_pid="$1"</div><div><br></div><div># extract value of a MySQL option from config files</div><div># Usage: get_mysql_option SECTION VARNAME DEFAULT</div><div># result is returned in $result</div><div># We use my_print_defaults which prints all options from multiple files,</div><div># with the more specific ones later; hence take the last match.</div><div>get_mysql_option(){</div><div><span class="" style="white-space:pre">      </span>result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" |</div><div><span class="" style="white-space:pre">       </span>tail -n 1`</div><div><span class="" style="white-space:pre"> </span>if [ -z "$result" ]; then</div><div><span class="" style="white-space:pre">        </span>    # not found, use default</div><div><span class="" style="white-space:pre">     </span>    result="$3"</div><div><span class="" style="white-space:pre">        </span>fi</div><div>}</div><div><br></div><div># Defaults here had better match what mysqld_safe will default to</div><div>get_mysql_option mysqld datadir "/var/lib/mysql"</div><div>datadir="$result"</div><div>get_mysql_option mysqld socket "/var/lib/mysql/mysql.sock"</div><div>socketfile="$result"</div><div><br></div><div># Wait for the server to come up or for the mysqld process to disappear</div><div>ret=0</div><div>while /bin/true; do</div><div><span class="" style="white-space:pre">        </span>MYSQLDRUNNING=0</div><div><span class="" style="white-space:pre">    </span>if [ -d "/proc/${daemon_pid}" ] ; then</div><div><span class="" style="white-space:pre">   </span>    MYSQLDRUNNING=1</div><div><span class="" style="white-space:pre">      </span>fi</div><div><span class="" style="white-space:pre"> </span>RESPONSE=`/usr/bin/mysqladmin --no-defaults --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1`</div><div><span class="" style="white-space:pre">   </span>mret=$?</div><div><span class="" style="white-space:pre">    </span>if [ $mret -eq 0 ] && [ $MYSQLDRUNNING -eq 1 ]; then</div><div><span class="" style="white-space:pre">       </span>    break</div><div><span class="" style="white-space:pre">        </span>fi</div><div><span class="" style="white-space:pre"> </span># exit codes 1, 11 (EXIT_CANNOT_CONNECT_TO_SERVICE) are expected,</div><div><span class="" style="white-space:pre">  </span># anything else suggests a configuration error</div><div><span class="" style="white-space:pre">     </span>if [ $mret -ne 1 -a $mret -ne 11 ]; then</div><div>            echo "Received bad response from mysql..."</div><div>            echo "$RESPONSE"</div><div>            echo "$mret"<br></div><div><span class="" style="white-space:pre">  </span>    ret=1</div><div><span class="" style="white-space:pre">        </span>    break</div><div><span class="" style="white-space:pre">        </span>fi</div><div><span class="" style="white-space:pre"> </span># "Access denied" also means the server is alive</div><div><span class="" style="white-space:pre"> </span>echo "$RESPONSE" | grep -q "Access denied for user" && break</div><div><br></div><div><span class="" style="white-space:pre">  </span># Check process still exists</div><div><span class="" style="white-space:pre">       </span>if ! /bin/kill -0 $daemon_pid 2>/dev/null; then</div><div>            echo "Process does not exist"</div><div><span class="" style="white-space:pre"> </span>    ret=1</div><div><span class="" style="white-space:pre">        </span>    break</div><div><span class="" style="white-space:pre">        </span>fi</div><div><span class="" style="white-space:pre"> </span>sleep 1</div><div>done</div><div><br></div><div>exit $ret</div></div><div></div></div></div></div></div>