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

 


Comments:

i try to use SMF but always in maintenance state... # svcs -x svc:/network/csk-mysql:default (?) State: maintenance since Wed May 30 13:44:47 2007 Reason: Method failed. See: http://sun.com/msg/SMF-8000-8Q See: /var/svc/log/network-csk-mysql:default.log Impact: This service is not running If I run the method independently it run normally...? but always in maintenance state when run with svcadm :(

Posted by Arisandy Arief on May 29, 2007 at 05:00 PM PDT #

What does the error log /var/svc/log/network-csk-mysql show ? Shanti

Posted by Shanti on May 30, 2007 at 02:30 AM PDT #

mine doesn't appear to be working either. the log-file says: 070531 18:40:37 InnoDB: Started; log sequence number 0 43655 070531 18:40:37 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist 070531 18:40:37 mysqld ended is there some sort of "installation" that needs to be done before starting (enabling) the mysql service the 1st time?

Posted by guest on May 31, 2007 at 08:47 AM PDT #

Yes indeed. You first need to do the MySQL setup and create a database. See the README file in the mysql directory or google to get instructions. Shanti

Posted by Shanti on June 01, 2007 at 12:58 AM PDT #

The Cool Stack 1.1 package CSKamp does not include any information on configuring MySQL database for use (creating mysql user, group, mysql_install_db, etc.).

Posted by Ken Hansen on June 14, 2007 at 04:18 AM PDT #

We do include these instructions in the CSKmysql package. Our intent was that people would use the CSKmysql package (which is 64bit) to run the database server, and the CSKamp package only for client-side access. But I realize that many people are using the 32bit mysql in CSKamp as their db server - so will update the README file in this package. Thanks for the feedback. Shanti

Posted by Shanti on June 14, 2007 at 05:24 AM PDT #

There is an error on the "privileges" line of the "start" method above - there was some word wrapping performed, and the net_privaddr needs to be preceeded by a "bang" (!). This caused me much confusion, because I know nothing about SMF, but when I clued in to the error message on system restart (couldn't interpret privileges) I looked at the line and saw that net_privaddr was not like the others. Also, I did a cut and paste into the file to be created, and I got an extraneous blank line - not a problem, but the missing "bang" was a problem. Not sure how I did it, but it looks like I got the "cool stack" working, and I have it restarting on system reboot via SMF - thanks!

Posted by Ken Hansen on June 14, 2007 at 06:49 AM PDT #

I followed the steps for the installation of MySQL, and your instructions posted in 'SMF support for MySQL in Cool Stack'. Result: MySQL run fine. When I execute 'svcadm -v enable csk-mysql' shows: svc:/network/csk-mysql:default habilitado. (in English: svc:/network/csk-mysql:default enabled.). 'svcs -a | grep my' shows: maintenance 14:56:36 svc:/network/csk-mysql:default and mysql don't start. If I use the command 'mysqld_safe' mysql starts ok and run fine. could you help me? thank you and sorry by the bad English.

Posted by David Torea on June 14, 2007 at 09:45 PM PDT #

David, did you notice the small typo/error I mentioned in my comment posted at 1:49 PM PDT yesterday? It has to do with the "privileges" section of the "start" method. Your descriptions sounds like my problem, and the above fixed it. I hope this helps, it worked in my case.

Posted by Ken Hansen on June 14, 2007 at 11:47 PM PDT #

Actually, it shouldn't matter whether the 'net_privaddr' is preceded by a bang (!) or not as far as this working. If it doesn't have the bang, it means that the MySQL server will be able to listen on privileged ports. But since MySQL typically listens on 3306, it doesn't need this privilege, so we can safely turn it off. Basic privileges don't include net_privaddr, so the correct entry should be : privileges='basic,!proc_session,!proc_info,!file_link_any' /> Sorry for the confusion Shanti

Posted by Shanti on June 18, 2007 at 06:16 AM PDT #

Why not just package DBI and DBD::mysql in? At least put them in the perl package. I have been trying for several days to get my x2100 and x2200 m2 built with mod_perl and mysql. So hard. I appended my problems to this thread http://forum.java.sun.com/thread.jspa?messageID=9790027 and some nice dead simple instructions for folks that are rather new to solaris, and that seemingly don't have access to the sun compiler would be nice. Like out of the box nevada. Thanks, Earl

Posted by spackest on July 25, 2007 at 05:23 PM PDT #

Hi Earl, Sorry to hear about the problems you're having. I'll try and answer this on the forum. We do plan to include DBI,DBD::mysql in the next version of Cool Stack. Shanti

Posted by Shanti on July 26, 2007 at 01:33 AM PDT #

Thanks for the note Shanti. Any idea on release date? And while I am wishing, any chance of having blastwave compatibility? Not sure what that would entail (just using the same perl and compiler?), but since I used coolstack, I had to wget, gtar, make test, make install quite a few perl libraries. Not looking too forward to doing that on two or three more zones. Not sure what indiana is going to bring, but hopefully a killer packaging system, so that with a single command or two, I would have my little samp stack pretty well installed. Thanks for your very good docs on getting the mysql and apache services set up.

Posted by spackest on July 26, 2007 at 02:27 AM PDT #

Which specific perl libraries are you using and for what purpose ? The more info we have on what our users need, the better able we are to support them. Post the list on the forum so we can see if others have the same requirement. Shanti

Posted by Shanti on July 26, 2007 at 02:56 AM PDT #

I installed enough to get JSON, WWW::Mechanize, Template::Alloy, CGI::Ex, Crypt::Blowfish, Cache::Memcached, XML::Simple each for various reasons. Which forum?

Posted by spackest on July 26, 2007 at 03:29 PM PDT #

Shanti, This CoolStack thing, is a good thing! You are doing a fine job. Keep up the great effort! Bill

Posted by Bill on July 28, 2007 at 03: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 07, 2007 at 09:41 PM 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 05:18 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

I'm a Senior Staff Engineer in the Performance & Applications Engineering Group (PAE). This blog focuses on tips to build, configure, tune and measure performance of popular open source web applications on Solaris.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today