CentOS7执行systemctl start MySQL报错:Failed to start mysql.service: Unit not found
Hey there, let's work through this error step by step. This issue usually boils down to a missing installation, incorrect service name, or incomplete setup. Here's what to check:
1. Verify if MySQL is actually installed
First, confirm you have MySQL (or its drop-in replacement MariaDB) installed on your system. Run either of these commands:
rpm -qa | grep mysql # OR yum list installed | grep mysql
If you get no output, MySQL isn't installed. Since CentOS 7's default repos don't include MySQL (they ship with MariaDB instead), you'll need to add the official MySQL YUM repository first:
# Download the MySQL repo package for CentOS 7 sudo wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm # Install the repo sudo rpm -ivh mysql80-community-release-el7-3.noarch.rpm # Install MySQL Server sudo yum install mysql-community-server
Once installed, note that the service name might be mysqld instead of mysql—try starting it with:
sudo systemctl start mysqld
2. Check if you're using the correct service name
CentOS 7 defaults to MariaDB, which uses the mariadb service name instead of mysql. To check which service units exist on your system:
systemctl list-unit-files | grep -E '(mysql|mariadb)'
If you see mariadb.service listed, use this command to start it instead:
sudo systemctl start mariadb
3. Repair incomplete MySQL installation
If you confirmed MySQL is installed but the service unit is missing, your installation might be corrupted. Try reinstalling the MySQL server package:
sudo yum reinstall mysql-community-server
After reinstalling, reload systemd to pick up any new service files:
sudo systemctl daemon-reload
Then try starting the service again.
4. Manual service file for manually compiled MySQL
If you installed MySQL from source (not via YUM), systemd won't have a pre-configured service file. You'll need to create one manually:
- Create the service file at
/etc/systemd/system/mysql.servicewith the following content (adjust paths to match your installation):
[Unit] Description=MySQL Server After=network.target [Service] User=mysql Group=mysql ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf Restart=always [Install] WantedBy=multi-user.target
- Reload systemd to recognize the new service:
sudo systemctl daemon-reload
- Now you can start MySQL with:
sudo systemctl start mysql
Verify the fix
After starting the service, check its status to confirm it's running:
sudo systemctl status mysqld # or mysql/mariadb depending on your setup
You can also test connecting to MySQL with:
mysql -u root -p
内容的提问来源于stack exchange,提问作者Chao Liang

