MySQL 启动失败的最常见的原因有两类,分别是无法访问系统资源和参数设置错误造成的,下面分别分析如下。
无法访问系统资源
MySQL 不能访问启动需要的资源是造成而 MySQL 无法启动的一个常见原因,如:文件,端口等。由于 linux 中用于启动 mysqld 进程的 mysql 用户通常是不能登陆的,可以使用类似下面的命令检查文件的访问权限。
# sudo -u mysql touch /var/lib/mysql/b
找出问题后,修改对应文件或目录的权限或属主后通常可以解决问题。但有时 mysql 用户有访问文件和目录的权限,但仍然会被拒绝访问,例如下面这个例子:
mysql> system sudo -u mysql touch /home/mysql/data/a mysql> create table t1 (id int primary key,n varchar(10)) data directory='/home/mysql/data'; ERROR 1030 (HY000): Got error 168 from storage engine
测试说明 mysql 用户有这个目录的访问权限,但创建文件还是失败,这种情况让很多人困惑,这个时候通常是 mysqld 进程的访问被 linux 的 selinux 或 apparmor 给阻止了,大家可以看到创建的表不是在 mysql 的默认目录下面,因此 selinux 或 apparmor 的 policy 里面没有包含这个目录的访问权限,此时只要对应的修改 policy 就行了,当然把 selinux 或 apparmor 停了也行。
有时虽然对系统资源有访问的权限,但系统资源已经被占用:
# mysqld --no-defaults --console --user mysql 2020-11-03T03:36:07.519419Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.19) starting as process 21171 2020-11-03T03:36:07.740347Z 1 [ERROR] [MY-012574] [InnoDB] Unable to lock ./ibdata1 error: 11
这个故障产生的原因是另外一个 mysqld 进程已经启动并占用了对应的文件。
参数设置错误
参数设置错误造成 MySQL 无法启动的原因也非常常见,此时先要检查 MySQL 启动时会调用的参数,下面的命令可以查询 MySQL 启动时调用参数文件的顺序:
$ mysqld --verbose --help | grep "Default options " -A 1 Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
知道了 MySQL 参数文件的调用顺序,我们就可以检查对应的参数文件,找出其中的错误,如果觉得参数文件的可读性不强,可以使用下面的命令显示 mysqld 程序将要调用的参数:
$ mysqld --print-defaults /usr/sbin/mysqld would have been started with the following arguments: ......
注意这个命令显示完参数后就退出,不会真正运行 mysqld。这个命令和 my_print_defaults mysqld 完全是等价的,只不过后者的显示方式是一行一个参数。
然后开始对可疑的参数进行调试,我个人喜欢加的参数和顺序如下:
在 mysqld 后加上第一个参数–no-defaults ,这个参数的作用是通知 mysqld 在启动的时候不要读任何参数文件;
第二个参数是 --console,这个参数会把错误信息输出到屏幕上,这个参数带来的一个弊端是所有的信息都输出到屏幕上,让屏幕显得比较乱,但对于我们调试却是很方便的;
第三个参数是 --log-error-verbosity=3,这个参数会显示详细的日志;
然后再在后面加上有把握的参数,可以一次只加一个参数,然后启动 mysqld,采用排除法逐步找出错误的参数。
看这个例子:
root@scutech:~# mysqld --no-defaults --console --log-error-verbosity=3 --user mysql --gtid_mode=on 2020-11-03T07:14:20.384223Z 0 [Note] [MY-010949] [Server] Basedir set to /usr/. 2020-11-03T07:14:20.384254Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.19) starting as process 22617 2020-11-03T07:14:20.400221Z 0 [Note] [MY-012366] [InnoDB] Using Linux native AIO 2020-11-03T07:14:20.400675Z 0 [Note] [MY-010747] [Server] Plugin 'FEDERATED' is disabled. 2020-11-03T07:14:20.405065Z 1 [Note] [MY-012932] [InnoDB] PUNCH HOLE support available 2020-11-03T07:14:20.405688Z 1 [Note] [MY-012943] [InnoDB] Mutexes and rw_locks use GCC atomic builtins 2020-11-03T07:14:20.406276Z 1 [Note] [MY-012944] [InnoDB] Uses event mutexes 2020-11-03T07:14:20.406531Z 1 [Note] [MY-012945] [InnoDB] GCC builtin __atomic_thread_fence() is used for memory barrier 2020-11-03T07:14:20.407000Z 1 [Note] [MY-012948] [InnoDB] Compressed tables use zlib 1.2.11 2020-11-03T07:14:20.422039Z 1 [Note] [MY-013251] [InnoDB] Number of pools: 1 2020-11-03T07:14:20.422635Z 1 [Note] [MY-012951] [InnoDB] Using CPU crc32 instructions 2020-11-03T07:14:20.424462Z 1 [Note] [MY-012203] [InnoDB] Directories to scan './' 2020-11-03T07:14:20.424846Z 1 [Note] [MY-012204] [InnoDB] Scanning './' 2020-11-03T07:14:20.432363Z 1 [Note] [MY-012208] [InnoDB] Completed space ID check of 6 files. 2020-11-03T07:14:20.437023Z 1 [Note] [MY-012955] [InnoDB] Initializing buffer pool, total size = 128.000000M, instances = 1, chunk size =128.000000M 2020-11-03T07:14:20.471991Z 1 [Note] [MY-012957] [InnoDB] Completed initialization of buffer pool 2020-11-03T07:14:20.478902Z 0 [Note] [MY-011952] [InnoDB] If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority(). 2020-11-03T07:14:20.584308Z 1 [Note] [MY-013086] [InnoDB] Starting to parse redo log at lsn = 3451887770, whereas checkpoint_lsn = 3451888113 2020-11-03T07:14:20.616162Z 1 [Note] [MY-013083] [InnoDB] Log background threads are being started... 2020-11-03T07:14:20.618276Z 1 [Note] [MY-012532] [InnoDB] Applying a batch of 0 redo log records ... 2020-11-03T07:14:20.618721Z 1 [Note] [MY-012535] [InnoDB] Apply batch completed! 2020-11-03T07:14:20.619684Z 1 [Note] [MY-013252] [InnoDB] Using undo tablespace './undo_001'. 2020-11-03T07:14:20.624221Z 1 [Note] [MY-013252] [InnoDB] Using undo tablespace './undo_002'. 2020-11-03T07:14:20.641573Z 1 [Note] [MY-012910] [InnoDB] Opened 2 existing undo tablespaces. 2020-11-03T07:14:20.641966Z 1 [Note] [MY-011980] [InnoDB] GTID recovery trx_no: 645128 2020-11-03T07:14:20.713883Z 1 [Note] [MY-012923] [InnoDB] Creating shared tablespace for temporary tables 2020-11-03T07:14:20.714534Z 1 [Note] [MY-012265] [InnoDB] Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ... 2020-11-03T07:14:20.842880Z 1 [Note] [MY-012266] [InnoDB] File './ibtmp1' size is now 12 MB. 2020-11-03T07:14:20.844121Z 1 [Note] [MY-011825] [InnoDB] Scanning temp tablespace dir:'./#innodb_temp/' 2020-11-03T07:14:21.190781Z 1 [Note] [MY-013018] [InnoDB] Created 128 and tracked 128 new rollback segment(s) in the temporary tablespace. 128 are now active. 2020-11-03T07:14:21.193330Z 1 [Note] [MY-012976] [InnoDB] 8.0.19 started; log sequence number 3451888113 2020-11-03T07:14:21.224117Z 1 [Note] [MY-011089] [Server] Data dictionary restarting version '80017'. 2020-11-03T07:14:21.423935Z 1 [Note] [MY-012357] [InnoDB] Reading DD tablespace files 2020-11-03T07:14:21.434098Z 1 [ERROR] [MY-012657] [InnoDB] Encryption can't find master key, please check the keyring plugin is loaded. 2020-11-03T07:14:21.434303Z 1 [ERROR] [MY-012226] [InnoDB] Encryption information in datafile: ./b/t1.ibd can't be decrypted, please confirm the keyfile is match and keyring plugin is loaded. 2020-11-03T07:14:21.434511Z 1 [Note] [MY-012355] [InnoDB] Tablespace 34, name 'b/t1', unable to open file './b/t1.ibd' - Invalid encryption meta-data information 2020-11-03T07:14:21.434807Z 1 [Note] [MY-012356] [InnoDB] Validated 8/8 tablespaces 2020-11-03T07:14:21.485964Z 1 [Note] [MY-010006] [Server] Using data dictionary with version '80017'. 2020-11-03T07:14:21.600652Z 0 [Note] [MY-012487] [InnoDB] DDL log recovery : begin 2020-11-03T07:14:21.601114Z 0 [Note] [MY-012488] [InnoDB] DDL log recovery : end 2020-11-03T07:14:21.601899Z 0 [Note] [MY-011946] [InnoDB] Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool 2020-11-03T07:14:21.603430Z 0 [Note] [MY-011946] [InnoDB] Buffer pool(s) load completed at 201103 3:44:21 2020-11-03T07:14:21.632851Z 0 [ERROR] [MY-010912] [Server] GTID_MODE = ON requires ENFORCE_GTID_CONSISTENCY = ON. 2020-11-03T07:14:21.634183Z 0 [ERROR] [MY-010119] [Server] Aborting 2020-11-03T07:14:21.748554Z 0 [Note] [MY-012330] [InnoDB] FTS optimize thread exiting. 2020-11-03T07:14:21.752581Z 0 [Note] [MY-010120] [Server] Binlog end 2020-11-03T07:14:21.753694Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'keyring_file' 2020-11-03T07:14:21.754159Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'mysqlx_cache_cleaner' 2020-11-03T07:14:21.754357Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'mysqlx' 2020-11-03T07:14:21.755531Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'ngram' 2020-11-03T07:14:21.755803Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'BLACKHOLE' 2020-11-03T07:14:21.756080Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'ARCHIVE' 2020-11-03T07:14:21.756320Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'TempTable' 2020-11-03T07:14:21.756596Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'PERFORMANCE_SCHEMA' 2020-11-03T07:14:21.757103Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'MRG_MYISAM' 2020-11-03T07:14:21.757522Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'MyISAM' 2020-11-03T07:14:21.757755Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_SESSION_TEMP_TABLESPACES' 2020-11-03T07:14:21.757963Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_CACHED_INDEXES' 2020-11-03T07:14:21.758157Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_VIRTUAL' 2020-11-03T07:14:21.758354Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_COLUMNS' 2020-11-03T07:14:21.758554Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_TABLESPACES' 2020-11-03T07:14:21.758880Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_INDEXES' 2020-11-03T07:14:21.759095Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_TABLESTATS' 2020-11-03T07:14:21.759387Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_TABLES' 2020-11-03T07:14:21.759720Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_FT_INDEX_TABLE' 2020-11-03T07:14:21.759998Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_FT_INDEX_CACHE' 2020-11-03T07:14:21.760233Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_FT_CONFIG' 2020-11-03T07:14:21.760472Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_FT_BEING_DELETED' 2020-11-03T07:14:21.760750Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_FT_DELETED' 2020-11-03T07:14:21.761007Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD' 2020-11-03T07:14:21.761260Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_METRICS' 2020-11-03T07:14:21.761559Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_TEMP_TABLE_INFO' 2020-11-03T07:14:21.761824Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_BUFFER_POOL_STATS' 2020-11-03T07:14:21.762095Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU' 2020-11-03T07:14:21.762369Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_BUFFER_PAGE' 2020-11-03T07:14:21.762647Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET' 2020-11-03T07:14:21.762932Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_CMP_PER_INDEX' 2020-11-03T07:14:21.763221Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_CMPMEM_RESET' 2020-11-03T07:14:21.763514Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_CMPMEM' 2020-11-03T07:14:21.763841Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_CMP_RESET' 2020-11-03T07:14:21.764172Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_CMP' 2020-11-03T07:14:21.764489Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'INNODB_TRX' 2020-11-03T07:14:21.764867Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'InnoDB' 2020-11-03T07:14:21.765259Z 0 [Note] [MY-013072] [InnoDB] Starting shutdown... 2020-11-03T07:14:21.765864Z 0 [Note] [MY-011944] [InnoDB] Dumping buffer pool(s) to /var/lib/mysql/ib_buffer_pool 2020-11-03T07:14:21.767071Z 0 [Note] [MY-011944] [InnoDB] Buffer pool(s) dump completed at 201103 3:44:21 2020-11-03T07:14:22.485174Z 0 [Note] [MY-013084] [InnoDB] Log background threads are being closed... 2020-11-03T07:14:23.013239Z 0 [Note] [MY-012980] [InnoDB] Shutdown completed; log sequence number 3451917484 2020-11-03T07:14:23.016772Z 0 [Note] [MY-012255] [InnoDB] Removed temporary tablespace data file: "ibtmp1" 2020-11-03T07:14:23.017200Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'MEMORY' 2020-11-03T07:14:23.017777Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'CSV' 2020-11-03T07:14:23.018155Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'sha2_cache_cleaner' 2020-11-03T07:14:23.018492Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'caching_sha2_password' 2020-11-03T07:14:23.018826Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'sha256_password' 2020-11-03T07:14:23.019159Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'mysql_native_password' 2020-11-03T07:14:23.025400Z 0 [Note] [MY-010733] [Server] Shutting down plugin 'binlog' 2020-11-03T07:14:23.026551Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.19) MySQL Community Server - GPL. root@scutech:~#
看这个例子,我们很容易知道是需要我们同时设置参数 GTID_MODE 和 ENFORCE_GTID_CONSISTENCY 同时为 on 才行。