2008年4月5日星期六

mysql脚本-mysqld_safe

mysqld_safe脚本是我们启动mysqld的一个脚本。他的作用在于: scripts to start the MySQL daemon and restart it if it dies unexpectedly(能够重启mysqld,如果mysqld意外的die掉。这在一些不稳定的进程中是比较常遇到的,比如某天我的lighttpd就意外退出,日志总没有任何记录,所以我们可能需要其他进程帮我们进行看管)
脚本共430行,下面可能对脚本进行下分析,力求能读懂他的实现过程.
1-12 :是注释和版权说明

13 KILL_MYSQLD=1;
14 MYSQLD=
15
16 trap '' 1 2 3 15 #捕捉信号 1 2 3 15,进行忽略 ( trap -l或kill -l能得到详细的信号表)
17
18 umask 007 #设置掩码
19
20 defaults=
21 case "$1" in
22 --no-defaults|--defaults-file=*|--defaults-extra-file=*)
23 defaults="$1"; shift
24 ;;
25 esac #读入主配置文件


####help的说明文档
27 usage () {
}



###函数parse_arguments()解析参数,需要带入arg
50 parse_arguments() {
51 # We only need to pass arguments through to the server if we don't
52 # handle them here. So, we collect unrecognized options (passed on
53 # the command line) into the args variable.
54 pick_args=
55 if test "$1" = PICK-ARGS-FROM-ARGV ##54-59我好像很少用到,看第一个参数是不是PICK-ARGS-FROM-ARGV,如果是,令pick_args=1,并且让参数数组左移一位
56 then
57 pick_args=1
58 shift
59 fi
60
61 for arg do ##arg是赋值进来的参数,并用同样的方法解析参数
62 case "$arg" in
63 --skip-kill-mysqld*)
64 KILL_MYSQLD=0;
65 ;;
66 # these get passed explicitly to mysqld
67 --basedir=*) MY_BASEDIR_VERSION=`echo "$arg" | sed -e "s;--basedir=;;"` ;;
68 --datadir=*) DATADIR=`echo "$arg" | sed -e "s;--datadir=;;"` ;;
69 --pid-file=*) pid_file=`echo "$arg" | sed -e "s;--pid-file=;;"` ;;
70 --user=*) user=`echo "$arg" | sed -e "s;--[^=]*=;;"` ; SET_USER=1 ;;
71
72 # these two might have been set in a [mysqld_safe] section of my.cnf
73 # they are added to mysqld command line to override settings from my.cnf
74 --socket=*) mysql_unix_port=`echo "$arg" | sed -e "s;--socket=;;"` ;;
75 --port=*) mysql_tcp_port=`echo "$arg" | sed -e "s;--port=;;"` ;;
76
77 # mysqld_safe-specific options - must be set in my.cnf ([mysqld_safe])!
78 --ledir=*) ledir=`echo "$arg" | sed -e "s;--ledir=;;"` ;;
79 --log-error=*) err_log=`echo "$arg" | sed -e "s;--log-error=;;"` ;;
80 --open-files-limit=*) open_files=`echo "$arg" | sed -e "s;--open-files-limit=;;"` ;;
81 --core-file-size=*) core_file_size=`echo "$arg" | sed -e "s;--core-file-size=;;"` ;;
82 --timezone=*) TZ=`echo "$arg" | sed -e "s;--timezone=;;"` ; export TZ; ;;
83 --mysqld=*) MYSQLD=`echo "$arg" | sed -e "s;--mysqld=;;"` ;;
84 --mysqld-version=*)
85 tmp=`echo "$arg" | sed -e "s;--mysqld-version=;;"`
86 if test -n "$tmp"
87 then
88 MYSQLD="mysqld-$tmp"
89 else
90 MYSQLD="mysqld"
91 fi
92 ;;
93 --nice=*) niceness=`echo "$arg" | sed -e "s;--nice=;;"` ;;
94 --help)
95 usage
96 ;;
97 *)
98 if test -n "$pick_args"
99 then
100 # This sed command makes sure that any special chars are quoted,
101 # so the arg gets passed exactly to the server.
102 args="$args "`echo "$arg" | sed -e 's,\([^a-zA-Z0-9_.-]\),\\\\\1,g'`
103 fi
104 ;;
105 esac
106 done
107 }
108




110 #找到根目录和mysqld所在的位置
111 # First, try to find BASEDIR and ledir (where mysqld is)
112 #
113
114 MY_PWD=`pwd`
115 # Check for the directories we would expect from a binary release install
116 if test -f ./share/mysql/english/errmsg.sys -a -x ./bin/mysqld
###test的用法详见man test errmsg.sys存在并且是一般的文件同时mysqld存在并且是可执行文件
117 then
118 MY_BASEDIR_VERSION=$MY_PWD # Where bin, share and data are
119 ledir=$MY_BASEDIR_VERSION/bin # Where mysqld is
120 # Check for the directories we would expect from a source install
121 elif test -f ./share/mysql/english/errmsg.sys -a \
122 -x ./libexec/mysqld
123 then
124 MY_BASEDIR_VERSION=$MY_PWD # Where libexec, share and var are
125 ledir=$MY_BASEDIR_VERSION/libexec # Where mysqld is
126 # Since we didn't find anything, used the compiled-in defaults
127 else
128 MY_BASEDIR_VERSION=/home/mysql5.0/
129 ledir=/home/mysql5.0/libexec
130 fi




131##找到数据所在的目录
132 #
133 # Second, try to find the data directory
134 #
135
136 # Try where the binary installs put it
137 if test -d $MY_BASEDIR_VERSION/data/mysql
138 then
139 DATADIR=$MY_BASEDIR_VERSION/data
140 if test -z "$defaults" -a -r "$DATADIR/my.cnf" ##
###$defaults长度为0且$DATADIR/my.cnf存在并可读
141 then
142 defaults="--defaults-extra-file=$DATADIR/my.cnf"
143 fi
144 # Next try where the source installs put it
145 elif test -d $MY_BASEDIR_VERSION/var/mysql
146 then
147 DATADIR=$MY_BASEDIR_VERSION/var
148 # Or just give up and use our compiled-in default
149 else
150 DATADIR=/home/mysql5.0/var
151 fi




######确认$MYSQK_HOME,并寻找my.cnf
153 if test -z "$MYSQL_HOME"
154 then
155 if test -r "$MY_BASEDIR_VERSION/my.cnf" && test -r "$DATADIR/my.cnf"
156 then
157 echo "WARNING: Found two instances of my.cnf -"
158 echo "$MY_BASEDIR_VERSION/my.cnf and"
159 echo "$DATADIR/my.cnf"
160 echo "IGNORING $DATADIR/my.cnf"
161 echo
162 MYSQL_HOME=$MY_BASEDIR_VERSION
163 elif test -r "$DATADIR/my.cnf"
164 then
165 echo "WARNING: Found $DATADIR/my.cnf"
166 echo "Datadir is deprecated place for my.cnf, please move it to $MY_BASEDIR_VERSION"
167 echo
168 MYSQL_HOME=$DATADIR
169 else
170 MYSQL_HOME=$MY_BASEDIR_VERSION
171 fi
172 fi
173 export MYSQL_HOME



#####我们需要设置的变量
175 user=mysql
176 niceness=0
177
178 # these rely on $DATADIR by default, so we'll set them later on
179 pid_file=
180 err_log=




182 # Get first arguments from the my.cnf file, groups [mysqld] and [mysqld_safe]
183 # and then merge with the command line arguments
184 if test -x ./bin/my_print_defaults
185 then
186 print_defaults="./bin/my_print_defaults"
187 elif test -x /home/mysql5.0/bin/my_print_defaults
188 then
189 print_defaults="/home/mysql5.0/bin/my_print_defaults"
190 elif test -x /home/mysql5.0/bin/mysql_print_defaults
191 then
192 print_defaults="/home/mysql5.0/bin/mysql_print_defaults"
193 else
194 print_defaults="my_print_defaults"
195 fi
##########确认print_default,具体用途不明白###############
197 args= #设置变量为空
198 SET_USER=2
199 parse_arguments `$print_defaults $defaults --loose-verbose mysqld server`
#####用上面确认的print_defaults去解析配置文件
# 解析成下面的格式,方便得到参数
# ./my_print_defaults -c /etc/my.cnf5.0 --loose-verbose mysqld server
#--port=3307
#--socket=/home/mysql5.0/tmp/mysql.sock
#--skip-locking
#--key_buffer=16M
#--max_allowed_packet=1M
#--table_cache=64
#--sort_buffer_size=512K
#--net_buffer_length=8K
#--read_buffer_size=256K
#--read_rnd_buffer_size=512K
#--myisam_sort_buffer_size=8M
#--log_slow_queries=/home/mysql5.0/var/mysql5.0_slow_query.log
#--long_query_time=3
#--log-bin=mysql5.0-bin
#--server-id=2
#--tmpdir=/home/mysql5.0/tmp/
###########################################
200 if test $SET_USER -eq 2
201 then
202 SET_USER=0
203 fi



#####从my.cnf中得到参数
204 parse_arguments `$print_defaults $defaults --loose-verbose mysqld_safe safe_mysqld`
#####从启动mysqld_safe中读取参数 $@是启动mysqld_safe的时候带入的
205 parse_arguments PICK-ARGS-FROM-ARGV "$@"
206 safe_mysql_unix_port=${mysql_unix_port:-${MYSQL_UNIX_PORT:-/home/mysql5/tmp/mysql.sock}}
######不太清楚这句赋值是什么意思


208 # Make sure that directory for $safe_mysql_unix_port exists
###确认socket文件存在
209 mysql_unix_port_dir=`dirname $safe_mysql_unix_port`
210 if [ ! -d $mysql_unix_port_dir ]
211 then
212 mkdir $mysql_unix_port_dir
213 chown $user $mysql_unix_port_dir
214 chmod 755 $mysql_unix_port_dir
215 fi
216



216###确认mysqld的存在
217 # Use the mysqld-max binary by default if the user doesn't specify a binary
218 if test -z "$MYSQLD"
219 then
220 if test -x $ledir/mysqld-max
221 then
222 MYSQLD=mysqld-max
223 else
224 MYSQLD=mysqld
225 fi
226 fi
227
228 if test ! -x $ledir/$MYSQLD
229 then
230 echo "The file $ledir/$MYSQLD doesn't exist or is not executable"
231 echo "Please do a cd to the mysql installation directory and restart"
232 echo "this script from there as follows:"
233 echo "./bin/mysqld_safe".
234 echo "See http://dev.mysql.com/doc/mysql/en/mysqld_safe.html for more"
235 echo "information"
236 exit 1
237 fi




238###整理相关的参数如pid_file,err_log,socket,tcp_port,nice#####
239 if test -z "$pid_file"
240 then
241 pid_file=$DATADIR/`/bin/hostname`.pid
242 else
243 case "$pid_file" in
244 /* ) ;;
245 * ) pid_file="$DATADIR/$pid_file" ;;
246 esac
247 fi
248 test -z "$err_log" && err_log=$DATADIR/`/bin/hostname`.err
249
250 if test -n "$mysql_unix_port"
251 then
252 args="--socket=$mysql_unix_port $args"
253 fi
254 if test -n "$mysql_tcp_port"
255 then
256 args="--port=$mysql_tcp_port $args"
257 fi
258
259 if test $niceness -eq 0
260 then
261 NOHUP_NICENESS="nohup"
262 else
263 NOHUP_NICENESS="nohup nice -$niceness"
264 fi



265
266 # Using nice with no args to get the niceness level is GNU-specific.
267 # This check could be extended for other operating systems (e.g.,
268 # BSD could use "nohup sh -c 'ps -o nice -p $$' | tail -1").
269 # But, it also seems that GNU nohup is the only one which messes
270 # with the priority, so this is okay.
271 if nohup nice > /dev/null 2>&1
272 then
273 normal_niceness=`nice`
274 nohup_niceness=`nohup nice`
275
276 numeric_nice_values=1
277 for val in $normal_niceness $nohup_niceness
278 do
279 case "$val" in
280 -[0-9] | -[0-9][0-9] | -[0-9][0-9][0-9] | \
281 [0-9] | [0-9][0-9] | [0-9][0-9][0-9] )
282 ;;
283 * )
284 numeric_nice_values=0 ;;
285 esac
286 done


还有100多行,头疼了,先休息去了

没有评论: