Home >Database >Mysql Tutorial >mysql service failed to start
MySQL is an open source relational database management system that is widely used in the field of Internet technology. However, when operating MySQL, you may encounter the problem of failure to start the mysql service.
There are many reasons why MySQL fails to start. This article will introduce some common reasons and provide solutions.
When starting the MySQL service, if the port is occupied, MySQL will fail to start. In this case, we can solve the problem through the following methods:
First, we need to confirm that the default port number of MySQL is 3306. We can use the following command to check:
$ mysqladmin -h localhost -u root -p status
If MySQL is not currently started, it will prompt "mysqladmin: connect to server at 'localhost' failed error", otherwise MySQL status information will be returned.
We can use the following command to check which processes are currently using port 3306:
$ sudo lsof -i:3306
If there are other processes using port 3306 in the returned information, you need to stop the process or set MySQL to something else Unoccupied port.
Under Linux system, we can solve the problem by modifying the port number in the MySQl configuration file. The details are as follows:
$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf #修改以下行 port = 3306 #修改为其他可用端口 port = 3307
After the modification is completed, restart the MySQL service.
If the MySQL database file is damaged, it may also cause startup failure. For this situation, we can solve the problem through the following methods:
First, we need to enter the MySQL data directory and check whether the database file is damaged. Under the Ubuntu system, the MySQL data directory is generally /var/lib/mysql/. We can use the following command to enter the directory:
$ cd /var/lib/mysql/
After entering the directory, we can use the following command to check whether the data file is normal:
$ sudo mysqlcheck -u root -p --auto-repair --check --all-databases
This command will check and sum up all databases repair.
If the data file is damaged beyond repair, we need to perform data recovery. At this time we need to back up the original MySQL database file first. The specific method is as follows:
$ sudo service mysql stop $ sudo cp -r /var/lib/mysql /var/lib/mysql-backup
After the backup is completed, we can try to use the mysql_upgrade command to update the MySQL database to the latest version. The specific command is as follows:
$ sudo mysql_upgrade -u root -p
If the problem still cannot be solved, we can consider reinstalling the MySQL server or restoring data from existing backups.
If the MySQL user permissions are insufficient, it will also cause startup failure. For this situation, we can solve the problem through the following methods:
Under the MySQL command line, sometimes error messages such as "Access denied for user 'root'@'localhost'" will appear. This is because Caused by insufficient MySQL user permissions.
We can solve this problem through the following methods:
First, under the root account of MySQL, we can query the permissions of the current user:
mysql> SHOW GRANTS FOR CURRENT_USER;
This command will return Permission information of the current user in MySQL. If the current user's permissions are insufficient, we can use the following command to configure higher permissions for the user:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost'; mysql> FLUSH PRIVILEGES;
This command authorizes the user 'username' on localhost, giving the user all the permissions in MySQL permissions. The last command is to refresh MySQL permissions.
Summary
When using MySQL, it is a very common problem that MySQL fails to start due to various reasons. This article introduces several common problems and solutions, including: port occupation, database file damage and user permission issues. When encountering the problem of MySQL service failure to start, readers can try the methods provided in this article one by one. I believe that most problems can be solved.
The above is the detailed content of mysql service failed to start. For more information, please follow other related articles on the PHP Chinese website!