Hướng dẫn find mysql pid

In previous articles have gone through options available with mysql_safe, one of the option is pid file i.e. process identifier file

Nội dung chính

  • MySQL server is not running, so no PID file was found
  • How do I get MySQL pid?
  • Where is MySQL pid located?
  • How do I find MySQL server?
  • How do I know if MySQL daemon is running?

Nội dung chính

  • MySQL server is not running, so no PID file was found
  • How do I get MySQL pid?
  • Where is MySQL pid located?
  • How do I find MySQL server?
  • How do I know if MySQL daemon is running?
  • The server writes its process id into pid file when mysql services are started and removes file when stopped.
  • The pid file is the means by which a server allows itself to be found by other processes.

Location
default location of  pid file is data directory or it can be specified by specific variable 
pid-file=file_name
generally pid file is given as hostname.pid

Content
The pid file content the process identification number of mysqld process, you can see content by using below command
cat

and also see the process id  of mysql daemon process using 
ps -ef|grep mysqld

you can see content of pid file and this process id identified using ps -ef command are same.

When running mysql.server related commands in your MySQL installation, you may encounter a PID file could not be found error.

The following example shows how the error appears when I ran the mysql.server stop command:

$ mysql.server status
ERROR! MySQL server PID file could not be found!

The PID file could not be found error is usually caused by two things:

  • MySQL server is not running, so no PID file was found
  • A mismatch between the running MySQL instance PID file location and the mysql.server command target location

This tutorial will help you understand both causes and how to fix them

MySQL server is not running, so no PID file was found

The MySQL PID file is a process identification file that stores the Process ID number of the running MySQL instance.

Each time you issue a command to mysql.server, MySQL would look for the PID file to find the Process ID and forward the command to the right process number.

When you start the MySQL server, the PID file is automatically generated by MySQL. When you want to stop the server, then MySQL needs to find the file to terminate the right PID from your computer.

When you run the mysql.server stop command when there’s no running MySQL instance, the response would be there’s no PID file found:

$ mysql.server stop
ERROR! MySQL server PID file could not be found!

This is why before attempting any other fix, you should make sure that the MySQL instance is running on your computer.

Depending on how you installed MySQL on your computer, you can use one of the following commands to start MySQL services:

  • mysql.service start - for MySQL installed using package managers
  • brew services start mysql - for MySQL installed with Homebrew
  • Using the preference pane for macOS official MySQL installation
  • Using the control panel for XAMPP, MAMP, or WAMP
  • Run the Windows service for MySQL from the services panel

Once you make sure that MySQL server is running, you can try the command that produces the PID file error.

If the error still appears, then it’s possible the PID file mismatch occurs on your computer. Let’s learn how to fix that next.

A MySQL service can be started from many ways: you can run the service from the Terminal or from a UI panel.

Each time you start MySQL service, the PID file is generated in a specific location depending on the configuration MySQL used.

The PID file location mismatch happens when you generate a PID file on one location, but the command you currently run seeks the PID file on another location.

In my local MySQL server, I started MySQL server using the MySQL Preference Pane that comes bundled with MySQL server installation file for Mac:

When I started the server using the Preference Pane, MySQL generates a PID file named mysqld.local.pid in the /usr/local/mysql/data/ directory.

When I tried to stop the server using mysql.server stop command, MySQL tries to find the nts-mac.pid file, which is generated if MySQL is started from the Terminal using mysql.server start command (nts-mac is the name of my Mac computer)

To fix the error, I needed to stop the currently running MySQL thread from the Preferences Pane before running the mysql.server start command again.

You may not be using MySQL Preferences Pane, but it’s possible that you have a MySQL process running in your computer or Linux server that uses a different PID file name or location than the one your command is trying to find.

If you don’t know how to stop MySQL server, then you can try to run the ps -e | grep mysql command from your terminal.

Here’s the output from my computer:

$ ps -e | grep mysql 
24836 ttys001    0:00.03 /bin/sh /usr/local/mysql/bin/mysqld_safe
24920 ttys001    0:01.04 /usr/local/mysql/bin/mysqld
24946 ttys001    0:00.01 grep mysql

The result tells me that there are running mysqld processes on my computer. If you found the same in your computer, then you need to kill the running process using one by one using the kill -9 command.

The PID code number is the first number on the left, so in my case, they are 24836 and 24920:

$ kill -9 24836
$ kill -9 24920

With that, there should be no running MySQL process on your computer. You can now try to start the MySQL server again:

$ mysql.server start
Starting MySQL
 SUCCESS!

Now that MySQL server is started from the command line, stopping the server using mysql.server stop shouldn’t cause any issue.

And those are the two ways you can fix MySQL server PID could not be found error.

If you still encounter the error, then you may need to reinstall your MySQL server. Please make sure that you save a backup of your MySQL database before you attempt to reinstall the program.

You can visit my tutorial on copying MySQL database to save a backup of your MySQL server.

I hope this tutorial helps you 👍

How do I get MySQL pid?

Login to DB; Run a command show full processlist; to get the process id with status and query itself which causes the database hanging; Select the process id and run a command KILL ; to kill that process.

Where is MySQL pid located?

pid in the /usr/local/mysql/data/ directory.

How do I find MySQL server?

Press Win+R. Type services. msc. Find MySQL service in the list, it is usually named as MySQL [version number], for example, MySQL 80.

How do I know if MySQL daemon is running?

We check the status with the systemctl status mysql command. We use the mysqladmin tool to check if MySQL server is running. The -u option specifies the user which pings the server. The -p option is a password for the user.