HiBlog
  • Home
  • Blog
  • Category
    • HiCademy
      Hosting and Server Articles
    • HiTube
      Educational Videos
    • HiCast
      Interesting Podcasts
    • Most Interesting
      Most Interesting Posts
  • About us
  • Contact Us
FA
EN
RU
Language:
FA
EN
RU
Article Stats
N/A
Category
Ali Ghanimati
Author
24 December 2025
Release Date
113
Views
FA
EN
RU
Post Languages
How to Connect a Database to Redis in cPanel

How to Connect a Database to Redis in cPanel

Table of Content

Article Stats
  1. What is a Database and Why Do We Need It?
  2. What is Redis?
  3. MySQL vs. Redis
  4. Step 1: Creating a Database in cPanel
  5. Enabling Redis in cPanel
  6. Connecting Your Application to MySQL and Redis

In the world of web development, proper data management and speed optimization are two crucial factors for the success of a website or application. If you are using a cPanel Shared Hosting service, you have likely encountered MySQL and perhaps heard of Redis.

What is a Database and Why Do We Need It?

A Database is a location for permanent data storage.
Any website or application that deals with user information, products, articles, or settings requires a database.

Examples of data stored in a database:

  • User information (name, email, hashed password)
  • Blog posts or store products
  • Orders, payments, and reports
  • System settings

On cPanel-based hosting, you typically use:

  • MySQL
  • MariaDB

It is used when both are relational databases.

What is Redis?

Redis is an in-memory database, but unlike MySQL, it is not designed for permanent data storage.

Also, in this HiData article, you can find complete information about Redis database and how it works.

MySQL vs. Redis

MySQLRedis
Permanent data storageTemporary data storage
Disk-basedRAM-based
Suitable for core dataSuitable for caching and sessions
Slower speedVery fast

It’s important to note that Redis is not a replacement for a MySQL database, but rather a complement to it.

Core data is stored in MySQL, while frequently used or temporary data is kept in Redis.

Step 1: Creating a Database in cPanel

  • Log in to your cPanel in your hosting account.
  • In the cPanel, select Manage My Databases from the Databases section.
  • Enter your database name and then select the Create Database option.
  • Scroll down to the bottom of the page and create a Database User for your database. You will also need to set a password for the user in this section.
  • In the Add User To Database section, add a user to your database. In this section, you will also need to configure the user’s access permissions within the database.

Note: Prefixed Names in cPanel

In cPanel, all databases and users are created in the following format:

username_databasename
username_dbuser

In the username portion, your cPanel hosting account name will be used. Therefore, you need to use this name combination when configuring your database.

Enabling Redis in cPanel

  • Log in to your cPanel account through your hosting service.
  • In the Software section, select Select PHP Version.
  • In Select PHP Version, ensure that your hosting account’s PHP version is set to a version other than Native.
  • Go to the Extensions tab and enable the Redis option.

This option is usually enabled by default on your hosting account. In cPanel services, the Redis database is created within a cagefs environment. In this case, you need to save the address related to the redis.sock file on your host. Also, in this Redis configuration, its default port is 0.

Connecting Your Application to MySQL and Redis

After completing the configuration steps, you can connect to the Redis database through your application.

There are several examples available in different programming languages that you can use as a reference.

Make sure to enter the host and database information in your existing code. When connecting to Redis, the Database Host name will be set to the address pointing to the redis.sock file.

Connecting to MySQL and Redis Databases Using PHP

In your PHP code, you can connect to MySQL and Redis databases on your hosting account as follows:

MySQL (PDO):

<?php
$dbHost = "localhost";
$dbName = "cpaneluser_appdb";
$dbUser = "cpaneluser_appuser";
$dbPass = "YOUR_PASSWORD";

$pdo = new PDO(
  "mysql:host=$dbHost;dbname=$dbName;charset=utf8mb4",
  $dbUser,
  $dbPass,
  [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);

Redis:

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 0);

// If your Redis requires authentication:
// $redis->auth('YOUR_REDIS_PASSWORD');

// quick test
$redis->set('ping', 'pong', 10);
echo $redis->get('ping');

Connecting to Redis Databases Using Node.js

Within cPanel, you can create and run Node.js applications through the Software section and the Setup Node.js App option.

import mysql from "mysql2/promise";
import Redis from "ioredis";

const db = await mysql.createPool({
  host: "localhost",
  user: "cpaneluser_appuser",
  password: "YOUR_PASSWORD",
  database: "cpaneluser_appdb",
});

const redis = new Redis({
  host: "localhost",
  port: 0,
  // password: "YOUR_REDIS_PASSWORD",
});

// Example cache-aside
async function getUser(id) {
  const key = `user:${id}`;
  const cached = await redis.get(key);
  if (cached) return JSON.parse(cached);
const [rows] = await db.query(“SELECT * FROM users WHERE id = ?”, [id]); const user = rows[0] ?? null; if (user) await redis.set(key, JSON.stringify(user), “EX”, 60); return user; }

Connecting to the Redis Database Using Python

Within cPanel, you can run your Python applications through the Setup Python App option.
Running Python Applications in cPanel is possible along with the ability to change the Python version and install various libraries.

import json
import mysql.connector
import redis

db = mysql.connector.connect(
    host="localhost",
    user="cpaneluser_appuser",
    password="YOUR_PASSWORD",
    database="cpaneluser_appdb",
)

r = redis.Redis(host="localhost", port=0, decode_responses=True)

def get_user(user_id: int):
    key = f"user:{user_id}"
    cached = r.get(key)
    if cached:
        return json.loads(cached)

    cur = db.cursor(dictionary=True)
    cur.execute("SELECT * FROM users WHERE id=%s", (user_id,))
    row = cur.fetchone()
    cur.close()

    if row:
        r.setex(key, 60, json.dumps(row))
    return row
Share Post
hidata.org/en/blog/how-to-connect-redis-database-in-cpanel/
Copy Link
In Social Media

Add Comment

You’re replying to
captcha
Web Hosting
  • Professional cPanel Hosting
  • High-Traffic cPanel Hosting
  • File Hosting
Dedicated Server
  • Europe Dedicated Server
About HiData
Hi Data
Good Hosting Experience
Since April 2014, HiData has been hosting thousands of online businesses with strong and premium infrastructure in the best datacenters in Iran and worldwide. We use new-generation servers with powerful processors, high-capacity networks, and strict data security policies to meet your hosting needs at the highest standards. Our support team is available 24/7 through ticket and phone. Speed, stability, and peace of mind are not just promises. They are the identity of HiData.
Virtual Server
  • Germany NVMe VPS
  • Russia NVMe VPS
  • Turkey SSD VPS
  • Germany SATA VPS
  • Storage VPS
  • Cloud VPS
Other Services
  • Domain Services
  • Licensing System
  • Reseller
Phone 1: +98-9009019
HiTube HiCademy HiCast HiBlog
Address
HQ Address: Unit 8, 2nd Floor, Tala Commercial Complex, Imam Khomeini St., Abhar
Contact Phones
+98-9009019
24/7 Support
Support Email: info@hidata.org
© 2025 HiData
All rights reserved for hidata.org
HiData Store
The best prices for the world's leading brands