MySQL 启动失败的常见原因---发表到爱可生开源社区

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: MySQL 启动失败的最常见的原因有两类,分别是无法访问系统资源和参数设置错误造成的,下面分别分析如下。

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 才行。

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
7月前
|
存储 Java 关系型数据库
社区医院管理服务系统【GUI/Swing+MySQL】(Java课设)
社区医院管理服务系统【GUI/Swing+MySQL】(Java课设)
105 1
|
7月前
|
Ubuntu 关系型数据库 MySQL
百度搜索:蓝易云【ubuntu20.4服务器安装mysql社区版并开放3306端口】
现在,你已经在Ubuntu 20.04服务器上成功安装了MySQL社区版,并且已经开放了3306端口,可以通过该端口访问MySQL服务器了。请确保在生产环境中设置安全措施,例如设置强密码、限制访问等,以保护数据库的安全性。
147 2
|
7月前
|
NoSQL 关系型数据库 MySQL
基于Python和mysql开发的BBS问答社区管理系统(源码+数据库+程序配置说明书+程序使用说明书)
基于Python和mysql开发的BBS问答社区管理系统(源码+数据库+程序配置说明书+程序使用说明书)
|
1月前
|
存储 关系型数据库 MySQL
MySQL vs. PostgreSQL:选择适合你的开源数据库
在众多开源数据库中,MySQL和PostgreSQL无疑是最受欢迎的两个。它们都有着强大的功能、广泛的社区支持和丰富的生态系统。然而,它们在设计理念、性能特点、功能特性等方面存在着显著的差异。本文将从这三个方面对MySQL和PostgreSQL进行比较,以帮助您选择更适合您需求的开源数据库。
180 4
|
2月前
|
SQL JSON 关系型数据库
MySQL是一个广泛使用的开源关系型数据库管理系统,它有许多不同的版本
【10月更文挑战第3天】MySQL是一个广泛使用的开源关系型数据库管理系统,它有许多不同的版本
199 5
|
6月前
|
关系型数据库 MySQL Java
关系型数据库mysql的开源与授权
【6月更文挑战第12天】
290 3
|
7月前
|
canal 缓存 关系型数据库
MySQL如何实时同步数据到ES?试试阿里开源的Canal
MySQL如何实时同步数据到ES?试试阿里开源的Canal
212 3
|
7月前
|
存储 关系型数据库 MySQL
【专栏】在众多开源数据库中,MySQL和PostgreSQL无疑是最受欢迎的两个
【4月更文挑战第27天】MySQL与PostgreSQL是两大主流开源数据库,各有特色。MySQL注重简单、便捷和高效,适合读操作密集场景,而PostgreSQL强调灵活、强大和兼容,擅长并发写入与复杂查询。MySQL支持多种存储引擎和查询缓存,PostgreSQL则具备扩展性、强事务支持和高可用特性。选择时应考虑项目需求、团队技能和预期收益。
109 2
|
7月前
|
关系型数据库 MySQL Linux
【Linux】安装Mysql(附加service mysql启动失败 提示unit not found 解决办法)
【Linux】安装Mysql(附加service mysql启动失败 提示unit not found 解决办法)
543 0
|
7月前
|
关系型数据库 MySQL 数据处理
MySQL vs. PostgreSQL:选择适合你的开源数据库
在当今信息时代,开源数据库成为许多企业和开发者的首选。本文将比较两个主流的开源数据库——MySQL和PostgreSQL,分析它们的特点、优势和适用场景,以帮助读者做出明智的选择。