This post will discuss how to create a MySQL Server Startup Script and configure “systemctl” commands using the “systemd” MySQL Server Startup Script.
Create MySQL Server Startup Script
To create a MySQL server startup script, let’s first create a directory and navigate in it by following these commands:
cd mysql
The working directory will change:
Create a bash script named “mysql.sh”, using any editor according to your preference:
Type this script inside this file to start the MySQL server:
/usr/bin/mysqld_safe &
Save the file, and exit by pressing the “CTRL + O” key.
After creating any bash file, change its permissions to make it executable. So type this command for changing the permission of the bash file to executable:
To configure the script to run at boot time, add it to the system’s startup file. For that, go to “Menu”, search for “Startup Application”, and open it:
Provide a “Name”, and the location of the bash file containing the startup script in the “Command:” option. Type a “Comment” for this Startup Program and click on the “Add” button:
Execute the script by typing:
The error-free output indicates that your MySQL server startup script is created and executed successfully:
Configure Systemd for MySQL Server Startup Script
To configure other functionalities for this Startup Script, such as reload, enable, and status. First, navigate to the directory containing systemd:
Create a file named “mysql.service”:
Add the configuration in the file:
Description=MySQL Server
After=network.target
[Service]
User=mysql
ExecStart=/bin/sh /path/to/mysql.sh
Restart=always
[Install]
WantedBy=multi-user.target
Make sure that you provide the location of the “mysql.sh” file correctly in “ExecStart”:
Enable the “mysql.service” file, to start it automatically after every reboot:
sudo systemctl enable mysql.service
To see the status of MySQL, type:
The output displays the status “active (running)”, which means that “systemctl” is enabled for “mysql.service”:
Conclusion
To enable the MySQL server to start automatically, create a bash script that will start MySQL using “mysqld_safe”. Change the permission to executable and add its location in “Startup Application” so that it can start at every reboot, and execute the script. Create a “mysql.service” file and configure “systemctl” command using “systemd” for MySQL Server Startup Script.