Skip to content

Latest commit

 

History

History

05-Client-Server-Architecture-using-MySQL-DBMS

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Implementing a Client-Server Architecture using MySQL Database Management System (DBMS)


Understanding Client-Server Architecture

Client-Server refers to an architecture in which two or more computers are connected together over a network to send and receive requests from one another. In their communication, each machine has its role: the machine sending requests is usually referred to as a Client and the machine responding is called a Server.

Clients and servers work together in a Client-Server Architecture to enable communication, data exchange and the provision of services over a network. This interaction is a fundamental concept in modern computing and is used in various applications, including web browsing, file sharing and database access.

A simple diagram of a Web Client-Server Architecture is shown below:

client server architecture

Here's how clients and servers work together:

1. Client Request

  • The client initiates communication by sending a request to the server. This request typically specifies what the client needs such as a web page, an email, a file or a database query.

  • The request includes information like the type of service required, any parameters and other relevant data.

2. Server Response

  • The server receives the client's request and processes it based on the service or resource requested.

  • The server performs the necessary operations to generate a response, which could be data, content or an acknowledgment.

3. Data Transmission

  • The server sends the response back to the client over the network.

  • The client receives and processes the response. This may involve rendering a web page, displaying an email, saving a file or presenting data.

4. Interaction

  • The client and server can engage in a back-and-forth interaction, with the client making additional requests and the server responding as needed.

  • This interaction continues until the client's requirements are met or until the client chooses to terminate the connection.

5. Statelessness

  • In many client-server interactions, the server is stateless, meaning it doesn't retain information about previous requests from the same client. Each request from the client is independent and the client must include any necessary context or session information.

  • However, stateful communication is also possible, where the server maintains some level of session state between requests.

The diagram below shows a machine trying to access a website using a web browser or simply curl command as a client and it sends HTTP requests to a web server (Apache, Nginx, IIS or any other server) over the internet.

illustration 1

If we extend this concept further and add a Database Server to our architecture, we can get the picture shown below:

illustration 2

In this case, the Web Server has the role of the client that connects and reads/writes to/from a Database (DB) Server (MySQL, MongoDB, Oracle or SQL Server) and the communication happens over a Local Network (it can also be Internet Connection but it is a common practice to place the Web Server and Database Server close to each other in a local network).

The setup on the diagram above is a typical generic Web Stack architecture (LAMP, LEMP, MEAN, MERN). This technology can be implemented with many other technologies (i.e. various Web and Database Servers from Small Page Applications to Large and Complex Portals).

How To Implement a Client-Server Architecture using MySQL Database Management System (DBMS)

Prerequisite

  1. Ensure you have an AWS account. If you don't have an account, sign up for AWS here.

The following steps are taken to implement a basic Client-Server Architecture using MySQL Relational Database Management System (RDBMS):

Step 1: Create and configure two Linux-Based Virtual Servers (EC2 Instance in AWS)

  • On the EC2 Dashboard, click on the Launch Instance button.

Launch Instance

  • On the Name Box and Amazon Machine Image, type mysql_server and ubuntu respectively then select 2 as the number of Instances you want to create.

namebox, ami and number of instance

  • Select Ubuntu Server 22.04 LTS (HVM), SSD Volume Type as the Amazon Machine Image.

ami_ubuntu server 22.04

  • Click on create new key pair.

create new keypair

  • Give the key pair name a name of your choice (i.e client-server-key), select RSA as the key pair type and .pem as the key file format then click on Create key pair.

keypair name, RSA .pem

  • The key pair will be downloaded into the Downloads folder on your computer.

  • Click on the Launch Instance button.

launch instance button

  • On the EC2 Dashboard, click on the Instances tab to display all the Instances on your AWS console.

instance tab

  • You will notice there are 2 Instances named mysql_server, rename one of them to mysql_client by clicking on the pencil icon that appears right beside the name of the Instance.

display instances

  • Click on the Instance ID of the mysql_client Instance and copy the Private IPv4 address.

client ip address

Step 2: Allow MySQL connection from the MySQL Client's IPv4 Address on the MySQL Server

  • Click on the Instance ID of the mysql_server Instance.

instance id mysql server

  • Click on the Security tab and then click on the Security group.

security tab and group

  • Click on Edit Inbound Rules.

edit inbound rules

  • Click on the Add Rule button.

add rule

  • Select MySQL/Aurora as the connection type and paste the MySQL-Client IPv4 address you copied into the Custom IPv4 address box and click on the save rules button.

select mysql and custom client ip address

Step 3: Install the MySQL-Server Software on the MySQL Server Linux Server

  • Click on the Instance ID of the mysql_server.

mysql server instance

  • Click on the Connect button.

connect button server

  • Copy the highlighted commands shown below:

connect to instance server

  • Open your terminal.

  • Go to the directory (i.e. /Downloads) where the .pem key pair is stored using the command shown below:

cd Downloads

cd Downloads

  • Paste the following command to give read permissions to the .pem key pair file:
sudo chmod 400 <private-key-pair-name>.pem

chmod pem_key

  • SSH into the MySQL Server Instance using the command shown below:
ssh -i <private-key-name>.pem ubuntu@<Public-IP-address>

ssh pem_key

  • Update the list of packages in the package manager.
sudo apt update

apt update

  • Run the MySQL Server package installation.
sudo apt install mysql-server -y

apt install mysql_server

In MySQL, the bind-address parameter in the mysqld.cnf file is used to specify the IP address on which the MySQL server should listen for incoming connections. Its default setting is bind-address = 127.0.0.1.

To listen to all connections from all available network interfaces, the default bind-address parameter is changed to 0.0.0.0 (i.e. Default Gateway).

However, it is best practice to set security precautions such as Firewall Rules and MySQL User Privileges to control incoming connections to the remote server. In our case, we set security precautions by configuring the Inbound Rules on the MySQL TCP port to only allow connections from the MySQL Client Private IPv4 Address on the MySQL Server.

  • Run the following command to open the MySQL configuration file to change the bind-address.
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf 

sudo vi sql conf.d

  • Log into the MySQL console by running the command shown below:
sudo mysql

mysql

  • Run a security script that sets the password (i.e. PassWord.1) for the root user using the following command:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'PassWord.1';

alter user

  • Exit the MySQL console using the following command:
mysql > exit;

exit mysql

  • Enable the MySQL service using the following command:
sudo systemctl enable mysql

systemctl enable mysql

  • Run the following command to check if the MySQL service is running:
sudo systemctl status mysql

systemctl status mysql

From the image above, it is evident that the MySQL service is running.

  • Run the following command to start the interactive script to improve the security of the MySQL Server installation:
sudo mysql_secure_installation

mysql secure installation

The script will give the following prompts and your responses should be as follows:

  1. Validate the password y.

validate password

  1. Select the level of password validation policy that matches the password you set initially, in this case, 2 matches the password I chose hence enter 2.

password validation policy

  1. Change the root password n.

change root password

  1. Remove anonymous users n.

remove anonymous users

  1. Disallow root login remotely n.

disallow root login

  1. Remove the test database and access to it y.

remove test database

  1. Reload privilege tables y.

reload privilege tables

  • Log into the MySQL console with the following command:
sudo mysql -u root -p

sudo mysql root

  • Run the following command to create a user named donald with the password PassWord.1:
CREATE USER 'donald'@'%' IDENTIFIED WITH mysql_native_password BY 'PassWord.1';

create user

Note that the % wildcard after the @ sign is used to represent any host. This means the user "donald" is allowed to connect to the MySQL Server from any host.

  • Run the following command to create a database called testing_123:
CREATE DATABASE testing_123;

create database

  • Run the following command to grant all privileges to the user donald:
GRANT ALL PRIVILEGES ON *.* TO 'donald'@'%' WITH GRANT OPTION;

grant all privileges

Note that the *.* wildcard means all databases and tables. Hence, the command above gives the user donald administrative privileges on all databases and tables.

  • Run the following command to apply and make changes effective:
FLUSH PRIVILEGES;

flush privileges

  • Exit the MySQL console.

exit

  • Restart the MySQL service using the command shown below:
sudo systemctl restart mysql

restart mysql

Step 4: Install the MySQL-Client Software on the MySQL Client Linux Server

  • Click on the Instance ID of the mysql_client.

client instance id

  • Click on the Connect button.

connect client

  • Copy the highlighted command shown below:

connecting to instance client

  • Open another terminal on your computer.

  • Go to the directory (i.e. /Downloads) where the .pem key pair is stored using the command shown below:

cd Downloads

cd Downloads

  • SSH into the MySQL Client Instance using the command shown below:
ssh -i <private-key-name>.pem ubuntu@<Public-IP-address>

ssh client

  • Update the list of packages in the package manager.
sudo apt update

apt update

  • Run the MySQL Client package installation.
sudo apt install mysql-client -y

install mysql client

Step 5: Remotely connect to the MySQL Server from the MySQL Client

  • On the terminal of the MySQL Server, run the following command to generate the Private IP address of the MySQL Server:
hostname -i

hostname -i

  • Copy the IP address of the MySQL Server shown above.

  • On the terminal of the MySQL Client, run the following command to connect to the MySQL Server:

sudo mysql -u <username_of_mysql_server> -h <ip_address_of_mysql_server> -p

sudo mysql user h server

  • To check that you have successfully connected to the remote MySQL Server, run the following query:
SHOW DATABASES;

show database

From the image above, you can see the testing_123 database you created on the MySQL Server. Hence, the connection to the remote MySQL Server from the MySQL Client was successful.