How to Install a Database Server (MySQL/PostgreSQL)
Database servers such as MySQL and PostgreSQL are essential for storing and managing data for your applications. This guide covers the installation and basic configuration of both MySQL and PostgreSQL on your VPS.
Step 1: Access Your VPS via SSH
- Open an SSH client (e.g., Terminal for macOS/Linux or PuTTY for Windows).
- Connect to your VPS by entering:
- Press Enter, then enter your password when prompted.
Installing MySQL
Step 2A: Update Your Package Manager
To ensure you’re installing the latest version of MySQL, first update the package list:
Step 3A: Install MySQL
- Install MySQL by running:
- Once installed, start the MySQL service and enable it to start on boot:
Step 4A: Secure MySQL Installation
To improve MySQL’s security, run the secure installation script:
- You’ll be prompted to set a root password and make security adjustments.
- Answer each prompt with your preferred security options.
Step 5A: Access the MySQL Command Line
- Log in to MySQL as root:
- Enter the password you set during the secure installation process.
Step 6A: Create a Database and User (Optional)
-
To create a new database, enter:
-
Create a new user and grant them privileges on the database:
-
Exit the MySQL shell:
Installing PostgreSQL
Step 2B: Update Your Package Manager
As with MySQL, first update your package list:
Step 3B: Install PostgreSQL
- Install PostgreSQL by running:
- Start the PostgreSQL service and enable it to start at boot:
Step 4B: Access the PostgreSQL Command Line
- Switch to the
postgres
user: - Start the PostgreSQL interactive terminal:
Step 5B: Create a Database and User
-
To create a new database, enter:
-
Create a user and grant them privileges on the new database:
-
Exit the PostgreSQL interactive terminal:
-
Switch back to the root user:
Step 6: Verify Installation
- MySQL: To confirm MySQL is running, you can check its status:
- PostgreSQL: Similarly, to confirm PostgreSQL is running:
If either displays as “active (running),” your database server is up and ready for use.
Additional Tips
- Remote Connections: By default, MySQL and PostgreSQL only accept local connections. You may need to adjust configuration settings to allow remote access if required.
- Backups: It’s important to regularly back up your database to prevent data loss. Each database system provides built-in tools for exporting data.
Your VPS is now equipped with a database server! You can use either MySQL or PostgreSQL to support applications, websites, and more.