How can I check if mysql is installed using a bash script?

14,680

Solution 1

type mysql >/dev/null 2>&1 && echo "MySQL present." || echo "MySQL not present."

Solution 2

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
    :
fi

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

Share:
14,680

Related videos on Youtube

Kit Sunde
Author by

Kit Sunde

CTO at Media Pop, hire us for DevOps and product development

Updated on September 18, 2022

Comments

  • Kit Sunde
    Kit Sunde over 1 year

    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" 
    fi
    

    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.

    • Admin
      Admin almost 12 years
      What exactly confuses you about the code you posted? What did you expect it to do, and what does it do instead?
    • Admin
      Admin almost 12 years
      which only returns regular files anyway. This test will always be false.
    • Admin
      Admin almost 12 years
      @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.
    • Admin
      Admin almost 12 years
      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.
    • Admin
      Admin almost 12 years
      Also, using which will fail if mysql is an alias.
  • Bram
    Bram almost 12 years
    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.
  • George M
    George M almost 12 years
    @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.
  • clerksx
    clerksx almost 12 years
    @Bram - I have absolutely no idea. I'm just providing an alternative for uther's answer.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 12 years
    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.