Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Bash check if MySQL is installed
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Bash check if MySQL is installed

DennisDDennisD Member
edited October 2014 in Help

I'm writing a simple bash script and I need to check whether MySQL is installed or not.

I tried "if [[ -n $(which mysql-server) ]]; then" but that doesn't seem to work. Anyone an idea?

Comments

  • mysqld

  • +1 to @ricardo

    It would probably be a good idea to use the full path to mysqld too.

  • I've used

    type mysql &> /dev/null && echo "installed" || echo "not installed"
  • DennisDDennisD Member
    edited October 2014

    @CharlesA said:
    +1 to ricardo

    It would probably be a good idea to use the full path to mysqld too.

    Can you please post a code snippet? Don't get it to work.

  • which is not recommended because of OS dependent exit status (it may not always work). type is prefered in Bash.

    For example:

    if type mysql >/dev/null 2>&1; then
        echo "Dumping databases..." >>$LOGLOC
        USER="root"
        # ....
    else
        echo "Skipping Database dump." >>$LOGLOC
    fi
    
  • @DennisD said:

    It should exist here, at least on Debian: /usr/sbin/mysqld

    Silvenga has a good idea too.

  • DennisDDennisD Member
    edited October 2014

    I'm now trying to check the MySQL connection, it works fine, but how do I stop the command from outputting "MySQL root password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)" when a wrong password is entered.

    http://pastebin.com/Ggu1GgSh

  • Got it, thanks for helping everyone!

  • hostnoobhostnoob Member
    edited October 2014

    @DennisD said:
    I'm now trying to check the MySQL connection, it works fine, but how do I stop the command from outputting "MySQL root password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)" when a wrong password is entered.

    http://pastebin.com/Ggu1GgSh

    I just use fsockopen to check port 3306 (or custom MySQL port if used) not sure what the Bash alternative is

  • Can you explain what you exactly want to achieve? How do you define installed? Relying on the path, like some mentioned, might help in many cases but not in all. There could be a MySQL instance sitting somewhere quietly in a directory and you'd never find it via the path.

    hostnoob said: I just use fsockopen to check port 3306 (or custom MySQL port if used)

    This works if you are in a familiar environment. It wouldnt work as generic way though. Something else could be listening on 3306, it could be on some random port or on no port at all.

    Thanked by 1natestamm
  • hostnoob said: not sure what the Bash alternative is

    Maybe check if the Unix socket file exists?

    alessio said: Something else could be listening on 3306

    Although this is probably overkill, we could check what process is using that port using something likenetstat. :D

  • Silvenga said: Although this is probably overkill, we could check what process is using that port using something likenetstat. :D

    I think on high-load, netstat will be worser than try to connect to port.

  • Silvenga said: Although this is probably overkill, we could check what process is using that port using something likenetstat. :D

    Checking for the port would be still a problem if it uses the domain socket. However using netstat might be actually the most promising approach, given the script executes under root, nobody had the funny idea of renaming the executable and you check for the listeners and domain sockets.

  • Profforg said: I think on high-load, netstat will be worser than try to connect to port.

    Why? Netstat is basically a data parser and it would only be used to ensure that the socket is owned by a mysql binary.

  • @alessio said:
    Can you explain what you exactly want to achieve? How do you define installed? Relying on the path, like some mentioned, might help in many cases but not in all. There could be a MySQL instance sitting somewhere quietly in a directory and you'd never find it via the path.

    This works if you are in a familiar environment. It wouldnt work as generic way though. Something else could be listening on 3306, it could be on some random port or on no port at all.

    It's part of a script that has a config file for the user to set the services to be monitored and which ports they're running on

  • @hostnoob said:
    It's part of a script that has a config file for the user to set the services to be monitored and which ports they're running on

    So an automatic pre-selection of services which the user can change and modify? In this case it probably doesnt need to be foolproof and they easiest way might be the which/type path.

Sign In or Register to comment.