Skip to content

Commit 237d637

Browse files
jaapmarcusKristan Kenney
authored andcommitted
hestiacp#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
1 parent 8c3d792 commit 237d637

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

bin/v-add-database-host

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ type=$1
1818
host=$2
1919
dbuser=$3
2020
password=$4; HIDE=4
21-
max_db=${6-500}
22-
charsets=${7-UTF8,LATIN1,WIN1250,WIN1251,WIN1252,WIN1256,WIN1258,KOI8}
23-
template=${8-template1}
21+
max_db=${5-500}
22+
charsets=${6}
23+
template=${7}
24+
port=${8}
2425

2526
# Includes
2627
source $HESTIA/func/main.sh
@@ -33,6 +34,8 @@ is_mysql_host_alive() {
3334
echo "host='$HOST'" >> $mycnf
3435
echo "user='$USER'" >> $mycnf
3536
echo "password='$PASSWORD'" >> $mycnf
37+
echo "port='$PORT'" >> $mycnf
38+
3639
chmod 600 $mycnf
3740
mysql --defaults-file=$mycnf -e 'SELECT VERSION()' >/dev/null 2>&1
3841
rm $mycnf
@@ -45,7 +48,7 @@ is_mysql_host_alive() {
4548

4649
is_pgsql_host_alive() {
4750
export PGPASSWORD="$dbpass"
48-
psql -h $host -U $dbuser -c "SELECT VERSION()" > /dev/null 2>&1
51+
psql -h $host -U $dbuser -p $port -c "SELECT VERSION()" > /dev/null 2>&1
4952
if [ '0' -ne "$?" ]; then
5053
echo "Error: PostgreSQL connection to $host failed"
5154
log_event "$E_CONNECT" "$ARGUMENTS"
@@ -58,14 +61,23 @@ is_pgsql_host_alive() {
5861
# Verifications #
5962
#----------------------------------------------------------#
6063

61-
args_usage='TYPE HOST DBUSER DBPASS [MAX_DB] [CHARSETS] [TPL]'
64+
args_usage='TYPE HOST DBUSER DBPASS [MAX_DB] [CHARSETS] [TPL] [PORT]'
6265
check_args '4' "$#" "$args_usage"
63-
is_format_valid 'host' 'dbuser' 'max_db' 'charsets' 'template'
66+
67+
if [ -z $charsets ]; then charsets="UTF8,LATIN1,WIN1250,WIN1251,WIN1252,WIN1256,WIN1258,KOI8"; fi
68+
if [ -z $template ]; then template="template1"; fi
69+
if [ -z $port ]; then
70+
if [ $type = 'mysql' ]; then port="3306"; fi
71+
if [ $type = 'pgsql' ]; then port="5432"; fi
72+
fi
73+
74+
is_format_valid 'host' 'dbuser' 'max_db' 'charsets' 'template' 'port'
6475
#is_system_enabled "$DB_SYSTEM" 'DB_SYSTEM'
6576
#is_type_valid "$DB_SYSTEM" "$type"
6677
is_dbhost_new
6778
is_password_valid
6879
dbpass="$password"
80+
6981
case $type in
7082
mysql) is_mysql_host_alive ;;
7183
pgsql) is_pgsql_host_alive ;;
@@ -85,11 +97,11 @@ date=$(echo "$time_n_date" |cut -f 2 -d \ )
8597
case $type in
8698
mysql) str="HOST='$host' USER='$dbuser' PASSWORD='$dbpass'";
8799
str="$str CHARSETS='$charsets' MAX_DB='$max_db' U_SYS_USERS=''";
88-
str="$str U_DB_BASES='0' SUSPENDED='no' TIME='$time' DATE='$date'";;
100+
str="$str U_DB_BASES='0' SUSPENDED='no' TIME='$time' DATE='$date' PORT='$port'";;
89101
pgsql) str="HOST='$host' USER='$dbuser' PASSWORD='$dbpass'";
90102
str="$str CHARSETS='$charsets' TPL='$template' MAX_DB='$max_db'";
91103
str="$str U_SYS_USERS='' U_DB_BASES='0' SUSPENDED='no'";
92-
str="$str TIME='$time' DATE='$date'";;
104+
str="$str TIME='$time' DATE='$date' PORT='$port";;
93105
esac
94106

95107

func/db.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
mysql_connect() {
33
host_str=$(grep "HOST='$1'" $HESTIA/conf/mysql.conf)
44
parse_object_kv_list "$host_str"
5+
if [ -z $PORT ]; then PORT=3306; fi
56
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ]; then
67
echo "Error: mysql config parsing failed"
78
log_event "$E_PARSING" "$ARGUMENTS"
89
exit $E_PARSING
910
fi
10-
1111
mycnf="$HESTIA/conf/.mysql.$HOST"
1212
if [ ! -e "$mycnf" ]; then
1313
echo "[client]">$mycnf
1414
echo "host='$HOST'" >> $mycnf
1515
echo "user='$USER'" >> $mycnf
1616
echo "password='$PASSWORD'" >> $mycnf
17+
echo "port='$PORT'" >> $mycnf
1718
chmod 600 $mycnf
1819
else
1920
mypw=$(grep password $mycnf|cut -f 2 -d \')
@@ -22,6 +23,7 @@ mysql_connect() {
2223
echo "host='$HOST'" >> $mycnf
2324
echo "user='$USER'" >> $mycnf
2425
echo "password='$PASSWORD'" >> $mycnf
26+
echo "port='$PORT'" >> $mycnf
2527
chmod 660 $mycnf
2628
fi
2729
fi
@@ -73,6 +75,7 @@ psql_connect() {
7375
host_str=$(grep "HOST='$1'" $HESTIA/conf/pgsql.conf)
7476
parse_object_kv_list "$host_str"
7577
export PGPASSWORD="$PASSWORD"
78+
if [ -z $PORT ]; then $PORT="5432"; fi
7679
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
7780
echo "Error: postgresql config parsing failed"
7881
log_event "$E_PARSING" "$ARGUMENTS"

0 commit comments

Comments
 (0)