Establishing a connection locally means running the SQL command-line utility on the same computer where Oracle Database 11g XE is installed, and initiating a database connection command using the valid database credentials.
The following screenshot is taken from Oracle Database 11g XE on Windows, and demonstrates establishing a local database connection to the database:
Navigate to the Oracle Database 11g XE database home and start the sqlplus session with the /nolog option. Using the connect statement establish a connection to the database by supplying the valid database username and password. As shown in the preceding screenshot, connect as SYSTEM user with the password entered while installing Oracle Database 11g XE.
ORA_DBA is a local Windows group that gets automatically created when you install Oracle Database XE and your Windows username automatically gets added to this group. Members of ORA_DBA can connect to the Oracle Database XE without a password, as shown in the following screenshot:
Oracle Net Listener
To connect to the database remotely, we should have Oracle Net Listener running on the host where Oracle Database 11g XE is installed. When a remote client initiates a database connection request, this connection request is received by Oracle Net Listener. The job of the listener is to listen to these incoming database connection requests and hand over the connection requests to the appropriate databases.
Thereafter the remote-client connection directly communicates with the database without the need of the listener. Without the listener service running we cannot connect to the database remotely. There may be more than one database running on the host server; however, you will only have one database listener for all incoming requests. Based on the incoming request, Oracle Net Listener will hand over the connection request to the appropriate database.
Oracle Net Listener and the tnsnames.ora files are configured by default when we install Oracle Database 11g XE. The listener's configuration file is located under $ORACLE_HOME\network\admin\listener.ora. Oracle Database XE automatically creates a Windows service for Oracle Net Listener (OracleXETNSListener).
Configuring Oracle Net Listener
listener.ora is the name of the Oracle Net Listener configuration file and it resides under $ORACLE_HOME\network\admin.
The content of a sample listener.ora file in its simplest form is as follows:
ORACLE_HOME: The Oracle software installation home directory
HOST: The name of the host where Oracle Net Listener is running
PORT: The port on which Oracle Net Listener is listening to the incoming requests
Make the necessary changes to the preceding parameters, save the file, and restart the Windows Oracle Listener service OracleXETNSListener.
Viewing the status of Oracle Net Listener
Navigate to Oracle home ($ORACLE_HOME\bin), start the listener utility (lsnrctl), enter status as shown in the following screenshot, and hit Enter.
If the listener is not started, the command displays an error message, as shown in the following screenshot:
If the listener is running, the command displays detailed listener information as shown in the following screenshot:
Starting and stopping the listener
The listener is automatically configured when we install Oracle Database 11g XE.
To stop the listener on Windows, launch the Services window by navigating to Start | Control Panel | Administrative Tools | Services and stop the OracleXETNSListener service, or launch the listener utility (lsnrctl) and enter the LSNRCTL> stop command. To stop the listener on Linux, within the listener utility (lsnrctl) enter the stop command as we did in the Windows environment.
To start the listener on Windows, launch the Services window by navigating to Start | Control Panel | Administrative Tools | Services and start the OracleXETNSListener service, or launch the listener utility (lsnrctl) and enter the LSNRCTL> start command. To start the listener on Linux, within the listener utility (lsnrctl) enter the start command as we did in Windows environment.
Configuring the tnsnames.ora file
Transparent Network Susbstrate (TNS) handles all remote database connections. Oracle software reads the TNS connection string to understand how to connect to the remote databases. Every Oracle database and the Oracle Client software will have this file. By default the tnsnames.ora file is located under $ORACLE_HOME\network\admin. A sample alias entry from the tnsnames.ora file is as follows: