Browse Source

#860 Allow use custom port database

Allow to use a different port then 3306 /  5432

For example when you to run a different version or are not able to use 3306  port
Jaap Marcus 5 years ago
parent
commit
237d637bae
2 changed files with 24 additions and 9 deletions
  1. 20 8
      bin/v-add-database-host
  2. 4 1
      func/db.sh

+ 20 - 8
bin/v-add-database-host

@@ -18,9 +18,10 @@ type=$1
 host=$2
 dbuser=$3
 password=$4; HIDE=4
-max_db=${6-500}
-charsets=${7-UTF8,LATIN1,WIN1250,WIN1251,WIN1252,WIN1256,WIN1258,KOI8}
-template=${8-template1}
+max_db=${5-500}
+charsets=${6}
+template=${7}
+port=${8}
 
 # Includes
 source $HESTIA/func/main.sh
@@ -33,6 +34,8 @@ is_mysql_host_alive() {
     echo "host='$HOST'" >> $mycnf
     echo "user='$USER'" >> $mycnf
     echo "password='$PASSWORD'" >> $mycnf
+    echo "port='$PORT'" >> $mycnf
+    
     chmod 600 $mycnf
     mysql --defaults-file=$mycnf -e 'SELECT VERSION()' >/dev/null 2>&1
     rm $mycnf
@@ -45,7 +48,7 @@ is_mysql_host_alive() {
 
 is_pgsql_host_alive() {
     export PGPASSWORD="$dbpass"
-    psql -h $host -U $dbuser -c "SELECT VERSION()" > /dev/null 2>&1
+    psql -h $host -U $dbuser -p $port -c "SELECT VERSION()" > /dev/null 2>&1
     if [ '0' -ne "$?" ]; then
         echo "Error: PostgreSQL connection to $host failed"
         log_event "$E_CONNECT" "$ARGUMENTS"
@@ -58,14 +61,23 @@ is_pgsql_host_alive() {
 #                    Verifications                         #
 #----------------------------------------------------------#
 
-args_usage='TYPE HOST DBUSER DBPASS [MAX_DB] [CHARSETS] [TPL]'
+args_usage='TYPE HOST DBUSER DBPASS [MAX_DB] [CHARSETS] [TPL] [PORT]'
 check_args '4' "$#" "$args_usage"
-is_format_valid 'host' 'dbuser' 'max_db' 'charsets' 'template'
+
+if [ -z $charsets ]; then charsets="UTF8,LATIN1,WIN1250,WIN1251,WIN1252,WIN1256,WIN1258,KOI8"; fi
+if [ -z $template ]; then template="template1"; fi
+if [ -z $port ]; then 
+    if [ $type = 'mysql' ]; then port="3306"; fi
+    if [ $type = 'pgsql' ]; then port="5432"; fi
+fi
+
+is_format_valid 'host' 'dbuser' 'max_db' 'charsets' 'template' 'port'
 #is_system_enabled "$DB_SYSTEM" 'DB_SYSTEM'
 #is_type_valid "$DB_SYSTEM" "$type"
 is_dbhost_new
 is_password_valid
 dbpass="$password"
+
 case $type in
     mysql) is_mysql_host_alive ;;
     pgsql) is_pgsql_host_alive ;;
@@ -85,11 +97,11 @@ date=$(echo "$time_n_date" |cut -f 2 -d \ )
 case $type in
     mysql) str="HOST='$host' USER='$dbuser' PASSWORD='$dbpass'";
            str="$str CHARSETS='$charsets' MAX_DB='$max_db' U_SYS_USERS=''";
-           str="$str U_DB_BASES='0' SUSPENDED='no' TIME='$time' DATE='$date'";;
+           str="$str U_DB_BASES='0' SUSPENDED='no' TIME='$time' DATE='$date' PORT='$port'";;
     pgsql) str="HOST='$host' USER='$dbuser' PASSWORD='$dbpass'";
            str="$str CHARSETS='$charsets' TPL='$template' MAX_DB='$max_db'";
            str="$str U_SYS_USERS='' U_DB_BASES='0' SUSPENDED='no'";
-           str="$str TIME='$time' DATE='$date'";;
+           str="$str TIME='$time' DATE='$date' PORT='$port";;
 esac
 
 

+ 4 - 1
func/db.sh

@@ -2,18 +2,19 @@
 mysql_connect() {
     host_str=$(grep "HOST='$1'" $HESTIA/conf/mysql.conf)
     parse_object_kv_list "$host_str"
+    if [ -z $PORT ]; then PORT=3306; fi
     if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ]; then
         echo "Error: mysql config parsing failed"
         log_event "$E_PARSING" "$ARGUMENTS"
         exit $E_PARSING
     fi
-
     mycnf="$HESTIA/conf/.mysql.$HOST"
     if [ ! -e "$mycnf" ]; then
         echo "[client]">$mycnf
         echo "host='$HOST'" >> $mycnf
         echo "user='$USER'" >> $mycnf
         echo "password='$PASSWORD'" >> $mycnf
+        echo "port='$PORT'" >> $mycnf
         chmod 600 $mycnf
     else
         mypw=$(grep password $mycnf|cut -f 2 -d \')
@@ -22,6 +23,7 @@ mysql_connect() {
             echo "host='$HOST'" >> $mycnf
             echo "user='$USER'" >> $mycnf
             echo "password='$PASSWORD'" >> $mycnf
+            echo "port='$PORT'" >> $mycnf
             chmod 660 $mycnf
         fi
     fi
@@ -73,6 +75,7 @@ psql_connect() {
     host_str=$(grep "HOST='$1'" $HESTIA/conf/pgsql.conf)
     parse_object_kv_list "$host_str"
     export PGPASSWORD="$PASSWORD"
+    if [ -z $PORT ]; then $PORT="5432"; fi
     if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
         echo "Error: postgresql config parsing failed"
         log_event "$E_PARSING" "$ARGUMENTS"