Can you query on a db "localhost" "root" error?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Can you query on a db "localhost" "root" error?

Post by simonmlewis »

We need to monitor a web site, to see if the database goes down.
Generally that side goes down, and a few minutes later the SQL side has been crashing.

It's stable now, but I want a monitoring script.

The error which is like this:

Code: Select all

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' 
Is there a means to query if that is showing, and then report back?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Can you query on a db "localhost" "root" error?

Post by Eric! »

You can check the error logs on the server.

The best choice is to define the error log with log-error, in both the [mysqld_safe] and [mysqld] section of your servers my.cnf[text][mysqld_safe]
log-error=/var/log/mysql/error.log

[mysqld]
log-error=/var/log/mysql/error.log
[/text]

Via PHP you can also check the error status after each query and if there is a connection error during establishment of a connection.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you query on a db "localhost" "root" error?

Post by simonmlewis »

I don't think either of those will help.
We had an issue where it wouldn't even connect to the database, and those would produce errors, but I am looking at something that would do so right at the "database connectin" point.

Code: Select all

<?php $sqlconn=@mysql_connect("localhost","root","");
$rs=@mysql_select_db("site",$sqlconn); 
?>
I use one dbconn file for this site, so if we need to make password changes, it's easy. Is there something I can put in here, that if the DB is producing that error, or it simply won't connect because the DB is out of operation (we had an error 500), it can trigger something?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Can you query on a db "localhost" "root" error?

Post by Eric! »

I would recommend you not use mysql and at least use mysqli. One step better is PDO. Where you could easily trap that problem with an exception, as well as take advantage of prepared statements and data abstraction.

Code: Select all

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Could not connect : ' . $e->getMessage(); // or do something smarter
}
But here is a quick hack if you want to keep doing it the old way.

Code: Select all

    $connect = @mysql_connect(HOST, USER, PASS);
    if(!$connect) { 
        echo 'Server error. Please try again later.'; 
        exit; //do something smarter than this
    }
    $select_db = @mysql_select_db(DATABASE);
    if(!$select_db) { 
         echo 'Server error. Please try again later.'; 
         exit;//ditto
    }
FYI -- I just read your title again. Your applications should not be accessing your database via the root account. Make a user with much more limited access for your applications to log in as. Especially if you are not using data abstraction and prepared statements because you're vulnerable to someone hacking your database and gaining access.
Post Reply