共計 14433 個字符,預計需要花費 37 分鐘才能閱讀完成。
本篇內(nèi)容介紹了“怎么理解并掌握 MySQL Server Startup Script”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
在 Unix 和類似于 Unix 的系統(tǒng)中的 MySQL 發(fā)布版本包含一個名叫 mysql.server 的腳本,它將調(diào)用 mysqld_safe 來啟動 MySQL 服務器。它也可以用于像 Linux 和 Solaris 系統(tǒng)上使用 System V-style 來運行指令來啟動和停止系統(tǒng)服務。也可以被 macOS 啟動項目來啟動 MySQL。
mysql.server 是 MySQL 源碼樹中的腳本名稱。其安裝后的名稱可能不一樣,例如,mysqld 或 mysql。下面將會介紹將 mysql.server 腳本調(diào)整成適合于你系統(tǒng)的名稱。注意對于某些 Linux 平臺,使用 RPM 或 Debian 包安裝的 MySQL 包含用來管理 MySQL 服務器啟動和關閉的 systemd 的支持。在這些平臺上,mysql.server 和 mysqld_safe 不需要安裝因為不需要它們。
為了使用 mysql.server 來手動啟動或停止 MySQL 服務,可以從命令調(diào)用它并使用 start 或 stop 參數(shù):
[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 安裝目錄,然后調(diào)用 mysqld_safe。為了讓某些特定的用戶能夠運行服務,在全局 /etc/my.cnf 選項文件中為 [mysqld] 組增加一個合適的 user 選項,后面將會介紹(如果你將二進制版本的 MySQL 安裝在非標準位置,那么可能必須要修改 mysql.server 腳本。修改它是為了在運行 mysqld_safe 之前進入到合適的目錄中。如果這樣做,當將來升級 MySQL 時你修改的 mysql.server 版本可能會被覆蓋,創(chuàng)建一個你修改版本的副本可以進行重新安裝)。
mysql.server stop 通過發(fā)送一個信號給它來停止 MySQL 服務。也可以通過執(zhí)行 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 并且修改權限讓其它可以執(zhí)行:
[root@cs2 ~]# cp /mysqlsoft/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@cs2 ~]# chmod 755 /etc/init.d/mysqld
在安裝腳本后,依賴于操作系統(tǒng)命令需要在系統(tǒng)啟動時來激活它。在 Linux 上,你可以使用 chkconfig:
[root@localhost init.d]# chkconfig --add mysqld
在有一些 Linux 系統(tǒng)中,也需要執(zhí)行以下命令來完全啟用 mysqld 腳本:
[root@localhost init.d]# chkconfig --level 345 mysqld on
在 FreeBSD 系統(tǒng)中,啟動腳本通常應該在 /usr/local/etc/rc.d/ 目錄中,安裝的 mysql.server 腳本就是 /usr/local/etc/rc.d/mysql.server.sh 來啟用自動啟動。rc(8)手冊頁指出,只有當腳本的基本名稱與 *.sh shell 文件名模式匹配時,才會執(zhí)行該目錄中的腳本。該目錄中存在的任何其他文件或目錄都將被默認忽略。
作為上述設置的一種替代方案,有些操作系統(tǒng)也會使用 /etc/rc.local 或 /etc/init.d/boot.local 來在操作系統(tǒng)啟動時啟動額外的服務。為了使用這種方法來啟動 MySQL。需要在合適的啟動文件中增加類似下面的命令:
/bin/sh -c cd /usr/local/mysql; ./bin/mysqld_safe --user=mysql
對于其它系統(tǒng),咨詢你操作系統(tǒng)文檔來查看如何安裝啟動腳本。
mysql.server 會讀取選項文件中的 [mysql.server] 和[mysqld]部分的選項。為了向后兼容,它也會讀取 [mysql_server] 部分的選項,但是現(xiàn)在你應該將它重命名為[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 作為命令行參數(shù)
表 4.4 mysql.server 選項文件選項
Option Name Description Type
basedir MySQL 安裝目錄路徑 目錄名
datadir MySQL 數(shù)據(jù)目錄路徑 目錄名
pid-file 服務器將它的進程 ID 需要寫入的文件 文件名
service-startup-timeout 服務器在啟動時所等待的時間長度 整數(shù)
basedir=dir_name
MySQL 安裝目錄路徑
datadir=dir_name
MySQL 數(shù)據(jù)目錄路徑
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 腳本中調(diào)用 mysqld_safe 時可以得到相同的值。
service-startup-timeout=seconds
確認服務器啟動時可以等待的時間長度。如果服務器在這個時間內(nèi)沒有啟動成功,mysql.server 會拋出錯誤并退出。缺省時間為 900 秒。0 意味著在啟動時不會執(zhí)行等待。負數(shù)意味著將會永久等待(不會超時)。
“怎么理解并掌握 MySQL Server Startup Script”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注丸趣 TV 網(wǎng)站,丸趣 TV 小編將為大家輸出更多高質(zhì)量的實用文章!