diff --git a/install/install.php b/install/install.php
index b9d2f0641eb01b2a0226f3e5f53026e3be2e1684..1b55a3dae6724dd3cf8b124f197c133c93ab6017 100644
--- a/install/install.php
+++ b/install/install.php
@@ -282,7 +282,7 @@ if($install_mode == 'expert' && strtolower($inst->simple_query('Shall this serve
 	// initialize the connection to the master database
 	$inst->dbmaster = new db();
 	if($inst->dbmaster->linkId) $inst->dbmaster->closeConn();
-	$inst->dbmaster->setDBData($conf['mysql']["master_host"], $conf['mysql']["master_admin_user"], $conf['mysql']["master_admin_password"]);
+	$inst->dbmaster->setDBData($conf['mysql']["master_host"], $conf['mysql']["master_admin_user"], $conf['mysql']["master_admin_password"], $conf['mysql']['master_port']);
 	$inst->dbmaster->setDBName($conf['mysql']["master_database"]);
 
 } else {
diff --git a/install/lib/mysql.lib.php b/install/lib/mysql.lib.php
index e3c12a6d3bb5d39c8a8a1d45334049615f56a328..bca8ac0f7549e854f22e0b156214e1def76fd462 100644
--- a/install/lib/mysql.lib.php
+++ b/install/lib/mysql.lib.php
@@ -36,6 +36,7 @@ class db
 	private $_iConnId;
 
 	private $dbHost = '';  // hostname of the MySQL server
+	private $dbPort = '';  // port of the MySQL server
 	private $dbName = '';  // logical database name on that server
 	private $dbUser = '';  // database authorized user
 	private $dbPass = '';  // user's password
@@ -68,7 +69,8 @@ class db
 		global $conf;
 		
 		if($this->_iConnId) return true;
-		$this->dbHost = $conf["mysql"]["host"];
+		$this->dbHost = $conf['mysql']['port'];
+		$this->dbPort = $conf[$prefix.'db_port'];
 		$this->dbName = false;//$conf["mysql"]["database"];
 		$this->dbUser = $conf["mysql"]["admin_user"];
 		$this->dbPass = $conf["mysql"]["admin_password"];
@@ -76,13 +78,13 @@ class db
 		$this->dbNewLink = false;
 		$this->dbClientFlags = null;
 		
-		$this->_iConnId = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPass);
+		$this->_iConnId = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPass, (int)$this->dbPort);
 		$try = 0;
 		while((!is_object($this->_iConnId) || mysqli_connect_error()) && $try < 5) {
 			if($try > 0) sleep(1);
 
 			$try++;
-			$this->_iConnId = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPass);
+			$this->_iConnId = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPass, (int)$this->dbPort);
 		}
 
 		if(!is_object($this->_iConnId) || mysqli_connect_error()) {
@@ -96,10 +98,11 @@ class db
 		$this->_setCharset();
 	}
 	
-	public function setDBData($host, $user, $password) {
+	public function setDBData($host, $user, $password, $port) {
 		$this->dbHost = $host;
 		$this->dbUser = $user;
 		$this->dbPass = $password;
+		$this->dbPort = $port;
 	}
 	
 	public function setDBName($name) {
@@ -201,7 +204,7 @@ class db
 			$try++;
 			$ok = mysqli_ping($this->_iConnId);
 			if(!$ok) {
-				if(!mysqli_connect($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName)) {
+				if(!mysqli_connect($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName, (int)$this->dbPort)) {
 					if($this->errorNumber == '111') {
 						// server is not available
 						if($try > 9) {
diff --git a/install/update.php b/install/update.php
index 230fa4db80e99d39a9a970a539fd3e6a0bffddf5..cae871128b6e40d944ed3e26d824324308cba497 100644
--- a/install/update.php
+++ b/install/update.php
@@ -162,6 +162,7 @@ if($conf['timezone'] == '{timezone}' or trim($conf['timezone']) == '') $conf['ti
 $conf['language_file_import_enabled'] = (isset($conf_old['language_file_import_enabled']))?$conf_old['language_file_import_enabled']:true;
 
 if(isset($conf_old["dbmaster_host"])) $conf["mysql"]["master_host"] = $conf_old["dbmaster_host"];
+if(isset($conf_old["dbmaster_port"])) $conf["mysql"]["master_host"] = $conf_old["dbmaster_port"];
 if(isset($conf_old["dbmaster_database"])) $conf["mysql"]["master_database"] = $conf_old["dbmaster_database"];
 if(isset($conf_old["dbmaster_user"])) $conf["mysql"]["master_ispconfig_user"] = $conf_old["dbmaster_user"];
 if(isset($conf_old["dbmaster_password"])) $conf["mysql"]["master_ispconfig_password"] = $conf_old["dbmaster_password"];
@@ -275,7 +276,7 @@ if($conf['mysql']['master_slave_setup'] == 'y') {
 	// initialize the connection to the master database
 	$inst->dbmaster = new db();
 	if($inst->dbmaster->linkId) $inst->dbmaster->closeConn();
-	$inst->dbmaster->setDBData($conf['mysql']["master_host"], $conf['mysql']["master_admin_user"], $conf['mysql']["master_admin_password"]);
+	$inst->dbmaster->setDBData($conf['mysql']["master_host"], $conf['mysql']["master_admin_user"], $conf['mysql']["master_admin_password"], $conf['mysql']["master_port"]);
 	$inst->dbmaster->setDBName($conf['mysql']["master_database"]);
 } else {
 	$inst->dbmaster = $inst->db;