通过Solaris10的SMF(Service Management Facility)来管理MySQL(附带MySQL安装)
http://www.sun.com/bigadmin/content/submitted/mysql_smf_tip.jsp
Configuring MySQL to Use With Service Management Facility (SMF)
William Pool (Puddle), October 2005
The Solaris 10 OS uses the Service Management Facility (SMF) to handleservices. Traditional means like /etc/rc?.d scripts still work, but as a legacy means. (For more information on SMF, see the References section.)
To take advantage of the SMF in the Solaris 10 OS using MySQL, follow these steps.
Note: Read the scripts and "change" the path of MySQL or MySQL's data-directory accordingly!
If you haven't initialized the MySQL database, do that first:
/opt/sfw/bin/mysql_install_db
This will install the database into /var/mysql to override that use:
/opt/sfw/bin/mysql_install_db -ldata=/opt/sfw/var/mysql
Note: If you change the location, change the information below!
First create a mysql group:
/usr/sbin/groupadd mysql
Then create the mysql user:
/usr/sbin/useradd -s /bin/false -g mysql -d /var/mysql -c "MySQL User" mysql
Create a service manifest file:
/var/svc/manifest/network/mysql.xml
This contains the following:
<?xml version='1.0'?>
<!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'>
<!--
William Pool (Puddle) 02/05
Service manifest for MySQL
E-mail: puddle@flipmotion.com
-->
<service_bundle type='manifest' name='mysql:mysql'>
<service
name='network/mysql'
type='service'
version='1'>
<create_default_instance enabled='false' />
<single_instance />
<dependency name='fs'
grouping='require_all'
restart_on='none'
type='service'>
<service_fmri value='svc:/system/filesystem/local' />
</dependency>
<dependency name='net'
grouping='require_all'
restart_on='none'
type='service'>
<service_fmri value='svc:/network/loopback' />
</dependency>
<exec_method
type='method'
name='start'
exec='/lib/svc/method/svc-mysql start'
timeout_seconds='-1'>
<method_context>
<method_credential user='mysql' group='mysql' />
</method_context>
</exec_method>
<exec_method
type='method'
name='stop'
exec=':kill'
timeout_seconds='-1'>
</exec_method>
<exec_method
type='method'
name='restart'
exec='/lib/svc/method/svc-mysql restart'
timeout_seconds='-1'>
</exec_method>
</service>
</service_bundle>
Now create your "Service Method File" in /lib/svc/method called svc-mysql:
#!/usr/bin/sh
#
# William Pool (Puddle) 01/05
# SMF Method file for MySQL
# E-mail: puddle@flipmotion.com
#
# This uses Sun's default MySQL packages
# SUNWmysqlu SUNWmysqlr
#
# Modify accordingly!
#
# NOTE: Make sure DB_DIR is owned BY the mysql user and group and chmod
# 700.
#
.. /lib/svc/share/smf_include.sh
DB_DIR=/var/mysql
PIDFILE=${DB_DIR}/`/usr/bin/uname -n`.pid
case "$1" in
start)
/usr/sfw/sbin/mysqld_safe --user=mysql --datadir=${DB_DIR} --pid-file=${PIDFILE} > /dev/null &
;;
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
;;
'restart')
stop
while pgrep mysqld > /dev/null
do
sleep 1
done
start
;;
*)
echo ""
echo "Usage: `basename $0` { start | stop | restart }"
echo ""
exit 64
;;
esac
#---EOF
Now fix the permissions for the two files created:
chown root:bin /lib/svc/method/svc-mysql
chmod 555 /lib/svc/method/svc-mysql
chown root:sys /var/svc/manifest/network/mysql.xml
chmod 444 /var/svc/manifest/network/mysql.xml
Fix permissions on the MySQL data directory:
chown -R mysql:mysql /var/mysql
chmod -R 700 /var/mysql
Import the service into the service repository:
# svccfg import /var/svc/manifest/network/mysql.xml
Enable the service:
# svcadm -v enable mysql
-----------------------------------
Solaris 10 已經內置安裝了 MySQL。安裝者必須要用 Root 身份登入系統。以下的安裝步驟可以在 /usr/sfw/src/mysql/Docs 目錄中找到。
1. 1. 用 root 身份 login 到 Console mode 中,並且進行資料庫的事前準備。
# /usr/sfw/bin/mysql_install_db
Preparing db table
Preparing host table
Preparing user table
Preparing func table
Preparing tables_priv table
Preparing columns_priv table
Installing all prepared tables
060118 21:24:03 /usr/sfw/sbin/mysqld: Shutdown Complete
<snip...>
2. 2. 建立 mysql user 和 group ,並且更改資料目錄的群組。
# groupadd mysql
# useradd -g mysql mysql
# chgrp -R mysql /var/mysql
# chmod -R 770 /var/mysql
# installf SUNWmysqlr /var/mysql d 770 root mysql
3. 3. 預設 MySQL 設定檔位置為 /var/mysql/my.cnf
4. 4. 複制 MySQL 設定檔到預設位置
# cp /usr/sfw/share/mysql/my-medium.cnf /var/mysql/my.cnf
5. 5. 手動啟動 mysql
# /usr/sfw/sbin/mysqld_safe --user=mysql &
6. 6. 設定 MySQL 的 root user密碼 ( 下文中的 new-password 為你想要的密碼,你可以自行更改為你喜歡的密碼。因為安全理由,切密不要使用 new-password 為你的密碼,一定要更改 )
# cd /usr/sfw/bin
# ./mysqladmin -u root password 'new-password'
# ./mysqladmin -u root -h `hostname` password 'new-password'
7. 7. 測試 MySQL Server
# ./mysqlshow -p
Enter password: new-password
+-----------+
| Databases |
+-----------+
| mysql |
| test |
+-----------+
# ./mysql -u root -p
Enter password: new-password
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3 to server version: 4.0.20-standard
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show databases;
+-----------+
| Databases |
+-----------+
| mysql |
| test |
+-----------+
2 rows in set (0.00 sec)
mysql> quit;
Bye
8. 8. 設定 Solaris Server 在啟動和關機時,自動運行和停止MySQL Server。
# ln /etc/sfw/mysql/mysql.server /etc/rc3.d/S99mysql
# ln /etc/sfw/mysql/mysql.server /etc/rc0.d/K00mysql
# ln /etc/sfw/mysql/mysql.server /etc/rc1.d/K00mysql
# ln /etc/sfw/mysql/mysql.server /etc/rc2.d/K00mysql
# ln /etc/sfw/mysql/mysql.server /etc/rcS.d/K00mysql
9. 9. 然後,試一試從新啟動 Solaris ,看一看 MySQL 能不自動啟動。預設 MySQL 設定檔位置為 /var/mysql/my.cnf 。