我需要在每次 MySQL 启动时运行一个查询来填充一个内存表。
有没有办法做到这一点?
MySQL 能够从init_file
启动时读取语句。
您必须将以下内容放入my.ini
或my.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
mysql 在 MEMORY 表被填充之前可用于查询,但更灵活。