MySQL启动时报 无法申请内存错误解决方式 Fatal error: cannot allocate memory for the buffer pool

国庆前配置的MySQL服务器重启后无法启动,通过调整innodb_buffer_pool_size参数无效后,最终通过增加交换分区大小解决了MySQL启动问题。

国庆前配置的MySQL服务器,今天重启,MySQL就启动不了,开始以为是MySQL 的PID文件没有权限,折腾了一下,还是没启动成功,于是度娘了一下,发现很多介绍的都是说my.cnf中的innodb_buffer_pool_size参数设置太小,然后我就修改这个参数值,再重试,依然没有启动成功,没办法,接着度娘,然而还是没有什么鸟用,千篇一律的都是介绍说innodb_buffer_pool_size的数值调大点,于是又接着度娘,终于找到一个解决方案,就是添加交换分区的大小,尝试了一下,标题中的错误得以排除,以下是具体实现:

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 #增加1G的SWAP进去
sudo mkswap /swapfile
sudo swapon /swapfile
free

然后再尝试启动MySQL,得以解决,MYSQL启动正常!

root@ecm-e00f:/# sudo journalctl -u mariadb -xe # 检查启动错误 Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: Initializing buffer pool, total> Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: Initialized memory pressure eve> Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: Completed initialization of buf> Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: File system buffers for log dis> Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: End of log at LSN=46996 Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: 128 rollback segments are activ> Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: Setting file './ibtmp1' size to> Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: File './ibtmp1' size is now 12.> Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: log sequence number 46996; tran> Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: Loading buffer pool(s) from /yu> Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] Plugin 'FEEDBACK' is disabled. Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] Server socket created on IP: '0.0.0.0'. Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] Server socket created on IP: '::'. Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: Buffer pool(s) load completed a> Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] /usr/sbin/mariadbd: ready for connectio> Jun 25 16:43:09 ecm-e00f mariadbd[336798]: Version: '10.11.11-MariaDB-0+deb12u1' socket: '/run/mysqld/mysqld.s> Jun 25 16:43:09 ecm-e00f systemd[1]: Started mariadb.service - MariaDB 10.11.11 database server. ░░ Subject: A start job for unit mariadb.service has finished successfully ░░ Defined-By: systemd ░░ Support: https://www.debian.org/support ░░ ░░ A start job for unit mariadb.service has finished successfully. ░░ ░░ The job identifier is 57427. Jun 25 16:43:09 ecm-e00f /etc/mysql/debian-start[336825]: Checking for insecure root accounts. Jun 25 16:43:09 ecm-e00f /etc/mysql/debian-start[336829]: Triggering myisam-recover for all MyISAM tables and a> lines 960-985/985 (END) Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: Initializing buffer pool, total size = 128.000MiB, chunk size = 2.000MiB Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: Initialized memory pressure event listener Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: Completed initialization of buffer pool Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: File system buffers for log disabled (block size=512 bytes) Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: End of log at LSN=46996 Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: 128 rollback segments are active. Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: Setting file './ibtmp1' size to 12.000MiB. Physically writing the file full; Please wait ... Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: File './ibtmp1' size is now 12.000MiB. Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: log sequence number 46996; transaction id 14 Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: Loading buffer pool(s) from /yunxiang/data/mysql/ib_buffer_pool Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] Plugin 'FEEDBACK' is disabled. Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] Server socket created on IP: '0.0.0.0'. Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] Server socket created on IP: '::'. Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] InnoDB: Buffer pool(s) load completed at 250625 16:43:09 Jun 25 16:43:09 ecm-e00f mariadbd[336798]: 2025-06-25 16:43:09 0 [Note] /usr/sbin/mariadbd: ready for connections. Jun 25 16:43:09 ecm-e00f mariadbd[336798]: Version: '10.11.11-MariaDB-0+deb12u1' socket: '/run/mysqld/mysqld.sock' port: 3306 Debian 12 Jun 25 16:43:09 ecm-e00f systemd[1]: Started mariadb.service - MariaDB 10.11.11 database server. ░░ Subject: A start job for unit mariadb.service has finished successfully ░░ Defined-By: systemd ░░ Support: https://www.debian.org/support ░░ ░░ A start job for unit mariadb.service has finished successfully. ░░ ░░ The job identifier is 57427. Jun 25 16:43:09 ecm-e00f /etc/mysql/debian-start[336825]: Checking for insecure root accounts. Jun 25 16:43:09 ecm-e00f /etc/mysql/debian-start[336829]: Triggering myisam-recover for all MyISAM tables and aria-recover for all Aria tables ~
最新发布
06-26
### 关于数据库错误 'Cannot enqueue Query after fatal error' 的解决方案 在使用 Node.js 连接 MySQL 数据库时,可能会遇到 `Cannot enqueue Query after fatal error` 错误。这种错误通常发生在以下几种情况中:连接池中的某个连接已经失效或断开,但应用程序仍然尝试使用该连接执行查询操作[^1]。 为了解决这个问题,可以采取以下措施: #### 1. 使用连接池管理数据库连接 通过使用连接池(Connection Pool),可以有效避免因单个连接长时间未使用而导致的自动断开问题。连接池会自动维护多个数据库连接,并确保每个连接在使用前是有效的。 以下是配置连接池的代码示例: ```javascript const mysql = require('mysql'); const pool = mysql.createPool({ host: 'localhost', user: 'root', password: 'password', database: 'testdb', connectionLimit: 10, // 设置连接池的最大连接数 acquireTimeout: 60000, // 设置获取连接的超时时间 waitForConnections: true, // 等待可用连接 }); // 查询数据库 pool.query('SELECT 1 + 1 AS solution', (error, results, fields) => { if (error) throw error; console.log('The solution is:', results[0].solution); }); ``` #### 2. 捕获并处理连接错误 在执行查询之前,可以通过捕获连接错误来确保当前连接仍然有效。如果发现连接无效,则重新获取一个新的连接。 以下是捕获和处理连接错误的代码示例: ```javascript pool.getConnection((err, connection) => { if (err) { if (err.code === 'PROTOCOL_CONNECTION_LOST') { console.error('Database connection was closed.'); } if (err.code === 'ER_CON_COUNT_ERROR') { console.error('Database has too many connections.'); } if (err.code === 'ECONNREFUSED') { console.error('Database connection was refused.'); } return; } connection.query('SELECT * FROM users', (error, results, fields) => { connection.release(); // 释放连接回连接池 if (error) throw error; console.log(results); }); }); ``` #### 3. 配置连接的超时时间和重连机制 MySQL 默认会在 8 小时无活动后自动断开连接[^3]。为了避免这种情况,可以在连接池中设置 `connectTimeout` 和 `acquireTimeout` 参数,以确保连接在超时后能够被正确释放并重新建立。 以下是配置超时参数的示例: ```javascript const pool = mysql.createPool({ host: 'localhost', user: 'root', password: 'password', database: 'testdb', connectTimeout: 60000, // 设置连接超时时间为 60 秒 acquireTimeout: 60000, // 设置获取连接的超时时间为 60 秒 waitForConnections: true, }); ``` #### 4. 定期验证连接的有效性 可以通过定期执行简单的查询(如 `SELECT 1`)来验证连接是否仍然有效。如果发现连接无效,则可以手动释放并重新获取连接。 以下是定期验证连接的代码示例: ```javascript setInterval(() => { pool.query('SELECT 1', (error, results) => { if (error) { console.error('Connection validation failed:', error.message); } else { console.log('Connection is still valid.'); } }); }, 60000); // 每分钟验证一次连接有效性 ``` ### 总结 通过使用连接池、捕获并处理连接错误、配置超时参数以及定期验证连接的有效性,可以有效解决 `Cannot enqueue Query after fatal error` 错误。这些方法不仅能够提高应用程序的稳定性,还可以减少因连接问题导致的性能下降。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值