Start, restart and shut down MySQL on Linux service
MySQL is a widely used open source relational database management system, commonly used in applications of all sizes. Managing MySQL services on Linux servers is a basic operation and maintenance task. This article will provide detailed instructions on how to start, restart, and shut down MySQL services on Linux systems, covering how to operate different Linux distributions such as Ubuntu and CentOS, as well as some common troubleshooting tips.
1. Environmental preparation
Before doing MySQL service management, make sure you have MySQL installed. Here are the basic steps to install MySQL on Ubuntu and CentOS:
1.1 Install MySQL on Ubuntu
First, update the package index:
sudo apt-get update
Then, install the MySQL server:
sudo apt-get install mysql-server
During the installation process, you will be prompted to set the password of the MySQL root user. After the installation is completed, the MySQL service will start automatically.
1.2 Install MySQL on CentOS
First, add the MySQL repository:
sudo yum localinstall /get/
Then, install the MySQL server:
sudo yum install mysql-community-server
After the installation is complete, start the MySQL service:
sudo systemctl start mysqld
2. Start MySQL service
The operation of starting the MySQL service may vary depending on the Linux distribution and the MySQL version. The following describes how to start MySQL service on Ubuntu and CentOS respectively.
2.1 Start MySQL Service on Ubuntu
On Ubuntu systems, the management of MySQL services is usually done through the systemctl command:
sudo systemctl start mysql
You can also check the status of the MySQL service using the following command:
sudo systemctl status mysql
If you are using an older MySQL version or Ubuntu version, you can useservice
Order:
sudo service mysql start
2.2 Start MySQL service on CentOS
On CentOS, it can also be usedsystemctl
Command to start MySQL service:
sudo systemctl start mysqld
Check the MySQL service status:
sudo systemctl status mysqld
For older MySQL versions or CentOS versions, you can useservice
Order:
sudo service mysqld start
3. Restart MySQL service
Restarting MySQL service is necessary in some cases, such as after changing the configuration file or resolving a MySQL service failure. The following is how to restart MySQL service.
3.1 Restart MySQL service on Ubuntu
usesystemctl
Command to restart MySQL service:
sudo systemctl restart mysql
useservice
Command to restart MySQL service:
sudo service mysql restart
3.2 Restart MySQL service on CentOS
usesystemctl
Command to restart MySQL service:
sudo systemctl restart mysqld
useservice
Command to restart MySQL service:
sudo service mysql restart
3.2 Restart MySQL service on CentOS
usesystemctl
Command to restart MySQL service:
sudo systemctl restart mysqld
useservice
Command to restart MySQL service:
sudo service mysqld restart
4. Close MySQL service
Turning off MySQL services is necessary for system maintenance or upgrade. Here is how to close MySQL service.
4.1 Turn off MySQL service on Ubuntu
usesystemctl
Command to close MySQL service:
sudo systemctl stop mysql
useservice
Command to close MySQL service:
sudo service mysql stop
4.2 Turn off MySQL service on CentOS
usesystemctl
Command to close MySQL service:
sudo systemctl stop mysqld
useservice
Command to close MySQL service:
sudo service mysqld stop
5. Set MySQL service to start automatically
To ensure that the MySQL service starts automatically after the server restarts, the MySQL service can be configured to start automatically.
5.1 Setting up MySQL service to start up on Ubuntu
usesystemctl
Order:
sudo systemctl enable mysql
5.2 Setting up MySQL service to start up on CentOS
usesystemctl
Order:
sudo systemctl enable mysqld
6. Troubleshooting
There are some common problems that may be encountered in the process of managing MySQL services. Here are some common failures and their solutions.
6.1 MySQL service cannot be started
If the MySQL service fails to start, check the error log first. MySQL error logs are usually located in /var/log/mysql/ (Ubuntu) or /var/log/ (CentOS). Viewing the log can help identify the problem.
Check if there is any error in the MySQL configuration file (/etc/mysql/ or /etc/). If there are incorrect configuration items in the configuration file, the MySQL service may not start.
Make sure the permissions of the MySQL data directory are correct. MySQL service requires read and write permissions to the data directory. Check permissions with the following command:
sudo ls -ld /var/lib/mysql
6.2 MySQL service starts but cannot connect
If the MySQL service starts successfully but fails to connect, it may be a firewall problem. Make sure the firewall allows the ports of MySQL services (default 3306). On Ubuntu, you can open port 3306 using the following command:
sudo ufw allow 3306
On CentOS, you can use the following command:
sudo firewall-cmd --permanent --add-port=3306/tcp sudo firewall-cmd --reload
Also make sure the binding address in the MySQL configuration file is correct. existIn the file, make sure
bind-address
Configuration items allow remote connections:
[mysqld] bind-address = 0.0.0.0
Then restart MySQL service:
sudo systemctl restart mysql
6.3 MySQL service crashes frequently
If the MySQL service crashes frequently, check if the system resources are sufficient, such as memory and disk space. Check system resources with the following command:
free -h df -h
Check the memory-related configuration in the MySQL configuration file, such as innodb_buffer_pool_size, to ensure reasonable configuration.
7. Summary
Managing MySQL services on Linux servers is a must-have skill for every operation and maintenance worker and developer. Through this article, we learned how to start, restart, and shut down MySQL services on Ubuntu and CentOS, as well as basic ways to set up power-on self-boot and troubleshoot. Whether in the development environment or the production environment, mastering these basic operations can help us better manage and maintain database services and improve the reliability and stability of the system.
The above is the detailed information on how to start, restart and shut down MySQL on Linux services. For more information about startup, restart and shutdown of Linux MySQL, please follow my other related articles!