MySQL Server Startup Script
在Unix和类似于Unix的系统中的MySQL发布版本包含一个名叫mysql.server的脚本,它将调用mysqld_safe来启动MySQL服务器。它也可以用于像Linux和Solaris系统上使用System V-style来运行指令来启动和停止系统服务。也可以被macOS启动项目来启动MySQL。
mysql.server是MySQL源码树中的脚本名称。其安装后的名称可能不一样,例如,mysqld或mysql。下面将会介绍将mysql.server脚本调整成适合于你系统的名称。注意对于某些Linux平台,使用RPM或Debian包安装的MySQL包含用来管理MySQL服务器启动和关闭的systemd的支持。在这些平台上,mysql.server和mysqld_safe不需要安装因为不需要它们。
为了使用mysql.server来手动启动或停止MySQL服务,可以从命令调用它并使用start或stop参数:
[root@localhost support-files]# ./mysql.server stop Shutting down MySQL.. SUCCESS! [root@localhost support-files]# ./mysql.server start Starting MySQL.. SUCCESS!
相关日志信息如下:
2019-08-22T13:29:13.429419Z 0 [Note] Giving 0 client threads a chance to die gracefully 2019-08-22T13:29:13.429508Z 0 [Note] Shutting down slave threads 2019-08-22T13:29:13.429535Z 0 [Note] Forcefully disconnecting 0 remaining clients 2019-08-22T13:29:13.429553Z 0 [Note] Event Scheduler: Purging the queue. 0 events 2019-08-22T13:29:13.429630Z 0 [Note] Binlog end 2019-08-22T13:29:13.460102Z 0 [Note] Shutting down plugin 'ngram' 2019-08-22T13:29:13.460187Z 0 [Note] Shutting down plugin 'partition' 2019-08-22T13:29:13.460214Z 0 [Note] Shutting down plugin 'BLACKHOLE' 2019-08-22T13:29:13.460235Z 0 [Note] Shutting down plugin 'ARCHIVE' 2019-08-22T13:29:13.460277Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA' 2019-08-22T13:29:13.460405Z 0 [Note] Shutting down plugin 'MRG_MYISAM' 2019-08-22T13:29:13.460446Z 0 [Note] Shutting down plugin 'MyISAM' 2019-08-22T13:29:13.460553Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL' 2019-08-22T13:29:13.460599Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES' 2019-08-22T13:29:13.460619Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES' 2019-08-22T13:29:13.460637Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS' 2019-08-22T13:29:13.460681Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN' 2019-08-22T13:29:13.460736Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS' 2019-08-22T13:29:13.460776Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS' 2019-08-22T13:29:13.460898Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES' 2019-08-22T13:29:13.460920Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS' 2019-08-22T13:29:13.460938Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES' 2019-08-22T13:29:13.460956Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE' 2019-08-22T13:29:13.460973Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE' 2019-08-22T13:29:13.460991Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG' 2019-08-22T13:29:13.461008Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED' 2019-08-22T13:29:13.461026Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED' 2019-08-22T13:29:13.461044Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD' 2019-08-22T13:29:13.461089Z 0 [Note] Shutting down plugin 'INNODB_METRICS' 2019-08-22T13:29:13.461140Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO' 2019-08-22T13:29:13.461161Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS' 2019-08-22T13:29:13.461180Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU' 2019-08-22T13:29:13.461220Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE' 2019-08-22T13:29:13.461286Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET' 2019-08-22T13:29:13.461306Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX' 2019-08-22T13:29:13.461325Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET' 2019-08-22T13:29:13.461343Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM' 2019-08-22T13:29:13.461361Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET' 2019-08-22T13:29:13.461378Z 0 [Note] Shutting down plugin 'INNODB_CMP' 2019-08-22T13:29:13.461396Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS' 2019-08-22T13:29:13.461414Z 0 [Note] Shutting down plugin 'INNODB_LOCKS' 2019-08-22T13:29:13.461431Z 0 [Note] Shutting down plugin 'INNODB_TRX' 2019-08-22T13:29:13.461450Z 0 [Note] Shutting down plugin 'InnoDB' 2019-08-22T13:29:13.461776Z 0 [Note] InnoDB: FTS optimize thread exiting. 2019-08-22T13:29:13.462092Z 0 [Note] InnoDB: Starting shutdown... 2019-08-22T13:29:13.562479Z 0 [Note] InnoDB: Dumping buffer pool(s) to /mysqldata/mysql/ib_buffer_pool 2019-08-22T13:29:13.563934Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 190822 21:29:13 2019-08-22T13:29:14.807857Z 0 [Note] InnoDB: Shutdown completed; log sequence number 2530229 2019-08-22T13:29:14.810644Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1" 2019-08-22T13:29:14.810690Z 0 [Note] Shutting down plugin 'MEMORY' 2019-08-22T13:29:14.810707Z 0 [Note] Shutting down plugin 'CSV' 2019-08-22T13:29:14.810721Z 0 [Note] Shutting down plugin 'sha256_password' 2019-08-22T13:29:14.810731Z 0 [Note] Shutting down plugin 'mysql_native_password' 2019-08-22T13:29:14.810961Z 0 [Note] Shutting down plugin 'binlog' 2019-08-22T13:29:14.811432Z 0 [Note] /mysqlsoft/mysql/bin/mysqld: Shutdown complete 2019-08-22T13:30:24.969246Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled 2019-08-22T13:30:24.969441Z 0 [Note] /mysqlsoft/mysql/bin/mysqld (mysqld 5.7.26) starting as process 32558 ... 2019-08-22T13:30:24.980591Z 0 [Note] InnoDB: PUNCH HOLE support available 2019-08-22T13:30:24.980678Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2019-08-22T13:30:24.980740Z 0 [Note] InnoDB: Uses event mutexes 2019-08-22T13:30:24.980758Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier 2019-08-22T13:30:24.980779Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11 2019-08-22T13:30:24.981460Z 0 [Note] InnoDB: Number of pools: 1 2019-08-22T13:30:24.981799Z 0 [Note] InnoDB: Using CPU crc32 instructions 2019-08-22T13:30:24.985591Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M 2019-08-22T13:30:25.002296Z 0 [Note] InnoDB: Completed initialization of buffer pool 2019-08-22T13:30:25.007382Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority(). 2019-08-22T13:30:25.021394Z 0 [Note] InnoDB: Highest supported file format is Barracuda. 2019-08-22T13:30:25.063462Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables 2019-08-22T13:30:25.063830Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ... 2019-08-22T13:30:25.758116Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB. 2019-08-22T13:30:25.760396Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active. 2019-08-22T13:30:25.760448Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active. 2019-08-22T13:30:25.761681Z 0 [Note] InnoDB: Waiting for purge to start 2019-08-22T13:30:25.811963Z 0 [Note] InnoDB: 5.7.26 started; log sequence number 2530229 2019-08-22T13:30:25.812330Z 0 [Note] InnoDB: Loading buffer pool(s) from /mysqldata/mysql/ib_buffer_pool 2019-08-22T13:30:25.813642Z 0 [Note] Plugin 'FEDERATED' is disabled. 2019-08-22T13:30:25.819058Z 0 [Note] InnoDB: Buffer pool(s) load completed at 190822 21:30:25 2019-08-22T13:30:25.824582Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them. 2019-08-22T13:30:25.825236Z 0 [Warning] CA certificate ca.pem is self signed. 2019-08-22T13:30:25.827375Z 0 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306 2019-08-22T13:30:25.827463Z 0 [Note] - '0.0.0.0' resolves to '0.0.0.0'; 2019-08-22T13:30:25.829361Z 0 [Note] Server socket created on IP: '0.0.0.0'. 2019-08-22T13:30:25.859593Z 0 [Note] Event Scheduler: Loaded 0 events 2019-08-22T13:30:25.859938Z 0 [Note] /mysqlsoft/mysql/bin/mysqld: ready for connections. Version: '5.7.26' socket: '/mysqlsoft/mysql/mysql.sock' port: 3306 Source distribution
mysql.server进入到MySQL安装目录,然后调用mysqld_safe。为了让某些特定的用户能够运行服务,在全局/etc/my.cnf选项文件中为[mysqld]组增加一个合适的user选项,后面将会介绍(如果你将二进制版本的MySQL安装在非标准位置,那么可能必须要修改mysql.server脚本。修改它是为了在运行mysqld_safe之前进入到合适的目录中。如果这样做,当将来升级MySQL时你修改的mysql.server版本可能会被覆盖,创建一个你修改版本的副本可以进行重新安装)。
mysql.server stop通过发送一个信号给它来停止MySQL服务。也可以通过执行mysqladmin shutdown来手动关闭服务。
[root@localhost bin]# ./mysqladmin -uroot -p123456 shutdown mysqladmin: [Warning] Using a password on the command line interface can be insecure.
日志信息如下:
2019-08-22T13:44:57.170282Z 0 [Note] Giving 0 client threads a chance to die gracefully 2019-08-22T13:44:57.170357Z 0 [Note] Shutting down slave threads 2019-08-22T13:44:57.170919Z 0 [Note] Forcefully disconnecting 0 remaining clients 2019-08-22T13:44:57.171016Z 0 [Note] Event Scheduler: Purging the queue. 0 events 2019-08-22T13:44:57.171140Z 0 [Note] Binlog end 2019-08-22T13:44:57.172096Z 0 [Note] Shutting down plugin 'ngram' 2019-08-22T13:44:57.172142Z 0 [Note] Shutting down plugin 'partition' 2019-08-22T13:44:57.172203Z 0 [Note] Shutting down plugin 'BLACKHOLE' 2019-08-22T13:44:57.172225Z 0 [Note] Shutting down plugin 'ARCHIVE' 2019-08-22T13:44:57.172259Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA' 2019-08-22T13:44:57.172317Z 0 [Note] Shutting down plugin 'MRG_MYISAM' 2019-08-22T13:44:57.172348Z 0 [Note] Shutting down plugin 'MyISAM' 2019-08-22T13:44:57.172398Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL' 2019-08-22T13:44:57.172428Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES' 2019-08-22T13:44:57.172441Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES' 2019-08-22T13:44:57.172452Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS' 2019-08-22T13:44:57.172476Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN' 2019-08-22T13:44:57.172506Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS' 2019-08-22T13:44:57.172564Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS' 2019-08-22T13:44:57.172579Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES' 2019-08-22T13:44:57.172590Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS' 2019-08-22T13:44:57.172601Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES' 2019-08-22T13:44:57.172612Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE' 2019-08-22T13:44:57.172623Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE' 2019-08-22T13:44:57.172634Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG' 2019-08-22T13:44:57.172644Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED' 2019-08-22T13:44:57.172655Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED' 2019-08-22T13:44:57.172666Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD' 2019-08-22T13:44:57.172676Z 0 [Note] Shutting down plugin 'INNODB_METRICS' 2019-08-22T13:44:57.172687Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO' 2019-08-22T13:44:57.172698Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS' 2019-08-22T13:44:57.172708Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU' 2019-08-22T13:44:57.172734Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE' 2019-08-22T13:44:57.172746Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET' 2019-08-22T13:44:57.172769Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX' 2019-08-22T13:44:57.172802Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET' 2019-08-22T13:44:57.172814Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM' 2019-08-22T13:44:57.172838Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET' 2019-08-22T13:44:57.172849Z 0 [Note] Shutting down plugin 'INNODB_CMP' 2019-08-22T13:44:57.172872Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS' 2019-08-22T13:44:57.172884Z 0 [Note] Shutting down plugin 'INNODB_LOCKS' 2019-08-22T13:44:57.172895Z 0 [Note] Shutting down plugin 'INNODB_TRX' 2019-08-22T13:44:57.172906Z 0 [Note] Shutting down plugin 'InnoDB' 2019-08-22T13:44:57.173010Z 0 [Note] InnoDB: FTS optimize thread exiting. 2019-08-22T13:44:57.173195Z 0 [Note] InnoDB: Starting shutdown... 2019-08-22T13:44:57.274546Z 0 [Note] InnoDB: Dumping buffer pool(s) to /mysqldata/mysql/ib_buffer_pool 2019-08-22T13:44:57.275296Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 190822 21:44:57 2019-08-22T13:44:58.528547Z 0 [Note] InnoDB: Shutdown completed; log sequence number 2530257 2019-08-22T13:44:58.531650Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1" 2019-08-22T13:44:58.531698Z 0 [Note] Shutting down plugin 'MEMORY' 2019-08-22T13:44:58.531736Z 0 [Note] Shutting down plugin 'CSV' 2019-08-22T13:44:58.531752Z 0 [Note] Shutting down plugin 'sha256_password' 2019-08-22T13:44:58.531764Z 0 [Note] Shutting down plugin 'mysql_native_password' 2019-08-22T13:44:58.531967Z 0 [Note] Shutting down plugin 'binlog' 2019-08-22T13:44:58.532460Z 0 [Note] /mysqlsoft/mysql/bin/mysqld: Shutdown complete
为了在服务器上自动启动和停止MySQL服务,你必须在你的/etc/rc*文件中合适的地方添加启动和停止命令:
.如果使用Linux服务RPM包(MySQL-server-VERSION.rpm)或一个原生的Linux包安装,那么mysql.server脚本可能以mysqld或mysql的名称安装到/etc/init.d目录中。
.如果使用源码或二进制版本来安装MySQL时不能自动安装mysql.server,可以手动安装这个脚本。它可以在MySQL安装目录或MySQL源码树下的support-files目录中找到。将这个脚本复制到/etc/init.d目录中并命名为mysql并且修改权限让其它可以执行:
[root@cs2 ~]# cp /mysqlsoft/mysql/support-files/mysql.server /etc/init.d/mysqld [root@cs2 ~]# chmod 755 /etc/init.d/mysqld
在安装脚本后,依赖于操作系统命令需要在系统启动时来激活它。在Linux上,你可以使用chkconfig:
[root@localhost init.d]# chkconfig --add mysqld
在有一些Linux系统中,也需要执行以下命令来完全启用mysqld脚本:
[root@localhost init.d]# chkconfig --level 345 mysqld on
在FreeBSD系统中,启动脚本通常应该在/usr/local/etc/rc.d/目录中,安装的mysql.server脚本就是/usr/local/etc/rc.d/mysql.server.sh来启用自动启动。rc(8)手册页指出,只有当脚本的基本名称与*.sh shell文件名模式匹配时,才会执行该目录中的脚本。该目录中存在的任何其他文件或目录都将被默认忽略。
作为上述设置的一种替代方案,有些操作系统也会使用/etc/rc.local或/etc/init.d/boot.local来在操作系统启动时启动额外的服务。为了使用这种方法来启动MySQL。需要在合适的启动文件中增加类似下面的命令:
/bin/sh -c 'cd /usr/local/mysql; ./bin/mysqld_safe --user=mysql &'
对于其它系统,咨询你操作系统文档来查看如何安装启动脚本。
mysql.server会读取选项文件中的[mysql.server]和[mysqld]部分的选项。为了向后兼容,它也会读取[mysql_server]部分的选项,但是现在你应该将它重命名为[mysql.server]。
可以在全局/etc/my.cnf文件中为mysql.server增加选项。一个常见的my.cnf文件可能看到类似于以下所示:
[mysqld] basedir=/mysqlsoft/mysql datadir=/mysqldata/mysql user=mysql port=3306 socket = /mysqlsoft/mysql/mysql.sock [mysql.server] basedir=/usr/local/mysql
mysql.server脚本支持下表中所列出的选项。如果指定,他们必须被存储在一个选项文件中,而不是在命令行中设置。mysql.server只支持start和stop作为命令行参数
表4.4 mysql.server选项文件选项
Option Name Description Type
basedir MySQL安装目录路径 目录名
datadir MySQL数据目录路径 目录名
pid-file 服务器将它的进程ID需要写入的文件 文件名
service-startup-timeout 服务器在启动时所等待的时间长度 整数
basedir=dir_name
MySQL安装目录路径
datadir=dir_name
MySQL数据目录路径
pid-file=file_name
服务器将它的进程ID需要写入的文件的路径名。如果这个选项没有指定,mysql.server将使用缺省值host_name.pid这种PID文件值传送给mysql_safe会覆盖在[mysqld_safe]选项文件组中所指定的任何值。因为mysql.server会读取[mysqld]选项文件组但不会读取[mysqld_safe]组,你也可以确保通过在[mysqld_safe]和[mysqld]组中设置相同的pid-file值使用在手动从mysql.server脚本中调用mysqld_safe时可以得到相同的值。
service-startup-timeout=seconds
确认服务器启动时可以等待的时间长度。如果服务器在这个时间内没有启动成功,mysql.server会抛出错误并退出。缺省时间为900秒。0意味着在启动时不会执行等待。负数意味着将会永久等待(不会超时)。