MySQL MariaDB

MySQL Server Startup Script

Sometimes, the user wants to ensure that the MySQL server is always enabled and started on their system. To do so, create MySQL Server Startup Script. MySQL Server startup Script is a bash script to start MySQL Server by invoking “mysqld_safe,” which is available for starting MySQL Server in a UNIX-based operating system.

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:

mkdir mysql

cd mysql

The working directory will change:

Create a bash script named “mysql.sh”, using any editor according to your preference:

nano mysql.sh

Type this script inside this file to start the MySQL server:

#!/bin/sh

/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:

chmod +x mysql.sh

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:

sudo bash mysql.sh

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:

cd /etc/systemd/system

Create a file named “mysql.service”:

sudo nano mysql.service

Add the configuration in the file:

[Unit]

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 daemon-reload

sudo systemctl enable mysql.service

To see the status of MySQL, type:

sudo systemctl status mysql.service

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.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.