我在重新启动旧程序时遇到了一些问题。我们有此服务器的映像,设置是 Ubuntu 10.04、mysql 数据库的 5gb 分区和 100gb 文件系统。我的问题是,每次调用这个应该创建与数据库连接的函数时,它都会抛出一个错误,提示数据库已关闭。
static function createConnection($dbOptions = null)
{
$dsn = array(
'phptype' => CATAPULT_DB_TYPE,
'username' => CATAPULT_DB_USERNAME,
'password' => CATAPULT_DB_PASSWORD,
'hostspec' => CATAPULT_DB_HOST,
'database' => CATAPULT_DB_DATABASE
);
$options = array();
if ($dbOptions != null)
{
if (array_key_exists('username', $dbOptions))
{
$dsn['username'] = $dbOptions['username'];
}
if (array_key_exists('password', $dbOptions))
{
$dsn['password'] = $dbOptions['password'];
}
if (array_key_exists('host', $dbOptions))
{
$dsn['hostspec'] = $dbOptions['hostspec'];
}
if (array_key_exists('database', $dbOptions))
{
$dsn['database'] = $dbOptions['database'];
}
if (array_key_exists('persistent', $dbOptions))
{
if ($dbOptions['persistent'])
{
$options['persistent'] = $dbOptions['persistent'];
}
}
if (array_key_exists('debug', $dbOptions))
{
$options['debug'] = $dbOptions;
}
if (array_key_exists('portability',$dbOptions))
{
$options['portability'] = $dbOptions;
}
}
else
{
$options['debug'] = 0;
$options['portability'] = MDB2_PORTABILITY_ALL;
}
// uses MDB2::factory() to create the instance
// and also attempts to connect to the host
//
// for some reason I was using connect rather than singleton... not sure why that was
// this was creating many duplicate database connections when I was dealing with transactions
//$mdb2 = MDB2::connect($dsn, $options);
$mdb2 = MDB2::singleton($dsn, $options);
if (PEAR::isError($mdb2))
{
$logger = Logger::getFileLogger('dbConnect.php:createConnection');
// setup email configuration
$conf = array('subject' => 'CRITICAL ERROR: SYSTEM DATABASE CONNECTION LOST');
$loggerEmail = Logger::getEmailLogger('dbConnect.php:createConnection');
// error's here should not only be logged but emailed as well
// TODO: log the error
$log_message = "Failed to connect to database, verify connection settings.";
if ($mdb2 != null)
{
$log_message .= ' Additional info: -- ' . $mdb2->getMessage();
}
$logger->log($log_message);
$loggerEmail->log($log_message);
return (NULL);
}
else
{
return ($mdb2);
}
再次说明,这是遗留代码,而且我并不是一名数据库专家,但我已经检查了用户的权限,它显示 GRANT ALL PRIVILEGES ON 'dbName'.* TO 'user'@'localhost'
/var/log/apache2/error.log 的输出
sh: /usr/sbin/sendmail: not found
它重复了 52 次。
对于故障排除有什么想法吗?
答案1
尝试通过命令行连接,看看是 PHP 问题还是 MySQL 问题:
mysql -u USERNAME -pPASSWORD -h 127.0.0.1 DBNAME
答案2
如果您查看 php.ini 文件,我想您会发现它被配置为通过电子邮件发送错误;但是它没有做到这一点(这可以解释为什么您看不到任何有用的错误,以及为什么您会收到 sendmail 错误消息)。
我建议至少暂时重新配置该文件,以便将其写入某个错误日志,以便您可以查看。根据您在此处发布的内容,我猜只有几个配置选项,因此一旦您知道错误,应该很容易解决。由于您可以本地连接,我猜您最终会修复应用程序配置文件中的错误配置,无论它在哪里。