3

我需要在每次 MySQL 启动时运行一个查询来填充一个内存表。

有没有办法做到这一点?

4

2 回答 2

4

MySQL 能够从init_file启动时读取语句。

您必须将以下内容放入my.inimy.cnf(取决于操作系统)文件中

[mysqld] 
init-file="/path/to/your/query/populate_memory_table.sql"
于 2018-10-22T14:25:01.877 回答
0

另一种选择是在 mysql 启动后运行系统脚本。在 Linux 中使用 systemd,以下将起作用:

cat > /lib/systemd/system/mysql-preload.service << END
[Unit]
Description=Pre-Load MEMORY tables after MySQL starts
PartOf=mysql.service
After=mysql.service

[Service]
Type=oneshot
ExecStart=/home/myuser/script.sh

[Install]
WantedBy=multi-user.target

END
systemctl daemon-reload

该脚本将在 mysql 下次启动或重新启动时运行。不如init_filemysql 在 MEMORY 表被填充之前可用于查询,但更灵活。

于 2019-11-29T15:56:36.677 回答