4

I need to run a query to populate a memory table on every MySQL start.

Is there any way to do this?

stramin
  • 2,183
  • 3
  • 29
  • 58
  • See https://stackoverflow.com/questions/27611685/run-sql-query-on-mysql-service-startup – Whome Oct 22 '18 at 14:23

2 Answers2

5

MySQL is able to read statements from init_file on startup.

You'd have to place the following in my.ini or my.cnf (depending on the OS) file

[mysqld] 
init-file="/path/to/your/query/populate_memory_table.sql"
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
0

Another option is to have a system script run after mysql starts. With systemd in Linux, the following would work:

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

The script will be run the next time mysql starts or restarts. Not quite as good as init_file because mysql will be available for queries before the MEMORY tables have been populated, but much more flexible.

Roger Dueck
  • 615
  • 7
  • 16