summarylogtreecommitdiffstats
path: root/oracle-xe-18c
blob: cb8f60ab924db60eb21fe63232e0b7aa404ebaab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#!/bin/bash
#
# chkconfig: 2345 80 05
# description: This script is responsible for taking care of configuring the RPM Oracle XE Database and its associated services. 
#
# processname: oracle-xe-18c
# Red Hat or SuSE config: /etc/sysconfig/oracle-xe-18c
#
# change log:
#    ivalopez  12/13/17 - Creation
#    mstalin   04/30/18 - DB configuration changes
#    rfgonzal  05/14/28 - Enh 27965960 & 27965939 - Enable service and clean env vars when launching dbca
#    rfgonzal  05/14/18 - Enh 27965960 & 27965939 - Enable service and clean env vars when launching dbca
#    mstalin   05/20/18 - 21261445 RTI, disable the checks
#    mstalin   05/29/18 - 28121518 password handling and connection string handling
#    mstalin   06/07/18 - Add memory distribution logic
#    rfgonzal  07/09/18 - Bug 28243127 - Cannot start/stop DB after fresh installation
#    mstalin   07/18/18 - 28243186 Print EM configuration URL
#    mstalin   07/06/18 - Add sample schema to XE
#    mstalin   08/02/18 - 28353388 Add delete option to the usage text
#    mstalin   08/02/18 - 28242894 Reduce verbose of the script 
#    mstalin   10/06/18 - 28735641 Fix duplicate success messages and product name
#


# Set path if path not set
case $PATH in
    "") PATH=/bin:/usr/bin:/sbin:/etc
        export PATH ;;
esac

# Check if the root user is running this script
if [ $(id -u) != "0" ]
then
    echo "You must be root user to run the configure script. Login as root user and then run the configure script."
    exit 1
fi

# DB defaults
export ORACLE_HOME=/opt/oracle/product/18c/dbhomeXE
export ORACLE_SID=XE
export TEMPLATE_NAME=XE_Database.dbc
export PDB_NAME=XEPDB1
export LISTENER_NAME=LISTENER
export NUMBER_OF_PDBS=1
export CREATE_AS_CDB=true

# General exports and vars
export PATH=$ORACLE_HOME/bin:$PATH
LSNR=$ORACLE_HOME/bin/lsnrctl
SQLPLUS=$ORACLE_HOME/bin/sqlplus
NETCA=$ORACLE_HOME/bin/netca
DBCA=$ORACLE_HOME/bin/dbca
ORACLE_OWNER=oracle
RETVAL=0
CONFIG_NAME="oracle-xe-18c.conf"
CONFIGURATION="/etc/sysconfig/$CONFIG_NAME"
ORACLE_HOME_NAME="OraHomeXE"
MINIMUM_MEMORY=1048576
MAXIMUM_MEMORY=2097152
MINIMUM_MEMORY_STR="1GB"

MINIMUM_SPACE=4718592
MINIMUM_SPACE_STR="4.5GB"

# Commands
if [ -z "$SU" ];then SU=/bin/su; fi
if [ -z "$AWK" ];then AWK=/bin/awk; fi
if [ -z "$DF" ];then DF=/bin/df; fi
if [ -z "$GREP" ]; then GREP=/usr/bin/grep; fi
if [ ! -f "$GREP" ]; then GREP=/bin/grep; fi
if [ -z "$TAIL" ]; then TAIL=/usr/bin/tail; fi
if [ ! -f "$TAIL" ]; then TAIL=/bin/tail; fi
HOSTNAME_CMD="/bin/hostname"
MKDIR_CMD="/bin/mkdir"

# To start the DB
start()
{
    check_for_configuration
    RETVAL=$?
    if [ $RETVAL -eq 1 ]
    then
        echo "The Oracle Database is not configured. You must run '/etc/init.d/oracle-xe-18c configure' as the root user to configure the database."
        exit
    fi
    # Check if the DB is already started
    pmon=`ps -ef | egrep pmon_$ORACLE_SID'\>' | $GREP -v grep`
    if [ "$pmon" = "" ];
    then
	# Unset the proxy env vars before calling sqlplus
        unset_proxy_vars
        echo "Starting Oracle Net Listener."
        $SU -s /bin/bash $ORACLE_OWNER -c "$LSNR  start $LISTENER_NAME" > /dev/null 2>&1
        RETVAL=$?
        if [ $RETVAL -eq 0 ]
        then
            echo "Oracle Net Listener started."
        fi
        
        echo "Starting Oracle Database instance $ORACLE_SID."
        $SU -s /bin/bash  $ORACLE_OWNER -c "$SQLPLUS -s /nolog << EOF
                                                                connect / as sysdba
                                                                startup
                                                                alter pluggable database all open
                                                                exit;
                                                                EOF" > /dev/null 2>&1
        RETVAL1=$?
        if [ $RETVAL1 -eq 0 ]
        then
            echo "Oracle Database instance $ORACLE_SID started."
        fi
    else
        echo "The Oracle Database instance $ORACLE_SID is already started."
        exit 0
    fi
    
    echo
    if [ $RETVAL -eq 0 ] && [ $RETVAL1 -eq 0 ]
    then
        return 0
     else
        echo "Failed to start Oracle Net Listener using $ORACLE_HOME/bin/tnslsnr and Oracle Database using $ORACLE_HOME/bin/sqlplus."
        exit 1
    fi
}

# To stop the DB
stop()
{
    check_for_configuration
    RETVAL=$?
    if [ $RETVAL -eq 1 ]
    then
        echo "The Oracle Database is not configured. You must run '/etc/init.d/oracle-xe-18c configure' as the root user to configure the database."
        exit 1
    fi
    # Check if the DB is already stopped
    pmon=`ps -ef | egrep pmon_$ORACLE_SID'\>' | $GREP -v grep`
    if [ "$pmon" = "" ]
    then
        echo "Oracle Database instance $ORACLE_SID is already stopped."
        exit 1
    else
	# Unset the proxy env vars before calling sqlplus
	unset_proxy_vars
        echo "Shutting down Oracle Database instance $ORACLE_SID."
        $SU -s /bin/bash $ORACLE_OWNER -c "$SQLPLUS -s /nolog << EOF
                                                                connect / as sysdba
                                                                shutdown immediate
                                                                exit;
                                                                EOF" > /dev/null 2>&1
        RETVAL=$?
        if [ $RETVAL -eq 0 ]
        then
            echo "Oracle Database instance $ORACLE_SID shut down."
        fi
        
        echo "Stopping Oracle Net Listener."
        $SU -s /bin/bash  $ORACLE_OWNER -c "$LSNR stop $LISTENER_NAME" > /dev/null 2>&1
        RETVAL1=$?  
        if [ $RETVAL1 -eq 0 ]
        then
            echo "Oracle Net Listener stopped."
        fi
    fi
    
    echo 
    if [ $RETVAL -eq 0 ] && [ $RETVAL1 -eq 0 ]
    then
        return 0
    else
        echo "Failed to stop Oracle Net Listener using $ORACLE_HOME/bin/tnslsnr and Oracle Database using $ORACLE_HOME/bin/sqlplus."
        exit 1
    fi
}

configure_delete()
{
    # Unset the proxy env vars before deleting the db
    unset_proxy_vars
    ORABASE=`$ORACLE_HOME/bin/orabase`
    NETCA_LOG_DIR="$ORABASE/cfgtoollogs/netca"
    if [ ! -d "$NETCA_LOG_DIR" ]
    then
       $SU -s /bin/bash $ORACLE_OWNER -c "$MKDIR_CMD -p $NETCA_LOG_DIR"   
    fi
    NETCA_LOG="$NETCA_LOG_DIR/netca_deinst_out.log"
 
    echo "Deleting Oracle Listener."

    $SU -s /bin/bash $ORACLE_OWNER -c "$LSNR  stop $LISTENER_NAME" > /dev/null 2>&1

    $SU -s /bin/bash $ORACLE_OWNER -c "$NETCA /deinst >>$NETCA_LOG"

    echo "Deleting Oracle Database $ORACLE_SID."

    $SU -s /bin/bash  $ORACLE_OWNER -c "$DBCA -silent -deleteDatabase -sourceDB $ORACLE_SID -skipSYSDBAPasswordPrompt true"

}
# To call DBCA to configure the DB
configure_perform()
{

    DBFILE_CONSTRUCT=""
    if [ "x$DBFILE_DEST" != "x" ] 
    then
	DBFILE_CONSTRUCT="-storageType FS -datafileDestination $DBFILE_DEST"
    fi
   
    LSNRPORT_CONSTRUCT=""
    if [ "x$LISTENER_PORT" != "x" ] 
    then
        LSNRPORT_CONSTRUCT="/lisport $LISTENER_PORT"
    fi
    
    # Unset the proxy env vars before calling DBCA and NETCA
    unset_proxy_vars

    MEMORY_CONSTRUCT=""
    mem=`cat /proc/meminfo |grep MemTotal`
    # Total physical memory 
    if [ "x$mem" != "x" ]
    then

       #Convert into string array

       OLDIFS="$IFS"
       IFS=' '
       str=($mem)
       IFS=$OLDIFS
       
       #40 percent of physical memory
       dbmem=$(( 40*${str[1]}/100 ))
       
       #Oracle memory is less than 2GB distribute to pga and sga
       if [ "$dbmem" -lt "$MAXIMUM_MEMORY" ]
       then
          memory=$(( $dbmem/1024 ))
          pga=$(( $memory/4 ))
          sga=$(( 3*$memory/4 )) 
          MEMORY_CONSTRUCT="-initParams sga_target=${sga}M,pga_aggregate_target=${pga}M"
       fi 
       
    fi
    ## Adding env var to disable custom scripts run post db creation
    #
    SQLSCRIPT_CONSTRUCT="-customScripts $ORACLE_HOME/assistants/dbca/postdb_creation.sql"
    if [ "x$NO_POSTSCRIPT" != "x" ]
    then
       SQLSCRIPT_CONSTRUCT=""
    fi
    
    echo "Configuring Oracle Listener."

    ORABASE=`$ORACLE_HOME/bin/orabase`
    NETCA_LOG_DIR="$ORABASE/cfgtoollogs/netca"
    if [ ! -d "$NETCA_LOG_DIR" ]
    then
       $SU -s /bin/bash $ORACLE_OWNER -c "$MKDIR_CMD -p $NETCA_LOG_DIR"
    fi
    NETCA_LOG="$NETCA_LOG_DIR/netca_configure_out.log"

    $SU -s /bin/bash $ORACLE_OWNER -c "$NETCA /orahome $ORACLE_HOME /instype typical /inscomp client,oraclenet,javavm,server,ano /insprtcl tcp /cfg local /authadp NO_VALUE /responseFile $ORACLE_HOME/network/install/netca_typ.rsp /silent  /orahnam $ORACLE_HOME_NAME /listenerparameters DEFAULT_SERVICE=XE  $LSNRPORT_CONSTRUCT >> $NETCA_LOG" 

    RETVALNETCA=$?
    if [ $RETVALNETCA -eq 0 ]
    then
        $SU -s /bin/bash $ORACLE_OWNER -c "$LSNR start $LISTENER_NAME > /dev/null"
        $SU -s /bin/bash $ORACLE_OWNER -c "$LSNR status $LISTENER_NAME > /dev/null"
        RETVALLSNRCTL=$?
        if [ $RETVALLSNRCTL -eq 0 ]
        then
                echo "Listener configuration succeeded."
        else
                echo "Listener configuration failed. Check logs under '$NETCA_LOG_DIR'."
                exit 1
        fi
    else
        echo "Listener configuration failed. Check log '$NETCA_LOG' for more details."
        exit 1
    fi


    echo "Configuring Oracle Database $ORACLE_SID."
   
 
    $SU -s /bin/bash  $ORACLE_OWNER -c "(echo '$ORACLE_PASSWORD'; echo '$ORACLE_PASSWORD'; echo '$ORACLE_PASSWORD') | $DBCA -silent -createDatabase -gdbName $ORACLE_SID -templateName $TEMPLATE_NAME -characterSet $CHARSET -createAsContainerDatabase $CREATE_AS_CDB -numberOfPDBs $NUMBER_OF_PDBS -pdbName $PDB_NAME -sid $ORACLE_SID -emConfiguration DBEXPRESS -emExpressPort $EM_EXPRESS_PORT -J-Doracle.assistants.dbca.validate.DBCredentials=false -sampleSchema true $SQLSCRIPT_CONSTRUCT $DBFILE_CONSTRUCT $MEMORY_CONSTRUCT" 
    
    RETVAL=$?
    
    echo
    if [ $RETVAL -eq 0 ]
    then
    	 print_success_connect
         return 0
    else
        echo "Database configuration failed. Check logs under '$ORABASE/cfgtoollogs/dbca'."
        exit 1
    fi
    return 0
}
print_success_connect()
{

  connectstr=`$ORACLE_HOME/bin/lsnrctl status $LISTENER_NAME |grep Connecting`
  if [ "x$connectstr" != "x" ]
  then
    str1=($connectstr)
    portfound=0
    i=0
    for str in "${str1[@]}"
    do
      istr=`echo $str`
      if [ "$portfound" = "1" ]
      then
        break
      else
        if [ "x$istr" != "x" ]
        then
                if [ "$istr" != "${istr/\(PORT/}" ] 
                then
                        portfound=1
                        index=$i
                fi
        fi
      fi
     i=$(( $i+1 ))
   done 
  fi
  if [ index = 0 ]
  then
    portv="PORT"
  else
    finalstr="${str1[index]}"
    finalstr=`echo $finalstr |cut -d'=' -f 6`
    portv="${finalstr/\)*/}"
  fi
  if [ "x$portv" = "x" ]
  then
    portv="PORT"
  fi
  hostname=`$HOSTNAME_CMD`
  if [ "x$portv" = "x1521" ]
  then
	connectstr1="$hostname"
	connectstr2="$hostname/$PDB_NAME"
  else
	connectstr1="$hostname:$portv"
	connectstr2="$hostname:$portv/$PDB_NAME"
  fi

  echo "Connect to Oracle Database using one of the connect strings:"
  echo '     Pluggable database: '$connectstr2''
  echo '     Multitenant container database: '$connectstr1''
  em_url="https://localhost:$EM_EXPRESS_PORT/em"
  echo 'Use '$em_url' to access Oracle Enterprise Manager for Oracle Database XE'

}

# Enh 27965939 - Unsets the proxy env variables
unset_proxy_vars()
{
    if [ "$http_proxy" != "" ]
    then
        unset http_proxy
    fi

    if [ "$HTTP_PROXY" != "" ]
    then
        unset HTTP_PROXY
    fi

    if [ "$https_proxy" != "" ]
    then
        unset https_proxy
    fi

    if [ "$HTTPS_PROXY" != "" ]
    then
        unset HTTPS_PROXY
    fi
}

# Check if the DB is already configured
check_for_configuration()
{
    configfile=`$GREP --no-messages $ORACLE_SID:$ORACLE_HOME /etc/oratab` > /dev/null 2>&1
    if [ "$configfile" = "" ]
    then
        return 1
    fi
    return 0
}

read_config_file()
{
    if [ -f "$CONFIGURATION" ]
    then
        . "$CONFIGURATION"
    else
        echo "The Oracle Database is not configured. Unable to read the configuration file '$CONFIGURATION'"
        exit 1;
    fi
}

# Validation method to check for the port availability for Oracle DB listener
check_port_availability()
{
    if [ "x$SKIP_VALIDATIONS" = "xtrue" ]
    then
	return 0
    fi
    if [ "x$LISTENER_PORT" = "x" ]
    then
       return 0    
    fi    
    
    $SU -s /bin/bash $ORACLE_OWNER -c "$LSNR  status LISTENER" > /dev/null 2>&1
        RETVAL=$?
        if [ $RETVAL -eq 0 ]
        then
            echo "Oracle Net Listener configured."
	    return 0
        fi
    port=`netstat -n --tcp --listen | $GREP :$LISTENER_PORT`
    if [ "$port" != "" ]
    then
        echo "Port $LISTENER_PORT appears to be in use by another application. Specify a different port in the configuration file '$CONFIGURATION'"
        exit 1;
    fi
}

# Validation method to check for port availability for Oracle EM Express
check_em_express_port_availability()
{

    if [ "x$SKIP_VALIDATIONS" = "xtrue" ]
    then
	return 0
    fi
    if [ "x$EM_EXPRESS_PORT" = "x" ]
    then
       EM_EXPRESS_PORT="5500"
    fi    
    port=`netstat -n --tcp --listen | $GREP :$EM_EXPRESS_PORT`
    if [ "$port" != "" ]
    then
        echo "Port $EM_EXPRESS_PORT appears to be in use by another application. Specify a different Oracle EM Express port in the configuration file '$CONFIGURATION'"
        exit 1;
    fi
}

check_memory()
{
        if [ "x$SKIP_VALIDATIONS" = "xtrue" ]
	then
		return 0
	fi
     	mem=`cat /proc/meminfo |grep MemFree`
	if [ "x$mem" != "x" ]
	then
   		str=($mem)
   		echo "Free memory available in the system ${str[1]}"
		if [ "${str[1]}" -lt "$MINIMUM_MEMORY" ]
		then
			echo "Free memory available is not enough for database configuration"
			echo "Need at least $MINIMUM_MEMORY_STR free for database configuration"
			exit 1
		fi
	fi
}

check_space()
{
        if [ "x$SKIP_VALIDATIONS" = "xtrue" ]
        then
                return 0
        fi
        
        DBF_DEST_VALID=$DBFILE_DEST	
    	if [ "x$DBF_DEST_VALID" = "x" ]
	then
		DBF_DEST_VALID=`$ORACLE_HOME/bin/orabase`
	fi
    	if [ "x$DBF_DEST_VALID" != "x" ]
	then
		dspace=`$DF -k $DBF_DEST_VALID --direct | $TAIL -n 1| $AWK '{print $4}'`
		dspace="${dspace//[$'\t\r\n ']}"
	        if [ "x$dspace" != "x" ]
        	then
                	if [ "$dspace" -lt "$MINIMUM_SPACE" ]
                	then
                              echo "The location '$DBF_DEST_VALID' specified for database files has insufficient space."
                              echo "Database creation needs at least '$MINIMUM_SPACE_STR' disk space."
                              echo "Specify a different database file destination that has enough space in the configuration file '$CONFIGURATION'."
                              exit 1
                	fi
		fi
        fi
}


# Entry point to configure the DB
configure()
{
    check_for_configuration
    RETVAL=$?
    if [ $RETVAL -eq 0 ]
    then
        echo "Oracle Database instance $ORACLE_SID is already configured."
        exit 1
    fi
    read_config_file
    check_space
    check_port_availability
    check_em_express_port_availability
    configure_ask
    configure_perform
}



configure_ask()
{
    if [ "x$ORACLE_PASSWORD" != "x" ]
        then
                return 0
        fi
    
    #get the database password
	    while :
	    do
	    echo -n "Specify a password to be used for database accounts. Oracle recommends that the password entered should be at least 8 characters in length, contain at least 1 uppercase character, 1 lower case character and 1 digit [0-9]. Note that the same password will be used for SYS, SYSTEM and PDBADMIN accounts:"
	     while [ 1 ]
             do
	     /bin/stty -echo > /dev/null 2>&1
	     temp=`echo $IFS`
             export IFS="\n"	
	     while [ 1 ]
  	     do
	     read LINE
             while [ -z "$LINE" ]
	     do
		 echo
		 echo -n "Password cannot be null. Enter password:"  
	         read LINE
	     done

	     result=`expr index "$LINE" [\!\@\%\^\&\*\(\)\+\=\\|~\[\]{}\;\:\'\"\,\<\>\/\?]`
	     if [ $result != 0 ];
	     then
             	echo 
			echo -n "The password you entered contains invalid characters. Enter password:"
	     else
		break
	     fi
	     done
		echo
		echo -n "Confirm the password:"
	        read LINE1
		echo
                if [ "$LINE" != "$LINE1" ];
		then
			if [ ! -z $1 ]
			then
				echo
				echo "Passwords do not match. Specify the same password for both ORACLE_PASSSWORD and 
ORACLE_CONFIRM_PASSWORD, and retry the configuration."
				trap "rm -fr $1" exit
				exit
			fi
			echo    
			echo -n "Passwords do not match.  Enter the password:"
		else
			break
		fi
	done
            /bin/stty echo > /dev/null 2>&1
            ORACLE_PASSWORD=$LINE
	    export IFS=$temp
            break;
	done

}

restart()
{
    # Check if the DB is already stopped
    pmon=`ps -ef | egrep pmon_$ORACLE_SID'\>' | $GREP -v grep`
    if [ "$pmon" = "" ]
    then
        start
    else
        stop
        start
    fi
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    configure)
        configure
    ;;
    delete)
        configure_delete
    ;;
    restart)
        restart
    ;;
    *)
        echo $"Usage: $0 {start|stop|restart|configure|delete}"
        exit 1
    ;;
esac

exit 0