MySQL数据库服务器通常指的的是mysqld,而命令行mysql则是mysql客户端程序,这两个概念通常容易混淆。通常启动mysql服务器即是启动mysqld进程,mysqld启动后,可以通过mysql连接到mysql服务器。本文主要描述了mysql服务器的几种启动方式以及如何关闭mysql服务器。
使用iis7服务器监控工具直接控制:
选择所需操作然后点击执行即可。
1、直接使用mysqld启动 mysqld 即是mysql服务器,可以调用该命令直接启动mysql服务器 mysqld 从配置文件中读取[mysqld]以及[server]选项组的内容,也可以通过直接在命令行跟随参数。 mysqld服务器读取配置文件的顺序,对于有多个配置文件存在,且同一参数有
多个值的情形,以最后一次读取为准。 命令行跟随参数具有最高优先级
#当前mysql服务器运行环境 [root@rhel64a ~]# cat /etc/issue Red Hat Enterprise Linux Server release 6.4 (Santiago) #查看mysqld启动时的缺省选项 [root@rhel64a ~]# mysqld --print-defaults mysqld would have been started with the following arguments: --socket=/tmp/mysql3306.sock --port=3306 --pid-file=/var/lib/mysql/my3306.pid --user=mysql --server-id=3306 --federated [root@rhel64a ~]# ps -ef|grep mysql root 2963 2840 0 14:10 pts/0 00:00:00 grep mysql # Author : Leshami # Blog : http://blog.csdn.net/leshami #查看mysqld启动配置文件的优先级 [root@rhel64a ~]# mysqld --verbose --help |grep -A 1 "Default options" Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf #查看当前的my.cnf配置文件 [root@rhel64a ~]# grep -v ^# /etc/my.cnf|head -n 7 [mysqld] socket = /tmp/mysql3306.sock port = 3306 pid-file = /var/lib/mysql/my3306.pid user = mysql server-id=3306 federated #清空当前mysql服务器的错误日志文件 [root@rhel64a ~]# cat /dev/null>/var/lib/mysql/rhel64a.ycdata.net.err [root@rhel64a ~]# mysqld & #启动mysqld服务器 [1] 3480 [root@rhel64a ~]# 2014-10-25 14:20:42 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2014-10-25 14:20:42 3480 [Note] InnoDB: Using atomics to ref count buffer pool pages 2014-10-25 14:20:42 3480 [Note] InnoDB: The InnoDB memory heap is disabled 2014-10-25 14:20:42 3480 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2014-10-25 14:20:42 3480 [Note] InnoDB: Memory barrier is not used 2014-10-25 14:20:42 3480 [Note] InnoDB: Compressed tables use zlib 1.2.3 2014-10-25 14:20:42 3480 [Note] InnoDB: Using Linux native AIO 2014-10-25 14:20:42 3480 [Note] InnoDB: Using CPU crc32 instructions 2014-10-25 14:20:42 3480 [Note] InnoDB: Initializing buffer pool, size = 128.0M 2014-10-25 14:20:42 3480 [Note] InnoDB: Completed initialization of buffer pool 2014-10-25 14:20:42 3480 [Note] InnoDB: Highest supported file format is Barracuda. 2014-10-25 14:20:42 3480 [Note] InnoDB: 128 rollback segment(s) are active. 2014-10-25 14:20:42 3480 [Note] InnoDB: Waiting for purge to start 2014-10-25 14:20:42 3480 [Note] InnoDB: 5.6.21 started; log sequence number 1626087 2014-10-25 14:20:42 3480 [Note] Server hostname (bind-address): '*'; port: 3306 2014-10-25 14:20:42 3480 [Note] IPv6 is available. 2014-10-25 14:20:42 3480 [Note] - '::' resolves to '::'; 2014-10-25 14:20:42 3480 [Note] Server socket created on IP: '::'. 2014-10-25 14:20:42 3480 [Note] Event Scheduler: Loaded 0 events 2014-10-25 14:20:42 3480 [Note] mysqld: ready for connections. Version: '5.6.21' socket: '/tmp/mysql3306.sock' port: 3306 MySQL Community Server (GPL)l #下面可以查询到mysqld进程 [root@rhel64a ~]# ps -ef|grep mysql|grep -v grep mysql 18240 2641 1 20:25 pts/2 00:00:00 mysqld [root@rhel64a ~]# mysql -uroot -pxxx -P3306 --protocol=tcp root@localhost[(none)]> show variables like 'version'; +---------------+--------+ | Variable_name | Value | +---------------+--------+ | version | 5.6.21 | +---------------+--------+
2、mysql安全启动(mysqld_safe) mysqld_safe是一个shell 脚本,会调用mysqld启动mysql服务器,并监听服务器。 如果mysqld进程异常终止,mysqld_safe将自动重启mysqld mysql_safe 从配置文件中读取[mysqld],[server],[mysqld_safe]等选项,为保证向后兼容,mysql_safe还读取[safe_mysql]选项。
#下面使用mysqladmin来关闭之前启动的mysql服务器 [root@rhel64a ~]# mysqladmin -uroot -pxxx shutdown -S /tmp/mysql3306.sock [root@rhel64a ~]# ps -ef|grep mysql root 4262 3418 0 14:40 pts/4 00:00:00 grep mysql
#下面使用mysqld_safe来启动实例 [root@rhel64a ~]# mysqld_safe --user=mysql & [1] 4273 [root@rhel64a ~]# 141025 14:40:34 mysqld_safe Logging to '/var/lib/mysql/rhel64a.ycdata.net.err'. 141025 14:40:34 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
#查看启动后的后台进程 [root@rhel64a ~]# ps -ef|grep mysql|grep -v grep #mysqld的进程为4438,其父进程为4273,即mysqld_safe root 4273 3418 0 14:40 pts/4 00:00:00 /bin/sh /usr/bin/mysqld_safe --user=mysql mysql 4438 4273 0 14:40 pts/4 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/rhel64a.ycdata.net.err --pid-file=/var/lib/mysql/my3306.pid --socket=/tmp/mysql3306.sock --port=3306
[root@rhel64a ~]# mysql -uroot -pxxx -P3306 --protocol=tcp root@localhost[(none)]> system kill -9 4438 #强制kill mysqld进程 root@localhost[(none)]> /usr/bin/mysqld_safe: line 166: 4438 Killed #下面的nohup为mysql启动进程 nohup /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/rhel64a.ycdata.net.err --pid-file=/var/lib/mysql/my3306.pid --socket=/tmp/mysql3306.sock --port=3306 < /dev/null >> /var/lib/mysql/rhel64a.ycdata.net.err 2>&1 141025 14:42:29 mysqld_safe Number of processes running now: 0 141025 14:42:29 mysqld_safe mysqld restarted #提示该进程已经自动重新启动 #校验mysqld是否已经成功重启 root@localhost[(none)]> system ps -ef|grep mysql|grep -v grep root 4273 3418 0 14:40 pts/4 00:00:00 /bin/sh /usr/bin/mysqld_safe --user=mysql root 4517 3418 0 14:41 pts/4 00:00:00 mysql -uroot -px x -P3306 --protocol=tcp mysql 4553 4273 0 14:42 pts/4 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/rhel64a.ycdata.net.err --pid-file=/var/lib/mysql/my3306.pid --socket=/tmp/mysql3306.sock --port=3306
root@localhost[(none)]> select databases(); ERROR 2013 (HY000): Lost connection to MySQL server during query #由于mysqld进程被杀掉,所以连接已经被断开 root@localhost[(none)]> connect #重新连接 Connection id: 1 Current database: *** NONE *** root@localhost[(none)]> select database(); +------------+ | database() | +------------+ | NULL | +------------+
3、mysql(mysql.server)服务方式启动 mysql.server为一个shell脚本,他会调用mysqld_safe,该脚本会读取配置文件的[mysql.server]以及[mysqld]选项。 为向后兼容,也会读取[mysql_server]选项
#首先使用mysqladmin关闭已存在的mysqld root@localhost[(none)]> system mysqladmin -uroot -pxxx shutdown -S /tmp/mysql3306.sock Warning: Using a password on the command line interface can be insecure. 141025 14:47:18 mysqld_safe mysqld from pid file /var/lib/mysql/my3306.pid ended root@localhost[(none)]> exit Bye [1]+ Done mysqld_safe --user=mysql
[root@rhel64a ~]# ls -hltr /etc/init.d/mysql #/etc/init.d目录下已经配置了mysql启动脚本,在安装时添加 -rwxr-xr-x 1 root root 11K Sep 11 22:18 /etc/init.d/mysql #如果为rpm方式安装会自动添加该启动脚本
[root@rhel64a ~]# /etc/init.d/mysql --help #获取mysql启动脚本的使用帮助 Usage: mysql {start|stop|restart|reload|force-reload|status} [ MySQL server options ] [root@rhel64a ~]# /etc/init.d/mysql start #启动mysqld Starting MySQL.[ OK ] [root@rhel64a ~]# /etc/init.d/mysql stop #停止mysqld Shutting down MySQL..[ OK ]
#缺省情况下mysql已经作为一个服务添加到系统目录已经配置了自启动,如下 [root@rhel64a ~]# chkconfig --list |grep mysql mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off root@localhost[(none)]> system ps -ef|grep mysql |grep -v grep root 5127 1 0 14:49 pts/4 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/my3306.pid mysql 5306 5127 0 14:49 pts/4 00:00:07 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/rhel64a.ycdata.net.err --pid-file=/var/lib/mysql/my3306.pid --socket=/tmp/mysql3306.sock --port=3306 root 5340 3418 0 14:49 pts/4 00:00:00 mysql -uroot -px x -P3306 --protocol=tcp #通过服务的方式启动mysql服务器,mysql.server调用mysqld_safe,然后mysql_safe调用了mysqld #从上面的方式可知,无论何种启动方式,mysqld最终都会被调用。因为mysqld就是mysql服务器。
4、关闭mysql服务器 在前面的演示中,我们已经使用了mysqladmin以及service方式来关闭实例。即可以通过这2种方式来关闭数据库。不再演示。 #获取mysqladmin命令行的帮助信息,如下包含shutdown [root@rhel64a ~]# mysqladmin --help|grep shutdown |grep server shutdown Take server down
5、 小结 a、注意理解mysqld与mysql。mysqld为mysql服务器或者称之为服务器上的实例,用于提供客户端访问。mysql为客户端访问管理工具。 b、可以通过三种方式启动mysql服务器(mysqld,mysqld_safe,service mysql start) c、对于mysqld_safe,service mysql start方式,总是通过调用mysqld_safe来启动mysqld进程,以防止mysqld意外宕掉而提供重启服务。 d、可以通过mysqladmin shutdown以及service mysql stop来终止mysql服务器