Remote MySQL database connection on the cPanel
Table of contents
- Step 1. Get the server IP address of the host cPanel account
- Step 2. Navigate to the databases section of the remote server
- Step 3. Whitelist/add the host IP address on the remote server
- Step 4: Get the server IP address of the remote cPanel account
- Step 5: Write the database connection script on the host cPanel account
cPanel is a web-based control panel that provides a graphical interface and automation tools designed to simplify the process of hosting and managing a server/website ranging from emails files, databases, domains, and more.
Assume you have databases hosted on different cPanel accounts e.g. your company may have multiple cPanel accounts or you may have various clients who want their information shared across their accounts.
Step 1. Get the server IP address of the host cPanel account
Login to the cPanel account of the host (server to use the database from another server). Navigate to the right side window. Under "General Information", get the server IP address of the The shared IP address adopts a format as this 140.105.20.19.
Step 2. Navigate to the databases section of the remote server
Login to the cPanel of the remote(server to have its database shared). Locate the "Database" section, and click on "Remote Database Access".
Step 3. Whitelist/add the host IP address on the remote server
You need to inform the remote server to allow traffic from the host server by whitelisting its IP address acquired in Step 1 above. Assume your host is 140.105.20.19, type it in the "Host" input. Specifying the comment is optional, however, it may be helpful where you have multiple hosts.
Once the hosts server IP address is whitelisted, you may choose to update or delete it when necessary.
Step 4: Get the server IP address of the remote cPanel account
Go to the remote cPanel account (the one whose database is being shared). Follow the procedures as shown in Step 1 to get the IP address of the remote server.
Step 5: Write the database connection script on the host cPanel account
Go back to the host cPanel and write/host your database connection script. The script can be used to test if the connection is successful. Below is a sample code using PHP.
Note: Your server name being used is the remote server IP address obtained from Step 4 above. Assume our remote IP address is 150.105.20.19
<?php
$servername = "150.105.20.19"; // Remote server IP address
$username = "database_username"; // MySQL database username
$password = "database_password"; // MySQL database password
$database = "database_name"; // MySQL database name
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
// Perform database operations here
// Close connection
$conn->close();
?>
Cheers! You can now test and run your script. For any inquiries, feel free to contact me using +254791323200 or mail@douglas.africa
Subscribe to my newsletter
Read articles from Douglas Sabwa Indumwa directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Douglas Sabwa Indumwa
Douglas Sabwa Indumwa
I am a full-stack software developer driven by the goal of creating scalable solutions to automate business processes. Throughout my career, I have successfully developed web applications that serve thousands of users, both for-profit and non-profit. I am currently focusing on expanding my skills in DevSecOps.