Page 1 of 1
Can you query on a db "localhost" "root" error?
Posted: Sun May 26, 2013 10:46 am
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?
Re: Can you query on a db "localhost" "root" error?
Posted: Sun May 26, 2013 11:51 am
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.
Re: Can you query on a db "localhost" "root" error?
Posted: Sun May 26, 2013 12:12 pm
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?
Re: Can you query on a db "localhost" "root" error?
Posted: Mon May 27, 2013 1:08 pm
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.