PHP, MySQL and crontab

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sandman

    PHP, MySQL and crontab

    I have a PHP cli script, that backups my databases, using mysqldump
    via system().

    When logged in with SSH, and launching the script with "./backup.php",
    all works just as it should, but when I put it in a crontab:

    00 2 * * * /home/sandman/bin/backup.php

    I get a mail that says:

    "mysqldump: Got error: 2002: Can't connect to local MySQL server
    through socket '/var/run/mysqld/mysqld.sock' (2) when trying to
    connect"

    How come? Anyone knows what the difference is between CLI and crontab
    that could cause this?

    --
    Sandman[.net]
  • Jerry Stuckle

    #2
    Re: PHP, MySQL and crontab

    Sandman wrote:
    I have a PHP cli script, that backups my databases, using mysqldump
    via system().
    >
    When logged in with SSH, and launching the script with "./backup.php",
    all works just as it should, but when I put it in a crontab:
    >
    00 2 * * * /home/sandman/bin/backup.php
    >
    I get a mail that says:
    >
    "mysqldump: Got error: 2002: Can't connect to local MySQL server
    through socket '/var/run/mysqld/mysqld.sock' (2) when trying to
    connect"
    >
    How come? Anyone knows what the difference is between CLI and crontab
    that could cause this?
    >
    Well, for one thing, what's the user you have set up for your cron job?
    Does it have permission to use the socket? Is it using the correct
    socket?

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Sandman

      #3
      Re: PHP, MySQL and crontab

      In article <9fednTNF__IlLm PZnZ2dnUVZ_sOdn Z2d@comcast.com >,
      Jerry Stuckle <jstucklex@attg lobal.netwrote:
      I have a PHP cli script, that backups my databases, using mysqldump
      via system().

      When logged in with SSH, and launching the script with "./backup.php",
      all works just as it should, but when I put it in a crontab:

      00 2 * * * /home/sandman/bin/backup.php

      I get a mail that says:

      "mysqldump: Got error: 2002: Can't connect to local MySQL server
      through socket '/var/run/mysqld/mysqld.sock' (2) when trying to
      connect"

      How come? Anyone knows what the difference is between CLI and crontab
      that could cause this?
      >
      Well, for one thing, what's the user you have set up for your cron job?
      The same user I'm executing it with when it's working.
      Does it have permission to use the socket? Is it using the correct
      socket?
      Well, after looking, the socket "/var/run/mysqld/mysqld.sock" doesn't
      seem to exist. I realize I should have checked that first, but since
      it worked before, I assumed that wasn't the problem.

      There DOES exist a /tmp/mysql.sock file, and I have now set up my PHP
      script with "mysqldump -S /tmp/mysql.sock" and I'll see in the morning
      if it throws the same errors.

      But how come mysqldump finds a correct socket when I'm running it from
      the shell but not when I'm running it through crontab?


      --
      Sandman[.net]

      Comment

      • Jerry Stuckle

        #4
        Re: PHP, MySQL and crontab

        Sandman wrote:
        In article <9fednTNF__IlLm PZnZ2dnUVZ_sOdn Z2d@comcast.com >,
        Jerry Stuckle <jstucklex@attg lobal.netwrote:
        >
        >
        >>>I have a PHP cli script, that backups my databases, using mysqldump
        >>>via system().
        >>>
        >>>When logged in with SSH, and launching the script with "./backup.php",
        >>>all works just as it should, but when I put it in a crontab:
        >>>
        >>>00 2 * * * /home/sandman/bin/backup.php
        >>>
        >>>I get a mail that says:
        >>>
        >>>"mysqldump : Got error: 2002: Can't connect to local MySQL server
        >>>through socket '/var/run/mysqld/mysqld.sock' (2) when trying to
        >>>connect"
        >>>
        >>>How come? Anyone knows what the difference is between CLI and crontab
        >>>that could cause this?
        >>>
        >>
        >>Well, for one thing, what's the user you have set up for your cron job?
        >
        >
        The same user I'm executing it with when it's working.
        >
        >
        >>Does it have permission to use the socket? Is it using the correct
        >>socket?
        >
        >
        Well, after looking, the socket "/var/run/mysqld/mysqld.sock" doesn't
        seem to exist. I realize I should have checked that first, but since
        it worked before, I assumed that wasn't the problem.
        >
        There DOES exist a /tmp/mysql.sock file, and I have now set up my PHP
        script with "mysqldump -S /tmp/mysql.sock" and I'll see in the morning
        if it throws the same errors.
        >
        But how come mysqldump finds a correct socket when I'm running it from
        the shell but not when I'm running it through crontab?
        >
        >
        Is it possible you have two copies of mysqldump on your system? The
        cron job is probably running as a different user - and therefore would
        have a different profile, among other things.

        Or maybe it's picking up a different my.ini file.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        Working...