SMF support for MySQL in Cool Stack
Here are instructions on how to use SMF to manage MySQL. These instructions assume no knowledge of SMF. All commands should be executed as root.
Create the manifest
The manifest defines the service and controls the privileges with which the service will execute. We use the service name csk-mysql to distinguish this as a Cool Stack service.
Create a file named /var/svc/manifest/network/cskmysql.xml with the following contents :
<?xml version='1.0'?>
<!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'>
<!--
Copyright 2006,2007 Sun Microsystems, Inc. All rights reserved.
Manifest for CSKmysql - should reside in /var/svc/manifest/network
-->
<service_bundle type='manifest' name='CSKmysql:mysql'>
<service
name='network/csk-mysql'
type='service'
version='1'>
<create_default_instance enabled='false' />
<single_instance />
<!--
Wait for network interfaces to be initialized.
-->
<dependency name='network'
grouping='require_all'
restart_on='error'
type='service'>
<service_fmri
value='svc:/milestone/network:default'/>
</dependency>
<!--
Wait for all local filesystems to be mounted.
-->
<dependency name='filesystem-local'
grouping='require_all'
restart_on='none'
type='service'>
<service_fmri
value='svc:/system/filesystem/local:default'/>
</dependency>
<exec_method
type='method'
name='start'
exec='/opt/coolstack/lib/svc/method/svc-cskmysql start'
timeout_seconds='60'>
<method_context
working_directory='/opt/coolstack'>
<method_credential
user='mysql' group='mysql'
privileges='basic,!proc_session,!proc_info,!file_link_any,net_privaddr' />
</method_context>
</exec_method>
<exec_method
type='method'
name='stop'
exec='/opt/coolstack/lib/svc/method/svc-cskmysql stop'
timeout_seconds='60'>
<method_context />
</exec_method>
<exec_method
type='method'
name='refresh'
exec='/opt/coolstack/lib/svc/method/svc-cskmysql restart'
timeout_seconds='60'>
<method_context working_directory='/opt/coolstack'>
<method_credential
user='mysql' group='mysql'
privileges='basic,!proc_session,!proc_info,!file_link_any,net_privaddr' />
</method_context>
</exec_method>
</service>
</service_bundle>
Create the method
Create the file /opt/coolstack/lib/svc/method/svc-cskmysql referenced in the manifest with the following contents and make it executable. You may have to create the directories below /opt/coolstack/lib first. This file needs to be edited to set DB_DIR to the path of your data directory (where data files reside), and MYSQL_DIR if you are using the 64-bit MySQL version.
#!/usr/bin/sh
#
# Copyright 2007 Sun Microsystems, Inc. All rights reserved.
# Method file for CSKMySQL
#
# This uses the MySQL packages from CoolStack 1.1 (CSKmysql)
# If you're using the 32bit mysql from CSKamp, change MYSQL_DIR below to mysql_32bit.
# This file should reside in /opt/coolstack/lib/svc/method
#
# NOTE: Make sure DB_DIR is owned BY the mysql user and group and chmod 700
#
. /lib/svc/share/smf_include.sh
DB_DIR=/data
PIDFILE=${DB_DIR}/`/usr/bin/uname -n`.pid
MYSQL_DIR=/opt/coolstack/mysql_32bit
mysql_stop () {
if [ -f ${PIDFILE} ]; then
/usr/bin/pkill mysqld_safe >/dev/null 2>&1
/usr/bin/kill `cat ${PIDFILE}` > /dev/null 2>&1 && echo -n ' mysqld'
fi
}
mysql_start () {
$MYSQL_DIR/bin/mysqld_safe --user=mysql --datadir=${DB_DIR} --pid-file=${PIDFILE} > /dev/null &
}
##
# Start of script
#
case "$1" in
start)
mysql_start
;;
stop)
mysql_stop
;;
restart)
mysql_stop
while pgrep mysqld > /dev/null
do
sleep 1
done
mysql_start
;;
*)
echo ""
echo "Usage: `basename $0` { start | stop | restart }"
echo ""
exit 64
;;
esac
Change file ownership
Ensure that the mysql user and group exist and this user owns $DB_DIR. It's also a good idea to chmod 0700 all files in $DB_DIR.
Start the csk-mysql service
Import the new mysql config :
# svccfg -v import /var/svc/manifest/network/cskmysql.xml
We are now ready to start our service. Start it as follows :
# svcadm -v enable csk-mysql
If the service starts successfully, you should see mysqld running. A log of the
service startup will be in /var/svc/log/network-csk-mysql:CSKmysql.log file. You can also get more detailed information for troubleshooting startup failures from the command svcs -x. More information on SMF can be found at http://www.sun.com/bigadmin/content/selfheal/smf-quickstart.html

Posted by Arisandy Arief on May 30, 2007 at 12:00 AM PDT #
Posted by Shanti on May 30, 2007 at 09:30 AM PDT #
Posted by 192.223.243.6 on May 31, 2007 at 03:47 PM PDT #
Posted by Shanti on June 01, 2007 at 07:58 AM PDT #
Posted by Ken Hansen on June 14, 2007 at 11:18 AM PDT #
Posted by Shanti on June 14, 2007 at 12:24 PM PDT #
Posted by Ken Hansen on June 14, 2007 at 01:49 PM PDT #
Posted by David Torea on June 15, 2007 at 04:45 AM PDT #
Posted by Ken Hansen on June 15, 2007 at 06:47 AM PDT #
Posted by Shanti on June 18, 2007 at 01:16 PM PDT #
Posted by spackest on July 26, 2007 at 12:23 AM PDT #
Posted by Shanti on July 26, 2007 at 08:33 AM PDT #
Posted by spackest on July 26, 2007 at 09:27 AM PDT #
Posted by Shanti on July 26, 2007 at 09:56 AM PDT #
Posted by spackest on July 26, 2007 at 10:29 PM PDT #
Posted by Bill on July 28, 2007 at 10:31 PM PDT #
Hi,
for mysql to start you have to :
./mysql_install_db --user=mysql --ldata=/opt/dbdir
I just thought to mention this if somebody else has problems with initial DB
Thanks
Posted by Pavel Sigarteu on August 08, 2007 at 04:41 AM PDT #
On your script, about the mysql_stop () 'function'... I've been setting up one server on a client and the problem is when you 'just kill' MySQLd, it doesn't get a chance to shut it down properly. As a result, on the next startup of the server, it'll have to do disaster recovery (since it kind'a crashed/got killed). Mark Demma, an SA which I work with, rewrote the shutdown part of the script as:
mysql_stop () {
if [ -f ${PIDFILE} ]; then
/opt/coolstack/mysql/bin/mysqladmin shutdown -u shutdown>/dev/null 2>&1
fi
And yes - I've created a local user with only shutdown privileges on the database with the following command:
GRANT USAGE, SHUTDOWN ON *.* to shutdown@localhost;
Just my 2 cents...
Posted by Augusto Bott on August 22, 2007 at 12:18 PM PDT #