Deploying a Database Server on Linux: MySQL or PostgreSQL

System

2023 Jun 14
Deploying a Database Server on Linux: MySQL or PostgreSQL

Databases are integral to the operation of most web applications, serving as the central repository for data that applications depend on. MySQL and PostgreSQL are two of the most prominent open-source relational database systems widely employed in web development. This guide will walk you through the installation and basic configuration of MySQL and PostgreSQL on a Linux server. We'll ensure that your database server is well set-up to support your web applications.

  • MySQL: MySQL is a powerful, robust, and scalable relational database management system. It is renowned for its high performance, simplicity, powerful data protection, and comprehensive transactional support. MySQL is an ideal choice for a wide range of applications, including large-scale web applications, e-commerce applications, and data warehousing.

  • PostgreSQL: PostgreSQL is a highly advanced open-source relational database that supports both SQL (relational) and JSON (non-relational) querying. It is highly respected for its proven architecture, robustness, transactional integrity, and extensibility. PostgreSQL is an excellent choice for complex, object-relational database solutions where scalability and performance are vital.

Prerequisites

Before initiating the installation, ensure you have:

  • A Linux server with root or sudo privileges (Ubuntu, CentOS, Debian, or Fedora recommended).
  • Basic familiarity with Linux terminal commands and text editors like nano or vi.

Installing MySQL

MySQL is a top choice for many developers when it comes to relational database management systems. Here's how you install and configure MySQL on your Linux server:

Update Package Repository

Ensure your system has the latest updates by executing:

sudo apt-get update

Install MySQL

To install MySQL server, run the following command:

sudo apt-get install mysql-server

During the installation, you will be asked to set a root password. Make sure to choose a secure password.

Start MySQL service

Once installed, start the MySQL service and enable it to launch on boot using the following commands:

sudo systemctl start mysql

 sudo systemctl enable mysql

Configuring MySQL

MySQL’s primary configuration file is located at /etc/mysql/my.cnf on most Linux distributions. You can modify this file to change the behavior of your MySQL server.

To open this file, use a text editor of your choice, such as nano:

sudo nano /etc/mysql/my.cnf

In this file, you can alter a number of parameters. Some common ones include:

  • max_connections: Specifies the maximum permitted number of simultaneous client connections.
  • innodb_buffer_pool_size: Designates the size of the memory buffer InnoDB uses to cache data and indexes of its tables.

After making changes, save your edits and close the file. Restart the MySQL service for changes to take effect:

sudo systemctl restart mysql

Securing MySQL

After installation, it is recommended to run a script bundled with MySQL that will improve the security of your MySQL installation:

sudo mysql_secure_installation

Follow the prompts on your screen to enhance the security of your MySQL installation. These include removing anonymous users, disallowing root login remotely, and removing test databases.

Installing PostgreSQL

PostgreSQL, with its transactional integrity and extensibility, is a popular choice for many developers seeking a robust, open-source relational database. Here's how you can install and configure PostgreSQL on your Linux server:

Update Package Repository

Ensure your system has the latest updates by executing:

sudo apt-get update

Install PostgreSQL

To install PostgreSQL along with its accompanying applications and libraries, execute:

sudo apt-get install postgresql postgresql-contrib

Start PostgreSQL service

Start the PostgreSQL service and ensure it launches on boot with:

sudo systemctl start postgresql

 sudo systemctl enable postgresql

Configuring PostgreSQL

The main configuration file for PostgreSQL is typically located at /etc/postgresql/12/main/postgresql.conf, where 12 represents the version of PostgreSQL installed.

To open this file for editing, use a text editor such as nano:

sudo nano /etc/postgresql/12/main/postgresql.conf

This file contains numerous parameters that control the behavior of your PostgreSQL server. Some common ones include:

  • max_connections: Determines the maximum number of concurrent connections to the database server.
  • shared_buffers: Sets the amount of memory dedicated to data caching.

Remember to save your changes and exit the file, then restart the PostgreSQL service for the changes to take effect:

sudo systemctl restart postgresql

Securing PostgreSQL

 PostgreSQL installation comes with a default user 'postgres' with full superadmin privileges. It's recommended to switch to this user and set a password:

sudo -u postgres psql

You'll now be in the PostgreSQL command line interface. Set a password for the 'postgres' user:

\password postgres

You'll be prompted to enter a new password. After providing a strong password, exit the PostgreSQL interface:

\q

Copyright © 2023, All rights reserved.