Question – I have a CentOS Linux web hosting server and I need to check whether MySQL is running. How do I check this? I am getting the following error: can’t connect to MySQL server on localhost. Is there a way to debug this problem?
There are several things to check whether MySQL is already running on your machine.
First, check if the MySQL process is running using the ps command:
ps aux | grep mysql
This outputs:
root@server [/etc]# ps aux | grep mysql
root 3820 0.0 0.0 2280 568 pts/0 S+ 07:12 0:00 grep mysql
mysql 21618 9.7 12.4 424844 261236 ? Sl 03:26 22:06 /usr/sbin/mysqld –basedir=/ –datadir=/var/lib/mysql –user=mysql
root@server [/etc]#
This clearly shows in bold that the mysql proess is running.
Second, check if you can connect to it using the command mysqladmin:
If you are root, use the following command:
mysqladmin status
If you don’t have root access, use the following command:
mysqladmin -u databaseusername -p status
This command will output something like the following if MySQL is running:
root@server [/etc]# mysqladmin status
Uptime: 13765 Threads: 14 Questions: 1213893 Slow queries: 1 Opens: 446 Flush tables: 1 Open tables: 256 Queries per second avg: 88.186
If MySQL is not running, mysqladmin will output something like the following:
root@server [/etc]# mysqladmin status
mysqladmin: connect to server at ‘localhost’ failed
error: ‘Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)’
Check that mysqld is running and that the socket: ‘/var/lib/mysql/mysql.sock’ exists!
So, output of mysqladmin status will give a definite answer on whether the MySQL service is running.
Third, if you are using CentOS or Red Hat Linux servers, you can use the service command:
Use the following command to check the status of the service mysql:
service mysql status
This will output:
root@server [/etc]# service mysql status
SUCCESS! MySQL running (4467)
root@server [/etc]#
So I showed you three ways in which you can check whether MySQL server is running on your Linux machine. The first two commands (ps and mysqladmin) can be used on any Linux distribution like Ubuntu, CentOS, Fedora, Debian etc. As far as I know, the service command is applicable only to CentOS or Red Hat based systems. Play with the commands and let me know what you find in your comments!
Leave a Reply