mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
feature: support non-standard unix socket (#5724)
* Add support for custom MySQL unix-socket * NULL must be lowercase! * Naive edit of html/install.php * fixup * Refactor dbConnect Use it everywhere * $config needs to be global Don't need to set $database_link * small cleanups
This commit is contained in:
committed by
Neil Lathwood
parent
66d9d54f73
commit
b1a414e785
@@ -1,51 +0,0 @@
|
||||
<?php
|
||||
|
||||
if (!isset($sql_file)) {
|
||||
$sql_file = 'build.sql';
|
||||
}
|
||||
|
||||
$sql_fh = fopen($sql_file, 'r');
|
||||
if ($sql_fh === false) {
|
||||
echo 'ERROR: Cannot open SQL build script '.$sql_file."\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$database_link = mysqli_connect('p:'.$config['db_host'], $config['db_user'], $config['db_pass']);
|
||||
if ($database_link === false) {
|
||||
echo 'ERROR: Cannot connect to database: '.mysqli_error($database_link)."\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$select = mysqli_select_db($database_link, $config['db_name']);
|
||||
if ($select === false) {
|
||||
echo 'ERROR: Cannot select database: '.mysqli_error($database_link)."\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$limit = 0;
|
||||
while (!feof($sql_fh)) {
|
||||
$line = fgetss($sql_fh);
|
||||
if (isset($_SESSION['stage'])) {
|
||||
$limit++;
|
||||
if (isset($_SESSION['offset']) && $limit < $_REQUEST['offset']) {
|
||||
continue;
|
||||
} elseif (time()-$_SESSION['last'] > 45) {
|
||||
$_SESSION['offset'] = $limit;
|
||||
$GLOBALS['refresh'] = '<b>Installing, please wait..</b><sub>'.date('r').'</sub><script>window.location.href = "install.php?offset='.$limit.'";</script>';
|
||||
return;
|
||||
} else {
|
||||
echo 'Step #'.$limit.' ...'.PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($line)) {
|
||||
$creation = mysqli_query($database_link, $line);
|
||||
if (!$creation) {
|
||||
echo 'WARNING: Cannot execute query ('.$line.'): '.mysqli_error($database_link)."\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose($sql_fh);
|
||||
|
||||
require 'includes/sql-schema/update.php';
|
@@ -17,6 +17,58 @@
|
||||
* 3. Oh, and dbFetchAll() is now dbFetchRows()
|
||||
*/
|
||||
|
||||
use LibreNMS\Exceptions\DatabaseConnectException;
|
||||
|
||||
/**
|
||||
* Connect to the database.
|
||||
* Will use global $config variables if they are not sent: db_host, db_user, db_pass, db_name, db_port, db_socket
|
||||
*
|
||||
* @param string $host
|
||||
* @param string $user
|
||||
* @param string $password
|
||||
* @param string $database
|
||||
* @param string $port
|
||||
* @param string $socket
|
||||
* @return mysqli
|
||||
* @throws DatabaseConnectException
|
||||
*/
|
||||
function dbConnect($host = null, $user = '', $password = '', $database = '', $port = null, $socket = null)
|
||||
{
|
||||
global $config, $database_link;
|
||||
$host = empty($host) ? $config['db_host'] : $host;
|
||||
$user = empty($user) ? $config['db_user'] : $user;
|
||||
$password = empty($password) ? $config['db_pass'] : $password;
|
||||
$database = empty($database) ? $config['db_name'] : $database;
|
||||
$port = empty($port) ? $config['db_port'] : $port;
|
||||
$socket = empty($socket) ? $config['db_socket'] : $socket;
|
||||
|
||||
$database_link = mysqli_connect('p:' . $host, $user, $password, null, $port, $socket);
|
||||
if ($database_link === false) {
|
||||
$error = mysqli_connect_error();
|
||||
if ($error == 'No such file or directory') {
|
||||
$error = 'Could not connect to ' . $host;
|
||||
}
|
||||
throw new DatabaseConnectException($error);
|
||||
}
|
||||
|
||||
$database_db = mysqli_select_db($database_link, $config['db_name']);
|
||||
if (!$database_db) {
|
||||
$db_create_sql = "CREATE DATABASE " . $config['db_name'] . " CHARACTER SET utf8 COLLATE utf8_unicode_ci";
|
||||
mysqli_query($database_link, $db_create_sql);
|
||||
$database_db = mysqli_select_db($database_link, $config['db_name']);
|
||||
}
|
||||
|
||||
if (!$database_db) {
|
||||
throw new DatabaseConnectException("Could not select database: $database. " . mysqli_error($database_link));
|
||||
}
|
||||
|
||||
dbQuery("SET NAMES 'utf8'");
|
||||
dbQuery("SET CHARACTER SET 'utf8'");
|
||||
dbQuery("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
|
||||
|
||||
return $database_link;
|
||||
}
|
||||
|
||||
/*
|
||||
* Performs a query using the given string.
|
||||
* Used by the other _query functions.
|
||||
|
@@ -36,6 +36,7 @@ $config['mysql_log_level'] = 'ERROR';
|
||||
|
||||
//MySQL port
|
||||
$config['db_port'] = 3306;
|
||||
$config['db_socket'] = null;
|
||||
|
||||
// What is my own hostname (used to identify this host in its own database)
|
||||
$config['own_hostname'] = 'localhost';
|
||||
|
@@ -27,6 +27,8 @@
|
||||
* @param array $modules Which modules to initialize
|
||||
*/
|
||||
|
||||
global $config;
|
||||
|
||||
$install_dir = realpath(__DIR__ . '/..');
|
||||
$config['install_dir'] = $install_dir;
|
||||
chdir($install_dir);
|
||||
@@ -103,20 +105,16 @@ if ($config['memcached']['enable'] === true) {
|
||||
|
||||
if (!module_selected('nodb', $init_modules)) {
|
||||
// Connect to database
|
||||
$database_link = mysqli_connect('p:' . $config['db_host'], $config['db_user'], $config['db_pass'], null, $config['db_port']);
|
||||
if (!$database_link) {
|
||||
echo '<h2>MySQL Error</h2>';
|
||||
echo mysqli_connect_error();
|
||||
die;
|
||||
try {
|
||||
dbConnect();
|
||||
} catch (\LibreNMS\Exceptions\DatabaseConnectException $e) {
|
||||
if (isCli()) {
|
||||
echo 'MySQL Error: ' . $e->getMessage() . PHP_EOL;
|
||||
} else {
|
||||
echo "<h2>MySQL Error</h2><p>" . $e->getMessage() . "</p>";
|
||||
}
|
||||
exit(2);
|
||||
}
|
||||
$database_db = mysqli_select_db($database_link, $config['db_name']);
|
||||
if (!$database_db) {
|
||||
mysqli_query($database_link, "CREATE DATABASE " . $config['db_name'] . " CHARACTER SET utf8 COLLATE utf8_unicode_ci");
|
||||
$database_db = mysqli_select_db($database_link, $config['db_name']);
|
||||
}
|
||||
dbQuery("SET NAMES 'utf8'");
|
||||
dbQuery("SET CHARACTER SET 'utf8'");
|
||||
dbQuery("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
|
||||
|
||||
// pull in the database config settings
|
||||
mergedb();
|
||||
|
Reference in New Issue
Block a user