Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I need to check if mysql is installed from within a bash script. What would be an efficient way of doing that? I thought I could do this:

if [ ! -f `which mysql` ] ; then
    echo "foo" 

But I think I might be confused about something. What would be a better solution? It should be portable and work on both Ubuntu and OSX.

share|improve this question
What exactly confuses you about the code you posted? What did you expect it to do, and what does it do instead? –  kopischke May 26 '12 at 19:47
which only returns regular files anyway. This test will always be false. –  Chris Down May 26 '12 at 20:16
@kopischke I expected which mysql to give back the path to mysql that then is checked if it's a file or not. But it seems to just not work. –  Kit Sunde May 26 '12 at 20:19
This will also potentially break in any shell where [ is an external command, as if which does not return anything, -f will be missing an argument. –  Chris Down May 26 '12 at 20:24
Also, using which will fail if mysql is an alias. –  uther May 26 '12 at 21:25

4 Answers 4

up vote 4 down vote accepted
type mysql >/dev/null 2>&1 && echo "MySQL present." || echo "MySQL not present."
share|improve this answer
What is the problem with searching only in $PATH? If it is installed in a non-standard location you need to know that location to run it anyway. And if you know the non-standard location it probably doesn't make much sense to run a script that checks if it is installed or not. –  Bram May 26 '12 at 21:16
@Bram Being installed does not implicitly mean it's going to be in a standard location. What if, as root, I want to know how many mysqld binaries users have compiled and installed on the system? It could be in /opt (not usually considered standard), could be under some user /home directory, could be in a subdirectory under /usr/local. There could also be multiple versions of mysqld installed. Assuming the binary is going to be in $PATH or it's irrelevant is not, in my opinion, a good view to have. –  uther May 26 '12 at 21:36
@Bram - I have absolutely no idea. I'm just providing an alternative for uther's answer. –  Chris Down May 26 '12 at 22:14
I do not recommend running find over the entire filesystem. It's rude. You may be traversing external drives, network mounts, … If mysql isn't in the path, it might as well not be installed. There might be a non-working installation of mysql that's not in the path on purpose. –  Gilles May 26 '12 at 23:47

If you exclusively use your Ubuntu package manager for MySQL installation, then you can just use the dpkg front-end to dpkg-query to find out whether the package is installed.

# Look up package with dpkg-query.
if dpkg --list mysql-client | egrep -q ^ii; then

This is fast and reliable, but obviously only works when you use your Ubuntu system packages.

share|improve this answer

I wrote a Rightscale script and needed to know if mysql-server was installed. I used:

if [ -f /etc/init.d/mysql* ]; then
    echo "installed"
    echo "not installed"
share|improve this answer

Your script will fail if mysql is not in the script PATH.

Try using find.

Note: This is only one way to do this. There are other examples that may be more efficient or more to your liking. One of the strengths of this site is the differing opinions and approaches to achieve the same result.

for file in $(find / -executable -type f -name mysql -print 2>/dev/null)
   echo "Found $file" 
   "$file" -V

This will return

Found /usr/bin/mysql
/usr/bin/mysql Ver 14.14 Distrib 5.5.23, for Linux (x86_64) using readline 5.1

Note that if you have a large filesystem and a lot of files, this may take a long time.

Realize that as per your request, this will see if the mysql client is installed, not the mysqld server. It's possible to install the client without the server. If you want to see if mysqld is installed, modify the find and remove the version test.

share|improve this answer
This will break on paths with whitespace. Why are you testing outside of find? Just use -type f. Note that there is pretty much no reason that mysql should exist outside of your $PATH. Also, this will potentially take a long time to execute if you have a lot of files. –  Chris Down May 26 '12 at 20:18
If you use this be aware that this may take a long time –  Ulrich Dangel May 26 '12 at 20:19
The find returns all files and directories named mysql. The test ensures it's a regular file (instead of a directory). I don't think relying on the PATH is optimal in cases of non-standard installs. –  uther May 26 '12 at 20:21
@uther I understand that, my comment is that it's a very inefficient, after-the-fact way to test (instead of just using find's -type f). –  Chris Down May 26 '12 at 20:22
@ChrisDown Good point. However, I still want to see just executable files, not files such as /etc/logrotate.d/mysqld. –  uther May 26 '12 at 20:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.