Home  >  Article  >  Database  >  How to start and stop the MySQL service on Linux server

How to start and stop the MySQL service on Linux server

PHPz
PHPzOriginal
2023-04-17 16:40:102754browse

MySQL is a popular relational database management system that is widely used in many applications and websites. Whether as a developer or system administrator, mastering the basic operations of MySQL is necessary. One of the most basic operations is starting and stopping the MySQL service.

The starting and stopping process of MySQL is very simple. In this article, we will show you how to start and stop the MySQL service on a Linux server.

Start the MySQL service

There are two ways to start the MySQL service on the Linux server: using the command line tool and using the self-starting script.

Method 1: Use command line tools

You can use command line tools to start the MySQL service. Please follow these steps:

  1. Open Terminal.
  2. Type the following command in the terminal to start the MySQL service:
sudo service mysql start
  1. You will be asked to enter your system login password to confirm your permissions.
  2. If MySQL starts successfully, you should see the following output:
$ sudo service mysql start
 * Starting MySQL database server mysqld
   ...done.

Method 2: Use a self-start script

Another way to start the MySQL service Is using a self-starting script. This will automatically start the MySQL service when your system boots.

Please follow these steps:

  1. Open Terminal.
  2. Type the following command in the terminal to edit the my.cnf file:
sudo nano /etc/mysql/my.cnf
  1. Add the following line at the end of the my.cnf file:
[mysqld]
should_restart_mysql = yes

This will ensure that MySQL starts automatically when the system starts.

  1. Save and close the my.cnf file.
  2. Type the following command in the terminal to update your system:
sudo update-rc.d mysql defaults
  1. If MySQL starts successfully, you should see the following output:
$ sudo update-rc.d mysql defaults
Adding system startup for /etc/init.d/mysql ...
   /etc/rc0.d/K20mysql -> ../init.d/mysql
   /etc/rc1.d/K20mysql -> ../init.d/mysql
   /etc/rc6.d/K20mysql -> ../init.d/mysql
   /etc/rc2.d/S20mysql -> ../init.d/mysql
   /etc/rc3.d/S20mysql -> ../init.d/mysql
   /etc/rc4.d/S20mysql -> ../init.d/mysql
   /etc/rc5.d/S20mysql -> ../init.d/mysql

Stop the MySQL service

There are also two ways to stop the MySQL service on the Linux server: using command line tools and using self-starting scripts.

Method 1: Use command line tools

You can use command line tools to stop the MySQL service. Please follow these steps:

  1. Open Terminal.
  2. Type the following command in the terminal to stop the MySQL service:
sudo service mysql stop
  1. You will be asked to enter your system login password to confirm your permissions.
  2. If MySQL is stopped successfully, you should see the following output:
$ sudo service mysql stop
 * Stopping MySQL database server mysqld
   ...done.

Method 2: Use a self-start script

Another way to stop the MySQL service Is using a self-starting script. This will automatically shut down the MySQL service when your system shuts down.

Please follow these steps:

  1. Open Terminal.
  2. Type the following command in the terminal to edit the my.cnf file:
sudo nano /etc/mysql/my.cnf
  1. Add the following line at the end of the my.cnf file:
[mysqld]
should_restart_mysql = no

This will ensure that MySQL automatically shuts down when the system shuts down.

  1. Save and close the my.cnf file.
  2. Type the following command in the terminal to update your system:
sudo update-rc.d mysql remove
  1. If MySQL stops successfully, you should see the following output:
$ sudo update-rc.d mysql remove
 Removing any system startup links for /etc/init.d/mysql ...
   /etc/rc0.d/K20mysql
   /etc/rc1.d/K20mysql
   /etc/rc2.d/S20mysql
   /etc/rc3.d/S20mysql
   /etc/rc4.d/S20mysql
   /etc/rc5.d/S20mysql
   /etc/rc6.d/K20mysql

Conclusion

Starting and stopping the MySQL service on a Linux server is a simple yet important task, which is necessary for both database developers and system administrators. In this article, we have shown you how to start and stop the MySQL service using command line tools and self-start scripts. Choose the method that works for you and always ensure that your MySQL service is available and secure.

The above is the detailed content of How to start and stop the MySQL service on Linux server. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn