RedHat5.2 默认安装MySQL文件主要布局表
/usr/bin 客户端程序和脚本
/usr/sbin mysql服务器
/var/lib/mysql 日志和数据库文件
1. 创建mysql用户
adduser mysql
2. 下载mysql
http://downloads.skysql.com/archives/mysql-5.5/http://downloads.skysql.com/archive/signature/p/mysql/f/mysql-5.5.10.tar.gz/v/5.5.10
3. 关闭mysql方法
[root@demoserver bin]# mysqladmin shutdown
4. ps命令查看MySQL进程
[root@demoserver bin]# ps -ef | grep mysql
root 10879 10741 0 18:10 pts/1 00:00:00 grep mysql
5. mysql服务器启动命令
[root@demoserver bin]# mysqld_safe &
[1] 11006
[root@demoserver bin]# Starting mysqld daemon with databases from /var/lib/mysql
[root@demoserver bin]#
[root@demoserver bin]# ps -ef | grep mysql
root 11006 10741 0 18:14 pts/1 00:00:00 /bin/sh /usr/bin/mysqld_safe
mysql 11049 11006 0 18:14 pts/1 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-external-locking --socket=/var/lib/mysql/mysql.sock
root 11074 10741 0 18:15 pts/1 00:00:00 grep mysql
[root@demoserver bin]#
6. 查看mysql服务器状态
[root@demoserver bin]# ps -ef | grep mysql
root 11006 10741 0 18:14 pts/1 00:00:00 /bin/sh /usr/bin/mysqld_safe
mysql 11049 11006 0 18:14 pts/1 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-external-locking --socket=/var/lib/mysql/mysql.sock
root 11074 10741 0 18:15 pts/1 00:00:00 grep mysql
[root@demoserver bin]# mysqladmin status
Uptime: 95 Threads: 1 Questions: 1 Slow queries: 0 Opens: 12 Flush tables: 1 Open tables: 6 Queries per second avg: 0.011
[root@demoserver bin]# mysqladmin shutdown
STOPPING server from pid file /var/run/mysqld/mysqld.pid
130601 18:17:09 mysqld ended
[1]+ Done mysqld_safe
[root@demoserver bin]# mysqladmin status
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/var/lib/mysql/mysql.sock' exists!
[root@demoserver bin]#
7. 编写mysql服务的启动关闭脚本,文件名mysql,并存放在/etc/rc.d/init.d目录下
脚本对应的内容:
#!/bin/bash
#MySQL daemon start/stop/restart script
#author: shenfl,date:20130601,vesion:1.0
#Comments to support chkconfig on RedHat Linux
#chkconfig : 2345 64 36 #服务的启动级别以及启动顺序
#description: A very fast and reliable SQL database engine.
#服务的描述信息
#source function library.
. /etc/rc.d/init.d/functions
#调用系统初始化脚本
if [ -f /etc/init.d/functions ] ; then
. /etc/init.d/functions # 执行. /etc/init.d/functions脚本
elif [ -f /etc/rc.d/init.d/functions ] ; then
. /etc/rc.d/init.d/functions
else
exit 0
fi
mysql_safe=/usr/bin/mysqld_safe
start_option= #使用start_option变量设置mysql的启动选项
mysqladmin=/usr/bin/mysqladmin
#使用mysqladmin变量设置myqladmin命令位置
stop_option=shutdown #使用stop_option 变量设置mysql的关闭选项
prog=mysqld #使用prog变量设置MySQL进程的名称
RETVAL=0 #0 表示该进程不存在
status_option=status
#mysql 启动函数
start()
{
# 如果mysql进程已经启动则返回提示信息并且退出 ,其中-n 字符串非空,$?表示输出结果: 0 表示正确 1 表示错误,表示已经存在
if [ -n "`/sbin/pidof $prog`" ]
then
echo $prog":already running"
echo
return 1 #返回数值为1
fi
echo "starting "$prog": "
base=$prog
#启动mysql
$mysql_safe $start_option &
RETVAL=$? #使用RETVAL变量保存命令的执行结果代码
echo "RETVAL:"$RETVAL
usleep 500000 #休眠0.5秒
# 如果mysql进程不存在,那么返回错误代码 ,其中 -z表示字符串为空 0:正确 1:不正确
if [ -z "`/sbin/pidof $prog`" ]
then
#The child processes have died after fork() ing
RETVAL=1
fi
# 根据错误代码显示对应的提示信息
if [ $RETVAL -ne 0 ]
then
echo 'Startup failure' # 提示进程启动失败
else
echo 'Startup sucess' #提示进程启动成功
fi
echo
return $RETVAL
}
# stop mysqld function
stop(){
echo "stopping "$prog" : "
$mysqladmin $stop_option
RETVAL=$?
if [ $RETVAL -ne 0 ]
then
echo 'stop failure'
else
echo 'stop sucess'
fi
echo
}
#myql restart function
restart()
{
#close mysql
stop
# 休眠0.5秒
usleep 500000
#start mysql
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
reload)
restart
;;
*)
echo $"Usage: mysql {start|stop|restart}"
exit 1
esac
exit $RETVAL
执行上述脚本的几种情况:
[root@demoserver ~]# sh mysqld.sh stop
stopping mysqld :
STOPPING server from pid file /var/run/mysqld/mysqld.pid
130601 20:27:16 mysqld ended
stop sucess
[root@demoserver ~]# sh mysqld.sh start
starting mysqld:
Starting mysqld daemon with databases from /var/lib/mysql
RETVAL:0
Startup sucess
[root@demoserver ~]# sh mysqld.sh restart
stopping mysqld :
STOPPING server from pid file /var/run/mysqld/mysqld.pid
130601 20:28:13 mysqld ended
stop sucess
starting mysqld:
RETVAL:0
Starting mysqld daemon with databases from /var/lib/mysql
Startup sucess
备注:关于在linux自动运行或者启动、停止服务、重启服务的shell脚本如上面类似,请大家学习,例如:tomcat,apache,svn,ftp等脚本。