Skip to content
Snippets Groups Projects
Commit 4610e896 authored by William Bell's avatar William Bell
Browse files

Splitting the connection details into a separate file

parent 821e9ba3
No related branches found
No related tags found
No related merge requests found
These exercises can be run using a MongoDB server on the local PC or using a remote MongoDB server. If a remote MongoDB server is used, then the connection details must be provided by setting environment variables as demonstrated in Listing~\ref{listing:envs}, where \texttt{server\_name}, \texttt{username}, \texttt{password} and \texttt{db\_name} should be replaced by the corresponding connection values. These variables must be set in the terminal window where the Python example programs are run. If the environment variables are not set, the default values from Table~\ref{table:env-defaults} are used.
\begin{lstlisting}[caption={Setting connection Bash environment variables.},label=listing:envs,numbers=none,language=Bash,showspaces=true]
export MONGODB_SERVER=server_name
export MONGODB_USERNAME=username
export MONGODB_PASSWD=password
export MONGODB_DB=db_name
\end{lstlisting}
\begin{table}[h!!]
\begin{center}
\caption{Default settings for environment variables.}
\label{table:env-defaults}
\begin{tabular}{l|l} \hline
\textbf{Variable} & \textbf{Default/Action}\\
\hline
\texttt{MONGODB\_SERVER} & \texttt{localhost} \\
\texttt{MONGODB\_USERNAME} & \texttt{root} \\
\texttt{MONGODB\_PASSWD} & \texttt{example} \\
\texttt{MONGODB\_DB} & \texttt{test} \\ \hline
\end{tabular}
\end{center}
\end{table}
The environment variable settings given in Listing~\ref{listing:envs} can be set for all Bash terminals by editing the \texttt{\~{}/.bashrc} file using a text editor such as \texttt{nano}. The file can be opened as shown in Listing~\ref{listing:nano-bashrc}, following which the lines given in Listing~\ref{listing:envs} should be appended, saving the file by typing \texttt{ctrl-x}, \texttt{y} and \texttt{enter}.
\begin{lstlisting}[caption={Opening the \texttt{.bashrc} file with the nano editor.},label=listing:nano-bashrc,numbers=none,language=Bash,showspaces=true]
nano ~/.bashrc
\end{lstlisting}
An initial version of the environment variables can be written to the \texttt{\~{}/.bashrc} file by running the command given in Listing~\ref{listing:update_bashrc} once the repository has been cloned using Listing~\ref{listing:git-clone}. The \texttt{\~{}/.bashrc} should then be edited, setting the remaining values as needed.
\begin{lstlisting}[caption={Updating the \texttt{\~{}/.bashrc} Bash configuration file.},label=listing:update_bashrc,numbers=none,language=Bash,showspaces=true]
./introduction-to-mongodb/bin/update_bashrc.sh
\end{lstlisting}
The settings that are given in the \texttt{\~{}/.bashrc} file are read when a new Bash window is opened. To cause them to be reread in the current window after the \texttt{\~{}/.bashrc} file has been updated, type the command given in Listing~\ref{listing:reread-bashrc}.
\begin{lstlisting}[caption={Re-reading the \texttt{\~{}/.bashrc} Bash configuration file.},label=listing:reread-bashrc,numbers=none,language=Bash,showspaces=true]
source ~/.bashrc
\end{lstlisting}
The environment variables are read by the functions that are defined in \texttt{mongo\_connect.py}. The functions in \texttt{mongo\_connect.py} are used to connect to the MongoDB database server and select a database.
\endinput
\ No newline at end of file
No preview for this file type
......@@ -80,50 +80,7 @@ This document includes Bash Linux commands to clone a software repository and ch
The commands that are given in this document have been tested with MongoDB server version 5.0. They may work with other recent versions of MongoDB.
\section{Server connection details \label{section:connection-details}}
These exercises can be run using a MongoDB server on the local PC or using a remote MongoDB server. If a remote MongoDB server is used, then the connection details must be provided by setting environment variables as demonstrated in Listing~\ref{listing:envs}, where \texttt{server\_name}, \texttt{username}, \texttt{password} and \texttt{db\_name} should be replaced by the corresponding connection values. These variables must be set in the terminal window where the Python example programs are run. If the environment variables are not set, the default values from Table~\ref{table:env-defaults} are used.
\begin{lstlisting}[caption={Setting connection Bash environment variables.},label=listing:envs,numbers=none,language=Bash,showspaces=true]
export MONGODB_SERVER=server_name
export MONGODB_USERNAME=username
export MONGODB_PASSWD=password
export MONGODB_DB=db_name
\end{lstlisting}
\begin{table}[h!!]
\begin{center}
\caption{Default settings for environment variables.}
\label{table:env-defaults}
\begin{tabular}{l|l} \hline
\textbf{Variable} & \textbf{Default/Action}\\
\hline
\texttt{MONGODB\_SERVER} & \texttt{localhost} \\
\texttt{MONGODB\_USERNAME} & \texttt{root} \\
\texttt{MONGODB\_PASSWD} & \texttt{example} \\
\texttt{MONGODB\_DB} & \texttt{test} \\ \hline
\end{tabular}
\end{center}
\end{table}
The environment variable settings given in Listing~\ref{listing:envs} can be set for all Bash terminals by editing the \texttt{\~{}/.bashrc} file using a text editor such as \texttt{nano}. The file can be opened as shown in Listing~\ref{listing:nano-bashrc}, following which the lines given in Listing~\ref{listing:envs} should be appended, saving the file by typing \texttt{ctrl-x}, \texttt{y} and \texttt{enter}.
\begin{lstlisting}[caption={Opening the \texttt{.bashrc} file with the nano editor.},label=listing:nano-bashrc,numbers=none,language=Bash,showspaces=true]
nano ~/.bashrc
\end{lstlisting}
An initial version of the environment variables can be written to the \texttt{\~{}/.bashrc} file by running the command given in Listing~\ref{listing:update_bashrc} once the repository has been cloned using Listing~\ref{listing:git-clone}. The \texttt{\~{}/.bashrc} should then be edited, setting the remaining values as needed.
\begin{lstlisting}[caption={Updating the \texttt{\~{}/.bashrc} Bash configuration file.},label=listing:update_bashrc,numbers=none,language=Bash,showspaces=true]
./introduction-to-mongodb/bin/update_bashrc.sh
\end{lstlisting}
The settings that are given in the \texttt{\~{}/.bashrc} file are read when a new Bash window is opened. To cause them to be reread in the current window after the \texttt{\~{}/.bashrc} file has been updated, type the command given in Listing~\ref{listing:reread-bashrc}.
\begin{lstlisting}[caption={Re-reading the \texttt{\~{}/.bashrc} Bash configuration file.},label=listing:reread-bashrc,numbers=none,language=Bash,showspaces=true]
source ~/.bashrc
\end{lstlisting}
The environment variables are read by the functions that are defined in \texttt{mongo\_connect.py}. The functions in \texttt{mongo\_connect.py} are used to connect to the MongoDB database server and select a database.
\input{connection-details}
\section{Exercises}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment