共计 14433 个字符,预计需要花费 37 分钟才能阅读完成。
本篇内容介绍了“怎么理解并掌握 MySQL Server Startup Script”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让丸趣 TV 小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
在 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 意味着在启动时不会执行等待。负数意味着将会永久等待(不会超时)。
“怎么理解并掌握 MySQL Server Startup Script”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注丸趣 TV 网站,丸趣 TV 小编将为大家输出更多高质量的实用文章!