Fix. Install with create new database for PgSQL/MySQL

* MySQL/PgSQL ignore error while create new user because user may already exists.
* MySQL on database page use same checkbox like one for PgSQL
* MySQL/PgSQL if create user empty try use db user.(may be better use root/postgres without pass)
* PgSQL fix `GRANT ALL ON DATABASE dbname` instead of `GRANT ALL ON dbname`
* MySQL fix connect string build
* MySQL fix use correct password to connect
This commit is contained in:
Alexey Melnichuk 2016-03-30 15:05:25 +03:00
parent da0e54da44
commit b9f2e358af
2 changed files with 162 additions and 139 deletions

View File

@ -332,12 +332,20 @@ include "root.php";
if ($this->global_settings->db_create()) {
//attempt to create new Postgres role and database
$this->write_progress("\tCreating database");
try {
if (strlen($this->global_settings->db_host()) > 0) {
$this->dbh = new PDO("pgsql:host={$this->global_settings->db_host()} port={$this->global_settings->db_port()} user={$this->global_settings->db_create_username()} password={$this->global_settings->db_create_password()} dbname=template1");
} else {
$this->dbh = new PDO("pgsql:host=localhost port={$this->global_settings->db_port()} user={$this->global_settings->db_create_username()} password={$this->global_settings->db_create_password()} dbname=template1");
$db_create_username = $this->global_settings->db_create_username();
$db_create_password = $this->global_settings->db_create_password();
$db_host = $this->global_settings->db_host();
$db_port = $this->global_settings->db_port();
if(strlen($db_create_username) == 0){
$db_create_username = $this->global_settings->db_username();
$db_create_password = $this->global_settings->db_password();
}
if (strlen($db_host) == 0) {
$db_host = 'localhost';
}
try {
$this->dbh = new PDO("pgsql:host=$db_host port=$db_port user=$db_create_username password=$db_create_password dbname=template1");
} catch (PDOException $error) {
throw new Exception("error connecting to database in order to create: " . $error->getMessage());
}
@ -346,11 +354,12 @@ include "root.php";
if($this->dbh->exec("CREATE DATABASE {$this->global_settings->db_name()}") === false) {
throw new Exception("Failed to create database {$this->global_settings->db_name()}: " . join(":", $this->dbh->errorInfo()));
}
if($this->global_settings->db_username() != $this->global_settings->db_create_username()){
if($this->global_settings->db_username() != $db_create_username){
if($this->dbh->exec("CREATE USER {$this->global_settings->db_username()} WITH PASSWORD '{$this->global_settings->db_password()}'") === false){
throw new Exception("Failed to create user {$this->global_settings->db_name()}: " . join(":", $this->dbh->errorInfo()));
// user may be already exists
// throw new Exception("Failed to create user {$this->global_settings->db_name()}: " . join(":", $this->dbh->errorInfo()));
}
if($this->dbh->exec("GRANT ALL ON {$this->global_settings->db_name()} TO {$this->global_settings->db_username()}") === false){
if($this->dbh->exec("GRANT ALL ON DATABASE {$this->global_settings->db_name()} TO {$this->global_settings->db_username()}") === false){
throw new Exception("Failed to create user {$this->global_settings->db_name()}: " . join(":", $this->dbh->errorInfo()));
}
}
@ -414,27 +423,38 @@ include "root.php";
$connect_string;
if (strlen($this->global_settings->db_host()) == 0 && strlen($this->global_settings->db_port()) == 0) {
//if both host and port are empty use the unix socket
$connect_string = "mysql:host=$this->global_settings->db_host();unix_socket=/var/run/mysqld/mysqld.sock;";
$connect_string = "mysql:host={$this->global_settings->db_host()};unix_socket=/var/run/mysqld/mysqld.sock;";
}
elseif (strlen($this->global_settings->db_port()) == 0) {
//leave out port if it is empty
$connect_string = "mysql:host=$this->global_settings->db_host();";
$connect_string = "mysql:host={$this->global_settings->db_host()};";
}
else {
$connect_string = "mysql:host=$this->global_settings->db_host();port=$this->global_settings->db_port();";
$connect_string = "mysql:host={$this->global_settings->db_host()};port={$this->global_settings->db_port()};";
}
//create the table, user and set the permissions only if the db_create_username was provided
if ($this->global_settings->db_create_username()) {
//if we need create new database
if ($this->global_settings->db_create()) {
//attempt to create new user and database
$this->write_progress("\tCreating database");
$db_create_username = $this->global_settings->db_create_username();
$db_create_password = $this->global_settings->db_create_password();
if(strlen($db_create_username) == 0){
$db_create_username = $this->global_settings->db_username();
$db_create_password = $this->global_settings->db_password();
}
//connect to MySQL
try {
$this->dbh = new PDO($connect_string, $this->global_settings->db_create_username(), $this->global_settings->db_create_password(), array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$this->dbh = new PDO($connect_string, $db_create_username, $db_create_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
}
catch (PDOException $error) {
throw new Exception("error connecting to database for ccreate: " . $error->getMessage() . "\n" . $sql );
throw new Exception("error connecting to database for create: " . $error->getMessage() . "\n" . $sql );
}
//select the mysql database
try {
$this->dbh->query("USE mysql;");
@ -443,13 +463,17 @@ include "root.php";
throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql );
}
//create user if we use separeate user to access and create
if($this->global_settings->db_username() != $db_create_username) {
//create user and set the permissions
try {
$tmp_sql = "CREATE USER '".$this->global_settings->db_username()."'@'%' IDENTIFIED BY '".$this->global_settings->db_password()."'; ";
$this->dbh->query($tmp_sql);
}
catch (PDOException $error) {
throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql );
// ignore error here because user may already exists
// (e.g. reinstall can be done via remove db)
// throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql );
}
//set account to unlimited use
@ -475,6 +499,7 @@ include "root.php";
catch (PDOException $error) {
throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql );
}
}
//create the database and set the create user with permissions
try {
@ -486,12 +511,14 @@ include "root.php";
}
//set user permissions
if($this->global_settings->db_username() != $db_create_username) {
try {
$this->dbh->query("GRANT ALL PRIVILEGES ON ".$this->global_settings->db_name().".* TO '".$this->global_settings->db_username()."'@'%'; ");
}
catch (PDOException $error) {
throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql );
}
}
//make the changes active
try {
@ -502,19 +529,21 @@ include "root.php";
throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql );
}
$this->dbh = null;
} //if (strlen($this->global_settings->db_create_username()) > 0)
}
$this->write_progress("\tInstalling data to database");
//select the database
//connect to the database
try {
$this->dbh = new PDO($connect_string, $this->global_settings->db_username(), db_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$this->dbh = new PDO($connect_string, $this->global_settings->db_username(), $this->global_settings->db_password(), array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
}
catch (PDOException $error) {
throw new Exception("error connecting to database: " . $error->getMessage() . "\n" . $sql );
}
//select the database
try {
$this->dbh->query("USE ".$this->global_settings->db_name().";");
}

View File

@ -142,15 +142,9 @@
echo " Create Database Options\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
echo " <label class='radio'><input type='radio' name='create_db_option' value='none'";
if($db_create_option=='none') { echo " checked='checked'"; }
echo "/>Do not create database</label>\n";
echo " <label class='radio'><input type='radio' name='create_db_option' value='same'";
if($db_create_option=='same') { echo " checked='checked'"; }
echo "/>Create database using above username/password</label>\n";
echo " <label class='radio'><input type='radio' name='create_db_option' value='user'";
if($db_create_option=='user') { echo " checked='checked'"; }
echo "/>Create database using below username/password</label>\n";
if($db_create=='1') { $checked = "checked='checked'"; } else { $checked = ''; }
echo " <input type='checkbox' name='db_create' value='1' $checked />&nbsp;";
echo "Create the database\n";
echo "<br />\n";
echo "Choose whether to create the database\n";
echo "</td>\n";