Knowledgebase / The Beginner's Guide / Connect database from your Onion Service

Connect database from your Onion Service

On our Customer Portal, click your Onion Service, and navigate to the "Database" tab.

On our Customer Portal, click your Onion Service, and navigate to the Database tab.

Scroll to the "Database Credential", you will see your dedicated database connection info and its credential.

Scroll to the Database Credential, you will see your dedicated database connection info and its credential.

This guide outlines three common methods to connect Database from your onion service.

The below script utilize MySQLi Object-Oriented method.
<?php
$servername = "db";
$username = "root";
$password = "UE*****De"; // copy your password here

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
The below script utilize MySQLi Procedural method.
<?php
$servername = "db";
$username = "root";
$password = "UE*****De"; // copy your password here

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
The below script utilize PDO method.
<?php
$servername = "db";
$username = "root";
$password = "UE*****De"; // copy your password here

try {
  $conn = new PDO("mysql:host=$servername", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "Connected successfully";
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
}
?>

After the database connection is successful, the message "Connected successfully" is shown.

Tags:
  • Onion Service
  • Database

24/7 Expert Support

Our experts are always on hand to help answer your questions, get you started, and grow your presence online. You can email us any time