如何在每次服务启动时运行 MySQL 查询?



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

有什么办法可以做到这一点吗?

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

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

[mysqld] 
init-file="/path/to/your/query/populate_memory_table.sql"

另一种选择是在 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_file,因为在填充MEMORY表之前,mysql可用于查询,但要灵活得多。

最新更新